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 'dart:collection' show Queue; | 7 import 'dart:collection' show Queue; |
8 import 'package:analyzer/file_system/file_system.dart'; | 8 import 'package:analyzer/file_system/file_system.dart'; |
9 import 'package:analyzer/file_system/memory_file_system.dart'; | 9 import 'package:analyzer/file_system/memory_file_system.dart'; |
10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 } | 171 } |
172 | 172 |
173 void _expectError(AstNode node, _ErrorExpectation expected) { | 173 void _expectError(AstNode node, _ErrorExpectation expected) { |
174 | 174 |
175 // See if we can find the expected error in our actual errors | 175 // See if we can find the expected error in our actual errors |
176 for (var actual in _actualErrors) { | 176 for (var actual in _actualErrors) { |
177 if (actual.offset == node.offset && actual.length == node.length) { | 177 if (actual.offset == node.offset && actual.length == node.length) { |
178 var actualMsg = _formatActualError(actual); | 178 var actualMsg = _formatActualError(actual); |
179 expect(_actualErrorLevel(actual), expected.level, | 179 expect(_actualErrorLevel(actual), expected.level, |
180 reason: 'expected different error code at:\n\n$actualMsg'); | 180 reason: 'expected different error code at:\n\n$actualMsg'); |
181 expect(actual.errorCode.name, expected.typeName, | 181 expect(errorCodeName(actual.errorCode), expected.typeName, |
182 reason: 'expected different error type at:\n\n$actualMsg'); | 182 reason: 'expected different error type at:\n\n$actualMsg'); |
183 | 183 |
184 // We found it. Stop the search. | 184 // We found it. Stop the search. |
185 _actualErrors.remove(actual); | 185 _actualErrors.remove(actual); |
186 return; | 186 return; |
187 } | 187 } |
188 } | 188 } |
189 | 189 |
190 var span = _createSpan(node.offset, node.length); | 190 var span = _createSpan(node.offset, node.length); |
191 var levelName = expected.level.name.toLowerCase(); | 191 var levelName = expected.level.name.toLowerCase(); |
192 var msg = span.message(expected.typeName, color: colorOf(levelName)); | 192 var msg = span.message(expected.typeName, color: colorOf(levelName)); |
193 fail('expected error was not reported at:\n\n$levelName: $msg'); | 193 fail('expected error was not reported at:\n\n$levelName: $msg'); |
194 } | 194 } |
195 | 195 |
196 Level _actualErrorLevel(AnalysisError actual) { | 196 Level _actualErrorLevel(AnalysisError actual) { |
197 return const <ErrorSeverity, Level>{ | 197 return const <ErrorSeverity, Level>{ |
198 ErrorSeverity.ERROR: Level.SEVERE, | 198 ErrorSeverity.ERROR: Level.SEVERE, |
199 ErrorSeverity.WARNING: Level.WARNING, | 199 ErrorSeverity.WARNING: Level.WARNING, |
200 ErrorSeverity.INFO: Level.INFO | 200 ErrorSeverity.INFO: Level.INFO |
201 }[actual.errorCode.errorSeverity]; | 201 }[actual.errorCode.errorSeverity]; |
202 } | 202 } |
203 | 203 |
204 String _formatActualError(AnalysisError actual) { | 204 String _formatActualError(AnalysisError actual) { |
205 var span = _createSpan(actual.offset, actual.length); | 205 var span = _createSpan(actual.offset, actual.length); |
206 var levelName = _actualErrorLevel(actual).name.toLowerCase(); | 206 var levelName = _actualErrorLevel(actual).name.toLowerCase(); |
207 var msg = span.message(actual.message, color: colorOf(levelName)); | 207 var msg = span.message(actual.message, color: colorOf(levelName)); |
208 return '$levelName: [${actual.errorCode.name}] $msg'; | 208 return '$levelName: [${errorCodeName(actual.errorCode)}] $msg'; |
209 } | 209 } |
210 | 210 |
211 SourceSpan _createSpan(int offset, int len) { | 211 SourceSpan _createSpan(int offset, int len) { |
212 return createSpanHelper( | 212 return createSpanHelper( |
213 _unit, offset, offset + len, _unit.element.source, _unitSourceCode); | 213 _unit.lineInfo, offset, offset + len, _unit.element.source, _unitSourceC
ode); |
214 } | 214 } |
215 } | 215 } |
216 | 216 |
217 /// Describes an expected message that should be produced by the checker. | 217 /// Describes an expected message that should be produced by the checker. |
218 class _ErrorExpectation { | 218 class _ErrorExpectation { |
219 final Level level; | 219 final Level level; |
220 final String typeName; | 220 final String typeName; |
221 _ErrorExpectation(this.level, this.typeName); | 221 _ErrorExpectation(this.level, this.typeName); |
222 | 222 |
223 static _ErrorExpectation _parse(String descriptor) { | 223 static _ErrorExpectation _parse(String descriptor) { |
(...skipping 19 matching lines...) Expand all Loading... |
243 expect(tokens[1], "should", reason: 'invalid error descriptor'); | 243 expect(tokens[1], "should", reason: 'invalid error descriptor'); |
244 expect(tokens[2], "be", reason: 'invalid error descriptor'); | 244 expect(tokens[2], "be", reason: 'invalid error descriptor'); |
245 if (tokens[0] == "pass") return null; | 245 if (tokens[0] == "pass") return null; |
246 // TODO(leafp) For now, we just use whatever the current expectation is, | 246 // TODO(leafp) For now, we just use whatever the current expectation is, |
247 // eventually we could do more automated reporting here. | 247 // eventually we could do more automated reporting here. |
248 return _parse(tokens[0]); | 248 return _parse(tokens[0]); |
249 } | 249 } |
250 | 250 |
251 String toString() => '$level $typeName'; | 251 String toString() => '$level $typeName'; |
252 } | 252 } |
OLD | NEW |