OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 import "package:expect/expect.dart"; | |
6 import 'dart:async'; | |
7 import "package:async_helper/async_helper.dart"; | |
8 import 'memory_compiler.dart' show runCompiler; | |
9 import 'package:compiler/src/apiimpl.dart' show | |
10 CompilerImpl; | |
11 import 'package:compiler/src/elements/elements.dart' show | |
12 Element, LibraryElement, ClassElement; | |
13 import 'package:compiler/src/tree/tree.dart' show | |
14 Block, ExpressionStatement, FunctionExpression, Node, Send; | |
15 import 'package:compiler/src/dart_backend/dart_backend.dart' show | |
16 DartBackend, ElementAst; | |
17 import 'package:compiler/src/mirror_renamer/mirror_renamer.dart' show | |
18 MirrorRenamerImpl; | |
19 | |
20 main() { | |
21 asyncTest(() async { | |
22 await testWithMirrorRenaming(minify: true); | |
23 await testWithMirrorRenaming(minify: false); | |
24 await testWithoutMirrorRenaming(minify: true); | |
25 await testWithoutMirrorRenaming(minify: false); | |
26 }); | |
27 } | |
28 | |
29 Future<CompilerImpl> run({useMirrorHelperLibrary: false, minify: false}) async { | |
30 List<String> options = ['--output-type=dart']; | |
31 if (minify) { | |
32 options.add('--minify'); | |
33 } | |
34 var result = await runCompiler( | |
35 memorySourceFiles: MEMORY_SOURCE_FILES, | |
36 options: options, | |
37 beforeRun: (CompilerImpl compiler) { | |
38 DartBackend backend = compiler.backend; | |
39 backend.useMirrorHelperLibrary = useMirrorHelperLibrary; | |
40 }); | |
41 return result.compiler; | |
42 } | |
43 | |
44 Future testWithMirrorRenaming({bool minify}) async { | |
45 CompilerImpl compiler = | |
46 await run(useMirrorHelperLibrary: true, minify: minify); | |
47 DartBackend backend = compiler.backend; | |
48 MirrorRenamerImpl mirrorRenamer = backend.mirrorRenamer; | |
49 Map<Node, String> renames = backend.placeholderRenamer.renames; | |
50 Iterable<LibraryElement> imports = | |
51 backend.placeholderRenamer.platformImports.keys; | |
52 | |
53 FunctionExpression node = backend.memberNodes.values.first.first; | |
54 Block block = node.body; | |
55 ExpressionStatement getNameFunctionNode = block.statements.nodes.head; | |
56 Send send = getNameFunctionNode.expression; | |
57 | |
58 Expect.equals(renames[mirrorRenamer.getNameFunctionNode.name], | |
59 renames[send.selector]); | |
60 Expect.equals("", | |
61 renames[send.receiver]); | |
62 Expect.equals(1, imports.length); | |
63 } | |
64 | |
65 Future testWithoutMirrorRenaming({bool minify}) async { | |
66 CompilerImpl compiler = | |
67 await run(useMirrorHelperLibrary: false, minify: minify); | |
68 DartBackend backend = compiler.backend; | |
69 Map<Node, String> renames = backend.placeholderRenamer.renames; | |
70 Iterable<LibraryElement> imports = | |
71 backend.placeholderRenamer.platformImports.keys; | |
72 FunctionExpression node = backend.memberNodes.values.first.first; | |
73 Block block = node.body; | |
74 ExpressionStatement getNameFunctionNode = block.statements.nodes.head; | |
75 Send send = getNameFunctionNode.expression; | |
76 | |
77 Expect.isFalse(renames.containsKey(send.selector)); | |
78 Expect.equals(1, imports.length); | |
79 } | |
80 | |
81 const MEMORY_SOURCE_FILES = const <String, String> { | |
82 'main.dart': """ | |
83 import 'dart:mirrors'; | |
84 | |
85 class Foo { | |
86 noSuchMethod(Invocation invocation) { | |
87 MirrorSystem.getName(invocation.memberName); | |
88 } | |
89 } | |
90 | |
91 void main() { | |
92 new Foo().fisk(); | |
93 } | |
94 """}; | |
OLD | NEW |