OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 library status_file_parser; | 5 library status_file_parser; |
6 | 6 |
7 import "dart:async"; | 7 import "dart:async"; |
8 import "dart:convert" show LineSplitter, UTF8; | 8 import "dart:convert" show LineSplitter, UTF8; |
9 import "dart:io"; | 9 import "dart:io"; |
10 | 10 |
11 import "path.dart"; | 11 import "path.dart"; |
12 import "status_expression.dart"; | 12 import "status_expression.dart"; |
13 | 13 |
14 class Expectation { | 14 import '../expectation.dart' show |
15 // Possible outcomes of running a test. | 15 Expectation, |
16 static Expectation PASS = byName('Pass'); | 16 ExpectationSet; |
17 static Expectation CRASH = byName('Crash'); | |
18 static Expectation TIMEOUT = byName('Timeout'); | |
19 static Expectation FAIL = byName('Fail'); | |
20 | |
21 // Special 'FAIL' cases | |
22 static Expectation RUNTIME_ERROR = byName('RuntimeError'); | |
23 static Expectation COMPILETIME_ERROR = byName('CompileTimeError'); | |
24 static Expectation MISSING_RUNTIME_ERROR = byName('MissingRuntimeError'); | |
25 static Expectation MISSING_COMPILETIME_ERROR = | |
26 byName('MissingCompileTimeError'); | |
27 static Expectation STATIC_WARNING = byName('StaticWarning'); | |
28 static Expectation MISSING_STATIC_WARNING = byName('MissingStaticWarning'); | |
29 static Expectation PUB_GET_ERROR = byName('PubGetError'); | |
30 | |
31 // "meta expectations" | |
32 static Expectation OK = byName('Ok'); | |
33 static Expectation SLOW = byName('Slow'); | |
34 static Expectation SKIP = byName('Skip'); | |
35 static Expectation SKIP_SLOW = byName('SkipSlow'); | |
36 static Expectation SKIP_BY_DESIGN = byName('SkipByDesign'); | |
37 | |
38 static Expectation byName(String name) { | |
39 _initialize(); | |
40 name = name.toLowerCase(); | |
41 if (!_AllExpectations.containsKey(name)) { | |
42 throw new Exception("Expectation.byName(name='$name'): Invalid name."); | |
43 } | |
44 return _AllExpectations[name]; | |
45 } | |
46 | |
47 // Keep a map of all possible Expectation objects, initialized lazily. | |
48 static Map<String, Expectation> _AllExpectations; | |
49 static void _initialize() { | |
50 if (_AllExpectations == null) { | |
51 _AllExpectations = new Map<String, Expectation>(); | |
52 | |
53 Expectation build(prettyName, {group: null, isMetaExpectation: false}) { | |
54 var expectation = new Expectation._(prettyName, | |
55 group: group, isMetaExpectation: isMetaExpectation); | |
56 assert(!_AllExpectations.containsKey(expectation.name)); | |
57 return _AllExpectations[expectation.name] = expectation; | |
58 } | |
59 | |
60 var fail = build("Fail"); | |
61 build("Pass"); | |
62 build("Crash"); | |
63 build("Timeout"); | |
64 | |
65 build("MissingCompileTimeError", group: fail); | |
66 build("MissingRuntimeError", group: fail); | |
67 build("CompileTimeError", group: fail); | |
68 build("RuntimeError", group: fail); | |
69 | |
70 build("MissingStaticWarning", group: fail); | |
71 build("StaticWarning", group: fail); | |
72 | |
73 build("PubGetError", group: fail); | |
74 | |
75 var skip = build("Skip", isMetaExpectation: true); | |
76 build("SkipByDesign", isMetaExpectation: true); | |
77 build("SkipSlow", group: skip, isMetaExpectation: true); | |
78 build("Ok", isMetaExpectation: true); | |
79 build("Slow", isMetaExpectation: true); | |
80 } | |
81 } | |
82 | |
83 final String prettyName; | |
84 final String name; | |
85 final Expectation group; | |
86 // Indicates whether this expectation cannot be a test outcome (i.e. it is a | |
87 // "meta marker"). | |
88 final bool isMetaExpectation; | |
89 | |
90 Expectation._(prettyName, | |
91 {Expectation this.group: null, bool this.isMetaExpectation: false}) | |
92 : prettyName = prettyName, | |
93 name = prettyName.toLowerCase(); | |
94 | |
95 bool canBeOutcomeOf(Expectation expectation) { | |
96 Expectation outcome = this; | |
97 while (outcome != null) { | |
98 if (outcome == expectation) { | |
99 return true; | |
100 } | |
101 outcome = outcome.group; | |
102 } | |
103 return false; | |
104 } | |
105 | |
106 String toString() => prettyName; | |
107 } | |
108 | 17 |
109 final RegExp SplitComment = new RegExp("^([^#]*)(#.*)?\$"); | 18 final RegExp SplitComment = new RegExp("^([^#]*)(#.*)?\$"); |
110 final RegExp HeaderPattern = new RegExp(r"^\[([^\]]+)\]"); | 19 final RegExp HeaderPattern = new RegExp(r"^\[([^\]]+)\]"); |
111 final RegExp RulePattern = new RegExp(r"\s*([^: ]*)\s*:(.*)"); | 20 final RegExp RulePattern = new RegExp(r"\s*([^: ]*)\s*:(.*)"); |
112 final RegExp IssueNumberPattern = new RegExp("[Ii]ssue ([0-9]+)"); | 21 final RegExp IssueNumberPattern = new RegExp("[Ii]ssue ([0-9]+)"); |
113 | 22 |
114 class StatusFile { | 23 class StatusFile { |
115 final Path location; | 24 final Path location; |
116 | 25 |
117 StatusFile(this.location); | 26 StatusFile(this.location); |
(...skipping 16 matching lines...) Expand all Loading... |
134 | 43 |
135 bool isEnabled(environment) => | 44 bool isEnabled(environment) => |
136 condition == null || condition.evaluate(environment); | 45 condition == null || condition.evaluate(environment); |
137 | 46 |
138 String toString() { | 47 String toString() { |
139 return "Section: $condition"; | 48 return "Section: $condition"; |
140 } | 49 } |
141 } | 50 } |
142 | 51 |
143 Future<TestExpectations> ReadTestExpectations( | 52 Future<TestExpectations> ReadTestExpectations( |
144 List<String> statusFilePaths, Map environment) { | 53 List<String> statusFilePaths, Map environment, |
145 var testExpectations = new TestExpectations(); | 54 ExpectationSet expectationSet) { |
| 55 var testExpectations = new TestExpectations(expectationSet); |
146 return Future.wait(statusFilePaths.map((String statusFile) { | 56 return Future.wait(statusFilePaths.map((String statusFile) { |
147 return ReadTestExpectationsInto(testExpectations, statusFile, environment); | 57 return ReadTestExpectationsInto(testExpectations, statusFile, environment); |
148 })).then((_) => testExpectations); | 58 })).then((_) => testExpectations); |
149 } | 59 } |
150 | 60 |
151 Future ReadTestExpectationsInto( | 61 Future ReadTestExpectationsInto( |
152 TestExpectations expectations, String statusFilePath, environment) { | 62 TestExpectations expectations, String statusFilePath, environment) { |
153 var completer = new Completer(); | 63 var completer = new Completer(); |
154 List<Section> sections = new List<Section>(); | 64 List<Section> sections = new List<Section>(); |
155 | 65 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 | 150 |
241 String toString() => 'TestRule($name, $expression, $issue)'; | 151 String toString() => 'TestRule($name, $expression, $issue)'; |
242 } | 152 } |
243 | 153 |
244 class TestExpectations { | 154 class TestExpectations { |
245 // Only create one copy of each Set<Expectation>. | 155 // Only create one copy of each Set<Expectation>. |
246 // We just use .toString as a key, so we may make a few | 156 // We just use .toString as a key, so we may make a few |
247 // sets that only differ in their toString element order. | 157 // sets that only differ in their toString element order. |
248 static Map _cachedSets = new Map(); | 158 static Map _cachedSets = new Map(); |
249 | 159 |
| 160 final ExpectationSet expectationSet; |
| 161 |
250 Map _map; | 162 Map _map; |
251 bool _preprocessed = false; | 163 bool _preprocessed = false; |
252 Map _regExpCache; | 164 Map _regExpCache; |
253 Map _keyToRegExps; | 165 Map _keyToRegExps; |
254 | 166 |
255 /** | 167 /** |
256 * Create a TestExpectations object. See the [expectations] method | 168 * Create a TestExpectations object. See the [expectations] method |
257 * for an explanation of matching. | 169 * for an explanation of matching. |
258 */ | 170 */ |
259 TestExpectations() : _map = new Map(); | 171 TestExpectations(this.expectationSet) : _map = new Map(); |
260 | 172 |
261 /** | 173 /** |
262 * Add a rule to the expectations. | 174 * Add a rule to the expectations. |
263 */ | 175 */ |
264 void addRule(testRule, environment) { | 176 void addRule(testRule, environment) { |
265 // Once we have started using the expectations we cannot add more | 177 // Once we have started using the expectations we cannot add more |
266 // rules. | 178 // rules. |
267 if (_preprocessed) { | 179 if (_preprocessed) { |
268 throw "TestExpectations.addRule: cannot add more rules"; | 180 throw "TestExpectations.addRule: cannot add more rules"; |
269 } | 181 } |
270 var names = testRule.expression.evaluate(environment); | 182 var names = testRule.expression.evaluate(environment); |
271 var expectations = names.map((name) => Expectation.byName(name)); | 183 var expectations = names.map((name) => expectationSet[name]); |
272 _map.putIfAbsent(testRule.name, () => new Set()).addAll(expectations); | 184 _map.putIfAbsent(testRule.name, () => new Set()).addAll(expectations); |
273 } | 185 } |
274 | 186 |
275 /** | 187 /** |
276 * Compute the expectations for a test based on the filename. | 188 * Compute the expectations for a test based on the filename. |
277 * | 189 * |
278 * For every (key, expectation) pair. Match the key with the file | 190 * For every (key, expectation) pair. Match the key with the file |
279 * name. Return the union of the expectations for all the keys | 191 * name. Return the union of the expectations for all the keys |
280 * that match. | 192 * that match. |
281 * | 193 * |
(...skipping 15 matching lines...) Expand all Loading... |
297 if (!regExps[i].hasMatch(splitFilename[i])) return; | 209 if (!regExps[i].hasMatch(splitFilename[i])) return; |
298 } | 210 } |
299 // If all components of the status file key matches the filename | 211 // If all components of the status file key matches the filename |
300 // add the expectations to the result. | 212 // add the expectations to the result. |
301 result.addAll(expectation); | 213 result.addAll(expectation); |
302 }); | 214 }); |
303 | 215 |
304 // If no expectations were found the expectation is that the test | 216 // If no expectations were found the expectation is that the test |
305 // passes. | 217 // passes. |
306 if (result.isEmpty) { | 218 if (result.isEmpty) { |
307 result.add(Expectation.PASS); | 219 result.add(Expectation.Pass); |
308 } | 220 } |
309 return _cachedSets.putIfAbsent(result.toString(), () => result); | 221 return _cachedSets.putIfAbsent(result.toString(), () => result); |
310 } | 222 } |
311 | 223 |
312 // Preprocess the expectations for matching against | 224 // Preprocess the expectations for matching against |
313 // filenames. Generate lists of regular expressions once and for all | 225 // filenames. Generate lists of regular expressions once and for all |
314 // for each key. | 226 // for each key. |
315 void _preprocessForMatching() { | 227 void _preprocessForMatching() { |
316 if (_preprocessed) return; | 228 if (_preprocessed) return; |
317 | 229 |
(...skipping 14 matching lines...) Expand all Loading... |
332 } | 244 } |
333 regExps[i] = regExp; | 245 regExps[i] = regExp; |
334 } | 246 } |
335 _keyToRegExps[key] = regExps; | 247 _keyToRegExps[key] = regExps; |
336 }); | 248 }); |
337 | 249 |
338 _regExpCache = null; | 250 _regExpCache = null; |
339 _preprocessed = true; | 251 _preprocessed = true; |
340 } | 252 } |
341 } | 253 } |
OLD | NEW |