all repos — grayfriday @ 44db7217089aa573472bdce02829528e8fb554eb

blackfriday fork with a few changes

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