| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2017, 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 import 'expectation.dart'; | |
| 6 import 'status_file.dart'; | |
| 7 | |
| 8 /// Tracks the [Expectation]s associated with a set of file paths. | |
| 9 /// | |
| 10 /// For any given file path, returns the expected test results for that file. | |
| 11 /// A set can be loaded from a collection of status files. A file path may | |
| 12 /// exist in multiple files (or even multiple sections within the file). When | |
| 13 /// that happens, all of the expectations of every entry are combined. | |
| 14 class ExpectationSet { | |
| 15 /// Reads the expectations defined by the status files at [statusFilePaths] | |
| 16 /// given [environment]. | |
| 17 static ExpectationSet read( | |
| 18 List<String> statusFilePaths, Map<String, dynamic> environment) { | |
| 19 var expectations = new ExpectationSet._(); | |
| 20 for (var path in statusFilePaths) { | |
| 21 var file = new StatusFile.read(path); | |
| 22 for (var section in file.sections) { | |
| 23 if (section.isEnabled(environment)) { | |
| 24 for (var entry in section.entries) { | |
| 25 expectations.addEntry(entry); | |
| 26 } | |
| 27 } | |
| 28 } | |
| 29 } | |
| 30 | |
| 31 return expectations; | |
| 32 } | |
| 33 | |
| 34 // Only create one copy of each Set<Expectation>. | |
| 35 // We just use .toString as a key, so we may make a few | |
| 36 // sets that only differ in their toString element order. | |
| 37 static Map<String, Set<Expectation>> _cachedSets = {}; | |
| 38 | |
| 39 Map<String, Set<Expectation>> _map = {}; | |
| 40 Map<String, List<RegExp>> _keyToRegExps; | |
| 41 | |
| 42 /// Create a TestExpectations object. See the [expectations] method | |
| 43 /// for an explanation of matching. | |
| 44 ExpectationSet._(); | |
| 45 | |
| 46 /// Add [entry] to the set of expectations. | |
| 47 void addEntry(StatusEntry entry) { | |
| 48 // Once we have started using the expectations we cannot add more | |
| 49 // rules. | |
| 50 if (_keyToRegExps != null) { | |
| 51 throw new StateError("Cannot add entries after it is already in use."); | |
| 52 } | |
| 53 | |
| 54 _map | |
| 55 .putIfAbsent(entry.path, () => new Set<Expectation>()) | |
| 56 .addAll(entry.expectations); | |
| 57 } | |
| 58 | |
| 59 /// Get the expectations for the test at [path]. | |
| 60 /// | |
| 61 /// For every (key, expectation) pair, matches the key with the file name. | |
| 62 /// Returns the union of the expectations for all the keys that match. | |
| 63 /// | |
| 64 /// Normal matching splits the key and the filename into path components and | |
| 65 /// checks that the anchored regular expression "^$keyComponent\$" matches | |
| 66 /// the corresponding filename component. | |
| 67 Set<Expectation> expectations(String path) { | |
| 68 var result = new Set<Expectation>(); | |
| 69 var parts = path.split('/'); | |
| 70 | |
| 71 // Create mapping from keys to list of RegExps once and for all. | |
| 72 _preprocessForMatching(); | |
| 73 | |
| 74 _map.forEach((key, expectations) { | |
| 75 var regExps = _keyToRegExps[key]; | |
| 76 if (regExps.length > parts.length) return; | |
| 77 | |
| 78 for (var i = 0; i < regExps.length; i++) { | |
| 79 if (!regExps[i].hasMatch(parts[i])) return; | |
| 80 } | |
| 81 | |
| 82 // If all components of the status file key matches the filename | |
| 83 // add the expectations to the result. | |
| 84 result.addAll(expectations); | |
| 85 }); | |
| 86 | |
| 87 // If no expectations were found the expectation is that the test | |
| 88 // passes. | |
| 89 if (result.isEmpty) { | |
| 90 result.add(Expectation.pass); | |
| 91 } | |
| 92 return _cachedSets.putIfAbsent(result.toString(), () => result); | |
| 93 } | |
| 94 | |
| 95 /// Preprocesses the expectations for matching against filenames. Generates | |
| 96 /// lists of regular expressions once and for all for each key. | |
| 97 void _preprocessForMatching() { | |
| 98 if (_keyToRegExps != null) return; | |
| 99 | |
| 100 _keyToRegExps = {}; | |
| 101 var regExpCache = <String, RegExp>{}; | |
| 102 | |
| 103 _map.forEach((key, expectations) { | |
| 104 if (_keyToRegExps[key] != null) return; | |
| 105 var splitKey = key.split('/'); | |
| 106 var regExps = new List<RegExp>(splitKey.length); | |
| 107 | |
| 108 for (var i = 0; i < splitKey.length; i++) { | |
| 109 var component = splitKey[i]; | |
| 110 var regExp = regExpCache.putIfAbsent(component, | |
| 111 () => new RegExp("^${splitKey[i]}\$".replaceAll('*', '.*'))); | |
| 112 regExps[i] = regExp; | |
| 113 } | |
| 114 | |
| 115 _keyToRegExps[key] = regExps; | |
| 116 }); | |
| 117 } | |
| 118 } | |
| OLD | NEW |