Debian Codegraph @

test/markdown_test.rb

raw symbols SCIP
1 rootdir = File.dirname(File.dirname(__FILE__))
2 $LOAD_PATH.unshift "#{rootdir}/lib"
3
4 require 'test/unit'
5 require 'markdown'
6
7 MARKDOWN_TEST_DIR = "#{rootdir}/test/MarkdownTest_1.0.3"
8
9 class MarkdownTest < Test::Unit::TestCase
10
11 def test_that_extension_methods_are_present_on_markdown_class
12 assert Markdown.instance_methods.map{|m| m.to_s }.include?('to_html'),
13 "Markdown class should respond to #to_html"
14 end
15
16 def test_that_simple_one_liner_goes_to_html
17 markdown = Markdown.new('Hello World.')
18 assert_respond_to markdown, :to_html
19 assert_equal "<p>Hello World.</p>", markdown.to_html.strip
20 end
21
22 def test_that_inline_markdown_goes_to_html
23 markdown = Markdown.new('_Hello World_!')
24 assert_equal "<p><em>Hello World</em>!</p>", markdown.to_html.strip
25 end
26
27 def test_that_inline_markdown_starts_and_ends_correctly
28 markdown = Markdown.new('_start _ foo_bar bar_baz _ end_ *italic* **bold** <a>_blah_</a>')
29 assert_respond_to markdown, :to_html
30 assert_equal "<p><em>start _ foo_bar bar_baz _ end</em> <em>italic</em> <strong>bold</strong> <a><em>blah</em></a></p>", markdown.to_html.strip
31
32 markdown = Markdown.new("Run 'rake radiant:extensions:rbac_base:migrate'")
33 assert_equal "<p>Run 'rake radiant:extensions:rbac_base:migrate'</p>", markdown.to_html.strip
34 end
35
36 def test_that_filter_html_works
37 markdown = Markdown.new('Through <em>NO</em> <script>DOUBLE NO</script>', :filter_html)
38 assert_equal "<p>Through &lt;em>NO&lt;/em> &lt;script>DOUBLE NO&lt;/script></p>", markdown.to_html.strip
39 end
40
41 def test_that_bluecloth_restrictions_are_supported
42 markdown = Markdown.new('Hello World.')
43 [:filter_html, :filter_styles].each do |restriction|
44 assert_respond_to markdown, restriction
45 assert_respond_to markdown, "#{restriction}="
46 end
47 assert_not_equal true, markdown.filter_html
48 assert_not_equal true, markdown.filter_styles
49
50 markdown = Markdown.new('Hello World.', :filter_html, :filter_styles)
51 assert_equal true, markdown.filter_html
52 assert_equal true, markdown.filter_styles
53 end
54
55 def test_that_redcloth_attributes_are_supported
56 markdown = Markdown.new('Hello World.')
57 assert_respond_to markdown, :fold_lines
58 assert_respond_to markdown, :fold_lines=
59 assert_not_equal true, markdown.fold_lines
60
61 markdown = Markdown.new('Hello World.', :fold_lines)
62 assert_equal true, markdown.fold_lines
63 end
64
65 def test_that_redcloth_to_html_with_single_arg_is_supported
66 markdown = Markdown.new('Hello World.')
67 assert_nothing_raised(ArgumentError) { markdown.to_html(true) }
68 end
69
70 def test_that_smart_converts_single_quotes_in_words_that_end_in_re
71 markdown = Markdown.new("They're not for sale.", :smart)
72 assert_equal "<p>They&rsquo;re not for sale.</p>\n", markdown.to_html
73 end
74
75 def test_that_smart_converts_single_quotes_in_words_that_end_in_ll
76 markdown = Markdown.new("Well that'll be the day", :smart)
77 assert_equal "<p>Well that&rsquo;ll be the day</p>\n", markdown.to_html
78 end
79
80 def test_that_urls_are_not_doubly_escaped
81 markdown = Markdown.new('[Page 2](/search?query=Markdown+Test&page=2)')
82 assert_equal "<p><a href=\"/search?query=Markdown+Test&amp;page=2\">Page 2</a></p>\n", markdown.to_html
83 end
84
85 def test_simple_inline_html
86 markdown = Markdown.new("before\n\n<div>\n foo\n</div>\nafter")
87 assert_equal "<p>before</p>\n\n<div>\n foo\n</div>\n\n\n<p>after</p>\n",
88 markdown.to_html
89 end
90
91 def test_that_html_blocks_do_not_require_their_own_end_tag_line
92 markdown = Markdown.new("Para 1\n\n<div><pre>HTML block\n</pre></div>\n\nPara 2 [Link](#anchor)")
93 assert_equal "<p>Para 1</p>\n\n<div><pre>HTML block\n</pre></div>\n\n\n<p>Para 2 <a href=\"#anchor\">Link</a></p>\n",
94 markdown.to_html
95 end
96
97 def test_filter_html_doesnt_break_two_space_hard_break
98 markdown = Markdown.new("Lorem, \nipsum\n", :filter_html)
99 assert_equal "<p>Lorem,<br/>\nipsum</p>\n",
100 markdown.to_html
101 end
102
103 # This isn't in the spec but is Markdown.pl behavior.
104 def test_block_quotes_preceded_by_spaces
105 markdown = Markdown.new(
106 "A wise man once said:\n\n" +
107 " > Isn't it wonderful just to be alive.\n"
108 )
109 assert_equal "<p>A wise man once said:</p>\n\n" +
110 "<blockquote><p>Isn't it wonderful just to be alive.</p></blockquote>\n",
111 markdown.to_html
112 end
113
114 def test_ul_with_zero_space_indent
115 markdown = Markdown.new("- foo\n\n- bar\n\n baz\n")
116 assert_equal "<ul><li><p>foo</p></li><li><p>bar</p><p>baz</p></li></ul>",
117 markdown.to_html.gsub("\n", "")
118 end
119
120 def test_ul_with_single_space_indent
121 markdown = Markdown.new(" - foo\n\n - bar\n\n baz\n")
122 assert_equal "<ul><li><p>foo</p></li><li><p>bar</p><p>baz</p></li></ul>",
123 markdown.to_html.gsub("\n", "")
124 end
125
126 # http://github.com/davidfstr/rdiscount/issues/#issue/13
127 def test_headings_with_trailing_space
128 text = "The Ant-Sugar Tales \n" +
129 "=================== \n\n" +
130 "By Candice Yellowflower \n"
131 markdown = Markdown.new(text)
132 assert_equal "<h1>The Ant-Sugar Tales </h1>\n\n<p>By Candice Yellowflower</p>\n",
133 markdown.to_html
134 end
135
136 # Build tests for each file in the MarkdownTest test suite
137
138 Dir["#{MARKDOWN_TEST_DIR}/Tests/*.text"].each do |text_file|
139
140 basename = File.basename(text_file).sub(/\.text$/, '')
141 method_name = basename.gsub(/[-,()]/, '').gsub(/\s+/, '_').downcase
142
143 define_method "test_#{method_name}" do
144 markdown = Markdown.new(File.read(text_file))
145 actual_html = markdown.to_html
146 assert_not_nil actual_html
147 end
148
149 define_method "test_#{method_name}_smart" do
150 markdown = Markdown.new(File.read(text_file), :smart)
151 actual_html = markdown.to_html
152 assert_not_nil actual_html
153 end
154
155 end
156
157 end