Merge: lib/markdown2: introduce a new Markdown engine
[nit.git] / lib / markdown2 / tests / test_markdown_inlines.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either htmlress or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Tests for markdown inline constructs
16 module test_markdown_inlines is test
17
18 import test_markdown
19
20 class TestMarkdownInlines
21 super TestMarkdownHtml
22 test
23
24 fun test_inlines_emph1 is test do
25 var md = """
26 *single asterisks*
27
28 _single underscores_
29
30 **double asterisks**
31
32 __double underscores__
33 """
34 var html = """<p><em>single asterisks</em></p>
35 <p><em>single underscores</em></p>
36 <p><strong>double asterisks</strong></p>
37 <p><strong>double underscores</strong></p>
38 """
39 assert md_to_html(md) == html
40 end
41
42 fun test_inlines_emph2 is test do
43 var md = "un*frigging*believable"
44 var html = "<p>un<em>frigging</em>believable</p>\n"
45 assert md_to_html(md) == html
46 end
47
48 fun test_inlines_emph3 is test do
49 var md = "Con _cat_ this"
50 var html = "<p>Con <em>cat</em> this</p>\n"
51 assert md_to_html(md) == html
52 end
53
54 fun test_inlines_emph_ext is test do
55 var md = "Con_cat_this"
56 var html = "<p>Con_cat_this</p>\n"
57 assert md_to_html(md) == html
58 end
59
60 fun test_inlines_xml1 is test do
61 var md = """
62 This is a regular paragraph.
63
64 <table>
65 <tr>
66 <td>Foo</td>
67 </tr>
68 </table>
69
70 This is another regular paragraph.
71 """
72 var html = """
73 <p>This is a regular paragraph.</p>
74 <table>
75 <tr>
76 <td>Foo</td>
77 </tr>
78 </table>
79 <p>This is another regular paragraph.</p>
80 """
81 assert md_to_html(md) == html
82 end
83
84 fun test_inlines_xml2 is test do
85 var md = """
86 This is an image <img src="foo/bar" alt="baz"/> in a regular paragraph.
87 """
88 var html = """
89 <p>This is an image <img src="foo/bar" alt="baz"/> in a regular paragraph.</p>
90 """
91 assert md_to_html(md) == html
92 end
93
94 fun test_inlines_xml3 is test do
95 var md = """
96 <div style=">"/>
97 """
98 var html = """
99 <div style=">"/>
100 """
101 assert md_to_html(md) == html
102 end
103
104 fun test_inlines_xml4 is test do
105 var md = """
106 <p>This is an example of a block element that should be escaped.</p>
107 <p>Idem for the second paragraph.</p>
108 """
109 assert md_to_html(md) == md
110 end
111
112 fun test_inlines_xml5 is test do
113 var md = """
114 # Some more XML tests
115
116 <p>This is an example of a block element that should be escaped.</p>
117 <p>Idem for the second paragraph.</p>
118
119 With a *md paragraph*!
120 """
121 var html = """
122 <h1>Some more XML tests</h1>
123 <p>This is an example of a block element that should be escaped.</p>
124 <p>Idem for the second paragraph.</p>
125 <p>With a <em>md paragraph</em>!</p>
126 """
127 assert md_to_html(md) == html
128 end
129
130 fun test_escape_bad_html is test do
131 var md = "-1 if < , +1 if > and 0 otherwise"
132 var html = "<p>-1 if &lt; , +1 if &gt; and 0 otherwise</p>\n"
133 assert md_to_html(md) == html
134 end
135
136 fun test_inlines_span_code1 is test do
137 var md = "Use the `printf()` function."
138 var html = "<p>Use the <code>printf()</code> function.</p>\n"
139 assert md_to_html(md) == html
140 end
141
142 fun test_inlines_span_code2 is test do
143 var md = "``There is a literal backtick (`) here.``"
144 var html = "<p><code>There is a literal backtick (`) here.</code></p>\n"
145 assert md_to_html(md) == html
146 end
147
148 fun test_inlines_span_code3 is test do
149 var md = """
150 A single backtick in a code span: `` ` ``
151
152 A backtick-delimited string in a code span: `` `foo` ``
153 """
154 var html = """
155 <p>A single backtick in a code span: <code>`</code></p>
156 <p>A backtick-delimited string in a code span: <code>`foo`</code></p>
157 """
158 assert md_to_html(md) == html
159 end
160
161 fun test_inlines_span_code4 is test do
162 var md = "Please don't use any `<blink>` tags."
163 var html = "<p>Please don't use any <code>&lt;blink&gt;</code> tags.</p>\n"
164 assert md_to_html(md) == html
165 end
166
167 fun test_inlines_span_code5 is test do
168 var md = "`&#8212;` is the decimal-encoded equivalent of `&mdash;`."
169 var html = "<p><code>&amp;#8212;</code> is the decimal-encoded equivalent of <code>&amp;mdash;</code>.</p>\n"
170 assert md_to_html(md) == html
171 end
172
173 fun test_inlines_escape1 is test do
174 var md = "\\*this text is surrounded by literal asterisks\\*"
175 var html = "<p>*this text is surrounded by literal asterisks*</p>\n"
176 assert md_to_html(md) == html
177 end
178
179 fun test_inlines_escape2 is test do
180 var md = "1986\\. What a great season."
181 var html = "<p>1986. What a great season.</p>\n"
182 assert md_to_html(md) == html
183 end
184
185 fun test_inlines_escape3 is test do
186 var md = "Ben & Lux"
187 var html = "<p>Ben &amp; Lux</p>\n"
188 assert md_to_html(md) == html
189 end
190
191 fun test_inlines_link1 is test do
192 var md = """
193 This is [an example](http://example.com/ "Title") inline link.
194
195 [This link](http://example.net/) has no title attribute.
196 """
197 var html = """<p>This is <a href="http://example.com/" title="Title">an example</a> inline link.</p>
198 <p><a href="http://example.net/">This link</a> has no title attribute.</p>
199 """
200 assert md_to_html(md) == html
201 end
202
203 fun test_inlines_link2 is test do
204 var md = "See my [About](/about/) page for details."
205 var html = "<p>See my <a href=\"/about/\">About</a> page for details.</p>\n"
206 assert md_to_html(md) == html
207 end
208
209 fun test_inlines_link3 is test do
210 var md = """
211 This is [an example][id] reference-style link.
212
213 Some lorem ipsum
214
215 [id]: http://example.com/ "Optional Title Here"
216
217 Some other lipsum
218 """
219 var html = """
220 <p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>
221 <p>Some lorem ipsum</p>
222 <p>Some other lipsum</p>
223 """
224 assert md_to_html(md) == html
225 end
226
227 fun test_inlines_link4 is test do
228 var md = """
229 This is multiple examples: [foo][1], [bar][2], [baz][3].
230
231 [1]: http://example.com/ "Optional Title Here"
232 [2]: http://example.com/ 'Optional Title Here'
233 [3]: http://example.com/ (Optional Title Here)
234 """
235 var html = """
236 <p>This is multiple examples: <a href="http://example.com/" title="Optional Title Here">foo</a>, <a href="http://example.com/" title="Optional Title Here">bar</a>, <a href="http://example.com/" title="Optional Title Here">baz</a>.</p>
237 """
238 assert md_to_html(md) == html
239 end
240
241 fun test_inlines_link5 is test do
242 var md = """
243 This is multiple examples: [foo][a], [bar][A], [a].
244
245 [a]: http://example.com/ "Optional Title Here"
246 """
247 var html = """<p>This is multiple examples: <a href="http://example.com/" title="Optional Title Here">foo</a>, <a href="http://example.com/" title="Optional Title Here">bar</a>, <a href="http://example.com/" title="Optional Title Here">a</a>.</p>
248 """
249 assert md_to_html(md) == html
250 end
251
252 fun test_inlines_link6 is test do
253 var md = """
254 I get 10 times more traffic from [Google][] than from [Yahoo][] or [MSN][].
255
256 [Google]: http://google.com/ "Google"
257 [Yahoo]: http://search.yahoo.com/ "Yahoo Search"
258 [MSN]: http://search.msn.com/ "MSN Search"
259 """
260 var html = """<p>I get 10 times more traffic from <a href="http://google.com/" title="Google">Google</a> than from <a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a> or <a href="http://search.msn.com/" title="MSN Search">MSN</a>.</p>
261 """
262 assert md_to_html(md) == html
263 end
264
265 fun test_inlines_link7 is test do
266 var md = """
267 Visit [Daring Fireball][] for more information.
268
269 [Daring Fireball]: http://daringfireball.net/
270 """
271 var html = """<p>Visit <a href="http://daringfireball.net/">Daring Fireball</a> for more information.</p>
272 """
273 assert md_to_html(md) == html
274 end
275
276 fun test_inlines_link8 is test do
277 var md = """
278 This one has a [line
279 break].
280
281 This one has a [line
282 break] with a line-ending space.
283
284 [line break]: /foo
285 """
286 var html = """
287 <p>This one has a <a href="/foo">line
288 break</a>.</p>
289 <p>This one has a <a href="/foo">line
290 break</a> with a line-ending space.</p>
291 """
292 assert md_to_html(md) == html
293 end
294
295 fun test_inlines_link9 is test do
296 var md = """
297 Foo [bar][].
298
299 Foo [bar](/url/ "Title with \\"quotes\\" inside").
300
301
302 [bar]: /url/ "Title with \\"quotes\\" inside"
303 """
304 var html = """
305 <p>Foo <a href="/url/" title="Title with &quot;quotes&quot; inside">bar</a>.</p>
306 <p>Foo <a href="/url/" title="Title with &quot;quotes&quot; inside">bar</a>.</p>
307 """
308 assert md_to_html(md) == html
309 end
310
311 fun test_inlines_img1 is test do
312 var md = """
313 ![Alt text](/path/to/img.jpg)
314
315 ![Alt text](/path/to/img.jpg "Optional title")
316 """
317 var html = """
318 <p><img src="/path/to/img.jpg" alt="Alt text" /></p>
319 <p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title" /></p>
320 """
321 assert md_to_html(md) == html
322 end
323
324 fun test_inlines_img2 is test do
325 var md = """
326 ![Alt text][id]
327
328 [id]: url/to/image "Optional title attribute"
329 """
330 var html = """
331 <p><img src="url/to/image" alt="Alt text" title="Optional title attribute" /></p>
332 """
333 assert md_to_html(md) == html
334 end
335 end