Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(672)

Side by Side Diff: tests/utils/markdown_test.dart

Issue 10909214: Move dartdoc tests into dartdoc. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/utils/dartdoc_test.dart ('k') | tests/utils/utils.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// Unit tests for markdown.
6 #library('markdown_tests');
7
8 // TODO(rnystrom): Move this test into pkg/dartdoc/test.
9 #import('../../pkg/dartdoc/lib/markdown.dart');
10
11 // TODO(rnystrom): Better path to unittest.
12 #import('../../pkg/unittest/unittest.dart');
13 #import('test_utils.dart');
14
15 /// Most of these tests are based on observing how showdown behaves:
16 /// http://softwaremaniacs.org/playground/showdown-highlight/
17 void main() {
18 group('Paragraphs', () {
19 validate('consecutive lines form a single paragraph', '''
20 This is the first line.
21 This is the second line.
22 ''', '''
23 <p>This is the first line.
24 This is the second line.</p>
25 ''');
26
27 // TODO(rnystrom): The rules here for what happens to lines following a
28 // paragraph appear to be completely arbitrary in markdown. If it makes the
29 // code significantly cleaner, we should consider ourselves free to change
30 // these tests.
31
32 validate('are terminated by a header', '''
33 para
34 # header
35 ''', '''
36 <p>para</p>
37 <h1>header</h1>
38 ''');
39
40 validate('are terminated by a setext header', '''
41 para
42 header
43 ==
44 ''', '''
45 <p>para</p>
46 <h1>header</h1>
47 ''');
48
49 validate('are terminated by a hr', '''
50 para
51 ___
52 ''', '''
53 <p>para</p>
54 <hr />
55 ''');
56
57 validate('consume an unordered list', '''
58 para
59 * list
60 ''', '''
61 <p>para
62 * list</p>
63 ''');
64
65 validate('consume an ordered list', '''
66 para
67 1. list
68 ''', '''
69 <p>para
70 1. list</p>
71 ''');
72 });
73
74 group('Setext headers', () {
75 validate('h1', '''
76 text
77 ===
78 ''', '''
79 <h1>text</h1>
80 ''');
81
82 validate('h2', '''
83 text
84 ---
85 ''', '''
86 <h2>text</h2>
87 ''');
88
89 validate('h1 on first line becomes text', '''
90 ===
91 ''', '''
92 <p>===</p>
93 ''');
94
95 validate('h2 on first line becomes text', '''
96 -
97 ''', '''
98 <p>-</p>
99 ''');
100
101 validate('h1 turns preceding list into text', '''
102 - list
103 ===
104 ''', '''
105 <h1>- list</h1>
106 ''');
107
108 validate('h2 turns preceding list into text', '''
109 - list
110 ===
111 ''', '''
112 <h1>- list</h1>
113 ''');
114
115 validate('h1 turns preceding blockquote into text', '''
116 > quote
117 ===
118 ''', '''
119 <h1>> quote</h1>
120 ''');
121
122 validate('h2 turns preceding blockquote into text', '''
123 > quote
124 ===
125 ''', '''
126 <h1>> quote</h1>
127 ''');
128 });
129
130 group('Headers', () {
131 validate('h1', '''
132 # header
133 ''', '''
134 <h1>header</h1>
135 ''');
136
137 validate('h2', '''
138 ## header
139 ''', '''
140 <h2>header</h2>
141 ''');
142
143 validate('h3', '''
144 ### header
145 ''', '''
146 <h3>header</h3>
147 ''');
148
149 validate('h4', '''
150 #### header
151 ''', '''
152 <h4>header</h4>
153 ''');
154
155 validate('h5', '''
156 ##### header
157 ''', '''
158 <h5>header</h5>
159 ''');
160
161 validate('h6', '''
162 ###### header
163 ''', '''
164 <h6>header</h6>
165 ''');
166
167 validate('trailing "#" are removed', '''
168 # header ######
169 ''', '''
170 <h1>header</h1>
171 ''');
172
173 });
174
175 group('Unordered lists', () {
176 validate('asterisk, plus and hyphen', '''
177 * star
178 - dash
179 + plus
180 ''', '''
181 <ul>
182 <li>star</li>
183 <li>dash</li>
184 <li>plus</li>
185 </ul>
186 ''');
187
188 validate('allow numbered lines after first', '''
189 * a
190 1. b
191 ''', '''
192 <ul>
193 <li>a</li>
194 <li>b</li>
195 </ul>
196 ''');
197
198 validate('allow a tab after the marker', '''
199 *\ta
200 +\tb
201 -\tc
202 1.\td
203 ''', '''
204 <ul>
205 <li>a</li>
206 <li>b</li>
207 <li>c</li>
208 <li>d</li>
209 </ul>
210 ''');
211
212 validate('wrap items in paragraphs if blank lines separate', '''
213 * one
214
215 * two
216 ''', '''
217 <ul>
218 <li><p>one</p></li>
219 <li><p>two</p></li>
220 </ul>
221 ''');
222
223 validate('force paragraph on item before and after blank lines', '''
224 * one
225 * two
226
227 * three
228 ''', '''
229 <ul>
230 <li>one</li>
231 <li>
232 <p>two</p>
233 </li>
234 <li>
235 <p>three</p>
236 </li>
237 </ul>
238 ''');
239
240 validate('do not force paragraph if item is already block', '''
241 * > quote
242
243 * # header
244 ''', '''
245 <ul>
246 <li><blockquote><p>quote</p></blockquote></li>
247 <li><h1>header</h1></li>
248 </ul>
249 ''');
250
251 validate('can contain multiple paragraphs', '''
252 * one
253
254 two
255
256 * three
257 ''', '''
258 <ul>
259 <li>
260 <p>one</p>
261 <p>two</p>
262 </li>
263 <li>
264 <p>three</p>
265 </li>
266 </ul>
267 ''');
268
269 // TODO(rnystrom): This is how most other markdown parsers handle
270 // this but that seems like a nasty special case. For now, let's not
271 // worry about it.
272 /*
273 validate('can nest using indentation', '''
274 * parent
275 * child
276 ''', '''
277 <ul>
278 <li>parent
279 <ul><li>child</li></ul></li>
280 </ul>
281 ''');
282 */
283 });
284
285 group('Ordered lists', () {
286 validate('start with numbers', '''
287 1. one
288 45. two
289 12345. three
290 ''', '''
291 <ol>
292 <li>one</li>
293 <li>two</li>
294 <li>three</li>
295 </ol>
296 ''');
297
298 validate('allow unordered lines after first', '''
299 1. a
300 * b
301 ''', '''
302 <ol>
303 <li>a</li>
304 <li>b</li>
305 </ol>
306 ''');
307 });
308
309 group('Blockquotes', () {
310 validate('single line', '''
311 > blah
312 ''', '''
313 <blockquote>
314 <p>blah</p>
315 </blockquote>
316 ''');
317
318 validate('with two paragraphs', '''
319 > first
320 >
321 > second
322 ''', '''
323 <blockquote>
324 <p>first</p>
325 <p>second</p>
326 </blockquote>
327 ''');
328
329 validate('nested', '''
330 > one
331 >> two
332 > > > three
333 ''', '''
334 <blockquote>
335 <p>one</p>
336 <blockquote>
337 <p>two</p>
338 <blockquote>
339 <p>three</p>
340 </blockquote>
341 </blockquote>
342 </blockquote>
343 ''');
344 });
345
346 group('Code blocks', () {
347 validate('single line', '''
348 code
349 ''', '''
350 <pre><code>code</code></pre>
351 ''');
352
353 validate('include leading whitespace after indentation', '''
354 zero
355 one
356 two
357 three
358 ''', '''
359 <pre><code>zero
360 one
361 two
362 three</code></pre>
363 ''');
364
365 validate('escape HTML characters', '''
366 <&>
367 ''', '''
368 <pre><code>&lt;&amp;&gt;</code></pre>
369 ''');
370 });
371
372 group('Horizontal rules', () {
373 validate('from dashes', '''
374 ---
375 ''', '''
376 <hr />
377 ''');
378
379 validate('from asterisks', '''
380 ***
381 ''', '''
382 <hr />
383 ''');
384
385 validate('from underscores', '''
386 ___
387 ''', '''
388 <hr />
389 ''');
390
391 validate('can include up to two spaces', '''
392 _ _ _
393 ''', '''
394 <hr />
395 ''');
396 });
397
398 group('Block-level HTML', () {
399 validate('single line', '''
400 <table></table>
401 ''', '''
402 <table></table>
403 ''');
404
405 validate('multi-line', '''
406 <table>
407 blah
408 </table>
409 ''', '''
410 <table>
411 blah
412 </table>
413 ''');
414
415 validate('blank line ends block', '''
416 <table>
417 blah
418 </table>
419
420 para
421 ''', '''
422 <table>
423 blah
424 </table>
425 <p>para</p>
426 ''');
427
428 validate('HTML can be bogus', '''
429 <bogus>
430 blah
431 </weird>
432
433 para
434 ''', '''
435 <bogus>
436 blah
437 </weird>
438 <p>para</p>
439 ''');
440 });
441
442 group('Strong', () {
443 validate('using asterisks', '''
444 before **strong** after
445 ''', '''
446 <p>before <strong>strong</strong> after</p>
447 ''');
448
449 validate('using underscores', '''
450 before __strong__ after
451 ''', '''
452 <p>before <strong>strong</strong> after</p>
453 ''');
454
455 validate('unmatched asterisks', '''
456 before ** after
457 ''', '''
458 <p>before ** after</p>
459 ''');
460
461 validate('unmatched underscores', '''
462 before __ after
463 ''', '''
464 <p>before __ after</p>
465 ''');
466
467 validate('multiple spans in one text', '''
468 a **one** b __two__ c
469 ''', '''
470 <p>a <strong>one</strong> b <strong>two</strong> c</p>
471 ''');
472
473 validate('multi-line', '''
474 before **first
475 second** after
476 ''', '''
477 <p>before <strong>first
478 second</strong> after</p>
479 ''');
480 });
481
482 group('Emphasis and strong', () {
483 validate('single asterisks', '''
484 before *em* after
485 ''', '''
486 <p>before <em>em</em> after</p>
487 ''');
488
489 validate('single underscores', '''
490 before _em_ after
491 ''', '''
492 <p>before <em>em</em> after</p>
493 ''');
494
495 validate('double asterisks', '''
496 before **strong** after
497 ''', '''
498 <p>before <strong>strong</strong> after</p>
499 ''');
500
501 validate('double underscores', '''
502 before __strong__ after
503 ''', '''
504 <p>before <strong>strong</strong> after</p>
505 ''');
506
507 validate('unmatched asterisk', '''
508 before *after
509 ''', '''
510 <p>before *after</p>
511 ''');
512
513 validate('unmatched underscore', '''
514 before _after
515 ''', '''
516 <p>before _after</p>
517 ''');
518
519 validate('multiple spans in one text', '''
520 a *one* b _two_ c
521 ''', '''
522 <p>a <em>one</em> b <em>two</em> c</p>
523 ''');
524
525 validate('multi-line', '''
526 before *first
527 second* after
528 ''', '''
529 <p>before <em>first
530 second</em> after</p>
531 ''');
532
533 validate('not processed when surrounded by spaces', '''
534 a * b * c _ d _ e
535 ''', '''
536 <p>a * b * c _ d _ e</p>
537 ''');
538
539 validate('strong then emphasis', '''
540 **strong***em*
541 ''', '''
542 <p><strong>strong</strong><em>em</em></p>
543 ''');
544
545 validate('emphasis then strong', '''
546 *em***strong**
547 ''', '''
548 <p><em>em</em><strong>strong</strong></p>
549 ''');
550
551 validate('emphasis inside strong', '''
552 **strong *em***
553 ''', '''
554 <p><strong>strong <em>em</em></strong></p>
555 ''');
556
557 validate('mismatched in nested', '''
558 *a _b* c_
559 ''', '''
560 <p><em>a _b</em> c_</p>
561 ''');
562
563 validate('cannot nest tags of same type', '''
564 *a _b *c* d_ e*
565 ''', '''
566 <p><em>a _b </em>c<em> d_ e</em></p>
567 ''');
568 });
569
570 group('Inline code', () {
571 validate('simple case', '''
572 before `source` after
573 ''', '''
574 <p>before <code>source</code> after</p>
575 ''');
576
577 validate('unmatched backtick', '''
578 before ` after
579 ''', '''
580 <p>before ` after</p>
581 ''');
582 validate('multiple spans in one text', '''
583 a `one` b `two` c
584 ''', '''
585 <p>a <code>one</code> b <code>two</code> c</p>
586 ''');
587
588 validate('multi-line', '''
589 before `first
590 second` after
591 ''', '''
592 <p>before <code>first
593 second</code> after</p>
594 ''');
595
596 validate('double backticks', '''
597 before ``can `contain` backticks`` after
598 ''', '''
599 <p>before <code>can `contain` backticks</code> after</p>
600 ''');
601
602 validate('double backticks with spaces', '''
603 before `` `tick` `` after
604 ''', '''
605 <p>before <code>`tick`</code> after</p>
606 ''');
607
608 validate('multiline double backticks with spaces', '''
609 before ``in `tick`
610 another`` after
611 ''', '''
612 <p>before <code>in `tick`
613 another</code> after</p>
614 ''');
615
616 validate('ignore markup inside code', '''
617 before `*b* _c_` after
618 ''', '''
619 <p>before <code>*b* _c_</code> after</p>
620 ''');
621
622 validate('escape HTML characters', '''
623 `<&>`
624 ''', '''
625 <p><code>&lt;&amp;&gt;</code></p>
626 ''');
627
628 validate('escape HTML tags', '''
629 '*' `<em>`
630 ''', '''
631 <p>'*' <code>&lt;em&gt;</code></p>
632 ''');
633 });
634
635 group('HTML encoding', () {
636 validate('less than and ampersand are escaped', '''
637 < &
638 ''', '''
639 <p>&lt; &amp;</p>
640 ''');
641 validate('greater than is not escaped', '''
642 not you >
643 ''', '''
644 <p>not you ></p>
645 ''');
646 validate('existing entities are untouched', '''
647 &amp;
648 ''', '''
649 <p>&amp;</p>
650 ''');
651 });
652
653 group('Autolinks', () {
654 validate('basic link', '''
655 before <http://foo.com/> after
656 ''', '''
657 <p>before <a href="http://foo.com/">http://foo.com/</a> after</p>
658 ''');
659 validate('handles ampersand in url', '''
660 <http://foo.com/?a=1&b=2>
661 ''', '''
662 <p><a href="http://foo.com/?a=1&b=2">http://foo.com/?a=1&amp;b=2</a></p>
663 ''');
664 });
665
666 group('Reference links', () {
667 validate('double quotes for title', '''
668 links [are] [a] awesome
669
670 [a]: http://foo.com "woo"
671 ''', '''
672 <p>links <a href="http://foo.com" title="woo">are</a> awesome</p>
673 ''');
674 validate('single quoted title', """
675 links [are] [a] awesome
676
677 [a]: http://foo.com 'woo'
678 """, '''
679 <p>links <a href="http://foo.com" title="woo">are</a> awesome</p>
680 ''');
681 validate('parentheses for title', '''
682 links [are] [a] awesome
683
684 [a]: http://foo.com (woo)
685 ''', '''
686 <p>links <a href="http://foo.com" title="woo">are</a> awesome</p>
687 ''');
688 validate('no title', '''
689 links [are] [a] awesome
690
691 [a]: http://foo.com
692 ''', '''
693 <p>links <a href="http://foo.com">are</a> awesome</p>
694 ''');
695 validate('unknown link becomes plaintext', '''
696 [not] [known]
697 ''', '''
698 <p>[not] [known]</p>
699 ''');
700 validate('can style link contents', '''
701 links [*are*] [a] awesome
702
703 [a]: http://foo.com
704 ''', '''
705 <p>links <a href="http://foo.com"><em>are</em></a> awesome</p>
706 ''');
707 validate('inline styles after a bad link are processed', '''
708 [bad] `code`
709 ''', '''
710 <p>[bad] <code>code</code></p>
711 ''');
712 validate('empty reference uses text from link', '''
713 links [are][] awesome
714
715 [are]: http://foo.com
716 ''', '''
717 <p>links <a href="http://foo.com">are</a> awesome</p>
718 ''');
719 validate('references are case-insensitive', '''
720 links [ARE][] awesome
721
722 [are]: http://foo.com
723 ''', '''
724 <p>links <a href="http://foo.com">ARE</a> awesome</p>
725 ''');
726 });
727
728 group('Inline links', () {
729 validate('double quotes for title', '''
730 links [are](http://foo.com "woo") awesome
731 ''', '''
732 <p>links <a href="http://foo.com" title="woo">are</a> awesome</p>
733 ''');
734 validate('no title', '''
735 links [are] (http://foo.com) awesome
736 ''', '''
737 <p>links <a href="http://foo.com">are</a> awesome</p>
738 ''');
739 validate('can style link contents', '''
740 links [*are*](http://foo.com) awesome
741 ''', '''
742 <p>links <a href="http://foo.com"><em>are</em></a> awesome</p>
743 ''');
744 });
745 }
746
747 validate(String description, String markdown, String html) {
748 test(description, () {
749 markdown = cleanUpLiteral(markdown);
750 html = cleanUpLiteral(html);
751
752 var result = markdownToHtml(markdown);
753 var passed = compareOutput(html, result);
754
755 if (!passed) {
756 // Remove trailing newline.
757 html = html.substring(0, html.length - 1);
758
759 print('FAIL: $description');
760 print(' expect: ${html.replaceAll("\n", "\n ")}');
761 print(' actual: ${result.replaceAll("\n", "\n ")}');
762 print('');
763 }
764
765 expect(passed, isTrue);
766 });
767 }
768
769 /// Does a loose comparison of the two strings of HTML. Ignores differences in
770 /// newlines and indentation.
771 compareOutput(String a, String b) {
772 int i = 0;
773 int j = 0;
774
775 skipIgnored(String s, int i) {
776 // Ignore newlines.
777 while ((i < s.length) && (s[i] == '\n')) {
778 i++;
779 // Ignore indentation.
780 while ((i < s.length) && (s[i] == ' ')) i++;
781 }
782
783 return i;
784 }
785
786 while (true) {
787 i = skipIgnored(a, i);
788 j = skipIgnored(b, j);
789
790 // If one string runs out of non-ignored strings, the other must too.
791 if (i == a.length) return j == b.length;
792 if (j == b.length) return i == a.length;
793
794 if (a[i] != b[j]) return false;
795 i++;
796 j++;
797 }
798 }
OLDNEW
« no previous file with comments | « tests/utils/dartdoc_test.dart ('k') | tests/utils/utils.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698