/
html_toc_render_test.rb
96 lines (75 loc) · 2.66 KB
/
html_toc_render_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# coding: UTF-8
require 'test_helper'
class HTMLTOCRenderTest < Redcarpet::TestCase
def setup
@renderer = Redcarpet::Render::HTML_TOC
@markdown = <<-Markdown.strip_heredoc
# A title
## A __nice__ subtitle
## Another one
### A sub-sub-title
### 見出し
Markdown
end
def test_simple_toc_render
output = render(@markdown)
assert output.start_with?("<ul>")
assert output.end_with?("</ul>")
assert_equal 3, output.scan("<ul>").length
assert_equal 5, output.scan("<li>").length
end
def test_granular_toc_render
output = render(@markdown, with: { nesting_level: 2 })
assert output.start_with?("<ul>")
assert output.end_with?("</ul>")
assert_equal 3, output.scan("<li>").length
assert !output.include?("A sub-sub title")
end
def test_granular_toc_render_with_range
output = render(@markdown, with: { nesting_level: 2..5 }).strip
assert output.start_with?("<ul>")
assert output.end_with?("</ul>")
assert output.match("Another one")
assert output.match("A sub-sub-title")
assert output.match("見出し")
refute output.match("A title")
refute output.match("A really tiny title")
end
def test_toc_heading_id
output = render(@markdown)
assert_match /a-title/, output
assert_match /a-nice-subtitle/, output
assert_match /another-one/, output
assert_match /a-sub-sub-title/, output
assert_match /part-37870bfa194139f/, output
end
def test_toc_heading_with_hyphen_and_equal
output = render("# Hello World\n\n-\n\n=")
assert_equal 1, output.scan("<li>").length
assert !output.include?('<a href=\"#\"></a>')
end
def test_anchor_generation_with_edge_cases
# Imported from ActiveSupport::Inflector#parameterize's tests
titles = {
"Donald E. Knuth" => "donald-e-knuth",
"Random text with *(bad)* characters" => "random-text-with-bad-characters",
"!@#Surrounding bad characters!@#" => "surrounding-bad-characters",
"Squeeze separators" => "squeeze-separators",
"Test with + sign" => "test-with-sign",
"Test with a Namespaced::Class" => "test-with-a-namespaced-class"
}
titles.each do |title, anchor|
assert_match %("##{anchor}"), render("# #{title}")
end
end
def test_inline_markup_is_not_escaped
output = render(@markdown)
assert_match "A <strong>nice</strong> subtitle", output
assert_no_match %r{<}, output
end
def test_inline_markup_escaping
output = render(@markdown, with: [:escape_html])
assert_match "<strong>", output
assert_no_match %r{<strong>}, output
end
end