Merge: src/model/model_index: model index uses BKTree
[nit.git] / lib / markdown2 / tests / test_markdown_latex.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 express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Tests for markdown rendering to LaTeX
16 module test_markdown_latex is test
17
18 import test_markdown
19 import markdown_latex_rendering
20
21 # Abstract test class that defines the test methods for LaTeX rendering
22 abstract class TestMarkdownLatex
23 super TestMarkdown
24
25 # LaTeX renderer used in tests
26 var tex_renderer = new LatexRenderer
27
28 # Render the `md` string as LaTeX format
29 fun md_to_tex(md: String): String do
30 var node = parse_md(md)
31 return tex_renderer.render(node)
32 end
33 end
34
35 class TestLatexRendering
36 super TestMarkdownLatex
37 test
38
39 fun after_test is after do
40 tex_renderer.wrap_document = false
41 tex_renderer.use_listings = false
42 end
43
44 fun test_document_wrapper is test do
45 var md = """
46 This example needs a document wrapper.
47 """
48
49 var tex = """
50 \\documentclass[letter,10pt]{article}
51
52 \\usepackage[utf8]{inputenc}
53 \\usepackage{hyperref}
54 \\usepackage{graphicx}
55 \\usepackage{ulem}
56
57 \\begin{document}
58
59 This example needs a document wrapper.
60
61 \\end{document}
62 """
63 tex_renderer.wrap_document = true
64 assert md_to_tex(md) == tex
65 end
66
67 fun test_atx_headings is test do
68 var md = """
69 # Title 1
70 ## Title 2
71 ### Title 3
72 #### Title 4
73 ##### Title 5
74 ###### Title 6
75 """
76 var tex = """
77 \\section{Title 1}
78
79 \\subsection{Title 2}
80
81 \\subsubsection{Title 3}
82
83 \\paragraph{Title 4}
84
85 \\subparagraph{Title 5}
86
87 \\textbf{Title 6}
88 """
89 assert md_to_tex(md) == tex
90 end
91
92 fun test_blockquotes is test do
93 var md = """
94 > this is a
95 > multiline quote
96 """
97 var tex = """
98 \\begin{quote}
99 this is a
100 multiline quote
101 \\end{quote}
102 """
103 assert md_to_tex(md) == tex
104 end
105
106 fun test_thematic_breaks is test do
107 var md = """
108 * * *
109 """
110 var tex = """
111 \\begin{center}\\rule{3in}{0.4pt}\\end{center}
112 """
113 assert md_to_tex(md) == tex
114 end
115
116 fun test_paragraphs is test do
117 var md = """
118 a paragraph
119 on two lines
120
121 another paragraph
122 """
123 var tex = """
124 a paragraph
125 on two lines
126
127 another paragraph
128 """
129 assert md_to_tex(md) == tex
130 end
131
132 fun test_html_block is test do
133 var md = """
134 <p>
135 <a href="url">foo</a>
136 </p>
137 """
138 var tex = """
139 \\begin{verbatim}
140 <p>
141 <a href="url">foo</a>
142 </p>
143 \\end{verbatim}
144 """
145 assert md_to_tex(md) == tex
146 end
147
148 fun test_indented_code is test do
149 var md = """
150 first line
151 second line
152 """
153 var tex = """
154 \\begin{verbatim}
155 first line
156 second line
157 \\end{verbatim}
158 """
159 assert md_to_tex(md) == tex
160 end
161
162 fun test_indented_code_with_listings is test do
163 var md = """
164 first line
165 second line
166 """
167 var tex = """
168 \\begin{lstlisting}
169 first line
170 second line
171 \\end{lstlisting}
172 """
173 tex_renderer.use_listings = true
174 assert md_to_tex(md) == tex
175 end
176
177 fun test_fenced_code is test do
178 var md = """
179 ~~~
180 first line
181 second line
182 ~~~
183 """
184 var tex = """
185 \\begin{verbatim}
186 first line
187 second line
188 \\end{verbatim}
189 """
190 assert md_to_tex(md) == tex
191 end
192
193 fun test_fenced_code_with_listings is test do
194 var md = """
195 ~~~
196 first line
197 second line
198 ~~~
199 """
200 var tex = """
201 \\begin{lstlisting}
202 first line
203 second line
204 \\end{lstlisting}
205 """
206 tex_renderer.use_listings = true
207 assert md_to_tex(md) == tex
208 end
209
210 fun test_fenced_code_with_listings_and_language is test do
211 var md = """
212 ~~~c
213 first line
214 second line
215 ~~~
216 """
217 var tex = """
218 \\begin{lstlisting}[language=c]
219 first line
220 second line
221 \\end{lstlisting}
222 """
223 tex_renderer.use_listings = true
224 assert md_to_tex(md) == tex
225 end
226
227 fun test_list_ordered is test do
228 var md = """
229 1) item 1
230 2) item 2
231 """
232 var tex = """
233 \\begin{enumerate}
234 \\item
235 item 1
236 \\item
237 item 2
238 \\end{enumerate}
239 """
240 assert md_to_tex(md) == tex
241 end
242
243 fun test_list_ordered_with_starting_number is test do
244 var md = """
245 4) item 1
246 5) item 2
247 """
248 var tex = """
249 \\begin{enumerate}
250 \\setcounter{enumi}{4}
251 \\item
252 item 1
253 \\item
254 item 2
255 \\end{enumerate}
256 """
257 assert md_to_tex(md) == tex
258 end
259
260 fun test_list_unordered is test do
261 var md = """
262 * item 1
263 * item 2
264 """
265 var tex = """
266 \\begin{itemize}
267 \\item
268 item 1
269 \\item
270 item 2
271 \\end{itemize}
272 """
273 assert md_to_tex(md) == tex
274 end
275
276 fun test_list_nested is test do
277 var md = """
278 * item 1
279 * item 2
280 1) item 3
281 2) item 4
282 """
283 var tex = """
284 \\begin{itemize}
285 \\item
286 item 1
287 \\item
288 item 2
289 \\begin{enumerate}
290 \\item
291 item 3
292 \\item
293 item 4
294 \\end{enumerate}
295 \\end{itemize}
296 """
297 assert md_to_tex(md) == tex
298 end
299
300 fun test_ordered_list_nested is test do
301 var md = """
302 4) item 1
303 5) item 2
304
305 4) item 3
306 5) item 4
307
308 4) item 3
309 5) item 4
310
311 4) item 3
312 5) item 4
313 """
314 var tex = """
315 \\begin{enumerate}
316 \\setcounter{enumi}{4}
317 \\item
318 item 1
319 \\item
320 item 2
321 \\begin{enumerate}
322 \\setcounter{enumii}{4}
323 \\item
324 item 3
325 \\item
326 item 4
327 \\begin{enumerate}
328 \\setcounter{enumiii}{4}
329 \\item
330 item 3
331 \\item
332 item 4
333 \\begin{enumerate}
334 \\setcounter{enumiv}{4}
335 \\item
336 item 3
337 \\item
338 item 4
339 \\end{enumerate}
340 \\end{enumerate}
341 \\end{enumerate}
342 \\end{enumerate}
343 """
344 assert md_to_tex(md) == tex
345 end
346
347 fun test_list_and_blockquote is test do
348 var md = """
349 * item 1
350 * item 2
351 > quote 1
352 > quote 2
353 """
354 var tex = """
355 \\begin{itemize}
356 \\item
357 item 1
358 \\item
359 item 2
360 \\begin{quote}
361 quote 1
362 quote 2
363 \\end{quote}
364 \\end{itemize}
365 """
366 assert md_to_tex(md) == tex
367 end
368
369 fun test_blockquote_and_list is test do
370 var md = """
371 > line 1
372 > line 2
373 > * item 1
374 > * item 2
375 """
376 var tex = """
377 \\begin{quote}
378 line 1
379 line 2
380 \\begin{itemize}
381 \\item
382 item 1
383 \\item
384 item 2
385 \\end{itemize}
386 \\end{quote}
387 """
388 assert md_to_tex(md) == tex
389 end
390
391 fun test_code is test do
392 var md = """
393 An `inline code`.
394 """
395 var tex = """
396 An \\texttt{inline code}.
397 """
398 assert md_to_tex(md) == tex
399 end
400
401 fun test_emphasis is test do
402 var md = """
403 An *emphasis* and a **strong emphasis**.
404 """
405 var tex = """
406 An \\textit{emphasis} and a \\textbf{strong emphasis}.
407 """
408 assert md_to_tex(md) == tex
409 end
410
411 fun test_autolink is test do
412 var md = """
413 <http://test>
414 """
415 var tex = """
416 \\url{http://test}
417 """
418 assert md_to_tex(md) == tex
419 end
420
421 fun test_link is test do
422 var md = """
423 A [link](url/).
424 """
425 var tex = """
426 A \\href{url/}{link}.
427 """
428 assert md_to_tex(md) == tex
429 end
430
431 fun test_link_with_title is test do
432 var md = """
433 A [link](url/ "with a title").
434 """
435 var tex = """
436 A \\href{url/}{link (with a title)}.
437 """
438 assert md_to_tex(md) == tex
439 end
440
441 fun test_image is test do
442 var md = """
443 ![image](url/).
444 """
445 var tex = """
446 \\includegraphics{url/}.
447 """
448 assert md_to_tex(md) == tex
449 end
450
451 fun test_softbreak is test do
452 var md = """
453 A soft
454 break.
455 """
456 var tex = """
457 A soft
458 break.
459 """
460 assert md_to_tex(md) == tex
461 end
462
463 fun test_hardbreak is test do
464 var md = """
465 A hard\\
466 break.
467 """
468 var tex = """
469 A hard
470 break.
471 """
472 assert md_to_tex(md) == tex
473 end
474
475 fun test_escaped is test do
476 var md = """
477 An escaped \\*.
478 """
479 var tex = """
480 An escaped *.
481 """
482 assert md_to_tex(md) == tex
483 end
484
485 fun test_forbidden_chars is test do
486 var md = """
487 %${_><#&}\\
488 """
489 var tex = """
490 \\%\\$\\{\\_\\textgreater\\textless\\#\\&\\}\\textbackslash
491 """
492 assert md_to_tex(md) == tex
493 end
494
495 fun test_full_document is test do
496 var md = """
497 # Title
498
499 A paragraph.
500
501 ## Another title
502
503 A list:
504
505 1. item 1
506 2. item 2
507
508 A code example:
509
510 line 1
511 line 2
512
513 Another paragraph.
514 """
515 var tex = """
516 \\section{Title}
517
518 A paragraph.
519
520 \\subsection{Another title}
521
522 A list:
523
524 \\begin{enumerate}
525 \\item
526 item 1
527 \\item
528 item 2
529 \\end{enumerate}
530
531 A code example:
532
533 \\begin{verbatim}
534 line 1
535 line 2
536 \\end{verbatim}
537
538 Another paragraph.
539 """
540 assert md_to_tex(md) == tex
541 end
542 end