| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2017, 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:kernel/ast.dart'; | |
| 6 import 'package:kernel/class_hierarchy.dart'; | |
| 7 import 'package:kernel/core_types.dart'; | |
| 8 import 'package:kernel/testing/mock_sdk_program.dart'; | |
| 9 import 'package:test/test.dart'; | |
| 10 | |
| 11 main() { | |
| 12 Library makeTestLibrary(Program program) { | |
| 13 var library = new Library(Uri.parse('org-dartlang:///test.dart')) | |
| 14 ..parent = program; | |
| 15 program.libraries.add(library); | |
| 16 return library; | |
| 17 } | |
| 18 | |
| 19 test('depth', () { | |
| 20 var program = createMockSdkProgram(); | |
| 21 var coreTypes = new CoreTypes(program); | |
| 22 var defaultSuper = coreTypes.objectClass.asThisSupertype; | |
| 23 var library = makeTestLibrary(program); | |
| 24 | |
| 25 Class addClass(Class c) { | |
| 26 library.addClass(c); | |
| 27 return c; | |
| 28 } | |
| 29 | |
| 30 var base = addClass(new Class(name: 'base', supertype: defaultSuper)); | |
| 31 var extends_ = | |
| 32 addClass(new Class(name: 'extends_', supertype: base.asThisSupertype)); | |
| 33 var with_ = addClass(new Class( | |
| 34 name: 'with_', | |
| 35 supertype: defaultSuper, | |
| 36 mixedInType: base.asThisSupertype)); | |
| 37 var implements_ = addClass(new Class( | |
| 38 name: 'implements_', | |
| 39 supertype: defaultSuper, | |
| 40 implementedTypes: [base.asThisSupertype])); | |
| 41 var hierarchy = new ClassHierarchy(program); | |
| 42 | |
| 43 expect(hierarchy.getClassDepth(coreTypes.objectClass), 0); | |
| 44 expect(hierarchy.getClassDepth(base), 1); | |
| 45 expect(hierarchy.getClassDepth(extends_), 2); | |
| 46 expect(hierarchy.getClassDepth(with_), 2); | |
| 47 expect(hierarchy.getClassDepth(implements_), 2); | |
| 48 }); | |
| 49 | |
| 50 test('ranked_superclasses', () { | |
| 51 var program = createMockSdkProgram(); | |
| 52 var coreTypes = new CoreTypes(program); | |
| 53 var defaultSuper = coreTypes.objectClass.asThisSupertype; | |
| 54 var library = makeTestLibrary(program); | |
| 55 | |
| 56 Class addClass(String name, List<Class> implements_) { | |
| 57 var c = new Class( | |
| 58 name: name, | |
| 59 supertype: defaultSuper, | |
| 60 implementedTypes: implements_.map((c) => c.asThisSupertype).toList()); | |
| 61 library.addClass(c); | |
| 62 return c; | |
| 63 } | |
| 64 | |
| 65 // Create the class hierarchy: | |
| 66 // | |
| 67 // Object | |
| 68 // | | |
| 69 // A | |
| 70 // / \ | |
| 71 // B C | |
| 72 // | | | |
| 73 // | D | |
| 74 // \ / | |
| 75 // E | |
| 76 var a = addClass('A', []); | |
| 77 var b = addClass('B', [a]); | |
| 78 var c = addClass('C', [a]); | |
| 79 var d = addClass('D', [c]); | |
| 80 var e = addClass('E', [b, d]); | |
| 81 var hierarchy = new ClassHierarchy(program); | |
| 82 | |
| 83 expect(hierarchy.getRankedSuperclasses(a), [a, coreTypes.objectClass]); | |
| 84 expect(hierarchy.getRankedSuperclasses(b), [b, a, coreTypes.objectClass]); | |
| 85 expect(hierarchy.getRankedSuperclasses(c), [c, a, coreTypes.objectClass]); | |
| 86 expect( | |
| 87 hierarchy.getRankedSuperclasses(d), [d, c, a, coreTypes.objectClass]); | |
| 88 if (hierarchy.getClassIndex(b) < hierarchy.getClassIndex(c)) { | |
| 89 expect(hierarchy.getRankedSuperclasses(e), | |
| 90 [e, d, b, c, a, coreTypes.objectClass]); | |
| 91 } else { | |
| 92 expect(hierarchy.getRankedSuperclasses(e), | |
| 93 [e, d, c, b, a, coreTypes.objectClass]); | |
| 94 } | |
| 95 }); | |
| 96 | |
| 97 test('least_upper_bound_non_generic', () { | |
| 98 var program = createMockSdkProgram(); | |
| 99 var coreTypes = new CoreTypes(program); | |
| 100 var defaultSuper = coreTypes.objectClass.asThisSupertype; | |
| 101 var library = makeTestLibrary(program); | |
| 102 | |
| 103 Class addClass(String name, List<Class> implements_) { | |
| 104 var c = new Class( | |
| 105 name: name, | |
| 106 supertype: defaultSuper, | |
| 107 implementedTypes: implements_.map((c) => c.asThisSupertype).toList()); | |
| 108 library.addClass(c); | |
| 109 return c; | |
| 110 } | |
| 111 | |
| 112 // Create the class hierarchy: | |
| 113 // | |
| 114 // Object | |
| 115 // / \ | |
| 116 // A B | |
| 117 // /|\ | |
| 118 // C D E | |
| 119 // |X|/ | |
| 120 // FG HI | |
| 121 // | |
| 122 // (F and G both implement (C, D); H and I both implement (C, D, E). | |
| 123 var a = addClass('A', []); | |
| 124 var b = addClass('B', []); | |
| 125 var c = addClass('C', [a]); | |
| 126 var d = addClass('D', [a]); | |
| 127 var e = addClass('E', [a]); | |
| 128 var f = addClass('F', [c, d]); | |
| 129 var g = addClass('G', [c, d]); | |
| 130 var h = addClass('H', [c, d, e]); | |
| 131 var i = addClass('I', [c, d, e]); | |
| 132 var hierarchy = new ClassHierarchy(program); | |
| 133 | |
| 134 expect(hierarchy.getClassicLeastUpperBound(a.rawType, b.rawType), | |
| 135 coreTypes.objectClass.rawType); | |
| 136 expect( | |
| 137 hierarchy.getClassicLeastUpperBound( | |
| 138 a.rawType, coreTypes.objectClass.rawType), | |
| 139 coreTypes.objectClass.rawType); | |
| 140 expect( | |
| 141 hierarchy.getClassicLeastUpperBound( | |
| 142 coreTypes.objectClass.rawType, b.rawType), | |
| 143 coreTypes.objectClass.rawType); | |
| 144 expect( | |
| 145 hierarchy.getClassicLeastUpperBound(c.rawType, d.rawType), a.rawType); | |
| 146 expect( | |
| 147 hierarchy.getClassicLeastUpperBound(c.rawType, a.rawType), a.rawType); | |
| 148 expect( | |
| 149 hierarchy.getClassicLeastUpperBound(a.rawType, d.rawType), a.rawType); | |
| 150 expect( | |
| 151 hierarchy.getClassicLeastUpperBound(f.rawType, g.rawType), a.rawType); | |
| 152 expect( | |
| 153 hierarchy.getClassicLeastUpperBound(h.rawType, i.rawType), a.rawType); | |
| 154 }); | |
| 155 | |
| 156 test('least_upper_bound_non_generic', () { | |
| 157 var program = createMockSdkProgram(); | |
| 158 var coreTypes = new CoreTypes(program); | |
| 159 var defaultSuper = coreTypes.objectClass.asThisSupertype; | |
| 160 var library = makeTestLibrary(program); | |
| 161 var int = coreTypes.intClass.rawType; | |
| 162 var double = coreTypes.doubleClass.rawType; | |
| 163 var bool = coreTypes.boolClass.rawType; | |
| 164 | |
| 165 Class addClass(String name, List<String> typeParameterNames, | |
| 166 List<Supertype> implements_(List<DartType> typeParameterTypes)) { | |
| 167 var typeParameters = typeParameterNames | |
| 168 .map((name) => new TypeParameter(name, coreTypes.objectClass.rawType)) | |
| 169 .toList(); | |
| 170 var typeParameterTypes = typeParameters | |
| 171 .map((parameter) => new TypeParameterType(parameter)) | |
| 172 .toList(); | |
| 173 var c = new Class( | |
| 174 name: name, | |
| 175 typeParameters: typeParameters, | |
| 176 supertype: defaultSuper, | |
| 177 implementedTypes: implements_(typeParameterTypes)); | |
| 178 library.addClass(c); | |
| 179 return c; | |
| 180 } | |
| 181 | |
| 182 // Create the class hierarchy: | |
| 183 // | |
| 184 // Object | |
| 185 // | | |
| 186 // A | |
| 187 // / \ | |
| 188 // B<T> C<U> | |
| 189 // \ / | |
| 190 // D<T,U> | |
| 191 // / \ | |
| 192 // E F | |
| 193 // | |
| 194 // Where E implements D<int, double> and F implements D<int, bool>. | |
| 195 var a = addClass('A', [], (_) => []); | |
| 196 var b = addClass('B', ['T'], (_) => [a.asThisSupertype]); | |
| 197 var c = addClass('C', ['U'], (_) => [a.asThisSupertype]); | |
| 198 var d = addClass('D', ['T', 'U'], (typeParameterTypes) { | |
| 199 var t = typeParameterTypes[0]; | |
| 200 var u = typeParameterTypes[1]; | |
| 201 return [ | |
| 202 new Supertype(b, [t]), | |
| 203 new Supertype(c, [u]) | |
| 204 ]; | |
| 205 }); | |
| 206 var e = addClass( | |
| 207 'E', | |
| 208 [], | |
| 209 (_) => [ | |
| 210 new Supertype(d, [int, double]) | |
| 211 ]); | |
| 212 var f = addClass( | |
| 213 'F', | |
| 214 [], | |
| 215 (_) => [ | |
| 216 new Supertype(d, [int, bool]) | |
| 217 ]); | |
| 218 var hierarchy = new ClassHierarchy(program); | |
| 219 | |
| 220 expect( | |
| 221 hierarchy.getClassicLeastUpperBound(new InterfaceType(d, [int, double]), | |
| 222 new InterfaceType(d, [int, double])), | |
| 223 new InterfaceType(d, [int, double])); | |
| 224 expect( | |
| 225 hierarchy.getClassicLeastUpperBound(new InterfaceType(d, [int, double]), | |
| 226 new InterfaceType(d, [int, bool])), | |
| 227 new InterfaceType(b, [int])); | |
| 228 expect( | |
| 229 hierarchy.getClassicLeastUpperBound(new InterfaceType(d, [int, double]), | |
| 230 new InterfaceType(d, [bool, double])), | |
| 231 new InterfaceType(c, [double])); | |
| 232 expect( | |
| 233 hierarchy.getClassicLeastUpperBound(new InterfaceType(d, [int, double]), | |
| 234 new InterfaceType(d, [bool, int])), | |
| 235 a.rawType); | |
| 236 expect(hierarchy.getClassicLeastUpperBound(e.rawType, f.rawType), | |
| 237 new InterfaceType(b, [int])); | |
| 238 }); | |
| 239 } | |
| OLD | NEW |