| 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, OutputCollector; | |
| 9 import 'package:compiler/src/apiimpl.dart' show | |
| 10 CompilerImpl; | |
| 11 import 'package:compiler/src/tree/tree.dart' show | |
| 12 Node; | |
| 13 import 'package:compiler/src/dart_backend/dart_backend.dart'; | |
| 14 import 'package:compiler/src/mirror_renamer/mirror_renamer.dart'; | |
| 15 | |
| 16 main() { | |
| 17 asyncTest(() async { | |
| 18 await testWithMirrorHelperLibrary(minify: true); | |
| 19 await testWithMirrorHelperLibrary(minify: false); | |
| 20 await testWithoutMirrorHelperLibrary(minify: true); | |
| 21 await testWithoutMirrorHelperLibrary(minify: false); | |
| 22 }); | |
| 23 } | |
| 24 | |
| 25 Future<CompilerImpl> run({OutputCollector outputCollector, | |
| 26 bool useMirrorHelperLibrary: false, | |
| 27 bool minify: false}) async { | |
| 28 List<String> options = ['--output-type=dart']; | |
| 29 if (minify) { | |
| 30 options.add('--minify'); | |
| 31 } | |
| 32 var result = await runCompiler( | |
| 33 memorySourceFiles: MEMORY_SOURCE_FILES, | |
| 34 outputProvider: outputCollector, | |
| 35 options: options, | |
| 36 beforeRun: (CompilerImpl compiler) { | |
| 37 DartBackend backend = compiler.backend; | |
| 38 backend.useMirrorHelperLibrary = useMirrorHelperLibrary; | |
| 39 }); | |
| 40 return result.compiler; | |
| 41 } | |
| 42 | |
| 43 Future testWithMirrorHelperLibrary({bool minify}) async { | |
| 44 OutputCollector outputCollector = new OutputCollector(); | |
| 45 CompilerImpl compiler = await run( | |
| 46 outputCollector: outputCollector, | |
| 47 useMirrorHelperLibrary: true, | |
| 48 minify: minify); | |
| 49 DartBackend backend = compiler.backend; | |
| 50 MirrorRenamerImpl mirrorRenamer = backend.mirrorRenamer; | |
| 51 Map<Node, String> renames = backend.placeholderRenamer.renames; | |
| 52 Map<String, String> symbols = mirrorRenamer.symbols; | |
| 53 | |
| 54 Expect.isFalse(null == mirrorRenamer.helperLibrary); | |
| 55 Expect.isFalse(null == mirrorRenamer.getNameFunction); | |
| 56 | |
| 57 for (Node n in renames.keys) { | |
| 58 if (symbols.containsKey(renames[n])) { | |
| 59 if(n.toString() == 'getName') { | |
| 60 Expect.equals( | |
| 61 MirrorRenamerImpl.MIRROR_HELPER_GET_NAME_FUNCTION, | |
| 62 symbols[renames[n]]); | |
| 63 } else { | |
| 64 Expect.equals(n.toString(), symbols[renames[n]]); | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 String output = outputCollector.getOutput('', 'dart'); | |
| 70 String getNameMatch = MirrorRenamerImpl.MIRROR_HELPER_GET_NAME_FUNCTION; | |
| 71 Iterable i = getNameMatch.allMatches(output); | |
| 72 print(output); | |
| 73 if (minify) { | |
| 74 Expect.equals(0, i.length); | |
| 75 } else { | |
| 76 // Appears twice in code (defined & called). | |
| 77 Expect.equals(2, i.length); | |
| 78 } | |
| 79 | |
| 80 RegExp mapMatch = new RegExp('const<String,( )?String>'); | |
| 81 i = mapMatch.allMatches(output); | |
| 82 Expect.equals(1, i.length); | |
| 83 } | |
| 84 | |
| 85 Future testWithoutMirrorHelperLibrary({bool minify}) async { | |
| 86 CompilerImpl compiler = | |
| 87 await run(useMirrorHelperLibrary: false, minify: minify); | |
| 88 DartBackend backend = compiler.backend; | |
| 89 MirrorRenamer mirrorRenamer = backend.mirrorRenamer; | |
| 90 | |
| 91 Expect.equals(null, mirrorRenamer.helperLibrary); | |
| 92 Expect.equals(null, mirrorRenamer.getNameFunction); | |
| 93 } | |
| 94 | |
| 95 const MEMORY_SOURCE_FILES = const <String, String> { | |
| 96 'main.dart': """ | |
| 97 import 'dart:mirrors'; | |
| 98 | |
| 99 class Foo { | |
| 100 noSuchMethod(Invocation invocation) { | |
| 101 MirrorSystem.getName(invocation.memberName); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void main() { | |
| 106 new Foo().fisk(); | |
| 107 } | |
| 108 """}; | |
| OLD | NEW |