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

Side by Side Diff: tests/support.dart

Issue 10916294: switch html5lib to new pkg layout (Closed) Base URL: https://github.com/dart-lang/html5lib.git@master
Patch Set: Created 8 years, 3 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
« no previous file with comments | « tests/run.sh ('k') | tests/tokenizer_test.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 /** Support code for the tests in this directory. */
2 #library('support');
3
4 #import('dart:io');
5 #import('../treebuilders/base.dart');
6 #import('../treebuilders/simpletree.dart');
7
8 typedef TreeBuilder TreeBuilderFactory(bool namespaceHTMLElements);
9
10 Map _treeTypes;
11 Map<String, TreeBuilderFactory> get treeTypes {
12 if (_treeTypes == null) {
13 // TODO(jmesserly): add DOM here once it's implemented
14 _treeTypes = { "simpletree": (useNs) => new TreeBuilder(useNs) };
15 }
16 return _treeTypes;
17 }
18
19 final testDataDir = '';
20
21 typedef bool FileMatcher(String fileName);
22
23 Future<List<String>> getDataFiles(String subdirectory, [FileMatcher matcher]) {
24 if (matcher == null) matcher = (path) => path.endsWith('.dat');
25
26 // TODO(jmesserly): should have listSync for scripting...
27 // This entire method was one line of Python code
28 var dir = new Directory.fromPath(new Path('tests/data/$subdirectory'));
29 var lister = dir.list();
30 var files = <String>[];
31 lister.onFile = (file) {
32 if (matcher(file)) files.add(file);
33 };
34 var completer = new Completer<List<String>>();
35 lister.onDone = (success) {
36 completer.complete(files);
37 };
38 return completer.future;
39 }
40
41 // TODO(jmesserly): make this class simpler. We could probably split on
42 // "\n#" instead of newline and remove a lot of code.
43 class TestData implements Iterable<Map> {
44 final String _text;
45 final String newTestHeading;
46
47 TestData(String filename, [this.newTestHeading = "data"])
48 // Note: can't use readAsLinesSync here because it splits on \r
49 : _text = new File(filename).readAsTextSync();
50
51 // Note: in Python this was a generator, but since we can't do that in Dart,
52 // it's easier to convert it into an upfront computation.
53 Iterator<Map> iterator() => _getData().iterator();
54
55 List<Map> _getData() {
56 var data = <String, String>{};
57 var key = null;
58 var result = <Map>[];
59 var lines = _text.split('\n');
60 int numLines = lines.length;
61 // Remove trailing newline to match Python
62 if (lines.last() == '') {
63 lines.removeLast();
64 }
65 for (var line in lines) {
66 var heading = sectionHeading(line);
67 if (heading != null) {
68 if (data.length > 0 && heading == newTestHeading) {
69 // Remove trailing newline
70 data[key] = data[key].substring(0, data[key].length - 1);
71 result.add(normaliseOutput(data));
72 data = <String, String>{};
73 }
74 key = heading;
75 data[key] = "";
76 } else if (key != null) {
77 data[key] = '${data[key]}$line\n';
78 }
79 }
80
81 if (data.length > 0) {
82 result.add(normaliseOutput(data));
83 }
84 return result;
85 }
86
87 /**
88 * If the current heading is a test section heading return the heading,
89 * otherwise return null.
90 */
91 static String sectionHeading(String line) {
92 return line.startsWith("#") ? line.substring(1).trim() : null;
93 }
94
95 static Map normaliseOutput(Map data) {
96 // Remove trailing newlines
97 data.forEach((key, value) {
98 if (value.endsWith("\n")) {
99 data[key] = value.substring(0, value.length - 1);
100 }
101 });
102 return data;
103 }
104 }
105
106 /**
107 * Serialize the [document] into the html5 test data format.
108 */
109 testSerializer(Document document) {
110 return (new TestSerializer()..visit(document)).toString();
111 }
112
113 /** Serializes the DOM into test format. See [testSerializer]. */
114 class TestSerializer extends TreeVisitor {
115 final StringBuffer _str;
116 int _indent = 0;
117 String _spaces = '';
118
119 TestSerializer() : _str = new StringBuffer();
120
121 String toString() => _str.toString();
122
123 int get indent => _indent;
124
125 set indent(int value) {
126 if (_indent == value) return;
127
128 var arr = new List<int>(value);
129 for (int i = 0; i < value; i++) {
130 arr[i] = 32;
131 }
132 _spaces = new String.fromCharCodes(arr);
133 _indent = value;
134 }
135
136 void _newline() {
137 if (_str.length > 0) _str.add('\n');
138 _str.add('|$_spaces');
139 }
140
141 visitNodeFallback(Node node) {
142 _newline();
143 _str.add(node);
144 visitChildren(node);
145 }
146
147 visitChildren(Node node) {
148 indent += 2;
149 for (var child in node.nodes) visit(child);
150 indent -= 2;
151 }
152
153 visitDocument(Document node) {
154 indent += 1;
155 for (var child in node.nodes) visit(child);
156 indent -= 1;
157 }
158
159 visitElement(Element node) {
160 _newline();
161 _str.add(node);
162 if (node.attributes.length > 0) {
163 indent += 2;
164 var keys = new List.from(node.attributes.getKeys());
165 keys.sort((x, y) => x.compareTo(y));
166 for (var key in keys) {
167 var v = node.attributes[key];
168 if (key is AttributeName) {
169 AttributeName attr = key;
170 key = "${attr.prefix} ${attr.name}";
171 }
172 _newline();
173 _str.add('$key="$v"');
174 }
175 indent -= 2;
176 }
177 visitChildren(node);
178 }
179 }
OLDNEW
« no previous file with comments | « tests/run.sh ('k') | tests/tokenizer_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698