OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 mock_compiler; | 5 library mock_compiler; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:compiler/compiler.dart' as api; | 10 import 'package:compiler/compiler.dart' as api; |
11 import 'package:compiler/src/common/names.dart' show | 11 import 'package:compiler/src/common/names.dart' show |
12 Uris; | 12 Uris; |
13 import 'package:compiler/src/constants/expressions.dart'; | 13 import 'package:compiler/src/constants/expressions.dart'; |
14 import 'package:compiler/src/diagnostics/diagnostic_listener.dart'; | 14 import 'package:compiler/src/diagnostics/diagnostic_listener.dart'; |
15 import 'package:compiler/src/diagnostics/source_span.dart'; | 15 import 'package:compiler/src/diagnostics/source_span.dart'; |
16 import 'package:compiler/src/diagnostics/spannable.dart'; | 16 import 'package:compiler/src/diagnostics/spannable.dart'; |
17 import 'package:compiler/src/elements/elements.dart'; | 17 import 'package:compiler/src/elements/elements.dart'; |
18 import 'package:compiler/src/elements/visitor.dart'; | 18 import 'package:compiler/src/elements/visitor.dart'; |
19 import 'package:compiler/src/js_backend/backend_helpers.dart' show | 19 import 'package:compiler/src/js_backend/backend_helpers.dart' show |
20 BackendHelpers; | 20 BackendHelpers; |
21 import 'package:compiler/src/js_backend/lookup_map_analysis.dart' show | 21 import 'package:compiler/src/js_backend/lookup_map_analysis.dart' show |
22 LookupMapAnalysis; | 22 LookupMapAnalysis; |
23 import 'package:compiler/src/io/source_file.dart'; | 23 import 'package:compiler/src/io/source_file.dart'; |
24 import 'package:compiler/src/options.dart' show | 24 import 'package:compiler/src/options.dart' show |
25 CompilerOptions; | 25 CompilerOptions; |
26 import 'package:compiler/src/resolution/members.dart'; | 26 import 'package:compiler/src/resolution/members.dart'; |
27 import 'package:compiler/src/resolution/registry.dart'; | 27 import 'package:compiler/src/resolution/registry.dart'; |
28 import 'package:compiler/src/resolution/scope.dart'; | 28 import 'package:compiler/src/resolution/scope.dart'; |
29 import 'package:compiler/src/resolution/tree_elements.dart'; | 29 import 'package:compiler/src/resolution/tree_elements.dart'; |
| 30 import 'package:compiler/src/resolved_uri_translator.dart'; |
30 import 'package:compiler/src/script.dart'; | 31 import 'package:compiler/src/script.dart'; |
31 import 'package:compiler/src/tree/tree.dart'; | 32 import 'package:compiler/src/tree/tree.dart'; |
32 import 'package:compiler/src/old_to_new_api.dart'; | 33 import 'package:compiler/src/old_to_new_api.dart'; |
33 import 'parser_helper.dart'; | 34 import 'parser_helper.dart'; |
34 | 35 |
35 import 'package:compiler/src/elements/modelx.dart' show | 36 import 'package:compiler/src/elements/modelx.dart' show |
36 ElementX, | 37 ElementX, |
37 LibraryElementX, | 38 LibraryElementX, |
38 ErroneousElementX, | 39 ErroneousElementX, |
39 FunctionElementX; | 40 FunctionElementX; |
(...skipping 18 matching lines...) Expand all Loading... |
58 /// Expected number of warnings. If `null`, the number of warnings is | 59 /// Expected number of warnings. If `null`, the number of warnings is |
59 /// not checked. | 60 /// not checked. |
60 final int expectedWarnings; | 61 final int expectedWarnings; |
61 /// Expected number of errors. If `null`, the number of errors is not checked. | 62 /// Expected number of errors. If `null`, the number of errors is not checked. |
62 final int expectedErrors; | 63 final int expectedErrors; |
63 final Map<String, SourceFile> sourceFiles; | 64 final Map<String, SourceFile> sourceFiles; |
64 Node parsedTree; | 65 Node parsedTree; |
65 final String testedPatchVersion; | 66 final String testedPatchVersion; |
66 final LibrarySourceProvider librariesOverride; | 67 final LibrarySourceProvider librariesOverride; |
67 final DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); | 68 final DiagnosticCollector diagnosticCollector = new DiagnosticCollector(); |
| 69 final ResolvedUriTranslator resolvedUriTranslator = |
| 70 new MockResolvedUriTranslator(); |
68 | 71 |
69 MockCompiler.internal( | 72 MockCompiler.internal( |
70 {Map<String, String> coreSource, | 73 {Map<String, String> coreSource, |
71 bool enableTypeAssertions: false, | 74 bool enableTypeAssertions: false, |
72 bool enableUserAssertions: false, | 75 bool enableUserAssertions: false, |
73 bool enableMinification: false, | 76 bool enableMinification: false, |
74 bool disableTypeInference: false, | 77 bool disableTypeInference: false, |
75 bool analyzeAll: false, | 78 bool analyzeAll: false, |
76 bool analyzeOnly: false, | 79 bool analyzeOnly: false, |
77 bool emitJavaScript: true, | 80 bool emitJavaScript: true, |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 Future scanBuiltinLibraries() { | 253 Future scanBuiltinLibraries() { |
251 // Do nothing. The mock core library is already handled in the constructor. | 254 // Do nothing. The mock core library is already handled in the constructor. |
252 return new Future.value(); | 255 return new Future.value(); |
253 } | 256 } |
254 | 257 |
255 Future<LibraryElement> scanBuiltinLibrary(String name) { | 258 Future<LibraryElement> scanBuiltinLibrary(String name) { |
256 // Do nothing. The mock core library is already handled in the constructor. | 259 // Do nothing. The mock core library is already handled in the constructor. |
257 return new Future.value(); | 260 return new Future.value(); |
258 } | 261 } |
259 | 262 |
260 Uri translateResolvedUri(LibraryElement importingLibrary, | |
261 Uri resolvedUri, Spannable spannable) => resolvedUri; | |
262 | |
263 // The mock library doesn't need any patches. | 263 // The mock library doesn't need any patches. |
264 Uri resolvePatchUri(String dartLibraryName) { | 264 Uri resolvePatchUri(String dartLibraryName) { |
265 if (dartLibraryName == 'core') { | 265 if (dartLibraryName == 'core') { |
266 return PATCH_CORE; | 266 return PATCH_CORE; |
267 } | 267 } |
268 return null; | 268 return null; |
269 } | 269 } |
270 | 270 |
271 Future<Script> readScript(Uri uri, [Spannable spannable]) { | 271 Future<Script> readScript(Uri uri, [Spannable spannable]) { |
272 SourceFile sourceFile = sourceFiles[uri.toString()]; | 272 SourceFile sourceFile = sourceFiles[uri.toString()]; |
273 if (sourceFile == null) throw new ArgumentError(uri); | 273 if (sourceFile == null) throw new ArgumentError(uri); |
274 return new Future.value(new Script(uri, uri, sourceFile)); | 274 return new Future.value(new Script(uri, uri, sourceFile)); |
275 } | 275 } |
276 | 276 |
277 Element lookupElementIn(ScopeContainerElement container, name) { | 277 Element lookupElementIn(ScopeContainerElement container, name) { |
278 Element element = container.localLookup(name); | 278 Element element = container.localLookup(name); |
279 return element != null | 279 return element != null |
280 ? element | 280 ? element |
281 : new ErroneousElementX(null, null, name, container); | 281 : new ErroneousElementX(null, null, name, container); |
282 } | 282 } |
283 | 283 |
284 /// Create a new [MockCompiler] and apply it asynchronously to [f]. | 284 /// Create a new [MockCompiler] and apply it asynchronously to [f]. |
285 static Future create(f(MockCompiler compiler)) { | 285 static Future create(f(MockCompiler compiler)) { |
286 MockCompiler compiler = new MockCompiler.internal(); | 286 MockCompiler compiler = new MockCompiler.internal(); |
287 return compiler.init().then((_) => f(compiler)); | 287 return compiler.init().then((_) => f(compiler)); |
288 } | 288 } |
289 } | 289 } |
290 | 290 |
| 291 class MockResolvedUriTranslator implements ResolvedUriTranslator { |
| 292 static final _emptySet = new Set(); |
| 293 |
| 294 Uri translate(LibraryElement importingLibrary, Uri resolvedUri, |
| 295 [Spannable spannable]) => |
| 296 resolvedUri; |
| 297 Set<Uri> get disallowedLibraryUris => _emptySet; |
| 298 bool get mockableLibraryUsed => false; |
| 299 Map<String, Uri> get sdkLibraries => const <String, Uri>{}; |
| 300 } |
| 301 |
291 class CollectingTreeElements extends TreeElementMapping { | 302 class CollectingTreeElements extends TreeElementMapping { |
292 final Map<Node, Element> map = new LinkedHashMap<Node, Element>(); | 303 final Map<Node, Element> map = new LinkedHashMap<Node, Element>(); |
293 | 304 |
294 CollectingTreeElements(Element currentElement) : super(currentElement); | 305 CollectingTreeElements(Element currentElement) : super(currentElement); |
295 | 306 |
296 operator []=(Node node, Element element) { | 307 operator []=(Node node, Element element) { |
297 map[node] = element; | 308 map[node] = element; |
298 } | 309 } |
299 | 310 |
300 operator [](Node node) => map[node]; | 311 operator [](Node node) => map[node]; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 trustTypeAnnotations: trustTypeAnnotations, | 386 trustTypeAnnotations: trustTypeAnnotations, |
376 enableTypeAssertions: enableTypeAssertions, | 387 enableTypeAssertions: enableTypeAssertions, |
377 enableUserAssertions: enableUserAssertions, | 388 enableUserAssertions: enableUserAssertions, |
378 expectedErrors: expectedErrors, | 389 expectedErrors: expectedErrors, |
379 expectedWarnings: expectedWarnings, | 390 expectedWarnings: expectedWarnings, |
380 outputProvider: outputProvider); | 391 outputProvider: outputProvider); |
381 compiler.registerSource(uri, code); | 392 compiler.registerSource(uri, code); |
382 compiler.diagnosticHandler = createHandler(compiler, code); | 393 compiler.diagnosticHandler = createHandler(compiler, code); |
383 return compiler; | 394 return compiler; |
384 } | 395 } |
OLD | NEW |