all repos — grayfriday @ cd5e4957ceb1c4cd0f506db2c5319d1345785eb6

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}