OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 library jsinterop.world_test; |
| 6 |
| 7 import 'package:expect/expect.dart'; |
| 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'package:compiler/src/common.dart'; |
| 10 import 'package:compiler/src/elements/elements.dart' |
| 11 show Element, ClassElement; |
| 12 import 'package:compiler/src/js_backend/js_backend.dart'; |
| 13 import 'package:compiler/src/world.dart'; |
| 14 import '../type_test_helper.dart'; |
| 15 |
| 16 void main() { |
| 17 asyncTest(() async { |
| 18 await testClasses(); |
| 19 }); |
| 20 } |
| 21 |
| 22 testClasses() async { |
| 23 test(String mainSource, |
| 24 {List<String> directlyInstantiated: const <String>[], |
| 25 List<String> indirectlyInstantiated: const <String>[]}) async { |
| 26 TypeEnvironment env = await TypeEnvironment.create(r""" |
| 27 @JS() |
| 28 class A { |
| 29 get foo; |
| 30 |
| 31 external A(var foo); |
| 32 } |
| 33 |
| 34 @JS() |
| 35 class B { |
| 36 get foo; |
| 37 |
| 38 external B(var foo); |
| 39 } |
| 40 |
| 41 @JS() |
| 42 @anonymous |
| 43 class C { |
| 44 final foo; |
| 45 |
| 46 external factory C({foo}); |
| 47 } |
| 48 |
| 49 @JS() |
| 50 @anonymous |
| 51 class D { |
| 52 final foo; |
| 53 |
| 54 external factory D({foo}); |
| 55 } |
| 56 |
| 57 class E { |
| 58 final foo; |
| 59 |
| 60 E(this.foo); |
| 61 } |
| 62 |
| 63 class F { |
| 64 final foo; |
| 65 |
| 66 F(this.foo); |
| 67 } |
| 68 |
| 69 newA() => new A(0); |
| 70 newB() => new B(1); |
| 71 newC() => new C(foo: 2); |
| 72 newD() => new D(foo: 3); |
| 73 newE() => new E(4); |
| 74 newF() => new F(5); |
| 75 """, mainSource: """ |
| 76 import 'package:js/js.dart'; |
| 77 |
| 78 $mainSource |
| 79 """, useMockCompiler: false); |
| 80 Map<String, ClassElement> classEnvironment = <String, ClassElement>{}; |
| 81 |
| 82 ClassElement registerClass(ClassElement cls) { |
| 83 classEnvironment[cls.name] = cls; |
| 84 return cls; |
| 85 } |
| 86 |
| 87 ClassWorld world = env.compiler.world; |
| 88 JavaScriptBackend backend = env.compiler.backend; |
| 89 ClassElement Object_ = registerClass(env.compiler.coreClasses.objectClass); |
| 90 ClassElement Interceptor = |
| 91 registerClass(backend.helpers.jsInterceptorClass); |
| 92 ClassElement JavaScriptObject = |
| 93 registerClass(backend.helpers.jsJavaScriptObjectClass); |
| 94 ClassElement A = registerClass(env.getElement('A')); |
| 95 ClassElement B = registerClass(env.getElement('B')); |
| 96 ClassElement C = registerClass(env.getElement('C')); |
| 97 ClassElement D = registerClass(env.getElement('D')); |
| 98 ClassElement E = registerClass(env.getElement('E')); |
| 99 ClassElement F = registerClass(env.getElement('F')); |
| 100 |
| 101 Expect.equals(Interceptor.superclass, Object_); |
| 102 Expect.equals(JavaScriptObject.superclass, Interceptor); |
| 103 |
| 104 Expect.equals(A.superclass, JavaScriptObject); |
| 105 Expect.equals(B.superclass, JavaScriptObject); |
| 106 Expect.equals(C.superclass, JavaScriptObject); |
| 107 Expect.equals(D.superclass, JavaScriptObject); |
| 108 Expect.equals(E.superclass, Object_); |
| 109 Expect.equals(F.superclass, Object_); |
| 110 |
| 111 for (String name in classEnvironment.keys) { |
| 112 ClassElement cls = classEnvironment[name]; |
| 113 bool isInstantiated = false; |
| 114 if (directlyInstantiated.contains(name)) { |
| 115 isInstantiated = true; |
| 116 Expect.isTrue(world.isDirectlyInstantiated(cls), |
| 117 "Expected $name to be directly instantiated in `${mainSource}`:" |
| 118 "\n${world.dump(cls)}"); |
| 119 } |
| 120 if (indirectlyInstantiated.contains(name)) { |
| 121 isInstantiated = true; |
| 122 Expect.isTrue(world.isIndirectlyInstantiated(cls), |
| 123 "Expected $name to be indirectly instantiated in `${mainSource}`:" |
| 124 "\n${world.dump(cls)}"); |
| 125 } |
| 126 if (!isInstantiated && (name != 'Object' && name != 'Interceptor')) { |
| 127 Expect.isFalse(world.isInstantiated(cls), |
| 128 "Expected $name to be uninstantiated in `${mainSource}`:" |
| 129 "\n${world.dump(cls)}"); |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 await test('main() {}'); |
| 135 |
| 136 await test('main() => newA();', |
| 137 directlyInstantiated: ['A', 'B', 'C', 'D'], |
| 138 indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']); |
| 139 |
| 140 await test('main() => newB();', |
| 141 directlyInstantiated: ['A', 'B', 'C', 'D'], |
| 142 indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']); |
| 143 |
| 144 await test('main() => newC();', |
| 145 directlyInstantiated: ['A', 'B', 'C', 'D'], |
| 146 indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']); |
| 147 |
| 148 await test('main() => newD();', |
| 149 directlyInstantiated: ['A', 'B', 'C', 'D'], |
| 150 indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']); |
| 151 |
| 152 await test('main() => newE();', |
| 153 directlyInstantiated: ['E']); |
| 154 |
| 155 await test('main() => newF();', |
| 156 directlyInstantiated: ['F']); |
| 157 |
| 158 await test('main() => [newD(), newE()];', |
| 159 directlyInstantiated: ['A', 'B', 'C', 'D', 'E'], |
| 160 indirectlyInstantiated: ['Object', 'Interceptor', 'JavaScriptObject']); |
| 161 } |
OLD | NEW |