| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Unit tests for markdown. | 5 /// Unit tests for markdown. |
| 6 library markdownTests; | 6 library markdownTests; |
| 7 | 7 |
| 8 import 'package:unittest/unittest.dart'; | 8 import 'package:unittest/unittest.dart'; |
| 9 | 9 import 'package:markdown/markdown.dart'; |
| 10 // TODO(rnystrom): Use "package:" URL (#4968). | |
| 11 import '../lib/markdown.dart'; | |
| 12 | 10 |
| 13 /// Most of these tests are based on observing how showdown behaves: | 11 /// Most of these tests are based on observing how showdown behaves: |
| 14 /// http://softwaremaniacs.org/playground/showdown-highlight/ | 12 /// http://softwaremaniacs.org/playground/showdown-highlight/ |
| 15 void main() { | 13 void main() { |
| 16 group('Paragraphs', () { | 14 group('Paragraphs', () { |
| 17 validate('consecutive lines form a single paragraph', ''' | 15 validate('consecutive lines form a single paragraph', ''' |
| 18 This is the first line. | 16 This is the first line. |
| 19 This is the second line. | 17 This is the second line. |
| 20 ''', ''' | 18 ''', ''' |
| 21 <p>This is the first line. | 19 <p>This is the first line. |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 ''', ''' | 430 ''', ''' |
| 433 <pre><code>code | 431 <pre><code>code |
| 434 </code></pre> | 432 </code></pre> |
| 435 '''); | 433 '''); |
| 436 | 434 |
| 437 validate('with an optional language identifier', ''' | 435 validate('with an optional language identifier', ''' |
| 438 ```dart | 436 ```dart |
| 439 code | 437 code |
| 440 ``` | 438 ``` |
| 441 ''', ''' | 439 ''', ''' |
| 442 <pre><code>code | 440 <pre class="dart"><code>code |
| 443 </code></pre> | 441 </code></pre> |
| 444 '''); | 442 '''); |
| 445 | 443 |
| 446 validate('escape HTML characters', ''' | 444 validate('escape HTML characters', ''' |
| 447 ``` | 445 ``` |
| 448 <&> | 446 <&> |
| 449 ``` | 447 ``` |
| 450 ''', ''' | 448 ''', ''' |
| 451 <pre><code><&> | 449 <pre><code><&> |
| 452 </code></pre> | 450 </code></pre> |
| 453 '''); | 451 '''); |
| 452 |
| 453 validate('Pandoc style without language identifier', ''' |
| 454 ~~~~~ |
| 455 code |
| 456 ~~~~~ |
| 457 ''', ''' |
| 458 <pre><code>code |
| 459 </code></pre> |
| 460 '''); |
| 461 |
| 462 validate('Pandoc style with language identifier', ''' |
| 463 ~~~~~dart |
| 464 code |
| 465 ~~~~~ |
| 466 ''', ''' |
| 467 <pre class="dart"><code>code |
| 468 </code></pre> |
| 469 '''); |
| 470 |
| 471 validate('Pandoc style with inner tildes row', ''' |
| 472 ~~~~~ |
| 473 ~~~ |
| 474 code |
| 475 ~~~ |
| 476 ~~~~~ |
| 477 ''', ''' |
| 478 <pre><code>~~~ |
| 479 code |
| 480 ~~~ |
| 481 </code></pre> |
| 482 '''); |
| 454 }); | 483 }); |
| 455 | 484 |
| 456 group('Horizontal rules', () { | 485 group('Horizontal rules', () { |
| 457 validate('from dashes', ''' | 486 validate('from dashes', ''' |
| 458 --- | 487 --- |
| 459 ''', ''' | 488 ''', ''' |
| 460 <hr /> | 489 <hr /> |
| 461 '''); | 490 '''); |
| 462 | 491 |
| 463 validate('from asterisks', ''' | 492 validate('from asterisks', ''' |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 853 | 882 |
| 854 validate('dart custom links', 'links [are<foo>] awesome', | 883 validate('dart custom links', 'links [are<foo>] awesome', |
| 855 '<p>links <a>are<foo></a> awesome</p>', | 884 '<p>links <a>are<foo></a> awesome</p>', |
| 856 linkResolver: (text) => new Element.text('a', text.replaceAll('<', | 885 linkResolver: (text) => new Element.text('a', text.replaceAll('<', |
| 857 '<'))); | 886 '<'))); |
| 858 | 887 |
| 859 // TODO(amouravski): need more tests here for custom syntaxes, as some | 888 // TODO(amouravski): need more tests here for custom syntaxes, as some |
| 860 // things are not quite working properly. The regexps are sometime a little | 889 // things are not quite working properly. The regexps are sometime a little |
| 861 // too greedy, I think. | 890 // too greedy, I think. |
| 862 }); | 891 }); |
| 892 |
| 893 group('Inline only', () { |
| 894 validate('simple line', ''' |
| 895 This would normally create a paragraph. |
| 896 ''', ''' |
| 897 This would normally create a paragraph. |
| 898 ''', inlineOnly: true); |
| 899 validate('strong and em', ''' |
| 900 This would _normally_ create a **paragraph**. |
| 901 ''', ''' |
| 902 This would <em>normally</em> create a <strong>paragraph</strong>. |
| 903 ''', inlineOnly: true); |
| 904 validate('link', ''' |
| 905 This [link](http://www.example.com/) will work normally. |
| 906 ''', ''' |
| 907 This <a href="http://www.example.com/">link</a> will work normally. |
| 908 ''', inlineOnly: true); |
| 909 validate('references do not work', ''' |
| 910 [This][] shouldn't work, though. |
| 911 ''', ''' |
| 912 [This][] shouldn't work, though. |
| 913 ''', inlineOnly: true); |
| 914 validate('less than and ampersand are escaped', ''' |
| 915 < & |
| 916 ''', ''' |
| 917 < & |
| 918 ''', inlineOnly: true); |
| 919 validate('keeps newlines', ''' |
| 920 This paragraph |
| 921 continues after a newline. |
| 922 ''', ''' |
| 923 This paragraph |
| 924 continues after a newline. |
| 925 ''', inlineOnly: true); |
| 926 validate('ignores block-level markdown syntax', ''' |
| 927 1. This will not be an <ol>. |
| 928 ''', ''' |
| 929 1. This will not be an <ol>. |
| 930 ''', inlineOnly: true); |
| 931 }); |
| 863 } | 932 } |
| 864 | 933 |
| 865 /** | 934 /** |
| 866 * Removes eight spaces of leading indentation from a multiline string. | 935 * Removes eight spaces of leading indentation from a multiline string. |
| 867 * | 936 * |
| 868 * Note that this is very sensitive to how the literals are styled. They should | 937 * Note that this is very sensitive to how the literals are styled. They should |
| 869 * be: | 938 * be: |
| 870 * ''' | 939 * ''' |
| 871 * Text starts on own line. Lines up with subsequent lines. | 940 * Text starts on own line. Lines up with subsequent lines. |
| 872 * Lines are indented exactly 8 characters from the left margin.''' | 941 * Lines are indented exactly 8 characters from the left margin.''' |
| (...skipping 10 matching lines...) Expand all Loading... |
| 883 if (lines[j].length > 8) { | 952 if (lines[j].length > 8) { |
| 884 lines[j] = lines[j].substring(8, lines[j].length); | 953 lines[j] = lines[j].substring(8, lines[j].length); |
| 885 } else { | 954 } else { |
| 886 lines[j] = ''; | 955 lines[j] = ''; |
| 887 } | 956 } |
| 888 } | 957 } |
| 889 | 958 |
| 890 return lines.join('\n'); | 959 return lines.join('\n'); |
| 891 } | 960 } |
| 892 | 961 |
| 893 validate(String description, String markdown, String html, | 962 void validate(String description, String markdown, String html, |
| 894 {bool verbose: false, inlineSyntaxes, linkResolver}) { | 963 {bool verbose: false, inlineSyntaxes, linkResolver, |
| 964 bool inlineOnly: false}) { |
| 895 test(description, () { | 965 test(description, () { |
| 896 markdown = cleanUpLiteral(markdown); | 966 markdown = cleanUpLiteral(markdown); |
| 897 html = cleanUpLiteral(html); | 967 html = cleanUpLiteral(html); |
| 898 | 968 |
| 899 var result = markdownToHtml(markdown, inlineSyntaxes: inlineSyntaxes, | 969 var result = markdownToHtml(markdown, inlineSyntaxes: inlineSyntaxes, |
| 900 linkResolver: linkResolver); | 970 linkResolver: linkResolver, inlineOnly: inlineOnly); |
| 901 var passed = compareOutput(html, result); | 971 var passed = compareOutput(html, result); |
| 902 | 972 |
| 903 if (!passed) { | 973 if (!passed) { |
| 904 // Remove trailing newline. | 974 // Remove trailing newline. |
| 905 html = html.substring(0, html.length - 1); | 975 html = html.substring(0, html.length - 1); |
| 906 | 976 |
| 907 print('FAIL: $description'); | 977 var sb = new StringBuffer(); |
| 908 print(' expect: ${html.replaceAll("\n", "\n ")}'); | 978 sb.writeln('Expected: ${html.replaceAll("\n", "\n ")}'); |
| 909 print(' actual: ${result.replaceAll("\n", "\n ")}'); | 979 sb.writeln(' Actual: ${result.replaceAll("\n", "\n ")}'); |
| 910 print(''); | 980 |
| 981 fail(sb.toString()); |
| 911 } | 982 } |
| 912 | |
| 913 expect(passed, isTrue, verbose: verbose); | |
| 914 }); | 983 }); |
| 915 } | 984 } |
| 916 | 985 |
| 917 /// Does a loose comparison of the two strings of HTML. Ignores differences in | 986 /// Does a loose comparison of the two strings of HTML. Ignores differences in |
| 918 /// newlines and indentation. | 987 /// newlines and indentation. |
| 919 compareOutput(String a, String b) { | 988 bool compareOutput(String a, String b) { |
| 920 int i = 0; | 989 int i = 0; |
| 921 int j = 0; | 990 int j = 0; |
| 922 | 991 |
| 923 skipIgnored(String s, int i) { | 992 skipIgnored(String s, int i) { |
| 924 // Ignore newlines. | 993 // Ignore newlines. |
| 925 while ((i < s.length) && (s[i] == '\n')) { | 994 while ((i < s.length) && (s[i] == '\n')) { |
| 926 i++; | 995 i++; |
| 927 // Ignore indentation. | 996 // Ignore indentation. |
| 928 while ((i < s.length) && (s[i] == ' ')) i++; | 997 while ((i < s.length) && (s[i] == ' ')) i++; |
| 929 } | 998 } |
| 930 | 999 |
| 931 return i; | 1000 return i; |
| 932 } | 1001 } |
| 933 | 1002 |
| 934 while (true) { | 1003 while (true) { |
| 935 i = skipIgnored(a, i); | 1004 i = skipIgnored(a, i); |
| 936 j = skipIgnored(b, j); | 1005 j = skipIgnored(b, j); |
| 937 | 1006 |
| 938 // If one string runs out of non-ignored strings, the other must too. | 1007 // If one string runs out of non-ignored strings, the other must too. |
| 939 if (i == a.length) return j == b.length; | 1008 if (i == a.length) return j == b.length; |
| 940 if (j == b.length) return i == a.length; | 1009 if (j == b.length) return i == a.length; |
| 941 | 1010 |
| 942 if (a[i] != b[j]) return false; | 1011 if (a[i] != b[j]) return false; |
| 943 i++; | 1012 i++; |
| 944 j++; | 1013 j++; |
| 945 } | 1014 } |
| 946 } | 1015 } |
| OLD | NEW |