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

Side by Side Diff: tests/utils/src/MarkdownTest.dart

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

Powered by Google App Engine
This is Rietveld 408576698