| 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 // TODO(jmesserly): this file needs to be refactored, it's a port from | 5 // TODO(jmesserly): this file needs to be refactored, it's a port from |
| 6 // package:dev_compiler's tests | 6 // package:dev_compiler's tests |
| 7 library test.src.task.strong.strong_test_helper; | 7 library test.src.task.strong.strong_test_helper; |
| 8 | 8 |
| 9 import 'package:analyzer/file_system/file_system.dart'; | 9 import 'package:analyzer/file_system/file_system.dart'; |
| 10 import 'package:analyzer/file_system/memory_file_system.dart'; | 10 import 'package:analyzer/file_system/memory_file_system.dart'; |
| 11 import 'package:analyzer/src/context/context.dart' show SdkAnalysisContext; | 11 import 'package:analyzer/src/context/context.dart' show SdkAnalysisContext; |
| 12 import 'package:analyzer/src/generated/ast.dart'; | 12 import 'package:analyzer/src/generated/ast.dart'; |
| 13 import 'package:analyzer/src/generated/element.dart'; | 13 import 'package:analyzer/src/generated/element.dart'; |
| 14 import 'package:analyzer/src/generated/engine.dart' hide SdkAnalysisContext; | 14 import 'package:analyzer/src/generated/engine.dart' hide SdkAnalysisContext; |
| 15 import 'package:analyzer/src/generated/error.dart'; | 15 import 'package:analyzer/src/generated/error.dart'; |
| 16 import 'package:analyzer/src/generated/sdk.dart'; | 16 import 'package:analyzer/src/generated/sdk.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 18 import 'package:analyzer/src/task/strong/checker.dart'; | 18 import 'package:analyzer/src/task/strong/checker.dart'; |
| 19 import 'package:analyzer/src/task/strong/rules.dart'; | 19 import 'package:analyzer/src/task/strong/rules.dart'; |
| 20 import 'package:logging/logging.dart'; // TODO(jmesserly): remove | 20 import 'package:logging/logging.dart'; // TODO(jmesserly): remove |
| 21 import 'package:source_span/source_span.dart'; // TODO(jmesserly): remove | 21 import 'package:source_span/source_span.dart'; // TODO(jmesserly): remove |
| 22 import 'package:unittest/unittest.dart'; | 22 import 'package:unittest/unittest.dart'; |
| 23 | 23 |
| 24 | |
| 25 /// Run the checker on a program with files contents as indicated in | 24 /// Run the checker on a program with files contents as indicated in |
| 26 /// [testFiles]. | 25 /// [testFiles]. |
| 27 /// | 26 /// |
| 28 /// This function makes several assumptions to make it easier to describe error | 27 /// This function makes several assumptions to make it easier to describe error |
| 29 /// expectations: | 28 /// expectations: |
| 30 /// | 29 /// |
| 31 /// * a file named `/main.dart` exists in [testFiles]. | 30 /// * a file named `/main.dart` exists in [testFiles]. |
| 32 /// * all expected failures are listed in the source code using comments | 31 /// * all expected failures are listed in the source code using comments |
| 33 /// immediately in front of the AST node that should contain the error. | 32 /// immediately in front of the AST node that should contain the error. |
| 34 /// * errors are formatted as a token `level:Type`, where `level` is the | 33 /// * errors are formatted as a token `level:Type`, where `level` is the |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 new MockDartSdk(mockSdkSources, reportMissing: true).resolver, | 68 new MockDartSdk(mockSdkSources, reportMissing: true).resolver, |
| 70 uriResolver | 69 uriResolver |
| 71 ]); | 70 ]); |
| 72 | 71 |
| 73 // Run the checker on /main.dart. | 72 // Run the checker on /main.dart. |
| 74 Source mainSource = uriResolver.resolveAbsolute(new Uri.file('/main.dart')); | 73 Source mainSource = uriResolver.resolveAbsolute(new Uri.file('/main.dart')); |
| 75 var initialLibrary = | 74 var initialLibrary = |
| 76 context.resolveCompilationUnit2(mainSource, mainSource); | 75 context.resolveCompilationUnit2(mainSource, mainSource); |
| 77 | 76 |
| 78 var collector = new _ErrorCollector(); | 77 var collector = new _ErrorCollector(); |
| 79 var errorReporter = new ErrorReporter(collector, mainSource); | 78 var checker = |
| 80 var checker = new CodeChecker(new TypeRules(context.typeProvider), | 79 new CodeChecker(new TypeRules(context.typeProvider), collector); |
| 81 errorReporter); | |
| 82 | 80 |
| 83 // Extract expectations from the comments in the test files, and | 81 // Extract expectations from the comments in the test files, and |
| 84 // check that all errors we emit are included in the expected map. | 82 // check that all errors we emit are included in the expected map. |
| 85 var allLibraries = reachableLibraries(initialLibrary.element.library); | 83 var allLibraries = reachableLibraries(initialLibrary.element.library); |
| 86 for (var lib in allLibraries) { | 84 for (var lib in allLibraries) { |
| 87 for (var unit in lib.units) { | 85 for (var unit in lib.units) { |
| 88 var errors = <AnalysisError>[]; | 86 var errors = <AnalysisError>[]; |
| 89 collector.errors = errors; | 87 collector.errors = errors; |
| 90 | 88 |
| 91 var source = unit.source; | 89 var source = unit.source; |
| 92 if (source.uri.scheme == 'dart') continue; | 90 if (source.uri.scheme == 'dart') continue; |
| 93 errorReporter.source = unit.source; | |
| 94 | 91 |
| 95 var librarySource = context.getLibrariesContaining(source).single; | 92 var librarySource = context.getLibrariesContaining(source).single; |
| 96 var resolved = context.resolveCompilationUnit2(source, librarySource); | 93 var resolved = context.resolveCompilationUnit2(source, librarySource); |
| 97 checker.visitCompilationUnit(resolved); | 94 checker.visitCompilationUnit(resolved); |
| 98 | 95 |
| 99 new _ExpectedErrorVisitor(errors).validate(resolved); | 96 new _ExpectedErrorVisitor(errors).validate(resolved); |
| 100 } | 97 } |
| 101 } | 98 } |
| 102 }); | 99 }); |
| 103 } | 100 } |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 expect(tokens[2], "be", reason: 'invalid error descriptor'); | 276 expect(tokens[2], "be", reason: 'invalid error descriptor'); |
| 280 if (tokens[0] == "pass") return null; | 277 if (tokens[0] == "pass") return null; |
| 281 // TODO(leafp) For now, we just use whatever the current expectation is, | 278 // TODO(leafp) For now, we just use whatever the current expectation is, |
| 282 // eventually we could do more automated reporting here. | 279 // eventually we could do more automated reporting here. |
| 283 return _parse(tokens[0]); | 280 return _parse(tokens[0]); |
| 284 } | 281 } |
| 285 | 282 |
| 286 String toString() => '$level $typeName'; | 283 String toString() => '$level $typeName'; |
| 287 } | 284 } |
| 288 | 285 |
| 289 | |
| 290 /// Dart SDK which contains a mock implementation of the SDK libraries. May be | 286 /// Dart SDK which contains a mock implementation of the SDK libraries. May be |
| 291 /// used to speed up execution when most of the core libraries is not needed. | 287 /// used to speed up execution when most of the core libraries is not needed. |
| 292 class MockDartSdk implements DartSdk { | 288 class MockDartSdk implements DartSdk { |
| 293 final Map<Uri, _MockSdkSource> _sources = {}; | 289 final Map<Uri, _MockSdkSource> _sources = {}; |
| 294 final bool reportMissing; | 290 final bool reportMissing; |
| 295 final Map<String, SdkLibrary> _libs = {}; | 291 final Map<String, SdkLibrary> _libs = {}; |
| 296 final String sdkVersion = '0'; | 292 final String sdkVersion = '0'; |
| 297 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | 293 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); |
| 298 final AnalysisContext context = new SdkAnalysisContext(); | 294 final AnalysisContext context = new SdkAnalysisContext(); |
| 299 DartUriResolver _resolver; | 295 DartUriResolver _resolver; |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 'dart:math': ''' | 445 'dart:math': ''' |
| 450 library dart.math; | 446 library dart.math; |
| 451 class Random { | 447 class Random { |
| 452 bool nextBool() {} | 448 bool nextBool() {} |
| 453 } | 449 } |
| 454 num min(num x, num y) {} | 450 num min(num x, num y) {} |
| 455 num max(num x, num y) {} | 451 num max(num x, num y) {} |
| 456 ''', | 452 ''', |
| 457 }; | 453 }; |
| 458 | 454 |
| 459 | |
| 460 /// Returns all libraries transitively imported or exported from [start]. | 455 /// Returns all libraries transitively imported or exported from [start]. |
| 461 List<LibraryElement> reachableLibraries(LibraryElement start) { | 456 List<LibraryElement> reachableLibraries(LibraryElement start) { |
| 462 var results = <LibraryElement>[]; | 457 var results = <LibraryElement>[]; |
| 463 var seen = new Set(); | 458 var seen = new Set(); |
| 464 void find(LibraryElement lib) { | 459 void find(LibraryElement lib) { |
| 465 if (seen.contains(lib)) return; | 460 if (seen.contains(lib)) return; |
| 466 seen.add(lib); | 461 seen.add(lib); |
| 467 results.add(lib); | 462 results.add(lib); |
| 468 lib.importedLibraries.forEach(find); | 463 lib.importedLibraries.forEach(find); |
| 469 lib.exportedLibraries.forEach(find); | 464 lib.exportedLibraries.forEach(find); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 495 if (levelName == 'warning') return _MAGENTA_COLOR; | 490 if (levelName == 'warning') return _MAGENTA_COLOR; |
| 496 if (levelName == 'info') return _CYAN_COLOR; | 491 if (levelName == 'info') return _CYAN_COLOR; |
| 497 return null; | 492 return null; |
| 498 } | 493 } |
| 499 | 494 |
| 500 const String _RED_COLOR = '\u001b[31m'; | 495 const String _RED_COLOR = '\u001b[31m'; |
| 501 const String _MAGENTA_COLOR = '\u001b[35m'; | 496 const String _MAGENTA_COLOR = '\u001b[35m'; |
| 502 const String _CYAN_COLOR = '\u001b[36m'; | 497 const String _CYAN_COLOR = '\u001b[36m'; |
| 503 const String GREEN_COLOR = '\u001b[32m'; | 498 const String GREEN_COLOR = '\u001b[32m'; |
| 504 const String NO_COLOR = '\u001b[0m'; | 499 const String NO_COLOR = '\u001b[0m'; |
| OLD | NEW |