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

Side by Side Diff: test/runtime/dart_runtime_test.dart

Issue 1043323002: Update dart_runtime.dart rules (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Fix comment Created 5 years, 8 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 unified diff | Download patch
« no previous file with comments | « lib/runtime/dart_runtime.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Test code 5 // Test code
6 import 'dart:collection'; 6 import 'dart:collection';
7 import 'dart:mirrors'; 7 import 'dart:mirrors';
8 import 'package:unittest/unittest.dart'; 8 import 'package:unittest/unittest.dart';
9 import 'package:dev_compiler/config.dart'; 9 import 'package:dev_compiler/config.dart';
10 import 'package:dev_compiler/runtime/dart_runtime.dart'; 10 import 'package:dev_compiler/runtime/dart_runtime.dart';
(...skipping 17 matching lines...) Expand all
28 T x; 28 T x;
29 U y; 29 U y;
30 } 30 }
31 31
32 class BB<T, U> extends AA<U, T> { 32 class BB<T, U> extends AA<U, T> {
33 T z; 33 T z;
34 } 34 }
35 35
36 class CC extends BB<String, List> {} 36 class CC extends BB<String, List> {}
37 37
38 typedef Func2(x, y);
38 typedef B Foo(B b, String s); 39 typedef B Foo(B b, String s);
39 40
40 A bar1(C c, String s) => null; 41 A bar1(C c, String s) => null;
41 bar2(B b, String s) => null; 42 bar2(B b, String s) => null;
42 B bar3(B b, Object o) => null; 43 B bar3(B b, Object o) => null;
43 B bar4(B b, o) => null; 44 B bar4(B b, o) => null;
44 C bar5(A a, Object o) => null; 45 C bar5(A a, Object o) => null;
45 B bar6(B b, String s, String o) => null; 46 B bar6(B b, String s, String o) => null;
46 B bar7(B b, String s, [Object o]) => null; 47 B bar7(B b, String s, [Object o]) => null;
47 B bar8(B b, String s, {Object p}) => null; 48 B bar8(B b, String s, {Object p}) => null;
48 49
49 class Bar { 50 class Bar {
50 B call(B b, String s) => null; 51 B call(B b, String s) => null;
51 } 52 }
52 53
53 class Baz { 54 class Baz {
54 Foo call = (B b, String s) => null; 55 Foo call = (B b, String s) => null;
55 } 56 }
56 57
58 class Checker<T> {
59 void isGround(bool expected) => expect(isGroundType(T), equals(expected));
60 void isGroundList(bool expected) => expect(isGroundType(new List<T>().runtimeT ype), equals(expected));
61 void check(x, bool expected) => checkType(x, T, expected);
62 void checkList(x, bool expected) => checkType(x, type((List<T> _) {}), expecte d);
63 }
64
57 bool dartIs(expr, Type type) { 65 bool dartIs(expr, Type type) {
58 var exprMirror = reflectType(expr.runtimeType); 66 var exprMirror = reflectType(expr.runtimeType);
59 var typeMirror = reflectType(type); 67 var typeMirror = reflectType(type);
60 return exprMirror.isSubtypeOf(typeMirror); 68 return exprMirror.isSubtypeOf(typeMirror);
61 } 69 }
62 70
63 void checkType(x, Type type, [bool expectedTrue = true]) { 71 void checkType(x, Type type, [bool expectedTrue = true]) {
64 var isGround = isGroundType(type); 72 var isGround = isGroundType(type);
65 var restrictedSubType = instanceOf(x, type); 73 var restrictedSubType = instanceOf(x, type);
66 var dartSubType = dartIs(x, type); 74 var dartSubType = dartIs(x, type);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 test('Map', () { 139 test('Map', () {
132 final m1 = new Map<String, String>(); 140 final m1 = new Map<String, String>();
133 final m2 = new Map<Object, Object>(); 141 final m2 = new Map<Object, Object>();
134 final m3 = new Map(); 142 final m3 = new Map();
135 final m4 = new HashMap<dynamic, dynamic>(); 143 final m4 = new HashMap<dynamic, dynamic>();
136 final m5 = new LinkedHashMap(); 144 final m5 = new LinkedHashMap();
137 145
138 expect(isGroundType(Map), isTrue); 146 expect(isGroundType(Map), isTrue);
139 expect(isGroundType(m1.runtimeType), isFalse); 147 expect(isGroundType(m1.runtimeType), isFalse);
140 expect(isGroundType(type((Map<String, String> _) {})), isFalse); 148 expect(isGroundType(type((Map<String, String> _) {})), isFalse);
141 expect(isGroundType(m2.runtimeType), isFalse); 149 expect(isGroundType(m2.runtimeType), isTrue);
142 expect(isGroundType(type((Map<Object, Object> _) {})), isFalse); 150 expect(isGroundType(type((Map<Object, Object> _) {})), isTrue);
143 expect(isGroundType(m3.runtimeType), isTrue); 151 expect(isGroundType(m3.runtimeType), isTrue);
144 expect(isGroundType(type((Map _) {})), isTrue); 152 expect(isGroundType(type((Map _) {})), isTrue);
145 expect(isGroundType(m4.runtimeType), isTrue); 153 expect(isGroundType(m4.runtimeType), isTrue);
146 expect(isGroundType(type((HashMap<dynamic, dynamic> _) {})), isTrue); 154 expect(isGroundType(type((HashMap<dynamic, dynamic> _) {})), isTrue);
147 expect(isGroundType(m5.runtimeType), isTrue); 155 expect(isGroundType(m5.runtimeType), isTrue);
148 expect(isGroundType(type((LinkedHashMap _) {})), isTrue); 156 expect(isGroundType(type((LinkedHashMap _) {})), isTrue);
149 expect(isGroundType(LinkedHashMap), isTrue); 157 expect(isGroundType(LinkedHashMap), isTrue);
150 158
151 // Map<T1,T2> <: Map 159 // Map<T1,T2> <: Map
152 checkType(m1, Map); 160 checkType(m1, Map);
153 checkType(m1, Object); 161 checkType(m1, Object);
154 162
155 // Instance of self 163 // Instance of self
156 checkType(m1, m1.runtimeType); 164 checkType(m1, m1.runtimeType);
157 checkType(m1, type((Map<String, String> _) {})); 165 checkType(m1, type((Map<String, String> _) {}));
158 166
167 // Object == dynamic == top as a type parameter
168 checkType(m2, m3.runtimeType);
169 checkType(m2, Map);
170 checkType(m3, m2.runtimeType);
171 checkType(m3, type((Map<Object, Object> _) {}));
172
159 // Covariance on generics 173 // Covariance on generics
160 checkType(m1, m2.runtimeType); 174 checkType(m1, m2.runtimeType);
161 checkType(m1, type((Map<Object, Object> _) {})); 175 checkType(m1, type((Map<Object, Object> _) {}));
162 176
163 // No contravariance on generics. 177 // No contravariance on generics.
164 checkType(m2, m1.runtimeType, false); 178 checkType(m2, m1.runtimeType, false);
165 checkType(m2, type((Map<String, String> _) {}), false); 179 checkType(m2, type((Map<String, String> _) {}), false);
166 180
167 // null is! Map 181 // null is! Map
168 checkType(null, Map, false); 182 checkType(null, Map, false);
169 183
170 // Raw generic types 184 // Raw generic types
171 checkType(m5, Map); 185 checkType(m5, Map);
186 checkType(m5, type((Map<Object, Object> _) {}));
172 checkType(m4, Map); 187 checkType(m4, Map);
188 checkType(m4, type((Map<Object, Object> _) {}));
189
190 // Mixin: the actual implementation class should implement MapMixin
191 checkType(m1, MapMixin);
192 checkType(m1, type((MapMixin<String, String> _) {}));
193 checkType(m1, type((MapMixin<Object, Object> _) {}));
194 checkType(m5, MapMixin);
195 checkType(m5, type((MapMixin<String, String> _) {}), false);
196 checkType(m5, type((MapMixin<Object, Object> _) {}));
173 }); 197 });
174 198
175 test('generic and inheritance', () { 199 test('generic and inheritance', () {
176 AA aaraw = new AA(); 200 AA aaraw = new AA();
177 final aarawtype = aaraw.runtimeType; 201 final aarawtype = aaraw.runtimeType;
178 AA<dynamic, dynamic> aadynamic = new AA<dynamic, dynamic>(); 202 AA<dynamic, dynamic> aadynamic = new AA<dynamic, dynamic>();
179 final aadynamictype = aadynamic.runtimeType; 203 final aadynamictype = aadynamic.runtimeType;
180 AA<String, List> aa = new AA<String, List>(); 204 AA<String, List> aa = new AA<String, List>();
181 final aatype = aa.runtimeType; 205 final aatype = aa.runtimeType;
182 BB<String, List> bb = new BB<String, List>(); 206 BB<String, List> bb = new BB<String, List>();
(...skipping 29 matching lines...) Expand all
212 checkType(aaraw, type((AA<String> _) {})); 236 checkType(aaraw, type((AA<String> _) {}));
213 checkType(aaraw, aadynamictype); 237 checkType(aaraw, aadynamictype);
214 checkType(aaraw, type((AA<dynamic, dynamic> _) {})); 238 checkType(aaraw, type((AA<dynamic, dynamic> _) {}));
215 checkType(aadynamic, aarawtype); 239 checkType(aadynamic, aarawtype);
216 checkType(aadynamic, AA); 240 checkType(aadynamic, AA);
217 }); 241 });
218 242
219 test('Functions', () { 243 test('Functions', () {
220 // - return type: Dart is bivariant. We're covariant. 244 // - return type: Dart is bivariant. We're covariant.
221 // - param types: Dart is bivariant. We're contravariant. 245 // - param types: Dart is bivariant. We're contravariant.
246 expect(isGroundType(Func2), isTrue);
222 expect(isGroundType(Foo), isFalse); 247 expect(isGroundType(Foo), isFalse);
223 expect(isGroundType(type((B _(B _1, String _2)) {})), isFalse); 248 expect(isGroundType(type((B _(B _1, String _2)) {})), isFalse);
224 checkType(bar1, Foo, false); 249 checkType(bar1, Foo, false);
225 checkType(bar1, type((B _(B _1, String _2)) {}), false); 250 checkType(bar1, type((B _(B _1, String _2)) {}), false);
226 checkType(bar2, Foo, false); 251 checkType(bar2, Foo, false);
227 checkType(bar2, type((B _(B _1, String _2)) {}), false); 252 checkType(bar2, type((B _(B _1, String _2)) {}), false);
228 checkType(bar3, Foo); 253 checkType(bar3, Foo);
229 checkType(bar3, type((B _(B _1, String _2)) {})); 254 checkType(bar3, type((B _(B _1, String _2)) {}));
230 checkType(bar4, Foo); 255 checkType(bar4, Foo, false);
231 checkType(bar4, type((B _(B _1, String _2)) {})); 256 // TODO(vsm): Revisit. bar4 is (B, *) -> B. Perhaps it should be treated a s top for a reified object.
257 checkType(bar4, type((B _(B _1, String _2)) {}), false);
232 checkType(bar5, Foo); 258 checkType(bar5, Foo);
233 checkType(bar5, type((B _(B _1, String _2)) {})); 259 checkType(bar5, type((B _(B _1, String _2)) {}));
234 checkType(bar6, Foo, false); 260 checkType(bar6, Foo, false);
235 checkType(bar6, type((B _(B _1, String _2)) {}), false); 261 checkType(bar6, type((B _(B _1, String _2)) {}), false);
236 checkType(bar7, Foo); 262 checkType(bar7, Foo);
237 checkType(bar7, type((B _(B _1, String _2)) {})); 263 checkType(bar7, type((B _(B _1, String _2)) {}));
238 checkType(bar7, bar6.runtimeType); 264 checkType(bar7, bar6.runtimeType);
239 checkType(bar8, Foo); 265 checkType(bar8, Foo);
240 checkType(bar8, type((B _(B _1, String _2)) {})); 266 checkType(bar8, type((B _(B _1, String _2)) {}));
241 checkType(bar8, bar6.runtimeType, false); 267 checkType(bar8, bar6.runtimeType, false);
242 checkType(bar7, bar8.runtimeType, false); 268 checkType(bar7, bar8.runtimeType, false);
243 checkType(bar8, bar7.runtimeType, false); 269 checkType(bar8, bar7.runtimeType, false);
244 }); 270 });
245 271
246 test('void', () { 272 test('void', () {
247 checkType((x) => x, type((void _(x)) {})); 273 checkType((x) => x, type((void _(x)) {}));
248 }); 274 });
249 275
276 test('null', () {
277 checkType(null, Object);
278 checkType(null, Null);
279 checkType(null, dynamic);
280 checkType(null, int, false);
281 checkType(null, String, false);
282 checkType(null, Map, false);
283
284 expect(cast(null, Object), equals(null));
285 expect(cast(null, String), equals(null));
286 expect(cast(null, Map), equals(null));
287 });
288
250 test('Function objects', () { 289 test('Function objects', () {
251 // Bar has a call method - it emulates the corresponding function. 290 // Bar has a call method - it emulates the corresponding function.
252 var bar = new Bar(); 291 var bar = new Bar();
253 checkType(bar, Bar); 292 checkType(bar, Bar);
254 checkType(bar, Function); 293 checkType(bar, Function);
255 checkType(bar, Foo); 294 checkType(bar, Foo);
256 checkType(bar, type((B _(B _1, String _2)) {})); 295 checkType(bar, type((B _(B _1, String _2)) {}));
257 checkType(bar, type((B _(String _1, String _2)) {}), false); 296 checkType(bar, type((B _(String _1, String _2)) {}), false);
258 297
259 // Baz has a call getter that is a closure - this does not make it a 298 // Baz has a call getter that is a closure - this does not make it a
260 // function. 299 // function.
261 var baz = new Baz(); 300 var baz = new Baz();
262 checkType(baz, Function, false); 301 checkType(baz, Function, false);
263 checkType(baz, Foo, false); 302 checkType(baz, Foo, false);
264 }); 303 });
265 304
266 test('arity', () { 305 test('arity', () {
267 checkArity(bar1, 2, 2); 306 checkArity(bar1, 2, 2);
268 checkArity(bar4, 2, 2); 307 checkArity(bar4, 2, 2);
269 checkArity(bar6, 3, 3); 308 checkArity(bar6, 3, 3);
270 checkArity(bar7, 2, 3); 309 checkArity(bar7, 2, 3);
271 checkArity(bar8, 2, 2); 310 checkArity(bar8, 2, 2);
272 checkArity(() {}, 0, 0); 311 checkArity(() {}, 0, 0);
273 checkArity((a, [b]) {}, 1, 2); 312 checkArity((a, [b]) {}, 1, 2);
274 }); 313 });
314
315 test('type variable', () {
316 var stringChecker = new Checker<String>();
317 stringChecker.isGround(true);
318 stringChecker.isGroundList(false);
319
320 stringChecker.check(5, false);
321 stringChecker.check("hello", true);
322 stringChecker.check(null, false);
323
324 var objectChecker = new Checker<Object>();
325 objectChecker.isGround(true);
326 objectChecker.isGroundList(true);
327
328 objectChecker.check(5, true);
329 objectChecker.check("hello", true);
330 objectChecker.check(null, true);
331 });
275 } 332 }
OLDNEW
« no previous file with comments | « lib/runtime/dart_runtime.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698