OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 library markdown.test.utils; |
| 6 |
| 7 import 'package:unittest/unittest.dart'; |
| 8 |
| 9 import 'package:markdown/markdown.dart'; |
| 10 |
| 11 /// Removes eight spaces of leading indentation from a multiline string. |
| 12 /// |
| 13 /// Note that this is very sensitive to how the literals are styled. They should |
| 14 /// be: |
| 15 /// ''' |
| 16 /// Text starts on own line. Lines up with subsequent lines. |
| 17 /// Lines are indented exactly 8 characters from the left margin.''' |
| 18 /// |
| 19 /// This does nothing if text is only a single line. |
| 20 // TODO(nweiz): Make this auto-detect the indentation level from the first |
| 21 // non-whitespace line. |
| 22 String cleanUpLiteral(String text) { |
| 23 var lines = text.split('\n'); |
| 24 if (lines.length <= 1) return text; |
| 25 |
| 26 for (var j = 0; j < lines.length; j++) { |
| 27 if (lines[j].length > 8) { |
| 28 lines[j] = lines[j].substring(8, lines[j].length); |
| 29 } else { |
| 30 lines[j] = ''; |
| 31 } |
| 32 } |
| 33 |
| 34 return lines.join('\n'); |
| 35 } |
| 36 |
| 37 void validate(String description, String markdown, String html, |
| 38 {List<InlineSyntax> inlineSyntaxes, |
| 39 Resolver linkResolver, Resolver imageLinkResolver, |
| 40 bool inlineOnly: false}) { |
| 41 test(description, () { |
| 42 markdown = cleanUpLiteral(markdown); |
| 43 html = cleanUpLiteral(html); |
| 44 |
| 45 var result = markdownToHtml(markdown, |
| 46 inlineSyntaxes: inlineSyntaxes, |
| 47 linkResolver: linkResolver, |
| 48 imageLinkResolver: imageLinkResolver, |
| 49 inlineOnly: inlineOnly); |
| 50 var passed = compareOutput(html, result); |
| 51 |
| 52 if (!passed) { |
| 53 // Remove trailing newline. |
| 54 html = html.substring(0, html.length - 1); |
| 55 |
| 56 var sb = new StringBuffer(); |
| 57 sb.writeln('Expected: ${html.replaceAll("\n", "\n ")}'); |
| 58 sb.writeln(' Actual: ${result.replaceAll("\n", "\n ")}'); |
| 59 |
| 60 fail(sb.toString()); |
| 61 } |
| 62 }); |
| 63 } |
| 64 |
| 65 /// Does a loose comparison of the two strings of HTML. Ignores differences in |
| 66 /// newlines and indentation. |
| 67 bool compareOutput(String a, String b) { |
| 68 int i = 0; |
| 69 int j = 0; |
| 70 |
| 71 skipIgnored(String s, int i) { |
| 72 // Ignore newlines. |
| 73 while ((i < s.length) && (s[i] == '\n')) { |
| 74 i++; |
| 75 // Ignore indentation. |
| 76 while ((i < s.length) && (s[i] == ' ')) i++; |
| 77 } |
| 78 |
| 79 return i; |
| 80 } |
| 81 |
| 82 while (true) { |
| 83 i = skipIgnored(a, i); |
| 84 j = skipIgnored(b, j); |
| 85 |
| 86 // If one string runs out of non-ignored strings, the other must too. |
| 87 if (i == a.length) return j == b.length; |
| 88 if (j == b.length) return i == a.length; |
| 89 |
| 90 if (a[i] != b[j]) return false; |
| 91 i++; |
| 92 j++; |
| 93 } |
| 94 } |
OLD | NEW |