inline_test.go (view raw)
1//
2// Black Friday Markdown Processor
3// Originally based on http://github.com/tanoku/upskirt
4// by Russ Ross <russ@russross.com>
5//
6
7//
8// Unit tests for inline parsing
9//
10
11package blackfriday
12
13import (
14 "testing"
15)
16
17func runMarkdownInline(input string) string {
18 var extensions uint32
19 extensions |= EXTENSION_AUTOLINK
20 extensions |= EXTENSION_STRIKETHROUGH
21
22 html_flags := 0
23 html_flags |= HTML_USE_XHTML
24
25 renderer := HtmlRenderer(html_flags)
26
27 return string(Markdown([]byte(input), renderer, extensions))
28}
29
30func doTestsInline(t *testing.T, tests []string) {
31 for i := 0; i+1 < len(tests); i += 2 {
32 input := tests[i]
33 expected := tests[i+1]
34 actual := runMarkdownInline(input)
35 if actual != expected {
36 t.Errorf("\nInput [%#v]\nExpected[%#v]\nActual [%#v]",
37 input, expected, actual)
38 }
39 }
40}
41
42func TestEmphasis(t *testing.T) {
43 var tests = []string{
44 "nothing inline\n",
45 "<p>nothing inline</p>\n",
46
47 "simple *inline* test\n",
48 "<p>simple <em>inline</em> test</p>\n",
49
50 "*at the* beginning\n",
51 "<p><em>at the</em> beginning</p>\n",
52
53 "at the *end*\n",
54 "<p>at the <em>end</em></p>\n",
55
56 "*try two* in *one line*\n",
57 "<p><em>try two</em> in <em>one line</em></p>\n",
58
59 "over *two\nlines* test\n",
60 "<p>over <em>two\nlines</em> test</p>\n",
61
62 "odd *number of* markers* here\n",
63 "<p>odd <em>number of</em> markers* here</p>\n",
64
65 "odd *number\nof* markers* here\n",
66 "<p>odd <em>number\nof</em> markers* here</p>\n",
67
68 "simple _inline_ test\n",
69 "<p>simple <em>inline</em> test</p>\n",
70
71 "_at the_ beginning\n",
72 "<p><em>at the</em> beginning</p>\n",
73
74 "at the _end_\n",
75 "<p>at the <em>end</em></p>\n",
76
77 "_try two_ in _one line_\n",
78 "<p><em>try two</em> in <em>one line</em></p>\n",
79
80 "over _two\nlines_ test\n",
81 "<p>over <em>two\nlines</em> test</p>\n",
82
83 "odd _number of_ markers_ here\n",
84 "<p>odd <em>number of</em> markers_ here</p>\n",
85
86 "odd _number\nof_ markers_ here\n",
87 "<p>odd <em>number\nof</em> markers_ here</p>\n",
88
89 "mix of *markers_\n",
90 "<p>mix of *markers_</p>\n",
91 }
92 doTestsInline(t, tests)
93}
94
95func TestStrong(t *testing.T) {
96 var tests = []string{
97 "nothing inline\n",
98 "<p>nothing inline</p>\n",
99
100 "simple **inline** test\n",
101 "<p>simple <strong>inline</strong> test</p>\n",
102
103 "**at the** beginning\n",
104 "<p><strong>at the</strong> beginning</p>\n",
105
106 "at the **end**\n",
107 "<p>at the <strong>end</strong></p>\n",
108
109 "**try two** in **one line**\n",
110 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
111
112 "over **two\nlines** test\n",
113 "<p>over <strong>two\nlines</strong> test</p>\n",
114
115 "odd **number of** markers** here\n",
116 "<p>odd <strong>number of</strong> markers** here</p>\n",
117
118 "odd **number\nof** markers** here\n",
119 "<p>odd <strong>number\nof</strong> markers** here</p>\n",
120
121 "simple __inline__ test\n",
122 "<p>simple <strong>inline</strong> test</p>\n",
123
124 "__at the__ beginning\n",
125 "<p><strong>at the</strong> beginning</p>\n",
126
127 "at the __end__\n",
128 "<p>at the <strong>end</strong></p>\n",
129
130 "__try two__ in __one line__\n",
131 "<p><strong>try two</strong> in <strong>one line</strong></p>\n",
132
133 "over __two\nlines__ test\n",
134 "<p>over <strong>two\nlines</strong> test</p>\n",
135
136 "odd __number of__ markers__ here\n",
137 "<p>odd <strong>number of</strong> markers__ here</p>\n",
138
139 "odd __number\nof__ markers__ here\n",
140 "<p>odd <strong>number\nof</strong> markers__ here</p>\n",
141
142 "mix of **markers__\n",
143 "<p>mix of **markers__</p>\n",
144 }
145 doTestsInline(t, tests)
146}
147
148func TestEmphasisMix(t *testing.T) {
149 var tests = []string{
150 "***triple emphasis***\n",
151 "<p><strong><em>triple emphasis</em></strong></p>\n",
152
153 "***triple\nemphasis***\n",
154 "<p><strong><em>triple\nemphasis</em></strong></p>\n",
155
156 "___triple emphasis___\n",
157 "<p><strong><em>triple emphasis</em></strong></p>\n",
158
159 "***triple emphasis___\n",
160 "<p>***triple emphasis___</p>\n",
161
162 "*__triple emphasis__*\n",
163 "<p><em><strong>triple emphasis</strong></em></p>\n",
164
165 "__*triple emphasis*__\n",
166 "<p><strong><em>triple emphasis</em></strong></p>\n",
167
168 "**improper *nesting** is* bad\n",
169 "<p><strong>improper *nesting</strong> is* bad</p>\n",
170
171 "*improper **nesting* is** bad\n",
172 "<p><em>improper **nesting</em> is** bad</p>\n",
173 }
174 doTestsInline(t, tests)
175}
176
177func TestStrikeThrough(t *testing.T) {
178 var tests = []string{
179 "nothing inline\n",
180 "<p>nothing inline</p>\n",
181
182 "simple ~~inline~~ test\n",
183 "<p>simple <del>inline</del> test</p>\n",
184
185 "~~at the~~ beginning\n",
186 "<p><del>at the</del> beginning</p>\n",
187
188 "at the ~~end~~\n",
189 "<p>at the <del>end</del></p>\n",
190
191 "~~try two~~ in ~~one line~~\n",
192 "<p><del>try two</del> in <del>one line</del></p>\n",
193
194 "over ~~two\nlines~~ test\n",
195 "<p>over <del>two\nlines</del> test</p>\n",
196
197 "odd ~~number of~~ markers~~ here\n",
198 "<p>odd <del>number of</del> markers~~ here</p>\n",
199
200 "odd ~~number\nof~~ markers~~ here\n",
201 "<p>odd <del>number\nof</del> markers~~ here</p>\n",
202 }
203 doTestsInline(t, tests)
204}
205
206func TestCodeSpan(t *testing.T) {
207 var tests = []string{
208 "`source code`\n",
209 "<p><code>source code</code></p>\n",
210
211 "` source code with spaces `\n",
212 "<p><code>source code with spaces</code></p>\n",
213
214 "` source code with spaces `not here\n",
215 "<p><code>source code with spaces</code>not here</p>\n",
216
217 "a `single marker\n",
218 "<p>a `single marker</p>\n",
219
220 "a single multi-tick marker with ``` no text\n",
221 "<p>a single multi-tick marker with ``` no text</p>\n",
222
223 "markers with ` ` a space\n",
224 "<p>markers with <code></code> a space</p>\n",
225
226 "`source code` and a `stray\n",
227 "<p><code>source code</code> and a `stray</p>\n",
228
229 "`source *with* _awkward characters_ in it`\n",
230 "<p><code>source *with* _awkward characters_ in it</code></p>\n",
231
232 "`split over\ntwo lines`\n",
233 "<p><code>split over\ntwo lines</code></p>\n",
234
235 "```multiple ticks``` for the marker\n",
236 "<p><code>multiple ticks</code> for the marker</p>\n",
237
238 "```multiple ticks `with` ticks inside```\n",
239 "<p><code>multiple ticks `with` ticks inside</code></p>\n",
240 }
241 doTestsInline(t, tests)
242}
243
244func TestLineBreak(t *testing.T) {
245 var tests = []string{
246 "this line \nhas a break\n",
247 "<p>this line<br />\nhas a break</p>\n",
248
249 "this line \ndoes not\n",
250 "<p>this line\ndoes not</p>\n",
251
252 "this has an \nextra space\n",
253 "<p>this has an<br />\nextra space</p>\n",
254 }
255 doTestsInline(t, tests)
256}
257
258func TestInlineLink(t *testing.T) {
259 var tests = []string{
260 "[foo](/bar/)\n",
261 "<p><a href=\"/bar/\">foo</a></p>\n",
262
263 "[foo with a title](/bar/ \"title\")\n",
264 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
265
266 "[foo with a title](/bar/\t\"title\")\n",
267 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
268
269 "[foo with a title](/bar/ \"title\" )\n",
270 "<p><a href=\"/bar/\" title=\"title\">foo with a title</a></p>\n",
271
272 "[foo with a title](/bar/ title with no quotes)\n",
273 "<p><a href=\"/bar/ title with no quotes\">foo with a title</a></p>\n",
274
275 "[foo]()\n",
276 "<p><a href=\"\">foo</a></p>\n",
277
278 "![foo](/bar/)\n",
279 "<p><img src=\"/bar/\" alt=\"foo\" />\n</p>\n",
280
281 "![foo with a title](/bar/ \"title\")\n",
282 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
283
284 "![foo with a title](/bar/\t\"title\")\n",
285 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
286
287 "![foo with a title](/bar/ \"title\" )\n",
288 "<p><img src=\"/bar/\" alt=\"foo with a title\" title=\"title\" />\n</p>\n",
289
290 "![foo with a title](/bar/ title with no quotes)\n",
291 "<p><img src=\"/bar/ title with no quotes\" alt=\"foo with a title\" />\n</p>\n",
292
293 "![foo]()\n",
294 "<p>[foo]()</p>\n",
295
296 "[a link]\t(/with_a_tab/)\n",
297 "<p><a href=\"/with_a_tab/\">a link</a></p>\n",
298
299 "[a link] (/with_spaces/)\n",
300 "<p><a href=\"/with_spaces/\">a link</a></p>\n",
301
302 "[text (with) [[nested] (brackets)]](/url/)\n",
303 "<p><a href=\"/url/\">text (with) [[nested] (brackets)]</a></p>\n",
304
305 "[text (with) [broken nested] (brackets)]](/url/)\n",
306 "<p>[text (with) <a href=\"brackets\">broken nested</a>]](/url/)</p>\n",
307
308 "[text\nwith a newline](/link/)\n",
309 "<p><a href=\"/link/\">text\nwith a newline</a></p>\n",
310
311 "[text in brackets] [followed](/by a link/)\n",
312 "<p>[text in brackets] <a href=\"/by a link/\">followed</a></p>\n",
313
314 "[link with\\] a closing bracket](/url/)\n",
315 "<p><a href=\"/url/\">link with] a closing bracket</a></p>\n",
316
317 "[link with\\[ an opening bracket](/url/)\n",
318 "<p><a href=\"/url/\">link with[ an opening bracket</a></p>\n",
319
320 "[link with\\) a closing paren](/url/)\n",
321 "<p><a href=\"/url/\">link with) a closing paren</a></p>\n",
322
323 "[link with\\( an opening paren](/url/)\n",
324 "<p><a href=\"/url/\">link with( an opening paren</a></p>\n",
325
326 "[link]( with whitespace)\n",
327 "<p><a href=\"with whitespace\">link</a></p>\n",
328
329 "[link]( with whitespace )\n",
330 "<p><a href=\"with whitespace\">link</a></p>\n",
331
332 "[link](url \"one quote)\n",
333 "<p><a href=\"url "one quote\">link</a></p>\n",
334
335 "[link](url 'one quote)\n",
336 "<p><a href=\"url 'one quote\">link</a></p>\n",
337
338 "[link](<url>)\n",
339 "<p><a href=\"url\">link</a></p>\n",
340
341 "[link & ampersand](/url/)\n",
342 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
343
344 "[link & ampersand](/url/)\n",
345 "<p><a href=\"/url/\">link & ampersand</a></p>\n",
346
347 "[link](/url/&query)\n",
348 "<p><a href=\"/url/&query\">link</a></p>\n",
349 }
350 doTestsInline(t, tests)
351}
352
353func TestReferenceLink(t *testing.T) {
354 var tests = []string{
355 "[link][ref]\n",
356 "<p>[link][ref]</p>\n",
357
358 "[link][ref]\n [ref]: /url/ \"title\"\n",
359 "<p><a href=\"/url/\" title=\"title\">link</a></p>\n",
360
361 "[link][ref]\n [ref]: /url/\n",
362 "<p><a href=\"/url/\">link</a></p>\n",
363
364 " [ref]: /url/\n",
365 "",
366
367 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
368 "",
369
370 " [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n [4spaces]: /url/\n",
371 "<pre><code>[4spaces]: /url/\n</code></pre>\n",
372
373 "[hmm](ref2)\n [ref]: /url/\n[ref2]: /url/\n [ref3]: /url/\n",
374 "<p><a href=\"ref2\">hmm</a></p>\n",
375
376 "[ref]\n",
377 "<p>[ref]</p>\n",
378
379 "[ref]\n [ref]: /url/ \"title\"\n",
380 "<p><a href=\"/url/\" title=\"title\">ref</a></p>\n",
381 }
382 doTestsInline(t, tests)
383}
384
385func TestTags(t *testing.T) {
386 var tests = []string{
387 "a <span>tag</span>\n",
388 "<p>a <span>tag</span></p>\n",
389
390 "<span>tag</span>\n",
391 "<p><span>tag</span></p>\n",
392
393 "<span>mismatch</spandex>\n",
394 "<p><span>mismatch</spandex></p>\n",
395
396 "a <singleton /> tag\n",
397 "<p>a <singleton /> tag</p>\n",
398 }
399 doTestsInline(t, tests)
400}
401
402func TestAutoLink(t *testing.T) {
403 var tests = []string{
404 "go to <http://foo.com/>\n",
405 "<p>go to <a href=\"http://foo.com/\">http://foo.com/</a></p>\n",
406
407 "a secure <https://link.org>\n",
408 "<p>a secure <a href=\"https://link.org\">https://link.org</a></p>\n",
409
410 "an email <mailto:some@one.com>\n",
411 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
412
413 "an email <mailto://some@one.com>\n",
414 "<p>an email <a href=\"mailto://some@one.com\">some@one.com</a></p>\n",
415
416 "an email <some@one.com>\n",
417 "<p>an email <a href=\"mailto:some@one.com\">some@one.com</a></p>\n",
418
419 "an ftp <ftp://old.com>\n",
420 "<p>an ftp <a href=\"ftp://old.com\">ftp://old.com</a></p>\n",
421
422 "an ftp <ftp:old.com>\n",
423 "<p>an ftp <a href=\"ftp:old.com\">ftp:old.com</a></p>\n",
424
425 "a link with <http://new.com?query=foo&bar>\n",
426 "<p>a link with <a href=\"http://new.com?query=foo&bar\">" +
427 "http://new.com?query=foo&bar</a></p>\n",
428
429 "quotes mean a tag <http://new.com?query=\"foo\"&bar>\n",
430 "<p>quotes mean a tag <http://new.com?query=\"foo\"&bar></p>\n",
431
432 "quotes mean a tag <http://new.com?query='foo'&bar>\n",
433 "<p>quotes mean a tag <http://new.com?query='foo'&bar></p>\n",
434
435 "unless escaped <http://new.com?query=\\\"foo\\\"&bar>\n",
436 "<p>unless escaped <a href=\"http://new.com?query="foo"&bar\">" +
437 "http://new.com?query="foo"&bar</a></p>\n",
438
439 "even a > can be escaped <http://new.com?q=\\>&etc>\n",
440 "<p>even a > can be escaped <a href=\"http://new.com?q=>&etc\">" +
441 "http://new.com?q=>&etc</a></p>\n",
442 }
443 doTestsInline(t, tests)
444}