Chromium Code Reviews| 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 'memory_compiler.dart' show compilerFor; | |
| 7 import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' show | |
| 8 Compiler; | |
| 9 import | |
| 10 '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dart' | |
| 11 show | |
| 12 Element, LibraryElement, ClassElement; | |
| 13 import | |
| 14 '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart' | |
| 15 show | |
| 16 Node; | |
| 17 import | |
| 18 '../../../sdk/lib/_internal/compiler/implementation/dart_backend/dart_backen d.dart' | |
| 19 show | |
| 20 DartBackend, ElementAst; | |
| 21 import | |
| 22 '../../../sdk/lib/_internal/compiler/implementation/mirror_renamer/mirror_re namer.dart' | |
| 23 show | |
| 24 MirrorRenamer; | |
| 25 | |
| 26 const String MIRROR_HELPER_CLASS = 'MirrorHelper'; | |
|
ahe
2013/08/06 15:53:36
It's nice that you define constants, but I feel it
zarah
2013/08/08 12:17:10
Removed! :-)
| |
| 27 const String MIRROR_HELPER_GET_NAME_FUNCTION = 'getName'; | |
| 28 const String MIRROR_HELPER_LIBRARY_NAME = 'mirror_helper.dart'; | |
| 29 const String MIRROR_HELPER_LIBRARY_PREFIX = 'm'; | |
| 30 | |
| 31 | |
| 32 main() { | |
| 33 testWithMirrorRenaming(); | |
| 34 testWithoutMirrorRenaming(); | |
| 35 testWithMirrorRenamingMinify(); | |
| 36 testWithoutMirrorRenamingMinify(); | |
| 37 } | |
| 38 | |
| 39 void testWithMirrorRenaming() { | |
| 40 Compiler compiler = compilerFor( | |
| 41 MEMORY_SOURCE_FILES, | |
| 42 options: ['--output-type=dart']); | |
| 43 DartBackend backend = compiler.backend; | |
| 44 backend.mirrorHelping = true; | |
|
ahe
2013/08/06 15:53:36
I think these four lines are almost always the sam
zarah
2013/08/08 12:17:10
Done.
| |
| 45 compiler.runCompiler(Uri.parse('memory:main.dart')); | |
| 46 Map<Node, String> renames = backend.renames; | |
| 47 Map<LibraryElement, String> imports = backend.imports; | |
| 48 | |
| 49 Node getNameFunctionNode = | |
| 50 backend.memberNodes.values.first.first.body.statements.nodes.head; | |
|
ahe
2013/08/06 15:53:36
This feels a bit brittle to me. How about (not tes
zarah
2013/08/08 12:17:10
I agree. The point is however, that in the backend
| |
| 51 | |
| 52 Expect.equals(renames[getNameFunctionNode.expression.selector], | |
| 53 MIRROR_HELPER_GET_NAME_FUNCTION); | |
| 54 Expect.equals(renames[getNameFunctionNode.expression.receiver], | |
| 55 '$MIRROR_HELPER_LIBRARY_PREFIX.$MIRROR_HELPER_CLASS'); | |
| 56 Expect.equals(2, imports.keys.length); | |
| 57 Expect.isTrue(imports.keys.any((library) => | |
| 58 library.canonicalUri == new Uri(path: MIRROR_HELPER_LIBRARY_NAME))); | |
| 59 } | |
|
ahe
2013/08/06 15:53:36
My comments above apply to the following functions
zarah
2013/08/08 12:17:10
Done.
| |
| 60 | |
| 61 void testWithMirrorRenamingMinify() { | |
| 62 Compiler compiler = compilerFor( | |
| 63 MEMORY_SOURCE_FILES, | |
| 64 options: ['--output-type=dart', '--minify']); | |
| 65 DartBackend backend = compiler.backend; | |
| 66 backend.mirrorHelping = true; | |
| 67 compiler.runCompiler(Uri.parse('memory:main.dart')); | |
| 68 Map<Node, String> renames = backend.renames; | |
| 69 Map<LibraryElement, String> imports = backend.imports; | |
| 70 | |
| 71 Node getNameFunctionNode = | |
| 72 backend.memberNodes.values.first.first.body.statements.nodes.head; | |
| 73 | |
| 74 Expect.equals(renames[getNameFunctionNode.expression.selector], | |
| 75 MIRROR_HELPER_GET_NAME_FUNCTION); | |
| 76 Expect.equals(renames[getNameFunctionNode.expression.receiver], | |
| 77 '$MIRROR_HELPER_LIBRARY_PREFIX.$MIRROR_HELPER_CLASS'); | |
| 78 Expect.equals(2, imports.keys.length); | |
| 79 Expect.isTrue(imports.keys.any((library) => | |
| 80 library.canonicalUri == new Uri(path: MIRROR_HELPER_LIBRARY_NAME))); | |
| 81 } | |
| 82 | |
| 83 void testWithoutMirrorRenaming() { | |
| 84 Compiler compiler = compilerFor( | |
| 85 MEMORY_SOURCE_FILES, | |
| 86 options: ['--output-type=dart']); | |
| 87 DartBackend backend = compiler.backend; | |
| 88 backend.mirrorHelping = false; | |
| 89 compiler.runCompiler(Uri.parse('memory:main.dart')); | |
| 90 Map<Node, String> renames = backend.renames; | |
| 91 Map<LibraryElement, String> imports = backend.imports; | |
| 92 | |
| 93 Node getNameFunctionNode = | |
| 94 backend.memberNodes.values.first.first.body.statements.nodes.head; | |
| 95 | |
| 96 Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.selector)); | |
| 97 Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.receiver)); | |
| 98 Expect.equals(1, imports.keys.length); | |
| 99 } | |
| 100 | |
| 101 void testWithoutMirrorRenamingMinify() { | |
| 102 Compiler compiler = compilerFor( | |
| 103 MEMORY_SOURCE_FILES, | |
| 104 options: ['--output-type=dart', '--minify']); | |
| 105 DartBackend backend = compiler.backend; | |
| 106 backend.mirrorHelping = false; | |
| 107 compiler.runCompiler(Uri.parse('memory:main.dart')); | |
| 108 Map<Node, String> renames = backend.renames; | |
| 109 Map<LibraryElement, String> imports = backend.imports; | |
| 110 | |
| 111 Node getNameFunctionNode = | |
| 112 backend.memberNodes.values.first.first.body.statements.nodes.head; | |
| 113 | |
| 114 Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.selector)); | |
| 115 Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.receiver)); | |
| 116 Expect.equals(1, imports.keys.length); | |
| 117 } | |
| 118 | |
| 119 const MEMORY_SOURCE_FILES = const <String, String> { | |
| 120 'main.dart': """ | |
| 121 import 'dart:mirrors'; | |
| 122 | |
| 123 | |
| 124 class Foo { | |
| 125 noSuchMethod(Invocation invocation) { | |
| 126 MirrorSystem.getName(invocation.memberName); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void main() { | |
| 131 new Foo().fist(); | |
| 132 } | |
| 133 """}; | |
| OLD | NEW |