Chromium Code Reviews| Index: tests/compiler/dart2js/mirror_helper_test.dart |
| diff --git a/tests/compiler/dart2js/mirror_helper_test.dart b/tests/compiler/dart2js/mirror_helper_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..158e5fd1b6b4f554b503efb2876ba1ee55c9e8b1 |
| --- /dev/null |
| +++ b/tests/compiler/dart2js/mirror_helper_test.dart |
| @@ -0,0 +1,133 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import "package:expect/expect.dart"; |
| +import 'memory_compiler.dart' show compilerFor; |
| +import '../../../sdk/lib/_internal/compiler/implementation/apiimpl.dart' show |
| + Compiler; |
| +import |
| + '../../../sdk/lib/_internal/compiler/implementation/elements/elements.dart' |
| +show |
| + Element, LibraryElement, ClassElement; |
| +import |
| + '../../../sdk/lib/_internal/compiler/implementation/tree/tree.dart' |
| +show |
| + Node; |
| +import |
| + '../../../sdk/lib/_internal/compiler/implementation/dart_backend/dart_backend.dart' |
| +show |
| + DartBackend, ElementAst; |
| +import |
| + '../../../sdk/lib/_internal/compiler/implementation/mirror_renamer/mirror_renamer.dart' |
| +show |
| + MirrorRenamer; |
| + |
| +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! :-)
|
| +const String MIRROR_HELPER_GET_NAME_FUNCTION = 'getName'; |
| +const String MIRROR_HELPER_LIBRARY_NAME = 'mirror_helper.dart'; |
| +const String MIRROR_HELPER_LIBRARY_PREFIX = 'm'; |
| + |
| + |
| +main() { |
| + testWithMirrorRenaming(); |
| + testWithoutMirrorRenaming(); |
| + testWithMirrorRenamingMinify(); |
| + testWithoutMirrorRenamingMinify(); |
| +} |
| + |
| +void testWithMirrorRenaming() { |
| + Compiler compiler = compilerFor( |
| + MEMORY_SOURCE_FILES, |
| + options: ['--output-type=dart']); |
| + DartBackend backend = compiler.backend; |
| + 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.
|
| + compiler.runCompiler(Uri.parse('memory:main.dart')); |
| + Map<Node, String> renames = backend.renames; |
| + Map<LibraryElement, String> imports = backend.imports; |
| + |
| + Node getNameFunctionNode = |
| + 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
|
| + |
| + Expect.equals(renames[getNameFunctionNode.expression.selector], |
| + MIRROR_HELPER_GET_NAME_FUNCTION); |
| + Expect.equals(renames[getNameFunctionNode.expression.receiver], |
| + '$MIRROR_HELPER_LIBRARY_PREFIX.$MIRROR_HELPER_CLASS'); |
| + Expect.equals(2, imports.keys.length); |
| + Expect.isTrue(imports.keys.any((library) => |
| + library.canonicalUri == new Uri(path: MIRROR_HELPER_LIBRARY_NAME))); |
| +} |
|
ahe
2013/08/06 15:53:36
My comments above apply to the following functions
zarah
2013/08/08 12:17:10
Done.
|
| + |
| +void testWithMirrorRenamingMinify() { |
| + Compiler compiler = compilerFor( |
| + MEMORY_SOURCE_FILES, |
| + options: ['--output-type=dart', '--minify']); |
| + DartBackend backend = compiler.backend; |
| + backend.mirrorHelping = true; |
| + compiler.runCompiler(Uri.parse('memory:main.dart')); |
| + Map<Node, String> renames = backend.renames; |
| + Map<LibraryElement, String> imports = backend.imports; |
| + |
| + Node getNameFunctionNode = |
| + backend.memberNodes.values.first.first.body.statements.nodes.head; |
| + |
| + Expect.equals(renames[getNameFunctionNode.expression.selector], |
| + MIRROR_HELPER_GET_NAME_FUNCTION); |
| + Expect.equals(renames[getNameFunctionNode.expression.receiver], |
| + '$MIRROR_HELPER_LIBRARY_PREFIX.$MIRROR_HELPER_CLASS'); |
| + Expect.equals(2, imports.keys.length); |
| + Expect.isTrue(imports.keys.any((library) => |
| + library.canonicalUri == new Uri(path: MIRROR_HELPER_LIBRARY_NAME))); |
| +} |
| + |
| +void testWithoutMirrorRenaming() { |
| + Compiler compiler = compilerFor( |
| + MEMORY_SOURCE_FILES, |
| + options: ['--output-type=dart']); |
| + DartBackend backend = compiler.backend; |
| + backend.mirrorHelping = false; |
| + compiler.runCompiler(Uri.parse('memory:main.dart')); |
| + Map<Node, String> renames = backend.renames; |
| + Map<LibraryElement, String> imports = backend.imports; |
| + |
| + Node getNameFunctionNode = |
| + backend.memberNodes.values.first.first.body.statements.nodes.head; |
| + |
| + Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.selector)); |
| + Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.receiver)); |
| + Expect.equals(1, imports.keys.length); |
| +} |
| + |
| +void testWithoutMirrorRenamingMinify() { |
| + Compiler compiler = compilerFor( |
| + MEMORY_SOURCE_FILES, |
| + options: ['--output-type=dart', '--minify']); |
| + DartBackend backend = compiler.backend; |
| + backend.mirrorHelping = false; |
| + compiler.runCompiler(Uri.parse('memory:main.dart')); |
| + Map<Node, String> renames = backend.renames; |
| + Map<LibraryElement, String> imports = backend.imports; |
| + |
| + Node getNameFunctionNode = |
| + backend.memberNodes.values.first.first.body.statements.nodes.head; |
| + |
| + Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.selector)); |
| + Expect.isFalse(renames.containsKey(getNameFunctionNode.expression.receiver)); |
| + Expect.equals(1, imports.keys.length); |
| +} |
| + |
| +const MEMORY_SOURCE_FILES = const <String, String> { |
| + 'main.dart': """ |
| +import 'dart:mirrors'; |
| + |
| + |
| +class Foo { |
| + noSuchMethod(Invocation invocation) { |
| + MirrorSystem.getName(invocation.memberName); |
| + } |
| +} |
| + |
| +void main() { |
| + new Foo().fist(); |
| +} |
| +"""}; |