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