OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 dev_compiler.src.testing; | 5 library dev_compiler.src.testing; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
9 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; | 9 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; |
10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 for (var unit in lib.units) { | 90 for (var unit in lib.units) { |
91 unit.unit.accept(visitor); | 91 unit.unit.accept(visitor); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 if (!checkExpectations) return results; | 95 if (!checkExpectations) return results; |
96 | 96 |
97 var total = expectedErrors.values.fold(0, (p, l) => p + l.length); | 97 var total = expectedErrors.values.fold(0, (p, l) => p + l.length); |
98 // Check that all errors we emit are included in the expected map. | 98 // Check that all errors we emit are included in the expected map. |
99 for (var lib in results.libraries) { | 99 for (var lib in results.libraries) { |
100 (reporter as TestReporter).infoMap[lib].forEach((node, actual) { | 100 var uri = lib.library.source.uri; |
| 101 (reporter as TestReporter).infoMap[uri].forEach((node, actual) { |
101 var expected = expectedErrors[node]; | 102 var expected = expectedErrors[node]; |
102 var expectedTotal = expected == null ? 0 : expected.length; | 103 var expectedTotal = expected == null ? 0 : expected.length; |
103 if (actual.length != expectedTotal) { | 104 if (actual.length != expectedTotal) { |
104 expect(actual.length, expectedTotal, | 105 expect(actual.length, expectedTotal, |
105 reason: 'The checker found ${actual.length} errors on the ' | 106 reason: 'The checker found ${actual.length} errors on the ' |
106 'expression `$node`, but we expected $expectedTotal. These are the ' | 107 'expression `$node`, but we expected $expectedTotal. These are the ' |
107 'errors the checker found:\n\n ${_unexpectedErrors(node, actual)}'); | 108 'errors the checker found:\n\n ${_unexpectedErrors(node, actual)}'); |
108 } | 109 } |
109 | 110 |
110 for (int i = 0; i < expected.length; i++) { | 111 for (int i = 0; i < expected.length; i++) { |
(...skipping 17 matching lines...) Expand all Loading... |
128 ' ${total - newTotal} out of $total expected errors were reported.\n' | 129 ' ${total - newTotal} out of $total expected errors were reported.\n' |
129 'The following errors were not reported:\n' | 130 'The following errors were not reported:\n' |
130 '${_unreportedErrors(expectedErrors)}'); | 131 '${_unreportedErrors(expectedErrors)}'); |
131 } | 132 } |
132 } | 133 } |
133 | 134 |
134 return results; | 135 return results; |
135 } | 136 } |
136 | 137 |
137 class TestReporter extends SummaryReporter { | 138 class TestReporter extends SummaryReporter { |
138 Map<LibraryInfo, Map<AstNode, List<StaticInfo>>> infoMap = {}; | 139 Map<Uri, Map<AstNode, List<StaticInfo>>> infoMap = {}; |
139 LibraryInfo _current; | 140 Uri _current; |
140 | 141 |
141 void enterLibrary(LibraryInfo info) { | 142 void enterLibrary(Uri uri) { |
142 super.enterLibrary(info); | 143 super.enterLibrary(uri); |
143 infoMap[info] = {}; | 144 infoMap[uri] = {}; |
144 _current = info; | 145 _current = uri; |
145 } | 146 } |
146 | 147 |
147 void log(StaticInfo info) { | 148 void log(Message info) { |
148 super.log(info); | 149 super.log(info); |
| 150 if (info is! StaticInfo) return; |
149 infoMap[_current].putIfAbsent(info.node, () => []).add(info); | 151 infoMap[_current].putIfAbsent(info.node, () => []).add(info); |
150 } | 152 } |
151 } | 153 } |
152 | 154 |
153 /// Create an error explanation for errors that were not expected, but that the | 155 /// Create an error explanation for errors that were not expected, but that the |
154 /// checker produced. | 156 /// checker produced. |
155 String _unexpectedErrors(AstNode node, List errors) { | 157 String _unexpectedErrors(AstNode node, List errors) { |
156 final span = _spanFor(node); | 158 final span = _spanFor(node); |
157 return errors.map((e) { | 159 return errors.map((e) { |
158 var level = e.level.name.toLowerCase(); | 160 var level = e.level.name.toLowerCase(); |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 | 323 |
322 SourceSpan spanFor(AstNode node) { | 324 SourceSpan spanFor(AstNode node) { |
323 final begin = node is AnnotatedNode | 325 final begin = node is AnnotatedNode |
324 ? node.firstTokenAfterCommentAndMetadata.offset | 326 ? node.firstTokenAfterCommentAndMetadata.offset |
325 : node.offset; | 327 : node.offset; |
326 return _file.span(begin, node.end); | 328 return _file.span(begin, node.end); |
327 } | 329 } |
328 | 330 |
329 String toString() => '[$runtimeType: $uri]'; | 331 String toString() => '[$runtimeType: $uri]'; |
330 } | 332 } |
OLD | NEW |