OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import 'dart:async'; |
| 7 import '../../async_helper.dart'; |
6 import 'memory_compiler.dart' show compilerFor; | 8 import 'memory_compiler.dart' show compilerFor; |
7 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' show | 9 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' show |
8 Compiler; | 10 Compiler; |
9 import | 11 import |
10 '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart' | 12 '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart' |
11 show | 13 show |
12 Node; | 14 Node; |
13 import | 15 import |
14 '../../../sdk/lib/_internal/compiler/implementation/dart_backend/dart_backen
d.dart'; | 16 '../../../sdk/lib/_internal/compiler/implementation/dart_backend/dart_backen
d.dart'; |
15 import | 17 import |
16 '../../../sdk/lib/_internal/compiler/implementation/mirror_renamer/mirror_re
namer.dart'; | 18 '../../../sdk/lib/_internal/compiler/implementation/mirror_renamer/mirror_re
namer.dart'; |
17 import | 19 import |
18 '../../../sdk/lib/_internal/compiler/implementation/scanner/scannerlib.dart' | 20 '../../../sdk/lib/_internal/compiler/implementation/scanner/scannerlib.dart' |
19 show | 21 show |
20 SourceString; | 22 SourceString; |
21 | 23 |
22 main() { | 24 main() { |
23 testWithMirrorHelperLibrary(minify: true); | 25 testWithMirrorHelperLibrary(minify: true); |
24 testWithMirrorHelperLibrary(minify: false); | 26 testWithMirrorHelperLibrary(minify: false); |
25 testWithoutMirrorHelperLibrary(minify: true); | 27 testWithoutMirrorHelperLibrary(minify: true); |
26 testWithoutMirrorHelperLibrary(minify: false); | 28 testWithoutMirrorHelperLibrary(minify: false); |
27 } | 29 } |
28 | 30 |
29 Compiler runCompiler({useMirrorHelperLibrary: false, minify: false}) { | 31 Future<Compiler> runCompiler({useMirrorHelperLibrary: false, minify: false}) { |
30 List<String> options = ['--output-type=dart']; | 32 List<String> options = ['--output-type=dart']; |
31 if (minify) { | 33 if (minify) { |
32 options.add('--minify'); | 34 options.add('--minify'); |
33 } | 35 } |
34 Compiler compiler = compilerFor(MEMORY_SOURCE_FILES, options: options); | 36 Compiler compiler = compilerFor(MEMORY_SOURCE_FILES, options: options); |
35 DartBackend backend = compiler.backend; | 37 DartBackend backend = compiler.backend; |
36 backend.useMirrorHelperLibrary = useMirrorHelperLibrary; | 38 backend.useMirrorHelperLibrary = useMirrorHelperLibrary; |
37 compiler.runCompiler(Uri.parse('memory:main.dart')); | 39 return |
38 return compiler; | 40 compiler.runCompiler(Uri.parse('memory:main.dart')).then((_) => compiler); |
39 } | 41 } |
40 | 42 |
41 void testWithMirrorHelperLibrary({bool minify}) { | 43 void testWithMirrorHelperLibrary({bool minify}) { |
42 Compiler compiler = runCompiler(useMirrorHelperLibrary: true, minify: minify); | 44 asyncTest(() => runCompiler(useMirrorHelperLibrary: true, minify: minify). |
| 45 then((Compiler compiler) { |
| 46 DartBackend backend = compiler.backend; |
| 47 MirrorRenamer mirrorRenamer = backend.mirrorRenamer; |
| 48 Map<Node, String> renames = backend.renames; |
| 49 Map<String, SourceString> symbols = mirrorRenamer.symbols; |
43 | 50 |
44 DartBackend backend = compiler.backend; | 51 Expect.isFalse(null == backend.mirrorHelperLibrary); |
45 MirrorRenamer mirrorRenamer = backend.mirrorRenamer; | 52 Expect.isFalse(null == backend.mirrorHelperGetNameFunction); |
46 Map<Node, String> renames = backend.renames; | 53 Expect.isTrue(symbols.containsValue( |
47 Map<String, SourceString> symbols = mirrorRenamer.symbols; | 54 const SourceString(MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION))); |
48 | 55 |
49 Expect.isFalse(null == backend.mirrorHelperLibrary); | 56 for (Node n in renames.keys) { |
50 Expect.isFalse(null == backend.mirrorHelperGetNameFunction); | 57 if (symbols.containsKey(renames[n])) { |
51 Expect.isTrue(symbols.containsValue( | 58 if(n.toString() == 'getName') { |
52 const SourceString(MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION))); | 59 Expect.equals( |
53 | 60 const SourceString(MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION), |
54 for (Node n in renames.keys) { | 61 symbols[renames[n]]); |
55 if (symbols.containsKey(renames[n])) { | 62 } else { |
56 if(n.toString() == 'getName') { | 63 Expect.equals(n.toString(), symbols[renames[n]].stringValue); |
57 Expect.equals( | 64 } |
58 const SourceString(MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION), | |
59 symbols[renames[n]]); | |
60 } else { | |
61 Expect.equals(n.toString(), symbols[renames[n]].stringValue); | |
62 } | 65 } |
63 } | 66 } |
64 } | |
65 | 67 |
66 String output = compiler.assembledCode; | 68 String output = compiler.assembledCode; |
67 String getNameMatch = MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION; | 69 String getNameMatch = MirrorRenamer.MIRROR_HELPER_GET_NAME_FUNCTION; |
68 Iterable i = getNameMatch.allMatches(output); | 70 Iterable i = getNameMatch.allMatches(output); |
69 | 71 |
70 | 72 |
71 if (minify) { | 73 if (minify) { |
| 74 Expect.equals(1, i.length); |
| 75 } else { |
| 76 // Appears twice in code (defined & called) and twice in renames map. |
| 77 Expect.equals(4, i.length); |
| 78 } |
| 79 |
| 80 String mapMatch = 'const<String,SourceString>'; |
| 81 i = mapMatch.allMatches(output); |
72 Expect.equals(1, i.length); | 82 Expect.equals(1, i.length); |
73 } else { | 83 })); |
74 // Appears twice in code (defined & called) and twice in renames map. | |
75 Expect.equals(4, i.length); | |
76 } | |
77 | |
78 String mapMatch = 'const<String,SourceString>'; | |
79 i = mapMatch.allMatches(output); | |
80 Expect.equals(1, i.length); | |
81 } | 84 } |
82 | 85 |
83 void testWithoutMirrorHelperLibrary({bool minify}) { | 86 void testWithoutMirrorHelperLibrary({bool minify}) { |
84 Compiler compiler = | 87 asyncTest(() => runCompiler(useMirrorHelperLibrary: false, minify: minify). |
85 runCompiler(useMirrorHelperLibrary: false, minify: minify); | 88 then((Compiler compiler) { |
86 DartBackend backend = compiler.backend; | 89 DartBackend backend = compiler.backend; |
87 | 90 |
88 Expect.equals(null, backend.mirrorHelperLibrary); | 91 Expect.equals(null, backend.mirrorHelperLibrary); |
89 Expect.equals(null, backend.mirrorHelperGetNameFunction); | 92 Expect.equals(null, backend.mirrorHelperGetNameFunction); |
90 Expect.equals(null, backend.mirrorRenamer); | 93 Expect.equals(null, backend.mirrorRenamer); |
| 94 })); |
91 } | 95 } |
92 | 96 |
93 const MEMORY_SOURCE_FILES = const <String, String> { | 97 const MEMORY_SOURCE_FILES = const <String, String> { |
94 'main.dart': """ | 98 'main.dart': """ |
95 import 'dart:mirrors'; | 99 import 'dart:mirrors'; |
96 | 100 |
97 class Foo { | 101 class Foo { |
98 noSuchMethod(Invocation invocation) { | 102 noSuchMethod(Invocation invocation) { |
99 MirrorSystem.getName(invocation.memberName); | 103 MirrorSystem.getName(invocation.memberName); |
100 } | 104 } |
101 } | 105 } |
102 | 106 |
103 void main() { | 107 void main() { |
104 new Foo().fisk(); | 108 new Foo().fisk(); |
105 } | 109 } |
106 """}; | 110 """}; |
OLD | NEW |