OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // TODO(jmesserly): this file needs to be refactored, it's a port from |
| 6 // package:dev_compiler's tests |
| 7 library test.src.task.strong.strong_test_helper; |
| 8 |
| 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 11 import 'package:analyzer/src/context/context.dart' show SdkAnalysisContext; |
| 12 import 'package:analyzer/src/generated/ast.dart'; |
| 13 import 'package:analyzer/src/generated/element.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart' hide SdkAnalysisContext; |
| 15 import 'package:analyzer/src/generated/error.dart'; |
| 16 import 'package:analyzer/src/generated/sdk.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; |
| 18 import 'package:analyzer/src/task/strong/checker.dart'; |
| 19 import 'package:analyzer/src/task/strong/rules.dart'; |
| 20 import 'package:logging/logging.dart'; // TODO(jmesserly): remove |
| 21 import 'package:source_span/source_span.dart'; // TODO(jmesserly): remove |
| 22 import 'package:unittest/unittest.dart'; |
| 23 |
| 24 /// Run the checker on a program with files contents as indicated in |
| 25 /// [testFiles]. |
| 26 /// |
| 27 /// This function makes several assumptions to make it easier to describe error |
| 28 /// expectations: |
| 29 /// |
| 30 /// * a file named `/main.dart` exists in [testFiles]. |
| 31 /// * all expected failures are listed in the source code using comments |
| 32 /// immediately in front of the AST node that should contain the error. |
| 33 /// * errors are formatted as a token `level:Type`, where `level` is the |
| 34 /// logging level were the error would be reported at, and `Type` is the |
| 35 /// concrete subclass of [StaticInfo] that denotes the error. |
| 36 /// |
| 37 /// For example, to check that an assignment produces a warning about a boxing |
| 38 /// conversion, you can describe the test as follows: |
| 39 /// |
| 40 /// testChecker({ |
| 41 /// '/main.dart': ''' |
| 42 /// testMethod() { |
| 43 /// dynamic x = /*warning:Box*/3; |
| 44 /// } |
| 45 /// ''' |
| 46 /// }); |
| 47 /// |
| 48 void testChecker(String name, Map<String, String> testFiles) { |
| 49 test(name, () { |
| 50 expect(testFiles.containsKey('/main.dart'), isTrue, |
| 51 reason: '`/main.dart` is missing in testFiles'); |
| 52 |
| 53 var provider = new MemoryResourceProvider(); |
| 54 testFiles.forEach((key, value) { |
| 55 var scheme = 'package:'; |
| 56 if (key.startsWith(scheme)) { |
| 57 key = '/packages/${key.substring(scheme.length)}'; |
| 58 } |
| 59 provider.newFile(key, value); |
| 60 }); |
| 61 var uriResolver = new TestUriResolver(provider); |
| 62 // Enable task model strong mode |
| 63 AnalysisEngine.instance.useTaskModel = true; |
| 64 var context = AnalysisEngine.instance.createAnalysisContext(); |
| 65 context.analysisOptions.strongMode = true; |
| 66 |
| 67 context.sourceFactory = new SourceFactory([ |
| 68 new MockDartSdk(mockSdkSources, reportMissing: true).resolver, |
| 69 uriResolver |
| 70 ]); |
| 71 |
| 72 // Run the checker on /main.dart. |
| 73 Source mainSource = uriResolver.resolveAbsolute(new Uri.file('/main.dart')); |
| 74 var initialLibrary = |
| 75 context.resolveCompilationUnit2(mainSource, mainSource); |
| 76 |
| 77 var collector = new _ErrorCollector(); |
| 78 var checker = new CodeChecker( |
| 79 new TypeRules(context.typeProvider), collector, |
| 80 hints: true); |
| 81 |
| 82 // Extract expectations from the comments in the test files, and |
| 83 // check that all errors we emit are included in the expected map. |
| 84 var allLibraries = reachableLibraries(initialLibrary.element.library); |
| 85 for (var lib in allLibraries) { |
| 86 for (var unit in lib.units) { |
| 87 var errors = <AnalysisError>[]; |
| 88 collector.errors = errors; |
| 89 |
| 90 var source = unit.source; |
| 91 if (source.uri.scheme == 'dart') continue; |
| 92 |
| 93 var librarySource = context.getLibrariesContaining(source).single; |
| 94 var resolved = context.resolveCompilationUnit2(source, librarySource); |
| 95 checker.visitCompilationUnit(resolved); |
| 96 |
| 97 new _ExpectedErrorVisitor(errors).validate(resolved); |
| 98 } |
| 99 } |
| 100 }); |
| 101 } |
| 102 |
| 103 class _ErrorCollector implements AnalysisErrorListener { |
| 104 List<AnalysisError> errors; |
| 105 final bool hints; |
| 106 _ErrorCollector({this.hints: true}); |
| 107 |
| 108 void onError(AnalysisError error) { |
| 109 // Unless DDC hints are requested, filter them out. |
| 110 var HINT = ErrorSeverity.INFO.ordinal; |
| 111 if (hints || error.errorCode.errorSeverity.ordinal > HINT) { |
| 112 errors.add(error); |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 class TestUriResolver extends ResourceUriResolver { |
| 118 final MemoryResourceProvider provider; |
| 119 TestUriResolver(provider) |
| 120 : provider = provider, |
| 121 super(provider); |
| 122 |
| 123 @override |
| 124 Source resolveAbsolute(Uri uri, [Uri actualUri]) { |
| 125 if (uri.scheme == 'package') { |
| 126 return (provider.getResource('/packages/' + uri.path) as File) |
| 127 .createSource(uri); |
| 128 } |
| 129 return super.resolveAbsolute(uri, actualUri); |
| 130 } |
| 131 } |
| 132 |
| 133 class _ExpectedErrorVisitor extends UnifyingAstVisitor { |
| 134 final Set<AnalysisError> _actualErrors; |
| 135 CompilationUnit _unit; |
| 136 String _unitSourceCode; |
| 137 |
| 138 _ExpectedErrorVisitor(List<AnalysisError> actualErrors) |
| 139 : _actualErrors = new Set.from(actualErrors); |
| 140 |
| 141 validate(CompilationUnit unit) { |
| 142 _unit = unit; |
| 143 // This reads the file. Only safe because tests use MemoryFileSystem. |
| 144 _unitSourceCode = unit.element.source.contents.data; |
| 145 |
| 146 // Visit the compilation unit. |
| 147 unit.accept(this); |
| 148 |
| 149 if (_actualErrors.isNotEmpty) { |
| 150 var actualMsgs = _actualErrors.map(_formatActualError).join('\n'); |
| 151 fail('Unexpected errors reported by checker:\n\n$actualMsgs'); |
| 152 } |
| 153 } |
| 154 |
| 155 visitNode(AstNode node) { |
| 156 var token = node.beginToken; |
| 157 var comment = token.precedingComments; |
| 158 // Use error marker found in an immediately preceding comment, |
| 159 // and attach it to the outermost expression that starts at that token. |
| 160 if (comment != null) { |
| 161 while (comment.next != null) { |
| 162 comment = comment.next; |
| 163 } |
| 164 if (comment.end == token.offset && node.parent.beginToken != token) { |
| 165 var commentText = '$comment'; |
| 166 var start = commentText.lastIndexOf('/*'); |
| 167 var end = commentText.lastIndexOf('*/'); |
| 168 if (start != -1 && end != -1) { |
| 169 expect(start, lessThan(end)); |
| 170 var errors = commentText.substring(start + 2, end).split(','); |
| 171 var expectations = |
| 172 errors.map(_ErrorExpectation.parse).where((x) => x != null); |
| 173 |
| 174 for (var e in expectations) _expectError(node, e); |
| 175 } |
| 176 } |
| 177 } |
| 178 return super.visitNode(node); |
| 179 } |
| 180 |
| 181 void _expectError(AstNode node, _ErrorExpectation expected) { |
| 182 // See if we can find the expected error in our actual errors |
| 183 for (var actual in _actualErrors) { |
| 184 if (actual.offset == node.offset && actual.length == node.length) { |
| 185 var actualMsg = _formatActualError(actual); |
| 186 expect(_actualErrorLevel(actual), expected.level, |
| 187 reason: 'expected different error code at:\n\n$actualMsg'); |
| 188 expect(errorCodeName(actual.errorCode), expected.typeName, |
| 189 reason: 'expected different error type at:\n\n$actualMsg'); |
| 190 |
| 191 // We found it. Stop the search. |
| 192 _actualErrors.remove(actual); |
| 193 return; |
| 194 } |
| 195 } |
| 196 |
| 197 var span = _createSpan(node.offset, node.length); |
| 198 var levelName = expected.level.name.toLowerCase(); |
| 199 var msg = span.message(expected.typeName, color: colorOf(levelName)); |
| 200 fail('expected error was not reported at:\n\n$levelName: $msg'); |
| 201 } |
| 202 |
| 203 Level _actualErrorLevel(AnalysisError actual) { |
| 204 return const <ErrorSeverity, Level>{ |
| 205 ErrorSeverity.ERROR: Level.SEVERE, |
| 206 ErrorSeverity.WARNING: Level.WARNING, |
| 207 ErrorSeverity.INFO: Level.INFO |
| 208 }[actual.errorCode.errorSeverity]; |
| 209 } |
| 210 |
| 211 String _formatActualError(AnalysisError actual) { |
| 212 var span = _createSpan(actual.offset, actual.length); |
| 213 var levelName = _actualErrorLevel(actual).name.toLowerCase(); |
| 214 var msg = span.message(actual.message, color: colorOf(levelName)); |
| 215 return '$levelName: [${errorCodeName(actual.errorCode)}] $msg'; |
| 216 } |
| 217 |
| 218 SourceSpan _createSpan(int offset, int len) { |
| 219 return createSpanHelper(_unit.lineInfo, offset, offset + len, |
| 220 _unit.element.source, _unitSourceCode); |
| 221 } |
| 222 } |
| 223 |
| 224 SourceLocation locationForOffset(LineInfo lineInfo, Uri uri, int offset) { |
| 225 var loc = lineInfo.getLocation(offset); |
| 226 return new SourceLocation(offset, |
| 227 sourceUrl: uri, line: loc.lineNumber - 1, column: loc.columnNumber - 1); |
| 228 } |
| 229 |
| 230 SourceSpanWithContext createSpanHelper( |
| 231 LineInfo lineInfo, int start, int end, Source source, String content) { |
| 232 var startLoc = locationForOffset(lineInfo, source.uri, start); |
| 233 var endLoc = locationForOffset(lineInfo, source.uri, end); |
| 234 |
| 235 var lineStart = startLoc.offset - startLoc.column; |
| 236 // Find the end of the line. This is not exposed directly on LineInfo, but |
| 237 // we can find it pretty easily. |
| 238 // TODO(jmesserly): for now we do the simple linear scan. Ideally we can get |
| 239 // some help from the LineInfo API. |
| 240 int lineEnd = endLoc.offset; |
| 241 int lineNum = lineInfo.getLocation(lineEnd).lineNumber; |
| 242 while (lineEnd < content.length && |
| 243 lineInfo.getLocation(++lineEnd).lineNumber == lineNum); |
| 244 |
| 245 var text = content.substring(start, end); |
| 246 var lineText = content.substring(lineStart, lineEnd); |
| 247 return new SourceSpanWithContext(startLoc, endLoc, text, lineText); |
| 248 } |
| 249 |
| 250 /// Describes an expected message that should be produced by the checker. |
| 251 class _ErrorExpectation { |
| 252 final Level level; |
| 253 final String typeName; |
| 254 _ErrorExpectation(this.level, this.typeName); |
| 255 |
| 256 static _ErrorExpectation _parse(String descriptor) { |
| 257 var tokens = descriptor.split(':'); |
| 258 expect(tokens.length, 2, reason: 'invalid error descriptor'); |
| 259 var name = tokens[0].toUpperCase(); |
| 260 var typeName = tokens[1]; |
| 261 |
| 262 var level = |
| 263 Level.LEVELS.firstWhere((l) => l.name == name, orElse: () => null); |
| 264 expect(level, isNotNull, |
| 265 reason: 'invalid level in error descriptor: `${tokens[0]}`'); |
| 266 expect(typeName, isNotNull, |
| 267 reason: 'invalid type in error descriptor: ${tokens[1]}'); |
| 268 return new _ErrorExpectation(level, typeName); |
| 269 } |
| 270 |
| 271 static _ErrorExpectation parse(String descriptor) { |
| 272 descriptor = descriptor.trim(); |
| 273 var tokens = descriptor.split(' '); |
| 274 if (tokens.length == 1) return _parse(tokens[0]); |
| 275 expect(tokens.length, 4, reason: 'invalid error descriptor'); |
| 276 expect(tokens[1], "should", reason: 'invalid error descriptor'); |
| 277 expect(tokens[2], "be", reason: 'invalid error descriptor'); |
| 278 if (tokens[0] == "pass") return null; |
| 279 // TODO(leafp) For now, we just use whatever the current expectation is, |
| 280 // eventually we could do more automated reporting here. |
| 281 return _parse(tokens[0]); |
| 282 } |
| 283 |
| 284 String toString() => '$level $typeName'; |
| 285 } |
| 286 |
| 287 /// Dart SDK which contains a mock implementation of the SDK libraries. May be |
| 288 /// used to speed up execution when most of the core libraries is not needed. |
| 289 class MockDartSdk implements DartSdk { |
| 290 final Map<Uri, _MockSdkSource> _sources = {}; |
| 291 final bool reportMissing; |
| 292 final Map<String, SdkLibrary> _libs = {}; |
| 293 final String sdkVersion = '0'; |
| 294 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); |
| 295 final AnalysisContext context = new SdkAnalysisContext(); |
| 296 DartUriResolver _resolver; |
| 297 DartUriResolver get resolver => _resolver; |
| 298 |
| 299 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { |
| 300 sources.forEach((uriString, contents) { |
| 301 var uri = Uri.parse(uriString); |
| 302 _sources[uri] = new _MockSdkSource(uri, contents); |
| 303 _libs[uriString] = new SdkLibraryImpl(uri.path) |
| 304 ..setDart2JsLibrary() |
| 305 ..setVmLibrary(); |
| 306 }); |
| 307 _resolver = new DartUriResolver(this); |
| 308 context.sourceFactory = new SourceFactory([_resolver]); |
| 309 } |
| 310 |
| 311 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); |
| 312 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; |
| 313 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); |
| 314 |
| 315 Source fromEncoding(UriKind kind, Uri uri) { |
| 316 if (kind != UriKind.DART_URI) { |
| 317 throw new UnsupportedError('expected dart: uri kind, got $kind.'); |
| 318 } |
| 319 return _getSource(uri); |
| 320 } |
| 321 |
| 322 Source _getSource(Uri uri) { |
| 323 var src = _sources[uri]; |
| 324 if (src == null) { |
| 325 if (reportMissing) print('warning: missing mock for $uri.'); |
| 326 _sources[uri] = |
| 327 src = new _MockSdkSource(uri, 'library dart.${uri.path};'); |
| 328 } |
| 329 return src; |
| 330 } |
| 331 |
| 332 @override |
| 333 Source fromFileUri(Uri uri) { |
| 334 throw new UnsupportedError('MockDartSdk.fromFileUri'); |
| 335 } |
| 336 } |
| 337 |
| 338 class _MockSdkSource implements Source { |
| 339 /// Absolute URI which this source can be imported from. |
| 340 final Uri uri; |
| 341 final String _contents; |
| 342 |
| 343 _MockSdkSource(this.uri, this._contents); |
| 344 |
| 345 bool exists() => true; |
| 346 |
| 347 int get hashCode => uri.hashCode; |
| 348 |
| 349 final int modificationStamp = 1; |
| 350 |
| 351 TimestampedData<String> get contents => |
| 352 new TimestampedData(modificationStamp, _contents); |
| 353 |
| 354 String get encoding => "${uriKind.encoding}$uri"; |
| 355 |
| 356 Source get source => this; |
| 357 |
| 358 String get fullName => shortName; |
| 359 |
| 360 String get shortName => uri.path; |
| 361 |
| 362 UriKind get uriKind => UriKind.DART_URI; |
| 363 |
| 364 bool get isInSystemLibrary => true; |
| 365 |
| 366 Source resolveRelative(Uri relativeUri) => |
| 367 throw new UnsupportedError('not expecting relative urls in dart: mocks'); |
| 368 |
| 369 Uri resolveRelativeUri(Uri relativeUri) => |
| 370 throw new UnsupportedError('not expecting relative urls in dart: mocks'); |
| 371 } |
| 372 |
| 373 /// Sample mock SDK sources. |
| 374 final Map<String, String> mockSdkSources = { |
| 375 // The list of types below is derived from: |
| 376 // * types we use via our smoke queries, including HtmlElement and |
| 377 // types from `_typeHandlers` (deserialize.dart) |
| 378 // * types that are used internally by the resolver (see |
| 379 // _initializeFrom in resolver.dart). |
| 380 'dart:core': ''' |
| 381 library dart.core; |
| 382 |
| 383 void print(Object o) {} |
| 384 |
| 385 class Object { |
| 386 int get hashCode {} |
| 387 Type get runtimeType {} |
| 388 String toString(){} |
| 389 bool ==(other){} |
| 390 } |
| 391 class Function {} |
| 392 class StackTrace {} |
| 393 class Symbol {} |
| 394 class Type {} |
| 395 |
| 396 class String { |
| 397 String operator +(String other) {} |
| 398 } |
| 399 class bool {} |
| 400 class num { |
| 401 num operator +(num other) {} |
| 402 } |
| 403 class int extends num { |
| 404 bool operator<(num other) {} |
| 405 int operator-() {} |
| 406 } |
| 407 class double extends num {} |
| 408 class DateTime {} |
| 409 class Null {} |
| 410 |
| 411 class Deprecated { |
| 412 final String expires; |
| 413 const Deprecated(this.expires); |
| 414 } |
| 415 const Object deprecated = const Deprecated("next release"); |
| 416 class _Override { const _Override(); } |
| 417 const Object override = const _Override(); |
| 418 class _Proxy { const _Proxy(); } |
| 419 const Object proxy = const _Proxy(); |
| 420 |
| 421 class Iterable<E> { |
| 422 fold(initialValue, combine(previousValue, E element)) {} |
| 423 Iterable map(f(E element)) {} |
| 424 } |
| 425 class List<E> implements Iterable<E> { |
| 426 List([int length]); |
| 427 List.filled(int length, E fill); |
| 428 } |
| 429 class Map<K, V> { |
| 430 Iterable<K> get keys {} |
| 431 } |
| 432 ''', |
| 433 'dart:async': ''' |
| 434 class Future<T> { |
| 435 Future(computation()) {} |
| 436 Future.value(T t) {} |
| 437 Future then(onValue(T value)) {} |
| 438 static Future<List> wait(Iterable<Future> futures) {} |
| 439 } |
| 440 class Stream<T> {} |
| 441 ''', |
| 442 'dart:html': ''' |
| 443 library dart.html; |
| 444 class HtmlElement {} |
| 445 ''', |
| 446 'dart:math': ''' |
| 447 library dart.math; |
| 448 class Random { |
| 449 bool nextBool() {} |
| 450 } |
| 451 num min(num x, num y) {} |
| 452 num max(num x, num y) {} |
| 453 ''', |
| 454 }; |
| 455 |
| 456 /// Returns all libraries transitively imported or exported from [start]. |
| 457 List<LibraryElement> reachableLibraries(LibraryElement start) { |
| 458 var results = <LibraryElement>[]; |
| 459 var seen = new Set(); |
| 460 void find(LibraryElement lib) { |
| 461 if (seen.contains(lib)) return; |
| 462 seen.add(lib); |
| 463 results.add(lib); |
| 464 lib.importedLibraries.forEach(find); |
| 465 lib.exportedLibraries.forEach(find); |
| 466 } |
| 467 find(start); |
| 468 return results; |
| 469 } |
| 470 |
| 471 String errorCodeName(ErrorCode errorCode) { |
| 472 var name = errorCode.name; |
| 473 final prefix = 'dev_compiler.'; |
| 474 if (name.startsWith(prefix)) { |
| 475 return name.substring(prefix.length); |
| 476 } else { |
| 477 // TODO(jmesserly): this is for backwards compat, but not sure it's very |
| 478 // useful to log this. |
| 479 return 'AnalyzerMessage'; |
| 480 } |
| 481 } |
| 482 |
| 483 /// Returns an ANSII color escape sequence corresponding to [levelName]. Colors |
| 484 /// are defined for: severe, error, warning, or info. Returns null if the level |
| 485 /// name is not recognized. |
| 486 String colorOf(String levelName) { |
| 487 levelName = levelName.toLowerCase(); |
| 488 if (levelName == 'shout' || levelName == 'severe' || levelName == 'error') { |
| 489 return _RED_COLOR; |
| 490 } |
| 491 if (levelName == 'warning') return _MAGENTA_COLOR; |
| 492 if (levelName == 'info') return _CYAN_COLOR; |
| 493 return null; |
| 494 } |
| 495 |
| 496 const String _RED_COLOR = '\u001b[31m'; |
| 497 const String _MAGENTA_COLOR = '\u001b[35m'; |
| 498 const String _CYAN_COLOR = '\u001b[36m'; |
| 499 const String GREEN_COLOR = '\u001b[32m'; |
| 500 const String NO_COLOR = '\u001b[0m'; |
OLD | NEW |