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