all repos — grayfriday @ c99557ec28d2958929d93f07b077a47de5444be8

blackfriday fork with a few changes

inline_test.go (view raw)

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