OLD | NEW |
(Empty) | |
| 1 library parser_test; |
| 2 |
| 3 import 'dart:convert'; |
| 4 import 'dart:io'; |
| 5 import 'package:path/path.dart' as pathos; |
| 6 import 'package:unittest/unittest.dart'; |
| 7 import 'package:html/dom.dart'; |
| 8 import 'package:html/parser.dart'; |
| 9 import 'package:html/parser_console.dart' as parser_console; |
| 10 import 'package:html/src/inputstream.dart' as inputstream; |
| 11 import 'support.dart'; |
| 12 |
| 13 // Run the parse error checks |
| 14 // TODO(jmesserly): presumably we want this on by default? |
| 15 final checkParseErrors = false; |
| 16 |
| 17 String namespaceHtml(String expected) { |
| 18 // TODO(jmesserly): this is a workaround for http://dartbug.com/2979 |
| 19 // We can't do regex replace directly =\ |
| 20 // final namespaceExpected = new RegExp(@"^(\s*)<(\S+)>", multiLine: true); |
| 21 // return expected.replaceAll(namespaceExpected, @"$1<html $2>"); |
| 22 final namespaceExpected = new RegExp(r"^(\|\s*)<(\S+)>"); |
| 23 var lines = expected.split("\n"); |
| 24 for (int i = 0; i < lines.length; i++) { |
| 25 var match = namespaceExpected.firstMatch(lines[i]); |
| 26 if (match != null) { |
| 27 lines[i] = "${match[1]}<html ${match[2]}>"; |
| 28 } |
| 29 } |
| 30 return lines.join("\n"); |
| 31 } |
| 32 |
| 33 void runParserTest(String groupName, String innerHTML, String input, |
| 34 String expected, List errors, TreeBuilderFactory treeCtor, |
| 35 bool namespaceHTMLElements) { |
| 36 |
| 37 // XXX - move this out into the setup function |
| 38 // concatenate all consecutive character tokens into a single token |
| 39 var builder = treeCtor(namespaceHTMLElements); |
| 40 var parser = new HtmlParser(input, tree: builder); |
| 41 |
| 42 Node document; |
| 43 if (innerHTML != null) { |
| 44 document = parser.parseFragment(innerHTML); |
| 45 } else { |
| 46 document = parser.parse(); |
| 47 } |
| 48 |
| 49 var output = testSerializer(document); |
| 50 |
| 51 if (namespaceHTMLElements) { |
| 52 expected = namespaceHtml(expected); |
| 53 } |
| 54 |
| 55 expect(output, equals(expected), |
| 56 reason: "\n\nInput:\n$input\n\nExpected:\n$expected\n\nReceived:\n$output"
); |
| 57 |
| 58 if (checkParseErrors) { |
| 59 expect(parser.errors.length, equals(errors.length), |
| 60 reason: "\n\nInput:\n$input\n\nExpected errors (${errors.length}):\n" |
| 61 "${errors.join('\n')}\n\n" |
| 62 "Actual errors (${parser.errors.length}):\n" |
| 63 "${parser.errors.map((e) => '$e').join('\n')}"); |
| 64 } |
| 65 } |
| 66 |
| 67 void main() { |
| 68 test('dart:io', () { |
| 69 // ensure IO support is unregistered |
| 70 expect(inputstream.consoleSupport, |
| 71 new isInstanceOf<inputstream.ConsoleSupport>()); |
| 72 var file = new File('$testDataDir/parser_feature/raw_file.html').openSync(); |
| 73 expect(() => parse(file), throwsA(new isInstanceOf<ArgumentError>())); |
| 74 parser_console.useConsole(); |
| 75 expect(parse(file).body.innerHtml.trim(), 'Hello world!'); |
| 76 }); |
| 77 |
| 78 for (var path in getDataFiles('tree-construction')) { |
| 79 if (!path.endsWith('.dat')) continue; |
| 80 |
| 81 var tests = new TestData(path, "data"); |
| 82 var testName = pathos.basenameWithoutExtension(path); |
| 83 |
| 84 group(testName, () { |
| 85 for (var testData in tests) { |
| 86 var input = testData['data']; |
| 87 var errors = testData['errors']; |
| 88 var innerHTML = testData['document-fragment']; |
| 89 var expected = testData['document']; |
| 90 if (errors != null) { |
| 91 errors = errors.split("\n"); |
| 92 } |
| 93 |
| 94 for (var treeCtor in treeTypes.values) { |
| 95 for (var namespaceHTMLElements in const [false, true]) { |
| 96 test(_nameFor(input), () { |
| 97 runParserTest(testName, innerHTML, input, expected, errors, |
| 98 treeCtor, namespaceHTMLElements); |
| 99 }); |
| 100 } |
| 101 } |
| 102 } |
| 103 }); |
| 104 } |
| 105 } |
| 106 |
| 107 /// Extract the name for the test based on the test input data. |
| 108 _nameFor(String input) { |
| 109 // Using JSON.decode to unescape other unicode characters |
| 110 var escapeQuote = input |
| 111 .replaceAll(new RegExp('\\\\.'), '_') |
| 112 .replaceAll(new RegExp('\u0000'), '_') |
| 113 .replaceAll('"', '\\"') |
| 114 .replaceAll(new RegExp('[\n\r\t]'), '_'); |
| 115 return JSON.decode('"$escapeQuote"'); |
| 116 } |
OLD | NEW |