all repos — grayfriday @ 90509d39d42dcfcdad9e529c8a2745d3bbc2938b

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