| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library world_test; | 5 library world_test; |
| 6 | 6 |
| 7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
| 8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
| 9 import 'type_test_helper.dart'; | 9 import 'type_test_helper.dart'; |
| 10 import 'package:compiler/src/common.dart'; | 10 import 'package:compiler/src/common.dart'; |
| 11 import 'package:compiler/src/elements/elements.dart' | 11 import 'package:compiler/src/elements/elements.dart' show Element, ClassElement; |
| 12 show Element, ClassElement; | |
| 13 import 'package:compiler/src/universe/class_set.dart'; | 12 import 'package:compiler/src/universe/class_set.dart'; |
| 14 import 'package:compiler/src/world.dart' show ClassWorld; | 13 import 'package:compiler/src/world.dart' show ClassWorld; |
| 15 | 14 |
| 16 void main() { | 15 void main() { |
| 17 asyncTest(() async { | 16 asyncTest(() async { |
| 18 await testClassSets(); | 17 await testClassSets(); |
| 19 await testProperties(); | 18 await testProperties(); |
| 20 }); | 19 }); |
| 21 } | 20 } |
| 22 | 21 |
| 23 testClassSets() async { | 22 testClassSets() async { |
| 24 var env = await TypeEnvironment.create(r""" | 23 var env = await TypeEnvironment.create( |
| 24 r""" |
| 25 class A implements X {} | 25 class A implements X {} |
| 26 class B {} | 26 class B {} |
| 27 class C_Super extends A {} | 27 class C_Super extends A {} |
| 28 class C extends C_Super {} | 28 class C extends C_Super {} |
| 29 class D implements A {} | 29 class D implements A {} |
| 30 class E extends B implements A {} | 30 class E extends B implements A {} |
| 31 class F extends Object with A implements B {} | 31 class F extends Object with A implements B {} |
| 32 class G extends Object with A, B {} | 32 class G extends Object with A, B {} |
| 33 class X {} | 33 class X {} |
| 34 """, | 34 """, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 49 ClassElement Object_ = env.getElement("Object"); | 49 ClassElement Object_ = env.getElement("Object"); |
| 50 ClassElement A = env.getElement("A"); | 50 ClassElement A = env.getElement("A"); |
| 51 ClassElement B = env.getElement("B"); | 51 ClassElement B = env.getElement("B"); |
| 52 ClassElement C = env.getElement("C"); | 52 ClassElement C = env.getElement("C"); |
| 53 ClassElement D = env.getElement("D"); | 53 ClassElement D = env.getElement("D"); |
| 54 ClassElement E = env.getElement("E"); | 54 ClassElement E = env.getElement("E"); |
| 55 ClassElement F = env.getElement("F"); | 55 ClassElement F = env.getElement("F"); |
| 56 ClassElement G = env.getElement("G"); | 56 ClassElement G = env.getElement("G"); |
| 57 ClassElement X = env.getElement("X"); | 57 ClassElement X = env.getElement("X"); |
| 58 | 58 |
| 59 void checkClasses( | 59 void checkClasses(String property, ClassElement cls, |
| 60 String property, | 60 Iterable<ClassElement> foundClasses, List<ClassElement> expectedClasses, |
| 61 ClassElement cls, | |
| 62 Iterable<ClassElement> foundClasses, | |
| 63 List<ClassElement> expectedClasses, | |
| 64 {bool exact: true}) { | 61 {bool exact: true}) { |
| 65 | |
| 66 for (ClassElement expectedClass in expectedClasses) { | 62 for (ClassElement expectedClass in expectedClasses) { |
| 67 Expect.isTrue(foundClasses.contains(expectedClass), | 63 Expect.isTrue( |
| 64 foundClasses.contains(expectedClass), |
| 68 "Expect $expectedClass in '$property' on $cls. " | 65 "Expect $expectedClass in '$property' on $cls. " |
| 69 "Found:\n ${foundClasses.join('\n ')}\n" | 66 "Found:\n ${foundClasses.join('\n ')}\n" |
| 70 "${env.compiler.world.dump(cls)}"); | 67 "${env.compiler.world.dump(cls)}"); |
| 71 } | 68 } |
| 72 if (exact) { | 69 if (exact) { |
| 73 Expect.equals(expectedClasses.length, foundClasses.length, | 70 Expect.equals( |
| 71 expectedClasses.length, |
| 72 foundClasses.length, |
| 74 "Unexpected classes " | 73 "Unexpected classes " |
| 75 "${foundClasses.where((c) => !expectedClasses.contains(c))} " | 74 "${foundClasses.where((c) => !expectedClasses.contains(c))} " |
| 76 "in '$property' on $cls.\n" | 75 "in '$property' on $cls.\n" |
| 77 "${env.compiler.world.dump(cls)}"); | 76 "${env.compiler.world.dump(cls)}"); |
| 78 } | 77 } |
| 79 } | 78 } |
| 80 | 79 |
| 81 void check( | 80 void check(String property, ClassElement cls, |
| 82 String property, | 81 Iterable<ClassElement> foundClasses, List<ClassElement> expectedClasses, |
| 83 ClassElement cls, | |
| 84 Iterable<ClassElement> foundClasses, | |
| 85 List<ClassElement> expectedClasses, | |
| 86 {bool exact: true, | 82 {bool exact: true, |
| 87 void forEach(ClassElement cls, ForEachFunction f), | 83 void forEach(ClassElement cls, ForEachFunction f), |
| 88 int getCount(ClassElement cls)}) { | 84 int getCount(ClassElement cls)}) { |
| 89 checkClasses(property, cls, foundClasses, expectedClasses, exact: exact); | 85 checkClasses(property, cls, foundClasses, expectedClasses, exact: exact); |
| 90 | 86 |
| 91 if (forEach != null) { | 87 if (forEach != null) { |
| 92 List<ClassElement> visited = <ClassElement>[]; | 88 List<ClassElement> visited = <ClassElement>[]; |
| 93 forEach(cls, (ClassElement c) { | 89 forEach(cls, (ClassElement c) { |
| 94 visited.add(c); | 90 visited.add(c); |
| 95 }); | 91 }); |
| 96 checkClasses( | 92 checkClasses('forEach($property)', cls, visited, expectedClasses, |
| 97 'forEach($property)', cls, visited, expectedClasses, exact: exact); | 93 exact: exact); |
| 98 } | 94 } |
| 99 | 95 |
| 100 if (getCount != null && exact) { | 96 if (getCount != null && exact) { |
| 101 int count = getCount(cls); | 97 int count = getCount(cls); |
| 102 Expect.equals(expectedClasses.length, count, | 98 Expect.equals( |
| 99 expectedClasses.length, |
| 100 count, |
| 103 "Unexpected class count in '$property' on $cls.\n" | 101 "Unexpected class count in '$property' on $cls.\n" |
| 104 "${env.compiler.world.dump(cls)}"); | 102 "${env.compiler.world.dump(cls)}"); |
| 105 } | 103 } |
| 106 | |
| 107 } | 104 } |
| 108 | 105 |
| 109 void testSubclasses( | 106 void testSubclasses(ClassElement cls, List<ClassElement> expectedClasses, |
| 110 ClassElement cls, | |
| 111 List<ClassElement> expectedClasses, | |
| 112 {bool exact: true}) { | 107 {bool exact: true}) { |
| 113 check( | 108 check('subclassesOf', cls, classWorld.subclassesOf(cls), expectedClasses, |
| 114 'subclassesOf', | 109 exact: exact); |
| 115 cls, | |
| 116 classWorld.subclassesOf(cls), | |
| 117 expectedClasses, | |
| 118 exact: exact); | |
| 119 } | 110 } |
| 120 | 111 |
| 121 void testStrictSubclasses( | 112 void testStrictSubclasses( |
| 122 ClassElement cls, | 113 ClassElement cls, List<ClassElement> expectedClasses, |
| 123 List<ClassElement> expectedClasses, | |
| 124 {bool exact: true}) { | 114 {bool exact: true}) { |
| 125 check( | 115 check('strictSubclassesOf', cls, classWorld.strictSubclassesOf(cls), |
| 126 'strictSubclassesOf', | 116 expectedClasses, |
| 127 cls, | 117 exact: exact, |
| 128 classWorld.strictSubclassesOf(cls), | 118 forEach: classWorld.forEachStrictSubclassOf, |
| 129 expectedClasses, | 119 getCount: classWorld.strictSubclassCount); |
| 130 exact: exact, | |
| 131 forEach: classWorld.forEachStrictSubclassOf, | |
| 132 getCount: classWorld.strictSubclassCount); | |
| 133 } | 120 } |
| 134 | 121 |
| 135 void testStrictSubtypes( | 122 void testStrictSubtypes(ClassElement cls, List<ClassElement> expectedClasses, |
| 136 ClassElement cls, | |
| 137 List<ClassElement> expectedClasses, | |
| 138 {bool exact: true}) { | 123 {bool exact: true}) { |
| 139 check( | 124 check('strictSubtypesOf', cls, classWorld.strictSubtypesOf(cls), |
| 140 'strictSubtypesOf', | 125 expectedClasses, |
| 141 cls, | 126 exact: exact, |
| 142 classWorld.strictSubtypesOf(cls), | 127 forEach: classWorld.forEachStrictSubtypeOf, |
| 143 expectedClasses, | 128 getCount: classWorld.strictSubtypeCount); |
| 144 exact: exact, | |
| 145 forEach: classWorld.forEachStrictSubtypeOf, | |
| 146 getCount: classWorld.strictSubtypeCount); | |
| 147 } | 129 } |
| 148 | 130 |
| 149 void testMixinUses( | 131 void testMixinUses(ClassElement cls, List<ClassElement> expectedClasses, |
| 150 ClassElement cls, | |
| 151 List<ClassElement> expectedClasses, | |
| 152 {bool exact: true}) { | 132 {bool exact: true}) { |
| 153 check( | 133 check('mixinUsesOf', cls, classWorld.mixinUsesOf(cls), expectedClasses, |
| 154 'mixinUsesOf', | 134 exact: exact); |
| 155 cls, | |
| 156 classWorld.mixinUsesOf(cls), | |
| 157 expectedClasses, | |
| 158 exact: exact); | |
| 159 } | 135 } |
| 160 | 136 |
| 161 testSubclasses(Object_, [A, B, C, D, E, F, G], exact: false); | 137 testSubclasses(Object_, [A, B, C, D, E, F, G], exact: false); |
| 162 testSubclasses(A, [A, C]); | 138 testSubclasses(A, [A, C]); |
| 163 testSubclasses(B, [B, E]); | 139 testSubclasses(B, [B, E]); |
| 164 testSubclasses(C, [C]); | 140 testSubclasses(C, [C]); |
| 165 testSubclasses(D, [D]); | 141 testSubclasses(D, [D]); |
| 166 testSubclasses(E, [E]); | 142 testSubclasses(E, [E]); |
| 167 testSubclasses(F, [F]); | 143 testSubclasses(F, [F]); |
| 168 testSubclasses(G, [G]); | 144 testSubclasses(G, [G]); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 193 testMixinUses(B, [G.superclass]); | 169 testMixinUses(B, [G.superclass]); |
| 194 testMixinUses(C, []); | 170 testMixinUses(C, []); |
| 195 testMixinUses(D, []); | 171 testMixinUses(D, []); |
| 196 testMixinUses(E, []); | 172 testMixinUses(E, []); |
| 197 testMixinUses(F, []); | 173 testMixinUses(F, []); |
| 198 testMixinUses(G, []); | 174 testMixinUses(G, []); |
| 199 testMixinUses(X, []); | 175 testMixinUses(X, []); |
| 200 } | 176 } |
| 201 | 177 |
| 202 testProperties() async { | 178 testProperties() async { |
| 203 var env = await TypeEnvironment.create(r""" | 179 var env = await TypeEnvironment.create( |
| 180 r""" |
| 204 class A {} | 181 class A {} |
| 205 class A1 extends A {} | 182 class A1 extends A {} |
| 206 class A2 implements A {} | 183 class A2 implements A {} |
| 207 class A3 extends Object with A {} | 184 class A3 extends Object with A {} |
| 208 | 185 |
| 209 class B {} | 186 class B {} |
| 210 class B1 extends B {} | 187 class B1 extends B {} |
| 211 class B2 implements B {} | 188 class B2 implements B {} |
| 212 class B3 extends Object with B {} | 189 class B3 extends Object with B {} |
| 213 | 190 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 new F1(); | 229 new F1(); |
| 253 new F2(); | 230 new F2(); |
| 254 new G2(); | 231 new G2(); |
| 255 new G3(); | 232 new G3(); |
| 256 new H4(); | 233 new H4(); |
| 257 } | 234 } |
| 258 """, | 235 """, |
| 259 useMockCompiler: false); | 236 useMockCompiler: false); |
| 260 ClassWorld classWorld = env.compiler.world; | 237 ClassWorld classWorld = env.compiler.world; |
| 261 | 238 |
| 262 check(String name, | 239 check(String name, {bool hasStrictSubtype, bool hasOnlySubclasses}) { |
| 263 {bool hasStrictSubtype, | |
| 264 bool hasOnlySubclasses}) { | |
| 265 ClassElement cls = env.getElement(name); | 240 ClassElement cls = env.getElement(name); |
| 266 Expect.equals(hasStrictSubtype, classWorld.hasAnyStrictSubtype(cls), | 241 Expect.equals(hasStrictSubtype, classWorld.hasAnyStrictSubtype(cls), |
| 267 "Unexpected hasAnyStrictSubtype property on $cls."); | 242 "Unexpected hasAnyStrictSubtype property on $cls."); |
| 268 Expect.equals(hasOnlySubclasses, classWorld.hasOnlySubclasses(cls), | 243 Expect.equals(hasOnlySubclasses, classWorld.hasOnlySubclasses(cls), |
| 269 "Unexpected hasOnlySubclasses property on $cls."); | 244 "Unexpected hasOnlySubclasses property on $cls."); |
| 270 } | 245 } |
| 271 | 246 |
| 272 check("Object", hasStrictSubtype: true, hasOnlySubclasses: true); | 247 check("Object", hasStrictSubtype: true, hasOnlySubclasses: true); |
| 273 | 248 |
| 274 // No instantiated Ax classes. | 249 // No instantiated Ax classes. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 check("G3", hasStrictSubtype: false, hasOnlySubclasses: true); | 291 check("G3", hasStrictSubtype: false, hasOnlySubclasses: true); |
| 317 check("G4", hasStrictSubtype: false, hasOnlySubclasses: true); | 292 check("G4", hasStrictSubtype: false, hasOnlySubclasses: true); |
| 318 | 293 |
| 319 // class H4 extends H2 with H extends H1 extends H instantiated | 294 // class H4 extends H2 with H extends H1 extends H instantiated |
| 320 check("H", hasStrictSubtype: true, hasOnlySubclasses: true); | 295 check("H", hasStrictSubtype: true, hasOnlySubclasses: true); |
| 321 check("H1", hasStrictSubtype: true, hasOnlySubclasses: true); | 296 check("H1", hasStrictSubtype: true, hasOnlySubclasses: true); |
| 322 check("H2", hasStrictSubtype: true, hasOnlySubclasses: true); | 297 check("H2", hasStrictSubtype: true, hasOnlySubclasses: true); |
| 323 check("H3", hasStrictSubtype: false, hasOnlySubclasses: true); | 298 check("H3", hasStrictSubtype: false, hasOnlySubclasses: true); |
| 324 check("H4", hasStrictSubtype: false, hasOnlySubclasses: true); | 299 check("H4", hasStrictSubtype: false, hasOnlySubclasses: true); |
| 325 } | 300 } |
| OLD | NEW |