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:mirrors'; |
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'; |
11 import 'package:analyzer/src/generated/engine.dart' | 11 import 'package:analyzer/src/generated/engine.dart' |
12 show AnalysisContext, AnalysisEngine; | 12 show AnalysisContext, AnalysisEngine, AnalysisOptionsImpl; |
13 import 'package:analyzer/src/generated/error.dart'; | 13 import 'package:analyzer/src/generated/error.dart'; |
| 14 import 'package:cli_util/cli_util.dart' show getSdkDir; |
14 import 'package:logging/logging.dart'; | 15 import 'package:logging/logging.dart'; |
| 16 import 'package:path/path.dart' as path; |
15 import 'package:source_span/source_span.dart'; | 17 import 'package:source_span/source_span.dart'; |
16 import 'package:test/test.dart'; | 18 import 'package:test/test.dart'; |
17 | 19 |
18 import 'package:dev_compiler/strong_mode.dart'; | 20 import 'package:dev_compiler/strong_mode.dart'; |
| 21 import 'package:dev_compiler/src/analysis_context.dart'; |
19 | 22 |
20 import 'analysis_context.dart'; | 23 import 'package:dev_compiler/src/server/dependency_graph.dart' |
21 import 'dependency_graph.dart' show runtimeFilesForServerMode; | 24 show runtimeFilesForServerMode; |
22 import 'info.dart'; | 25 import 'package:dev_compiler/src/info.dart'; |
23 import 'options.dart'; | 26 import 'package:dev_compiler/src/options.dart'; |
24 import 'utils.dart'; | 27 import 'package:dev_compiler/src/utils.dart'; |
| 28 |
| 29 /// Shared analysis context used for compilation. |
| 30 final realSdkContext = createAnalysisContextWithSources(new StrongModeOptions(), |
| 31 new SourceResolverOptions(dartSdkPath: getSdkDir().path)) |
| 32 ..analysisOptions = (new AnalysisOptionsImpl()..cacheSize = 512); |
| 33 |
| 34 final String testDirectory = |
| 35 path.dirname((reflectClass(_TestUtils).owner as LibraryMirror).uri.path); |
| 36 |
| 37 class _TestUtils {} |
25 | 38 |
26 /// Run the checker on a program with files contents as indicated in | 39 /// Run the checker on a program with files contents as indicated in |
27 /// [testFiles]. | 40 /// [testFiles]. |
28 /// | 41 /// |
29 /// This function makes several assumptions to make it easier to describe error | 42 /// This function makes several assumptions to make it easier to describe error |
30 /// expectations: | 43 /// expectations: |
31 /// | 44 /// |
32 /// * a file named `/main.dart` exists in [testFiles]. | 45 /// * a file named `/main.dart` exists in [testFiles]. |
33 /// * all expected failures are listed in the source code using comments | 46 /// * all expected failures are listed in the source code using comments |
34 /// immediately in front of the AST node that should contain the error. | 47 /// immediately in front of the AST node that should contain the error. |
(...skipping 20 matching lines...) Expand all Loading... |
55 nonnullableTypes: StrongModeOptions.NONNULLABLE_TYPES}) { | 68 nonnullableTypes: StrongModeOptions.NONNULLABLE_TYPES}) { |
56 expect(testFiles.containsKey('/main.dart'), isTrue, | 69 expect(testFiles.containsKey('/main.dart'), isTrue, |
57 reason: '`/main.dart` is missing in testFiles'); | 70 reason: '`/main.dart` is missing in testFiles'); |
58 | 71 |
59 var provider = createTestResourceProvider(testFiles); | 72 var provider = createTestResourceProvider(testFiles); |
60 var uriResolver = new TestUriResolver(provider); | 73 var uriResolver = new TestUriResolver(provider); |
61 var context = AnalysisEngine.instance.createAnalysisContext(); | 74 var context = AnalysisEngine.instance.createAnalysisContext(); |
62 context.sourceFactory = createSourceFactory(new SourceResolverOptions( | 75 context.sourceFactory = createSourceFactory(new SourceResolverOptions( |
63 customUrlMappings: customUrlMappings, | 76 customUrlMappings: customUrlMappings, |
64 useMockSdk: sdkDir == null, | 77 useMockSdk: sdkDir == null, |
65 dartSdkPath: sdkDir, | 78 dartSdkPath: sdkDir), fileResolvers: [uriResolver]); |
66 entryPointFile: '/main.dart'), fileResolvers: [uriResolver]); | |
67 | 79 |
68 var checker = new StrongChecker(context, new StrongModeOptions( | 80 var checker = new StrongChecker(context, new StrongModeOptions( |
69 relaxedCasts: relaxedCasts, | 81 relaxedCasts: relaxedCasts, |
70 inferDownwards: inferDownwards, | 82 inferDownwards: inferDownwards, |
71 inferFromOverrides: inferFromOverrides, | 83 inferFromOverrides: inferFromOverrides, |
72 inferTransitively: inferTransitively, | 84 inferTransitively: inferTransitively, |
73 nonnullableTypes: nonnullableTypes, | 85 nonnullableTypes: nonnullableTypes, |
74 hints: true)); | 86 hints: true)); |
75 | 87 |
76 // Run the checker on /main.dart. | 88 // Run the checker on /main.dart. |
(...skipping 10 matching lines...) Expand all Loading... |
87 var errorInfo = checker.computeErrors(unit.source); | 99 var errorInfo = checker.computeErrors(unit.source); |
88 new _ExpectedErrorVisitor(errorInfo.errors).validate(unit.unit); | 100 new _ExpectedErrorVisitor(errorInfo.errors).validate(unit.unit); |
89 } | 101 } |
90 } | 102 } |
91 } | 103 } |
92 | 104 |
93 /// Creates a [MemoryResourceProvider] with test data | 105 /// Creates a [MemoryResourceProvider] with test data |
94 MemoryResourceProvider createTestResourceProvider( | 106 MemoryResourceProvider createTestResourceProvider( |
95 Map<String, String> testFiles) { | 107 Map<String, String> testFiles) { |
96 var provider = new MemoryResourceProvider(); | 108 var provider = new MemoryResourceProvider(); |
97 runtimeFilesForServerMode().forEach((filepath) { | 109 runtimeFilesForServerMode.forEach((filepath) { |
98 testFiles['/dev_compiler_runtime/$filepath'] = | 110 testFiles['/dev_compiler_runtime/$filepath'] = |
99 '/* test contents of $filepath */'; | 111 '/* test contents of $filepath */'; |
100 }); | 112 }); |
101 testFiles.forEach((key, value) { | 113 testFiles.forEach((key, value) { |
102 var scheme = 'package:'; | 114 var scheme = 'package:'; |
103 if (key.startsWith(scheme)) { | 115 if (key.startsWith(scheme)) { |
104 key = '/packages/${key.substring(scheme.length)}'; | 116 key = '/packages/${key.substring(scheme.length)}'; |
105 } | 117 } |
106 provider.newFile(key, value); | 118 provider.newFile(key, value); |
107 }); | 119 }); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 expect(tokens[1], "should", reason: 'invalid error descriptor'); | 255 expect(tokens[1], "should", reason: 'invalid error descriptor'); |
244 expect(tokens[2], "be", reason: 'invalid error descriptor'); | 256 expect(tokens[2], "be", reason: 'invalid error descriptor'); |
245 if (tokens[0] == "pass") return null; | 257 if (tokens[0] == "pass") return null; |
246 // TODO(leafp) For now, we just use whatever the current expectation is, | 258 // TODO(leafp) For now, we just use whatever the current expectation is, |
247 // eventually we could do more automated reporting here. | 259 // eventually we could do more automated reporting here. |
248 return _parse(tokens[0]); | 260 return _parse(tokens[0]); |
249 } | 261 } |
250 | 262 |
251 String toString() => '$level $typeName'; | 263 String toString() => '$level $typeName'; |
252 } | 264 } |
OLD | NEW |