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 AnalysisContext, Timest
ampedData; |
10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
11 import 'package:logging/logging.dart'; | 11 import 'package:logging/logging.dart'; |
12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
13 import 'package:source_span/source_span.dart'; | 13 import 'package:source_span/source_span.dart'; |
14 import 'package:unittest/unittest.dart'; | 14 import 'package:unittest/unittest.dart'; |
15 | 15 |
16 import 'package:dev_compiler/src/checker/dart_sdk.dart' | 16 import 'package:dev_compiler/src/checker/dart_sdk.dart' |
17 show mockSdkSources, dartSdkDirectory; | 17 show mockSdkSources, dartSdkDirectory; |
18 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; | 18 import 'package:dev_compiler/src/checker/resolver.dart' show TypeResolver; |
19 import 'package:dev_compiler/src/utils.dart'; | 19 import 'package:dev_compiler/src/utils.dart'; |
(...skipping 23 matching lines...) Expand all Loading... |
43 /// | 43 /// |
44 /// testChecker({ | 44 /// testChecker({ |
45 /// '/main.dart': ''' | 45 /// '/main.dart': ''' |
46 /// testMethod() { | 46 /// testMethod() { |
47 /// dynamic x = /*warning:Box*/3; | 47 /// dynamic x = /*warning:Box*/3; |
48 /// } | 48 /// } |
49 /// ''' | 49 /// ''' |
50 /// }); | 50 /// }); |
51 /// | 51 /// |
52 CheckerResults testChecker(Map<String, String> testFiles, | 52 CheckerResults testChecker(Map<String, String> testFiles, |
53 {bool allowConstCasts: true, String sdkDir, CheckerReporter reporter, | 53 {bool allowConstCasts: true, String sdkDir, |
| 54 CheckerReporter createReporter(AnalysisContext context), |
54 covariantGenerics: true, relaxedCasts: true, | 55 covariantGenerics: true, relaxedCasts: true, |
55 inferDownwards: RulesOptions.inferDownwardsDefault, | 56 inferDownwards: RulesOptions.inferDownwardsDefault, |
56 inferFromOverrides: ResolverOptions.inferFromOverridesDefault, | 57 inferFromOverrides: ResolverOptions.inferFromOverridesDefault, |
57 inferTransitively: ResolverOptions.inferTransitivelyDefault, | 58 inferTransitively: ResolverOptions.inferTransitivelyDefault, |
58 nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, | 59 nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, |
59 wrapClosures: RulesOptions.wrapClosuresDefault}) { | 60 wrapClosures: RulesOptions.wrapClosuresDefault}) { |
60 expect(testFiles.containsKey('/main.dart'), isTrue, | 61 expect(testFiles.containsKey('/main.dart'), isTrue, |
61 reason: '`/main.dart` is missing in testFiles'); | 62 reason: '`/main.dart` is missing in testFiles'); |
62 | 63 |
63 // Create a resolver that can load test files from memory. | 64 // Create a resolver that can load test files from memory. |
64 var testUriResolver = new TestUriResolver(testFiles); | 65 var testUriResolver = new TestUriResolver(testFiles); |
65 var options = new CompilerOptions( | 66 var options = new CompilerOptions( |
66 allowConstCasts: allowConstCasts, | 67 allowConstCasts: allowConstCasts, |
67 covariantGenerics: covariantGenerics, | 68 covariantGenerics: covariantGenerics, |
68 relaxedCasts: relaxedCasts, | 69 relaxedCasts: relaxedCasts, |
69 inferDownwards: inferDownwards, | 70 inferDownwards: inferDownwards, |
70 inferFromOverrides: inferFromOverrides, | 71 inferFromOverrides: inferFromOverrides, |
71 inferTransitively: inferTransitively, | 72 inferTransitively: inferTransitively, |
72 nonnullableTypes: nonnullableTypes, | 73 nonnullableTypes: nonnullableTypes, |
73 wrapClosures: wrapClosures, | 74 wrapClosures: wrapClosures, |
74 useMockSdk: sdkDir == null, | 75 useMockSdk: sdkDir == null, |
75 dartSdkPath: sdkDir, | 76 dartSdkPath: sdkDir, |
76 runtimeDir: '/dev_compiler_runtime/', | 77 runtimeDir: '/dev_compiler_runtime/', |
77 entryPointFile: '/main.dart'); | 78 entryPointFile: '/main.dart'); |
78 var resolver = sdkDir == null | 79 var resolver = sdkDir == null |
79 ? new TypeResolver.fromMock(mockSdkSources, options, | 80 ? new TypeResolver.fromMock(mockSdkSources, options, |
80 otherResolvers: [testUriResolver]) | 81 otherResolvers: [testUriResolver]) |
81 : new TypeResolver.fromDir(sdkDir, options, | 82 : new TypeResolver.fromDir(sdkDir, options, |
82 otherResolvers: [testUriResolver]); | 83 otherResolvers: [testUriResolver]); |
| 84 var context = resolver.context; |
83 | 85 |
84 // Run the checker on /main.dart. | 86 // Run the checker on /main.dart. |
85 var mainFile = new Uri.file('/main.dart'); | 87 var mainFile = new Uri.file('/main.dart'); |
86 var checkExpectations = reporter == null; | 88 var checkExpectations = createReporter == null; |
87 if (reporter == null) reporter = new TestReporter(); | 89 var reporter = (createReporter == null) ? new TestReporter(context) : |
| 90 createReporter(context); |
88 var results = new Compiler(options, resolver, reporter).run(); | 91 var results = new Compiler(options, resolver, reporter).run(); |
89 | 92 |
90 // Extract expectations from the comments in the test files. | 93 // Extract expectations from the comments in the test files. |
91 var expectedErrors = <AstNode, List<_ErrorExpectation>>{}; | 94 var expectedErrors = <AstNode, List<_ErrorExpectation>>{}; |
92 var visitor = new _ErrorMarkerVisitor(expectedErrors); | 95 var visitor = new _ErrorMarkerVisitor(expectedErrors); |
93 var initialLibrary = | 96 var initialLibrary = |
94 resolver.context.getLibraryElement(testUriResolver.files[mainFile]); | 97 resolver.context.getLibraryElement(testUriResolver.files[mainFile]); |
95 for (var lib in reachableLibraries(initialLibrary)) { | 98 for (var lib in reachableLibraries(initialLibrary)) { |
96 for (var unit in lib.units) { | 99 for (var unit in lib.units) { |
97 unit.unit.accept(visitor); | 100 unit.unit.accept(visitor); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 } | 141 } |
139 } | 142 } |
140 | 143 |
141 return results; | 144 return results; |
142 } | 145 } |
143 | 146 |
144 class TestReporter extends SummaryReporter { | 147 class TestReporter extends SummaryReporter { |
145 Map<Uri, Map<AstNode, List<StaticInfo>>> infoMap = {}; | 148 Map<Uri, Map<AstNode, List<StaticInfo>>> infoMap = {}; |
146 Uri _current; | 149 Uri _current; |
147 | 150 |
| 151 TestReporter(AnalysisContext context) : super(context); |
| 152 |
148 void enterLibrary(Uri uri) { | 153 void enterLibrary(Uri uri) { |
149 super.enterLibrary(uri); | 154 super.enterLibrary(uri); |
150 infoMap[uri] = {}; | 155 infoMap[uri] = {}; |
151 _current = uri; | 156 _current = uri; |
152 } | 157 } |
153 | 158 |
154 void log(Message info) { | 159 void log(Message info) { |
155 super.log(info); | 160 super.log(info); |
156 if (info is StaticInfo) { | 161 if (info is StaticInfo) { |
157 infoMap[_current].putIfAbsent(info.node, () => []).add(info); | 162 infoMap[_current].putIfAbsent(info.node, () => []).add(info); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 345 |
341 SourceSpan spanFor(AstNode node) { | 346 SourceSpan spanFor(AstNode node) { |
342 final begin = node is AnnotatedNode | 347 final begin = node is AnnotatedNode |
343 ? node.firstTokenAfterCommentAndMetadata.offset | 348 ? node.firstTokenAfterCommentAndMetadata.offset |
344 : node.offset; | 349 : node.offset; |
345 return _file.span(begin, node.end); | 350 return _file.span(begin, node.end); |
346 } | 351 } |
347 | 352 |
348 String toString() => '[$runtimeType: $uri]'; | 353 String toString() => '[$runtimeType: $uri]'; |
349 } | 354 } |
OLD | NEW |