Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(618)

Unified Diff: tests/compiler/dart2js/jsinterop/world_test.dart

Issue 2124443002: Add type test for jsinterop types. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Cleanup Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tests/html/js_typed_interop_type_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/compiler/dart2js/jsinterop/world_test.dart
diff --git a/tests/compiler/dart2js/jsinterop/world_test.dart b/tests/compiler/dart2js/jsinterop/world_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f196b8e8520f9a0fb573a56f0448f5b0948b0aef
--- /dev/null
+++ b/tests/compiler/dart2js/jsinterop/world_test.dart
@@ -0,0 +1,161 @@
+// Copyright (c) 2016, 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.
+
+library jsinterop.world_test;
+
+import 'package:expect/expect.dart';
+import 'package:async_helper/async_helper.dart';
+import 'package:compiler/src/common.dart';
+import 'package:compiler/src/elements/elements.dart'
+ show Element, ClassElement;
+import 'package:compiler/src/js_backend/js_backend.dart';
+import 'package:compiler/src/world.dart';
+import '../type_test_helper.dart';
+
+void main() {
+ asyncTest(() async {
+ await testClasses();
+ });
+}
+
+testClasses() async {
+ test(String mainSource,
+ {List<String> directlyInstantiated: const <String>[],
+ List<String> indirectlyInstantiated: const <String>[]}) async {
+ TypeEnvironment env = await TypeEnvironment.create(r"""
+@JS()
+class A {
+ get foo;
+
+ external A(var foo);
+}
+
+@JS()
+class B {
+ get foo;
+
+ external B(var foo);
+}
+
+@JS()
+@anonymous
+class C {
+ final foo;
+
+ external factory C({foo});
+}
+
+@JS()
+@anonymous
+class D {
+ final foo;
+
+ external factory D({foo});
+}
+
+class E {
+ final foo;
+
+ E(this.foo);
+}
+
+class F {
+ final foo;
+
+ F(this.foo);
+}
+
+newA() => new A(0);
+newB() => new B(1);
+newC() => new C(foo: 2);
+newD() => new D(foo: 3);
+newE() => new E(4);
+newF() => new F(5);
+""", mainSource: """
+import 'package:js/js.dart';
+
+$mainSource
+""", useMockCompiler: false);
+ Map<String, ClassElement> classEnvironment = <String, ClassElement>{};
+
+ ClassElement registerClass(ClassElement cls) {
+ classEnvironment[cls.name] = cls;
+ return cls;
+ }
+
+ ClassWorld world = env.compiler.world;
+ JavaScriptBackend backend = env.compiler.backend;
+ ClassElement Object_ = registerClass(env.compiler.coreClasses.objectClass);
+ ClassElement Interceptor =
+ registerClass(backend.helpers.jsInterceptorClass);
+ ClassElement JavaScriptObject =
+ registerClass(backend.helpers.jsJavaScriptObjectClass);
+ ClassElement A = registerClass(env.getElement('A'));
+ ClassElement B = registerClass(env.getElement('B'));
+ ClassElement C = registerClass(env.getElement('C'));
+ ClassElement D = registerClass(env.getElement('D'));
+ ClassElement E = registerClass(env.getElement('E'));
+ ClassElement F = registerClass(env.getElement('F'));
+
+ Expect.equals(Interceptor.superclass, Object_);
+ Expect.equals(JavaScriptObject.superclass, Interceptor);
+
+ Expect.equals(A.superclass, JavaScriptObject);
+ Expect.equals(B.superclass, JavaScriptObject);
+ Expect.equals(C.superclass, JavaScriptObject);
+ Expect.equals(D.superclass, JavaScriptObject);
+ Expect.equals(E.superclass, Object_);
+ Expect.equals(F.superclass, Object_);
+
+ for (String name in classEnvironment.keys) {
+ ClassElement cls = classEnvironment[name];
+ bool isInstantiated = false;
+ if (directlyInstantiated.contains(name)) {
+ isInstantiated = true;
+ Expect.isTrue(world.isDirectlyInstantiated(cls),
+ "Expected $name to be directly instantiated in `${mainSource}`:"
+ "\n${world.dump(cls)}");
+ }
+ if (indirectlyInstantiated.contains(name)) {
+ isInstantiated = true;
+ Expect.isTrue(world.isIndirectlyInstantiated(cls),
+ "Expected $name to be indirectly instantiated in `${mainSource}`:"
+ "\n${world.dump(cls)}");
+ }
+ if (!isInstantiated && (name != 'Object' && name != 'Interceptor')) {
+ Expect.isFalse(world.isInstantiated(cls),
+ "Expected $name to be uninstantiated in `${mainSource}`:"
+ "\n${world.dump(cls)}");
+ }
+ }
+ }
+
+ await test('main() {}');
+
+ await test('main() => newA();',
+ directlyInstantiated: ['A', 'B', 'C', 'D'],
+ indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']);
+
+ await test('main() => newB();',
+ directlyInstantiated: ['A', 'B', 'C', 'D'],
+ indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']);
+
+ await test('main() => newC();',
+ directlyInstantiated: ['A', 'B', 'C', 'D'],
+ indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']);
+
+ await test('main() => newD();',
+ directlyInstantiated: ['A', 'B', 'C', 'D'],
+ indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']);
+
+ await test('main() => newE();',
+ directlyInstantiated: ['E']);
+
+ await test('main() => newF();',
+ directlyInstantiated: ['F']);
+
+ await test('main() => [newD(), newE()];',
+ directlyInstantiated: ['A', 'B', 'C', 'D', 'E'],
+ indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']);
+}
« no previous file with comments | « no previous file | tests/html/js_typed_interop_type_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698