| 1 |
|
| 2 |
rootdir = File.dirname(File.dirname(__FILE__)) |
| 3 |
$LOAD_PATH.unshift "#{rootdir}/lib" |
| 4 |
|
| 5 |
require 'test/unit' |
| 6 |
require 'rdiscount' |
| 7 |
|
| 8 |
class RDiscountTest < Test::Unit::TestCase |
| 9 |
def test_that_version_looks_valid |
| 10 |
if not (/^[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?$/) =~ RDiscount::VERSION |
| 11 |
assert false, 'Expected RDiscount::VERSION to be a 3 or 4 component version string but found ' + RDiscount::VERSION.to_s |
| 12 |
end |
| 13 |
end |
| 14 |
|
| 15 |
def test_that_discount_does_not_blow_up_with_weird_formatting_case |
| 16 |
text = (<<-TEXT).gsub(/^ {4}/, '').rstrip |
| 17 |
1. some text |
| 18 |
|
| 19 |
1. |
| 20 |
TEXT |
| 21 |
RDiscount.new(text).to_html |
| 22 |
end |
| 23 |
|
| 24 |
def test_that_oversized_input_raises_argument_error |
| 25 |
|
| 26 |
|
| 27 |
int_max = 2_147_483_647 |
| 28 |
begin |
| 29 |
oversized_text = 'a' * (int_max + 1) |
| 30 |
|
| 31 |
rescue NoMemoryError, ArgumentError, RangeError |
| 32 |
|
| 33 |
respond_to?(:omit, true) ? |
| 34 |
omit("Insufficient memory to create oversized input for this test") : |
| 35 |
skip("Insufficient memory to create oversized input for this test") |
| 36 |
end |
| 37 |
|
| 38 |
assert_raise(ArgumentError) do |
| 39 |
RDiscount.new(oversized_text).to_html |
| 40 |
end |
| 41 |
end |
| 42 |
|
| 43 |
def test_that_smart_converts_double_quotes_to_curly_quotes |
| 44 |
rd = RDiscount.new(%("Quoted text"), :smart) |
| 45 |
assert_equal %(<p>“Quoted text”</p>\n), rd.to_html |
| 46 |
end |
| 47 |
|
| 48 |
def test_that_smart_converts_double_quotes_to_curly_quotes_before_a_heading |
| 49 |
rd = RDiscount.new(%("Quoted text"\n\n# Heading), :smart) |
| 50 |
assert_equal %(<p>“Quoted text”</p>\n\n<h1>Heading</h1>\n), rd.to_html |
| 51 |
end |
| 52 |
|
| 53 |
def test_that_smart_converts_double_quotes_to_curly_quotes_after_a_heading |
| 54 |
rd = RDiscount.new(%(# Heading\n\n"Quoted text"), :smart) |
| 55 |
assert_equal %(<h1>Heading</h1>\n\n<p>“Quoted text”</p>\n), rd.to_html |
| 56 |
end |
| 57 |
|
| 58 |
def test_that_smart_gives_ve_suffix_a_rsquo |
| 59 |
rd = RDiscount.new("I've been meaning to tell you ..", :smart) |
| 60 |
assert_equal "<p>I’ve been meaning to tell you ..</p>\n", rd.to_html |
| 61 |
end |
| 62 |
|
| 63 |
def test_that_smart_gives_m_suffix_a_rsquo |
| 64 |
rd = RDiscount.new("I'm not kidding", :smart) |
| 65 |
assert_equal "<p>I’m not kidding</p>\n", rd.to_html |
| 66 |
end |
| 67 |
|
| 68 |
def test_that_smart_gives_d_suffix_a_rsquo |
| 69 |
rd = RDiscount.new("what'd you say?", :smart) |
| 70 |
assert_equal "<p>what’d you say?</p>\n", rd.to_html |
| 71 |
end |
| 72 |
|
| 73 |
def test_that_generate_toc_sets_toc_ids |
| 74 |
rd = RDiscount.new("# Level 1\n\n## Level 2", :generate_toc) |
| 75 |
assert rd.generate_toc |
| 76 |
assert_equal %(<a name="Level-1"></a>\n<h1>Level 1</h1>\n\n<a name="Level-2"></a>\n<h2>Level 2</h2>\n), rd.to_html |
| 77 |
end |
| 78 |
|
| 79 |
def test_should_get_the_generated_toc |
| 80 |
rd = RDiscount.new("# Level 1\n\n## Level 2", :generate_toc) |
| 81 |
exp = %(<ul>\n <li><a href=\"#Level-1\">Level 1</a>\n <ul>\n <li><a href=\"#Level-2\">Level 2</a></li>\n </ul>\n </li>\n</ul>) |
| 82 |
assert_equal exp, rd.toc_content.strip |
| 83 |
end |
| 84 |
|
| 85 |
def test_toc_should_escape_apostropes |
| 86 |
rd = RDiscount.new("# A'B\n\n## C", :generate_toc) |
| 87 |
exp = %(<ul>\n <li><a href=\"#A-27-B\">A'B</a>\n <ul>\n <li><a href=\"#C\">C</a></li>\n </ul>\n </li>\n</ul>) |
| 88 |
assert_equal exp, rd.toc_content.strip |
| 89 |
end |
| 90 |
|
| 91 |
def test_toc_should_escape_question_marks |
| 92 |
rd = RDiscount.new("# A?B\n\n## C", :generate_toc) |
| 93 |
exp = %(<ul>\n <li><a href=\"#A-3f-B\">A?B</a>\n <ul>\n <li><a href=\"#C\">C</a></li>\n </ul>\n </li>\n</ul>) |
| 94 |
assert_equal exp, rd.toc_content.strip |
| 95 |
end |
| 96 |
|
| 97 |
if "".respond_to?(:encoding) |
| 98 |
def test_should_return_string_in_same_encoding_as_input |
| 99 |
input = "Yogācāra" |
| 100 |
output = RDiscount.new(input).to_html |
| 101 |
assert_equal input.encoding.name, output.encoding.name |
| 102 |
end |
| 103 |
end |
| 104 |
|
| 105 |
def test_that_no_image_flag_works |
| 106 |
rd = RDiscount.new(%( <img src="image.png" />), :no_image) |
| 107 |
assert rd.to_html !~ /<img/ |
| 108 |
end |
| 109 |
|
| 110 |
def test_that_no_links_flag_works |
| 111 |
rd = RDiscount.new(%([This link](http://example.net/) <a href="links.html">links</a>), :no_links) |
| 112 |
assert rd.to_html !~ /<a / |
| 113 |
end |
| 114 |
|
| 115 |
def test_that_no_tables_flag_works |
| 116 |
rd = RDiscount.new(<<EOS, :no_tables) |
| 117 |
aaa | bbbb |
| 118 |
-----|------ |
| 119 |
hello|sailor |
| 120 |
EOS |
| 121 |
assert rd.to_html !~ /<table/ |
| 122 |
end |
| 123 |
|
| 124 |
def test_that_strict_flag_works |
| 125 |
rd = RDiscount.new("foo_bar_baz", :strict) |
| 126 |
assert_equal "<p>foo<em>bar</em>baz</p>\n", rd.to_html |
| 127 |
end |
| 128 |
|
| 129 |
def test_that_autolink_flag_works |
| 130 |
rd = RDiscount.new("http://github.com/davidfstr/rdiscount", :autolink) |
| 131 |
assert_equal "<p><a href=\"http://github.com/davidfstr/rdiscount\">http://github.com/davidfstr/rdiscount</a></p>\n", rd.to_html |
| 132 |
end |
| 133 |
|
| 134 |
def test_that_safelink_flag_works |
| 135 |
rd = RDiscount.new("[IRC](irc://chat.freenode.org/#freenode)", :safelink) |
| 136 |
assert_equal "<p>[IRC](irc://chat.freenode.org/#freenode)</p>\n", rd.to_html |
| 137 |
end |
| 138 |
|
| 139 |
def test_that_no_pseudo_protocols_flag_works |
| 140 |
rd = RDiscount.new("[foo](id:bar)", :no_pseudo_protocols) |
| 141 |
assert_equal "<p>[foo](id:bar)</p>\n", rd.to_html |
| 142 |
end |
| 143 |
|
| 144 |
def test_that_no_superscript_flag_works |
| 145 |
rd = RDiscount.new("A^B", :no_superscript) |
| 146 |
assert_equal "<p>A^B</p>\n", rd.to_html |
| 147 |
|
| 148 |
rd = RDiscount.new("A^B") |
| 149 |
assert_equal "<p>A<sup>B</sup></p>\n", rd.to_html |
| 150 |
end |
| 151 |
|
| 152 |
def test_that_no_strikethrough_flag_works |
| 153 |
rd = RDiscount.new("~~A~~", :no_strikethrough) |
| 154 |
assert_equal "<p>~~A~~</p>\n", rd.to_html |
| 155 |
|
| 156 |
rd = RDiscount.new("~~A~~") |
| 157 |
assert_equal "<p><del>A</del></p>\n", rd.to_html |
| 158 |
end |
| 159 |
|
| 160 |
def test_that_tags_can_have_dashes_and_underscores |
| 161 |
rd = RDiscount.new("foo <asdf-qwerty>bar</asdf-qwerty> and <a_b>baz</a_b>") |
| 162 |
assert_equal "<p>foo <asdf-qwerty>bar</asdf-qwerty> and <a_b>baz</a_b></p>\n", rd.to_html |
| 163 |
end |
| 164 |
|
| 165 |
def test_that_footnotes_flag_works |
| 166 |
rd = RDiscount.new(<<EOS, :footnotes) |
| 167 |
Obtuse text.[^1] |
| 168 |
|
| 169 |
[^1]: Clarification |
| 170 |
EOS |
| 171 |
assert rd.to_html.include?('<a href="#fn:1" rel="footnote">1</a>') |
| 172 |
end |
| 173 |
|
| 174 |
def test_that_footnotes_in_span_works |
| 175 |
rd = RDiscount.new(<<EOS, :footnotes) |
| 176 |
[Obtuse text.[^1]](class:someclass) |
| 177 |
|
| 178 |
[^1]: Clarification |
| 179 |
EOS |
| 180 |
assert rd.to_html.include?('<a href="#fn:1" rel="footnote">1</a>') |
| 181 |
end |
| 182 |
|
| 183 |
def test_that_unicode_urls_encoded_correctly |
| 184 |
rd = RDiscount.new("[Test](http://example.com/ß)") |
| 185 |
assert_equal "<p><a href=\"http://example.com/%C3%9F\">Test</a></p>\n", rd.to_html |
| 186 |
end |
| 187 |
|
| 188 |
def test_that_dashes_encoded_correctly |
| 189 |
rd = RDiscount.new("A--B", :smart) |
| 190 |
assert_equal "<p>A–B</p>\n", rd.to_html |
| 191 |
|
| 192 |
rd = RDiscount.new("A---B", :smart) |
| 193 |
assert_equal "<p>A—B</p>\n", rd.to_html |
| 194 |
end |
| 195 |
|
| 196 |
def test_that_superscripts_can_be_escaped |
| 197 |
rd = RDiscount.new("A\\^B") |
| 198 |
assert_equal "<p>A^B</p>\n", rd.to_html |
| 199 |
|
| 200 |
rd = RDiscount.new("A^B") |
| 201 |
assert_equal "<p>A<sup>B</sup></p>\n", rd.to_html |
| 202 |
end |
| 203 |
|
| 204 |
def test_that_style_tag_is_not_filtered_by_default |
| 205 |
rd = RDiscount.new(<<EOS) |
| 206 |
Hello |
| 207 |
<style> |
| 208 |
p { margin: 5px; } |
| 209 |
</style> |
| 210 |
EOS |
| 211 |
assert_equal "<p>Hello</p>\n\n<style>\np { margin: 5px; }\n</style>\n\n", rd.to_html |
| 212 |
end |
| 213 |
|
| 214 |
def test_that_style_tag_is_filtered_with_flag |
| 215 |
rd = RDiscount.new(<<EOS, :filter_styles) |
| 216 |
Hello |
| 217 |
<style> |
| 218 |
p { margin: 5px; } |
| 219 |
</style> |
| 220 |
EOS |
| 221 |
assert_equal "<p>Hello</p>\n\n\n", rd.to_html |
| 222 |
end |
| 223 |
|
| 224 |
def test_that_tables_can_have_leading_and_trailing_pipes |
| 225 |
rd = RDiscount.new(<<EOS) |
| 226 |
| A | B | |
| 227 |
|---|---| |
| 228 |
| C | D | |
| 229 |
EOS |
| 230 |
assert_equal "<table>\n<thead>\n<tr>\n<th> A </th>\n<th> B </th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td> C </td>\n<td> D </td>\n</tr>\n</tbody>\n</table>\n\n", rd.to_html |
| 231 |
end |
| 232 |
|
| 233 |
def test_that_gfm_code_blocks_work |
| 234 |
rd = RDiscount.new(<<EOS) |
| 235 |
``` |
| 236 |
line 1 |
| 237 |
|
| 238 |
line 2 |
| 239 |
``` |
| 240 |
EOS |
| 241 |
assert_equal "<pre><code>line 1\n\nline 2\n</code></pre>\n", rd.to_html |
| 242 |
end |
| 243 |
|
| 244 |
def test_that_gfm_code_blocks_work_with_language |
| 245 |
rd = RDiscount.new(<<EOS) |
| 246 |
```ruby |
| 247 |
line 1 |
| 248 |
|
| 249 |
line 2 |
| 250 |
``` |
| 251 |
EOS |
| 252 |
assert_equal "<pre><code class=\"ruby\">line 1\n\nline 2\n</code></pre>\n", rd.to_html |
| 253 |
end |
| 254 |
|
| 255 |
def test_that_pandoc_code_blocks_work |
| 256 |
rd = RDiscount.new(<<EOS) |
| 257 |
~~~ |
| 258 |
line 1 |
| 259 |
|
| 260 |
line 2 |
| 261 |
~~~ |
| 262 |
EOS |
| 263 |
assert_equal "<pre><code>line 1\n\nline 2\n</code></pre>\n", rd.to_html |
| 264 |
end |
| 265 |
|
| 266 |
def test_that_discount_definition_lists_work |
| 267 |
rd = RDiscount.new(<<EOS) |
| 268 |
=tag1= |
| 269 |
=tag2= |
| 270 |
data. |
| 271 |
EOS |
| 272 |
assert_equal <<EOS, rd.to_html |
| 273 |
<dl> |
| 274 |
<dt>tag1</dt> |
| 275 |
<dt>tag2</dt> |
| 276 |
<dd>data.</dd> |
| 277 |
</dl> |
| 278 |
EOS |
| 279 |
end |
| 280 |
|
| 281 |
def test_latex_passtrough_dont_render_link |
| 282 |
rd = RDiscount.new("$$[(1+2)*3-4](1-2)$$", :latex) |
| 283 |
assert_equal "<p>$$[(1+2)*3-4](1-2)$$</p>\n", rd.to_html |
| 284 |
end |
| 285 |
|
| 286 |
def test_that_emphasis_beside_international_characters_detected |
| 287 |
rd = RDiscount.new(%(*foo ä bar*)) |
| 288 |
assert_equal %(<p><em>foo ä bar</em></p>\n), rd.to_html |
| 289 |
|
| 290 |
rd = RDiscount.new(%(*ä foobar*)) |
| 291 |
assert_equal %(<p><em>ä foobar</em></p>\n), rd.to_html |
| 292 |
|
| 293 |
rd = RDiscount.new(%(*foobar ä*)) |
| 294 |
assert_equal %(<p><em>foobar ä</em></p>\n), rd.to_html |
| 295 |
end |
| 296 |
|
| 297 |
def test_taht |
| 298 |
rd = RDiscount.new(<<EOS, :explicitlist) |
| 299 |
- Bullet |
| 300 |
- Bullet |
| 301 |
|
| 302 |
1. Numbered |
| 303 |
2. Numbered |
| 304 |
EOS |
| 305 |
|
| 306 |
assert_equal <<EOS, rd.to_html |
| 307 |
<ul> |
| 308 |
<li>Bullet</li> |
| 309 |
<li>Bullet</li> |
| 310 |
</ul> |
| 311 |
|
| 312 |
|
| 313 |
<ol> |
| 314 |
<li>Numbered</li> |
| 315 |
<li>Numbered</li> |
| 316 |
</ol> |
| 317 |
|
| 318 |
EOS |
| 319 |
end |
| 320 |
|
| 321 |
def test_that_extra_definition_lists_work |
| 322 |
rd = RDiscount.new(<<EOS) |
| 323 |
tag1 |
| 324 |
: data |
| 325 |
EOS |
| 326 |
assert_equal <<EOS, rd.to_html |
| 327 |
<dl> |
| 328 |
<dt>tag1</dt> |
| 329 |
<dd>data</dd> |
| 330 |
</dl> |
| 331 |
EOS |
| 332 |
end |
| 333 |
end |