| 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' |
| 10 show AnalysisContext, TimestampedData; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 11 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:logging/logging.dart'; | 12 import 'package:logging/logging.dart'; |
| 12 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
| 13 import 'package:source_span/source_span.dart'; | 14 import 'package:source_span/source_span.dart'; |
| 14 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
| 15 | 16 |
| 16 import 'package:dev_compiler/src/checker/dart_sdk.dart' | 17 import 'package:dev_compiler/src/checker/dart_sdk.dart' |
| 17 show mockSdkSources, dartSdkDirectory; | 18 show mockSdkSources, dartSdkDirectory; |
| 18 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; | 19 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; |
| 19 import 'package:dev_compiler/src/utils.dart'; | 20 import 'package:dev_compiler/src/utils.dart'; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 44 /// | 45 /// |
| 45 /// testChecker({ | 46 /// testChecker({ |
| 46 /// '/main.dart': ''' | 47 /// '/main.dart': ''' |
| 47 /// testMethod() { | 48 /// testMethod() { |
| 48 /// dynamic x = /*warning:Box*/3; | 49 /// dynamic x = /*warning:Box*/3; |
| 49 /// } | 50 /// } |
| 50 /// ''' | 51 /// ''' |
| 51 /// }); | 52 /// }); |
| 52 /// | 53 /// |
| 53 CheckerResults testChecker(Map<String, String> testFiles, | 54 CheckerResults testChecker(Map<String, String> testFiles, |
| 54 {bool allowConstCasts: true, String sdkDir, CheckerReporter reporter, | 55 {bool allowConstCasts: true, String sdkDir, |
| 56 CheckerReporter createReporter(AnalysisContext context), |
| 55 covariantGenerics: true, relaxedCasts: true, | 57 covariantGenerics: true, relaxedCasts: true, |
| 56 inferDownwards: RulesOptions.inferDownwardsDefault, | 58 inferDownwards: RulesOptions.inferDownwardsDefault, |
| 57 inferFromOverrides: ResolverOptions.inferFromOverridesDefault, | 59 inferFromOverrides: ResolverOptions.inferFromOverridesDefault, |
| 58 inferTransitively: ResolverOptions.inferTransitivelyDefault, | 60 inferTransitively: ResolverOptions.inferTransitivelyDefault, |
| 59 nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, | 61 nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, |
| 60 wrapClosures: RulesOptions.wrapClosuresDefault}) { | 62 wrapClosures: RulesOptions.wrapClosuresDefault}) { |
| 61 expect(testFiles.containsKey('/main.dart'), isTrue, | 63 expect(testFiles.containsKey('/main.dart'), isTrue, |
| 62 reason: '`/main.dart` is missing in testFiles'); | 64 reason: '`/main.dart` is missing in testFiles'); |
| 63 | 65 |
| 64 // Create a resolver that can load test files from memory. | 66 // Create a resolver that can load test files from memory. |
| 65 var testUriResolver = new InMemoryUriResolver(testFiles); | 67 var testUriResolver = new InMemoryUriResolver(testFiles); |
| 66 var options = new CompilerOptions( | 68 var options = new CompilerOptions( |
| 67 allowConstCasts: allowConstCasts, | 69 allowConstCasts: allowConstCasts, |
| 68 covariantGenerics: covariantGenerics, | 70 covariantGenerics: covariantGenerics, |
| 69 relaxedCasts: relaxedCasts, | 71 relaxedCasts: relaxedCasts, |
| 70 inferDownwards: inferDownwards, | 72 inferDownwards: inferDownwards, |
| 71 inferFromOverrides: inferFromOverrides, | 73 inferFromOverrides: inferFromOverrides, |
| 72 inferTransitively: inferTransitively, | 74 inferTransitively: inferTransitively, |
| 73 nonnullableTypes: nonnullableTypes, | 75 nonnullableTypes: nonnullableTypes, |
| 74 wrapClosures: wrapClosures, | 76 wrapClosures: wrapClosures, |
| 75 useMockSdk: sdkDir == null, | 77 useMockSdk: sdkDir == null, |
| 76 dartSdkPath: sdkDir, | 78 dartSdkPath: sdkDir, |
| 77 runtimeDir: '/dev_compiler_runtime/', | 79 runtimeDir: '/dev_compiler_runtime/', |
| 78 entryPointFile: '/main.dart'); | 80 entryPointFile: '/main.dart'); |
| 79 var resolver = sdkDir == null | 81 var resolver = sdkDir == null |
| 80 ? new TypeResolver.fromMock(mockSdkSources, options, | 82 ? new TypeResolver.fromMock(mockSdkSources, options, |
| 81 otherResolvers: [testUriResolver]) | 83 otherResolvers: [testUriResolver]) |
| 82 : new TypeResolver.fromDir(sdkDir, options, | 84 : new TypeResolver.fromDir(sdkDir, options, |
| 83 otherResolvers: [testUriResolver]); | 85 otherResolvers: [testUriResolver]); |
| 86 var context = resolver.context; |
| 84 | 87 |
| 85 // Run the checker on /main.dart. | 88 // Run the checker on /main.dart. |
| 86 var mainFile = new Uri.file('/main.dart'); | 89 var mainFile = new Uri.file('/main.dart'); |
| 87 var checkExpectations = reporter == null; | 90 var checkExpectations = createReporter == null; |
| 88 if (reporter == null) reporter = new TestReporter(); | 91 var reporter = (createReporter == null) |
| 92 ? new TestReporter(context) |
| 93 : createReporter(context); |
| 89 var results = | 94 var results = |
| 90 new Compiler(options, resolver: resolver, reporter: reporter).run(); | 95 new Compiler(options, resolver: resolver, reporter: reporter).run(); |
| 91 | 96 |
| 92 // Extract expectations from the comments in the test files. | 97 // Extract expectations from the comments in the test files. |
| 93 var expectedErrors = <AstNode, List<_ErrorExpectation>>{}; | 98 var expectedErrors = <AstNode, List<_ErrorExpectation>>{}; |
| 94 var visitor = new _ErrorMarkerVisitor(expectedErrors); | 99 var visitor = new _ErrorMarkerVisitor(expectedErrors); |
| 95 var initialLibrary = | 100 var initialLibrary = |
| 96 resolver.context.getLibraryElement(testUriResolver.files[mainFile]); | 101 resolver.context.getLibraryElement(testUriResolver.files[mainFile]); |
| 97 for (var lib in reachableLibraries(initialLibrary)) { | 102 for (var lib in reachableLibraries(initialLibrary)) { |
| 98 for (var unit in lib.units) { | 103 for (var unit in lib.units) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 145 } |
| 141 } | 146 } |
| 142 | 147 |
| 143 return results; | 148 return results; |
| 144 } | 149 } |
| 145 | 150 |
| 146 class TestReporter extends SummaryReporter { | 151 class TestReporter extends SummaryReporter { |
| 147 Map<Uri, Map<AstNode, List<StaticInfo>>> infoMap = {}; | 152 Map<Uri, Map<AstNode, List<StaticInfo>>> infoMap = {}; |
| 148 Uri _current; | 153 Uri _current; |
| 149 | 154 |
| 155 TestReporter(AnalysisContext context) : super(context); |
| 156 |
| 150 void enterLibrary(Uri uri) { | 157 void enterLibrary(Uri uri) { |
| 151 super.enterLibrary(uri); | 158 super.enterLibrary(uri); |
| 152 infoMap[uri] = {}; | 159 infoMap[uri] = {}; |
| 153 _current = uri; | 160 _current = uri; |
| 154 } | 161 } |
| 155 | 162 |
| 156 void log(Message info) { | 163 void log(Message info) { |
| 157 super.log(info); | 164 super.log(info); |
| 158 if (info is StaticInfo) { | 165 if (info is StaticInfo) { |
| 159 infoMap[_current].putIfAbsent(info.node, () => []).add(info); | 166 infoMap[_current].putIfAbsent(info.node, () => []).add(info); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 expect(tokens[1], "should", reason: 'invalid error descriptor'); | 272 expect(tokens[1], "should", reason: 'invalid error descriptor'); |
| 266 expect(tokens[2], "be", reason: 'invalid error descriptor'); | 273 expect(tokens[2], "be", reason: 'invalid error descriptor'); |
| 267 if (tokens[0] == "pass") return null; | 274 if (tokens[0] == "pass") return null; |
| 268 // TODO(leafp) For now, we just use whatever the current expectation is, | 275 // TODO(leafp) For now, we just use whatever the current expectation is, |
| 269 // eventually we could do more automated reporting here. | 276 // eventually we could do more automated reporting here. |
| 270 return _parse(tokens[0]); | 277 return _parse(tokens[0]); |
| 271 } | 278 } |
| 272 | 279 |
| 273 String toString() => '$level $type'; | 280 String toString() => '$level $type'; |
| 274 } | 281 } |
| OLD | NEW |