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 var assert = chai.assert; | 5 define(['dart_sdk'], function(dart_sdk) { |
6 var dart_sdk = dart_library.import('dart_sdk'); | 6 const assert = chai.assert; |
7 var core = dart_sdk.core; | 7 const core = dart_sdk.core; |
8 var collection = dart_sdk.collection; | 8 const collection = dart_sdk.collection; |
9 var dart = dart_sdk.dart; | 9 const dart = dart_sdk.dart; |
10 var dartx = dart.dartx; | 10 const dartx = dart.dartx; |
11 | 11 |
12 suite('generic', () => { | 12 |
13 "use strict"; | 13 suite('generic', () => { |
14 | 14 "use strict"; |
15 let generic = dart.generic; | 15 |
16 | 16 let generic = dart.generic; |
17 test('zero arguments is not allowed', () => { | 17 |
18 assert.throws(() => { generic(function(){}); }); | 18 test('zero arguments is not allowed', () => { |
| 19 assert.throws(() => { generic(function(){}); }); |
| 20 }); |
| 21 |
| 22 test('dcall noSuchMethod has correct error target', () => { |
| 23 assert.throws(() => dart.dcall(42), |
| 24 new RegExp('NoSuchMethodError.*\nReceiver: 42', 'm'), |
| 25 'Calls with non-function receiver should throw a NoSuchMethodError' + |
| 26 ' with correct target'); |
| 27 |
| 28 // TODO(jmesserly): we should show the name "print" in there somewhere. |
| 29 assert.throws(() => dart.dcall(core.print, 1, 2, 3), |
| 30 new RegExp('NoSuchMethodError.*\n' + |
| 31 "Receiver: Instance of '\\(Object\\) -> void'", 'm'), |
| 32 'Calls with incorrect argument types should throw a NoSuchMethodError'
+ |
| 33 ' with correct target'); |
| 34 }); |
| 35 |
| 36 test('can throw number', () => { |
| 37 try { |
| 38 dart.throw(42); |
| 39 } catch (e) { |
| 40 assert.equal(e, 42); |
| 41 } |
| 42 }); |
| 43 |
| 44 test('argument count cannot change', () => { |
| 45 let SomeType = generic(function(x) { return {x: x}; }); |
| 46 assert.throws(() => { SomeType(1,2) }); |
| 47 let obj = {}; |
| 48 assert.equal(SomeType(obj).x, obj); |
| 49 assert.equal(SomeType(obj).x, obj); |
| 50 assert.equal(SomeType().x, dart.dynamic); |
| 51 }); |
| 52 |
| 53 test('undefined/null are not allowed', () => { |
| 54 let SomeType = generic(function(x) {}); |
| 55 assert.throws(() => { SomeType(void 0) }); |
| 56 SomeType(1); |
| 57 assert.throws(() => { SomeType(void 0) }); |
| 58 SomeType(1); |
| 59 assert.throws(() => { SomeType(null) }); |
| 60 }); |
| 61 |
| 62 test('result is memoized', () => { |
| 63 let t1 = Object.create(null); |
| 64 let t2 = Object.create(null); |
| 65 |
| 66 let count = 0; |
| 67 let SomeType = generic(function(x, y) { |
| 68 count++; |
| 69 return Object.create(null); |
| 70 }); |
| 71 |
| 72 let x12 = SomeType(1, 2); |
| 73 assert.strictEqual(SomeType(1, 2), x12); |
| 74 assert.strictEqual(SomeType(1, 2), x12); |
| 75 assert.strictEqual(count, 1); |
| 76 let x11 = SomeType(1, 1); |
| 77 assert.strictEqual(count, 2); |
| 78 assert.strictEqual(SomeType(1, 1), x11); |
| 79 assert.strictEqual(count, 2); |
| 80 count = 0; |
| 81 |
| 82 let t1t2 = SomeType(t1, t2); |
| 83 assert.strictEqual(count, 1); |
| 84 let t2t1 = SomeType(t2, t1); |
| 85 assert.strictEqual(count, 2); |
| 86 assert.notStrictEqual(t1t2, t2t1); |
| 87 assert.strictEqual(SomeType(t1, t2), t1t2); |
| 88 assert.strictEqual(SomeType(t2, t1), t2t1); |
| 89 assert.strictEqual(SomeType(t1, t2), t1t2); |
| 90 count = 0; |
| 91 |
| 92 // Nothing has been stored on the object |
| 93 assert.strictEqual(Object.keys(t1).length, 0); |
| 94 assert.strictEqual(Object.keys(t2).length, 0); |
| 95 }); |
| 96 |
| 97 test('type constructor is reflectable', () => { |
| 98 let SomeType = generic(function(x, y) { return Object.create(null); }); |
| 99 let someValue = SomeType('hi', 123); |
| 100 assert.equal(dart.getGenericClass(someValue), SomeType); |
| 101 assert.deepEqual(dart.getGenericArgs(someValue), ['hi', 123]); |
| 102 }); |
| 103 |
| 104 test('proper type constructor is called', () => { |
| 105 // This tests https://github.com/dart-lang/dev_compiler/issues/178 |
| 106 let l = dart.list([1, 2, 3], core.int); |
| 107 let s = l[dartx.join](); |
| 108 assert.equal(s, '123'); |
| 109 }); |
19 }); | 110 }); |
20 | 111 |
21 test('dcall noSuchMethod has correct error target', () => { | 112 |
22 assert.throws(() => dart.dcall(42), | 113 suite('instanceOf', () => { |
23 new RegExp('NoSuchMethodError.*\nReceiver: 42', 'm'), | 114 "use strict"; |
24 'Calls with non-function receiver should throw a NoSuchMethodError' + | 115 |
25 ' with correct target'); | 116 let expect = assert.equal; |
26 | 117 let isGroundType = dart.isGroundType; |
27 // TODO(jmesserly): we should show the name "print" in there somewhere. | 118 let generic = dart.generic; |
28 assert.throws(() => dart.dcall(core.print, 1, 2, 3), | 119 let intIsNonNullable = false; |
29 new RegExp('NoSuchMethodError.*\n' + | 120 let cast = dart.as; |
30 "Receiver: Instance of '\\(Object\\) -> void'", 'm'), | 121 let instanceOf = dart.is; |
31 'Calls with incorrect argument types should throw a NoSuchMethodError' + | 122 let strongInstanceOf = dart.strongInstanceOf; |
32 ' with correct target'); | 123 let getReifiedType = dart.getReifiedType; |
| 124 let functionType = dart.functionType; |
| 125 let typedef = dart.typedef; |
| 126 let isSubtype = dart.isSubtype; |
| 127 |
| 128 let Object = core.Object; |
| 129 let String = core.String; |
| 130 let dynamic = dart.dynamic; |
| 131 let List = core.List; |
| 132 let Map = core.Map; |
| 133 let Map$ = core.Map$; |
| 134 let int = core.int; |
| 135 let num = core.num; |
| 136 let bool = core.bool; |
| 137 |
| 138 class A {} |
| 139 class B extends A {} |
| 140 class C extends B {} |
| 141 |
| 142 let AA$ = generic((T, U) => class AA extends core.Object {}); |
| 143 let AA = AA$(); |
| 144 let BB$ = generic((T, U) => class BB extends AA$(U, T) {}); |
| 145 let BB = BB$(); |
| 146 class CC extends BB$(String, List) {} |
| 147 |
| 148 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])
); |
| 149 let Foo = typedef('Foo', () => functionType(B, [B, String])); |
| 150 |
| 151 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U]
))) |
| 152 let FuncG = FuncG$(); |
| 153 |
| 154 // TODO(vsm): Revisit when we encode types on functions properly. |
| 155 // A bar1(C c, String s) => null; |
| 156 function bar1(c, s) { return null; } |
| 157 dart.fn(bar1, dart.definiteFunctionType(A, [C, String])); |
| 158 |
| 159 // bar2(B b, String s) => null; |
| 160 function bar2(b, s) { return null; } |
| 161 dart.fn(bar2, dart.definiteFunctionType(dynamic, [B, String])); |
| 162 |
| 163 // B bar3(B b, Object o) => null; |
| 164 function bar3(b, o) { return null; } |
| 165 dart.fn(bar3, dart.definiteFunctionType(B, [B, Object])); |
| 166 |
| 167 // B bar4(B b, o) => null; |
| 168 function bar4(b, o) { return null; } |
| 169 dart.fn(bar4, dart.definiteFunctionType(B, [B, dynamic])); |
| 170 |
| 171 // C bar5(A a, Object o) => null; |
| 172 function bar5(a, o) { return null; } |
| 173 dart.fn(bar5, dart.definiteFunctionType(C, [A, Object])); |
| 174 |
| 175 // B bar6(B b, String s, String o) => null; |
| 176 function bar6(b, s, o) { return null; } |
| 177 dart.fn(bar6, dart.definiteFunctionType(B, [B, String, String])); |
| 178 |
| 179 // B bar7(B b, String s, [Object o]) => null; |
| 180 function bar7(b, s, o) { return null; } |
| 181 dart.fn(bar7, dart.definiteFunctionType(B, [B, String], [Object])); |
| 182 |
| 183 // B bar8(B b, String s, {Object p}) => null; |
| 184 function bar8(b, s, o) { return null; } |
| 185 dart.fn(bar8, dart.definiteFunctionType(B, [B, String], {p: Object})); |
| 186 |
| 187 let cls1 = dart.fn((c, s) => { return null; }, |
| 188 dart.definiteFunctionType(A, [C, String])); |
| 189 |
| 190 let cls2 = dart.fn((b, s) => { return null; }, |
| 191 dart.definiteFunctionType(dynamic, [B, String])); |
| 192 |
| 193 let cls3 = dart.fn((b, o) => { return null; }, |
| 194 dart.definiteFunctionType(B, [B, Object])); |
| 195 |
| 196 let cls4 = dart.fn((b, o) => { return null; }, |
| 197 dart.definiteFunctionType(B, [B, dynamic])); |
| 198 |
| 199 let cls5 = dart.fn((a, o) => { return null; }, |
| 200 dart.definiteFunctionType(C, [A, Object])); |
| 201 |
| 202 let cls6 = dart.fn((b, s, o) => { return null; }, |
| 203 dart.definiteFunctionType(B, [B, String, String])); |
| 204 |
| 205 let cls7 = dart.fn((b, s, o) => { return null; }, |
| 206 dart.definiteFunctionType(B, [B, String], [Object])); |
| 207 |
| 208 let cls8 = |
| 209 dart.fn((b, s, o) => { return null; }, |
| 210 dart.definiteFunctionType(B, [B, String], {p: Object})); |
| 211 |
| 212 function checkType(x, type, expectedTrue, strongOnly) { |
| 213 if (expectedTrue === undefined) expectedTrue = true; |
| 214 if (strongOnly == undefined) strongOnly = false; |
| 215 if (!strongOnly) { |
| 216 assert.doesNotThrow(() => instanceOf(x, type)); |
| 217 expect(instanceOf(x, type), expectedTrue); |
| 218 } else { |
| 219 assert.throws(() => instanceOf(x, type), dart.StrongModeError); |
| 220 expect(expectedTrue, false); |
| 221 expect(strongInstanceOf(x, type), null); |
| 222 } |
| 223 } |
| 224 |
| 225 test('int', () => { |
| 226 expect(isGroundType(int), true); |
| 227 expect(isGroundType(getReifiedType(5)), true); |
| 228 |
| 229 checkType(5, int); |
| 230 checkType(5, dynamic); |
| 231 checkType(5, Object); |
| 232 checkType(5, num); |
| 233 |
| 234 checkType(5, bool, false); |
| 235 checkType(5, String, false); |
| 236 |
| 237 expect(cast(5, int), 5); |
| 238 if (intIsNonNullable) { |
| 239 expect(() => cast(null, int), throws); |
| 240 } else { |
| 241 expect(cast(null, int), null); |
| 242 } |
| 243 }); |
| 244 |
| 245 test('dynamic', () => { |
| 246 expect(isGroundType(dynamic), true); |
| 247 checkType(new Object(), dynamic); |
| 248 checkType(null, dynamic); |
| 249 |
| 250 expect(cast(null, dynamic), null); |
| 251 }); |
| 252 |
| 253 test('Object', () => { |
| 254 expect(isGroundType(Object), true); |
| 255 checkType(new Object(), dynamic); |
| 256 checkType(null, Object); |
| 257 |
| 258 expect(cast(null, Object), null); |
| 259 }); |
| 260 |
| 261 test('null', () => { |
| 262 // Object, dynamic cases are already handled above. |
| 263 checkType(null, core.Null); |
| 264 checkType(null, core.String, false); |
| 265 checkType(null, core.int, false); |
| 266 checkType(null, Map, false); |
| 267 checkType(void 0, core.Null); |
| 268 checkType(void 0, core.Object); |
| 269 checkType(void 0, dart.dynamic); |
| 270 }); |
| 271 |
| 272 test('String', () => { |
| 273 expect(isGroundType(String), true); |
| 274 expect(isGroundType(getReifiedType("foo")), true); |
| 275 checkType("foo", String); |
| 276 checkType("foo", Object); |
| 277 checkType("foo", dynamic); |
| 278 |
| 279 expect(cast(null, String), null); |
| 280 }); |
| 281 |
| 282 test('Map', () => { |
| 283 let m1 = new (Map$(String, String))(); |
| 284 let m2 = new (Map$(Object, Object))(); |
| 285 let m3 = new Map(); |
| 286 let m4 = new (collection.HashMap$(dart.dynamic, dart.dynamic))(); |
| 287 let m5 = new collection.LinkedHashMap(); |
| 288 let m6 = new (Map$(String, dart.dynamic))(); |
| 289 |
| 290 |
| 291 expect(isGroundType(Map), true); |
| 292 expect(isGroundType(getReifiedType(m1)), false); |
| 293 expect(isGroundType(Map$(String, String)), false); |
| 294 expect(isGroundType(getReifiedType(m2)), true); |
| 295 expect(isGroundType(Map$(Object, Object)), true); |
| 296 expect(isGroundType(getReifiedType(m3)), true); |
| 297 expect(isGroundType(Map), true); |
| 298 expect(isGroundType(getReifiedType(m4)), true); |
| 299 expect(isGroundType(collection.HashMap$(dynamic, dynamic)), true); |
| 300 expect(isGroundType(getReifiedType(m5)), true); |
| 301 expect(isGroundType(collection.LinkedHashMap), true); |
| 302 expect(isGroundType(collection.LinkedHashMap), true); |
| 303 |
| 304 // Map<T1,T2> <: Map |
| 305 checkType(m1, Map); |
| 306 checkType(m1, Object); |
| 307 |
| 308 // Instance of self |
| 309 checkType(m1, getReifiedType(m1)); |
| 310 checkType(m1, Map$(String, String)); |
| 311 |
| 312 // Covariance on generics |
| 313 checkType(m1, getReifiedType(m2)); |
| 314 checkType(m1, Map$(Object, Object)); |
| 315 |
| 316 // No contravariance on generics. |
| 317 checkType(m2, getReifiedType(m1), false); |
| 318 checkType(m2, Map$(String, String), false); |
| 319 |
| 320 // null is! Map |
| 321 checkType(null, Map, false); |
| 322 |
| 323 // Raw generic types |
| 324 checkType(m5, Map); |
| 325 checkType(m4, Map); |
| 326 |
| 327 // Is checks |
| 328 assert.throws(() => dart.is(m3, Map$(String, String)), |
| 329 dart.StrongModeError); |
| 330 assert.throws(() => dart.is(m6, Map$(String, String)), |
| 331 dart.StrongModeError); |
| 332 assert.isTrue(dart.is(m1, Map$(String, String))); |
| 333 assert.isFalse(dart.is(m2, Map$(String, String))); |
| 334 |
| 335 // As checks |
| 336 // TODO(vsm): Enable these. We're currently only logging warnings on |
| 337 // StrongModeErrors. |
| 338 // assert.throws(() => dart.as(m3, Map$(String, String)), |
| 339 // dart.StrongModeError); |
| 340 // assert.throws(() => dart.as(m6, Map$(String, String)), |
| 341 // dart.StrongModeError); |
| 342 assert.equal(dart.as(m1, Map$(String, String)), m1); |
| 343 // assert.throws(() => dart.as(m2, Map$(String, String)), |
| 344 // dart.StrongModeError); |
| 345 }); |
| 346 |
| 347 test('constructors', () => { |
| 348 class C extends core.Object { |
| 349 C(x) {}; |
| 350 named(x, y) {}; |
| 351 } |
| 352 dart.defineNamedConstructor(C, 'named'); |
| 353 dart.setSignature(C, { |
| 354 constructors: () => ({ |
| 355 C: dart.definiteFunctionType(C, [core.int]), |
| 356 named: dart.definiteFunctionType(C, [core.int, core.int]) |
| 357 }) |
| 358 }); |
| 359 let getType = dart.classGetConstructorType; |
| 360 isSubtype(getType(C), dart.functionType(C, [core.int])); |
| 361 isSubtype(getType(C), dart.functionType(C, [core.String]), false); |
| 362 isSubtype(getType(C, 'C'), dart.functionType(C, [core.int])); |
| 363 isSubtype(getType(C, 'C'), dart.functionType(C, [core.String]), false); |
| 364 isSubtype(getType(C, 'named'), dart.functionType(C, [core.int, core.int]))
; |
| 365 isSubtype(getType(C, 'named'), |
| 366 dart.functionType(C, [core.int, core.String]), false); |
| 367 }); |
| 368 |
| 369 test('generic and inheritance', () => { |
| 370 let aaraw = new AA(); |
| 371 let aarawtype = getReifiedType(aaraw); |
| 372 let aadynamic = new (AA$(dynamic, dynamic))(); |
| 373 let aadynamictype = getReifiedType(aadynamic); |
| 374 let aa = new (AA$(String, List))(); |
| 375 let aatype = getReifiedType(aa); |
| 376 let bb = new (BB$(String, List))(); |
| 377 let bbtype = getReifiedType(bb); |
| 378 let cc = new CC(); |
| 379 let cctype = getReifiedType(cc); |
| 380 // We don't allow constructing bad types. |
| 381 // This was AA<String> in Dart (wrong number of type args). |
| 382 let aabad = new (AA$(dart.dynamic, dart.dynamic))(); |
| 383 let aabadtype = getReifiedType(aabad); |
| 384 |
| 385 expect(isGroundType(aatype), false); |
| 386 expect(isGroundType(AA$(String, List)), false); |
| 387 expect(isGroundType(bbtype), false); |
| 388 expect(isGroundType(BB$(String, List)), false); |
| 389 expect(isGroundType(cctype), true); |
| 390 expect(isGroundType(CC), true); |
| 391 checkType(cc, aatype, false); |
| 392 checkType(cc, AA$(String, List), false); |
| 393 checkType(cc, bbtype); |
| 394 checkType(cc, BB$(String, List)); |
| 395 checkType(aa, cctype, false); |
| 396 checkType(aa, CC, false); |
| 397 checkType(aa, bbtype, false); |
| 398 checkType(aa, BB$(String, List), false); |
| 399 checkType(bb, cctype, false); |
| 400 checkType(bb, CC, false); |
| 401 checkType(aa, aabadtype); |
| 402 checkType(aa, dynamic); |
| 403 checkType(aabad, aatype, false, true); |
| 404 checkType(aabad, AA$(String, List), false, true); |
| 405 checkType(aabad, aarawtype); |
| 406 checkType(aabad, AA); |
| 407 checkType(aaraw, aabadtype); |
| 408 checkType(aaraw, AA$(dart.dynamic, dart.dynamic)); |
| 409 checkType(aaraw, aadynamictype); |
| 410 checkType(aaraw, AA$(dynamic, dynamic)); |
| 411 checkType(aadynamic, aarawtype); |
| 412 checkType(aadynamic, AA); |
| 413 }); |
| 414 |
| 415 test('void', () => { |
| 416 //checkType((x) => x, type((void _(x)) {})); |
| 417 }); |
| 418 |
| 419 test('mixins', () => { |
| 420 let c = collection; |
| 421 var s1 = new (c.SplayTreeSet$(String))(); |
| 422 |
| 423 checkType(s1, c.IterableMixin); |
| 424 checkType(s1, c.IterableMixin$(String)); |
| 425 checkType(s1, c.IterableMixin$(int), false); |
| 426 |
| 427 checkType(s1, c.SetMixin); |
| 428 checkType(s1, c.SetMixin$(String)); |
| 429 checkType(s1, c.SetMixin$(int), false); |
| 430 }); |
| 431 |
| 432 test('Type', () => { |
| 433 checkType(int, core.Type, true); |
| 434 checkType(num, core.Type, true); |
| 435 checkType(bool, core.Type, true); |
| 436 checkType(String, core.Type, true); |
| 437 checkType(dynamic, core.Type, true); |
| 438 checkType(Object, core.Type, true); |
| 439 checkType(List, core.Type, true); |
| 440 checkType(Map, core.Type, true); |
| 441 checkType(Map$(int, String), core.Type, true); |
| 442 checkType(Func2, core.Type, true); |
| 443 checkType(functionType(dynamic, [dynamic]), core.Type, true); |
| 444 checkType(core.Type, core.Type, true); |
| 445 |
| 446 checkType(3, core.Type, false); |
| 447 checkType("hello", core.Type, false); |
| 448 }) |
| 449 |
| 450 test('Functions', () => { |
| 451 // - return type: Dart is bivariant. We're covariant. |
| 452 // - param types: Dart is bivariant. We're contravariant. |
| 453 expect(isGroundType(Func2), true); |
| 454 expect(isGroundType(Foo), false); |
| 455 expect(isGroundType(functionType(B, [B, String])), false); |
| 456 checkType(bar1, Foo, false, true); |
| 457 checkType(cls1, Foo, false, true); |
| 458 checkType(bar1, functionType(B, [B, String]), false, true); |
| 459 checkType(cls1, functionType(B, [B, String]), false, true); |
| 460 checkType(bar2, Foo, false, true); |
| 461 checkType(cls2, Foo, false, true); |
| 462 checkType(bar2, functionType(B, [B, String]), false, true); |
| 463 checkType(cls2, functionType(B, [B, String]), false, true); |
| 464 checkType(bar3, Foo); |
| 465 checkType(cls3, Foo); |
| 466 checkType(bar3, functionType(B, [B, String])); |
| 467 checkType(cls3, functionType(B, [B, String])); |
| 468 checkType(bar4, Foo, true); |
| 469 checkType(cls4, Foo, true); |
| 470 checkType(bar4, functionType(B, [B, String]), true); |
| 471 checkType(cls4, functionType(B, [B, String]), true); |
| 472 checkType(bar5, Foo); |
| 473 checkType(cls5, Foo); |
| 474 checkType(bar5, functionType(B, [B, String])); |
| 475 checkType(cls5, functionType(B, [B, String])); |
| 476 checkType(bar6, Foo, false); |
| 477 checkType(cls6, Foo, false); |
| 478 checkType(bar6, functionType(B, [B, String]), false); |
| 479 checkType(cls6, functionType(B, [B, String]), false); |
| 480 checkType(bar7, Foo); |
| 481 checkType(cls7, Foo); |
| 482 checkType(bar7, functionType(B, [B, String])); |
| 483 checkType(cls7, functionType(B, [B, String])); |
| 484 checkType(bar7, getReifiedType(bar6)); |
| 485 checkType(cls7, getReifiedType(bar6)); |
| 486 checkType(bar8, Foo); |
| 487 checkType(cls8, Foo); |
| 488 checkType(bar8, functionType(B, [B, String])); |
| 489 checkType(cls8, functionType(B, [B, String])); |
| 490 checkType(bar8, getReifiedType(bar6), false); |
| 491 checkType(cls8, getReifiedType(bar6), false); |
| 492 checkType(bar7, getReifiedType(bar8), false); |
| 493 checkType(cls7, getReifiedType(bar8), false); |
| 494 checkType(bar8, getReifiedType(bar7), false); |
| 495 checkType(cls8, getReifiedType(bar7), false); |
| 496 |
| 497 // Parameterized typedefs |
| 498 expect(isGroundType(FuncG), true); |
| 499 expect(isGroundType(FuncG$(B, String)), false); |
| 500 checkType(bar1, FuncG$(B, String), false, true); |
| 501 checkType(cls1, FuncG$(B, String), false, true); |
| 502 checkType(bar3, FuncG$(B, String)); |
| 503 checkType(cls3, FuncG$(B, String)); |
| 504 }); |
| 505 |
| 506 test('dcall', () => { |
| 507 function dd2d(x, y) {return x}; |
| 508 dart.fn(dd2d); |
| 509 function ii2i(x, y) {return x}; |
| 510 dart.fn(ii2i, dart.definiteFunctionType(core.int, [core.int, core.int])); |
| 511 function ii_2i(x, y) {return x}; |
| 512 dart.fn(ii_2i, dart.definiteFunctionType(core.int, [core.int], [core.int])
); |
| 513 function i_i2i(x, opts) {return x}; |
| 514 dart.fn(i_i2i, |
| 515 dart.definiteFunctionType(core.int, [core.int], {extra: core.int})
); |
| 516 |
| 517 assert.equal(dart.dcall(dd2d, 0, 1), 0); |
| 518 assert.equal(dart.dcall(dd2d, "hello", "world"), "hello"); |
| 519 assert.throws(() => dart.dcall(dd2d, 0)); |
| 520 assert.throws(() => dart.dcall(dd2d, 0, 1, 2)); |
| 521 assert.throws(() => dart.dcall(dd2d, 0, 1, {extra : 3})); |
| 522 // This should throw but currently doesn't. |
| 523 // assert.throws(() => dart.dcall(dd2d, 0, {extra:3})); |
| 524 |
| 525 assert.equal(dart.dcall(ii2i, 0, 1), 0); |
| 526 assert.throws(() => dart.dcall(ii2i, "hello", "world")); |
| 527 assert.throws(() => dart.dcall(ii2i, 0)); |
| 528 assert.throws(() => dart.dcall(ii2i, 0, 1, 2)); |
| 529 |
| 530 assert.equal(dart.dcall(ii_2i, 0, 1), 0); |
| 531 assert.throws(() => dart.dcall(ii_2i, "hello", "world")); |
| 532 assert.equal(dart.dcall(ii_2i, 0), 0); |
| 533 assert.throws(() => dart.dcall(ii_2i, 0, 1, 2)); |
| 534 |
| 535 assert.throws(() => dart.dcall(i_i2i, 0, 1)); |
| 536 assert.throws(() => dart.dcall(i_i2i, "hello", "world")); |
| 537 assert.equal(dart.dcall(i_i2i, 0), 0); |
| 538 assert.throws(() => dart.dcall(i_i2i, 0, 1, 2)); |
| 539 assert.equal(dart.dcall(i_i2i, 0, {extra: 3}), 0); |
| 540 }); |
| 541 |
| 542 test('dsend', () => { |
| 543 class Tester extends core.Object { |
| 544 new() { |
| 545 this.f = dart.fn(x => x, |
| 546 dart.definiteFunctionType(core.int, [core.int])); |
| 547 this.me = this; |
| 548 } |
| 549 m(x, y) {return x;} |
| 550 call(x) {return x;} |
| 551 static s(x, y) { return x;} |
| 552 } |
| 553 dart.setSignature(Tester, { |
| 554 methods: () => ({ |
| 555 m: dart.definiteFunctionType(core.int, [core.int, core.int]), |
| 556 call: dart.definiteFunctionType(core.int, [core.int]) |
| 557 }), |
| 558 statics: () => ({ |
| 559 s: dart.definiteFunctionType(core.String, [core.String]) |
| 560 }), |
| 561 names: ['s'] |
| 562 }) |
| 563 let o = new Tester(); |
| 564 |
| 565 // Method send |
| 566 assert.equal(dart.dsend(o, 'm', 3, 4), 3); |
| 567 assert.equal(dart.dsend(o, 'm', null, 4), null); |
| 568 assert.throws(() => dart.dsend(o, 'm', 3)); |
| 569 assert.throws(() => dart.dsend(o, 'm', "hello", "world")); |
| 570 assert.throws(() => dart.dsend(o, 'q', 3)); |
| 571 |
| 572 // Method send through a field |
| 573 assert.equal(dart.dsend(o, 'f', 3), 3); |
| 574 assert.equal(dart.dsend(o, 'f', null), null); |
| 575 assert.throws(() => dart.dsend(o, 'f', "hello")); |
| 576 assert.throws(() => dart.dsend(o, 'f', 3, 4)); |
| 577 |
| 578 // Static method call |
| 579 assert.equal(dart.dcall(Tester.s, "hello"), "hello"); |
| 580 assert.equal(dart.dcall(Tester.s, null), null); |
| 581 assert.throws(() => dart.dcall(Tester.s, "hello", "world")); |
| 582 assert.throws(() => dart.dcall(Tester.s, 0, 1)); |
| 583 |
| 584 // Calling an object with a call method |
| 585 assert.equal(dart.dcall(o, 3), 3); |
| 586 assert.equal(dart.dcall(o, null), null); |
| 587 assert.throws(() => dart.dcall(o, "hello")); |
| 588 assert.throws(() => dart.dcall(o, 3, 4)); |
| 589 |
| 590 // Calling through a field containing an object with a call method |
| 591 assert.equal(dart.dsend(o, 'me', 3), 3); |
| 592 assert.equal(dart.dsend(o, 'me', null), null); |
| 593 assert.throws(() => dart.dsend(o, 'me', "hello")); |
| 594 assert.throws(() => dart.dsend(o, 'me', 3, 4)); |
| 595 }); |
| 596 |
| 597 test('Types on top level functions', () => { |
| 598 // Test some generated code |
| 599 // Test the lazy path |
| 600 checkType(core.identityHashCode, |
| 601 dart.functionType(core.int, [core.Object])); |
| 602 // Test the normal path |
| 603 checkType(core.identical, |
| 604 dart.functionType(core.bool, |
| 605 [core.Object, core.Object])); |
| 606 |
| 607 // Hand crafted tests |
| 608 // All dynamic |
| 609 function dd2d(x, y) {return x}; |
| 610 dart.fn(dd2d); |
| 611 checkType(dd2d, dart.functionType(dart.dynamic, |
| 612 [dart.dynamic, dart.dynamic])); |
| 613 |
| 614 // Set the type eagerly |
| 615 function ii2i(x, y) {return x}; |
| 616 dart.fn(ii2i, dart.definiteFunctionType(core.int, [core.int, core.int])); |
| 617 checkType(ii2i, dart.functionType(core.int, |
| 618 [core.int, core.int])); |
| 619 |
| 620 // Set the type lazily |
| 621 function ss2s(x, y) {return x}; |
| 622 var coreString; |
| 623 dart.lazyFn(ss2s, |
| 624 () => dart.definiteFunctionType(coreString, |
| 625 [coreString, coreString])); |
| 626 coreString = core.String; |
| 627 checkType(ss2s, dart.functionType(core.String, |
| 628 [core.String, core.String])); |
| 629 |
| 630 // Optional types |
| 631 function ii_2i(x, y) {return x}; |
| 632 dart.fn(ii_2i, dart.definiteFunctionType(core.int, [core.int], [core.int])
); |
| 633 checkType(ii_2i, dart.functionType(core.int, [core.int], |
| 634 [core.int])); |
| 635 checkType(ii_2i, dart.functionType(core.int, [core.int, |
| 636 core.int])); |
| 637 checkType(ii_2i, dart.functionType(core.int, [], [core.int, |
| 638 core.int]), |
| 639 false); |
| 640 checkType(ii_2i, dart.functionType(core.int, [core.int], |
| 641 {extra: core.int}), false); |
| 642 |
| 643 // Named types |
| 644 function i_i2i(x, opts) {return x}; |
| 645 dart.fn(i_i2i, dart.definiteFunctionType(core.int, [core.int], |
| 646 {extra: core.int})); |
| 647 checkType(i_i2i, dart.functionType(core.int, [core.int], |
| 648 {extra: core.int})); |
| 649 checkType(i_i2i, dart.functionType(core.int, |
| 650 [core.int, core.int]), false); |
| 651 checkType(i_i2i, dart.functionType(core.int, [core.int], {})); |
| 652 checkType(i_i2i, |
| 653 dart.functionType(core.int, [], {extra: core.int, |
| 654 also: core.int}), false); |
| 655 checkType(i_i2i, |
| 656 dart.functionType(core.int, [core.int], [core.int]), false); |
| 657 }); |
| 658 |
| 659 test('Method tearoffs', () => { |
| 660 let c = collection; |
| 661 // Tear off of an inherited method |
| 662 let map = new (Map$(core.int, core.String))(); |
| 663 checkType(dart.bind(map, 'toString'), |
| 664 dart.functionType(String, [])); |
| 665 checkType(dart.bind(map, 'toString'), |
| 666 dart.functionType(int, []), false, true); |
| 667 |
| 668 // Tear off of a method directly on the object |
| 669 let smap = new (c.SplayTreeMap$(core.int, core.String))(); |
| 670 checkType(dart.bind(smap, 'forEach'), |
| 671 dart.functionType(dart.void, |
| 672 [dart.functionType(dart.void, [core.int, core.String
])])); |
| 673 checkType(dart.bind(smap, 'forEach'), |
| 674 dart.functionType(dart.void, |
| 675 [dart.functionType(dart.void, |
| 676 [core.String, core.String])]), false, true); |
| 677 |
| 678 // Tear off of a mixed in method |
| 679 let mapB = new (c.MapBase$(core.int, core.int))(); |
| 680 checkType(dart.bind(mapB, 'forEach'), |
| 681 dart.functionType(dart.void, [ |
| 682 dart.functionType(dart.void, [core.int, core.int])])); |
| 683 checkType(dart.bind(mapB, 'forEach'), |
| 684 dart.functionType(dart.void, [ |
| 685 dart.functionType(dart.void, [core.int, core.String])]), |
| 686 false, true); |
| 687 |
| 688 // Tear off of a method with a symbol name |
| 689 let listB = new (c.ListBase$(core.int))(); |
| 690 checkType(dart.bind(listB, dartx.add), |
| 691 dart.functionType(dart.void, [core.int])); |
| 692 checkType(dart.bind(listB, dartx.add), |
| 693 dart.functionType(dart.void, [core.String]), false, true); |
| 694 |
| 695 // Tear off of a static method |
| 696 checkType(c.ListBase.listToString, |
| 697 dart.functionType(core.String, [core.List])); |
| 698 checkType(c.ListBase.listToString, |
| 699 dart.functionType(core.String, [core.String]), false, true); |
| 700 |
| 701 // Tear-off of extension methods on primitives |
| 702 checkType(dart.bind(3.0, dartx.floor), |
| 703 dart.functionType(core.int, [])); |
| 704 checkType(dart.bind(3.0, dartx.floor), |
| 705 dart.functionType(core.String, []), false, true); |
| 706 checkType(dart.bind("", dartx.endsWith), |
| 707 dart.functionType(core.bool, [core.String])); |
| 708 checkType(dart.bind("", dartx.endsWith), |
| 709 dart.functionType(core.bool, [core.int]), false, true); |
| 710 |
| 711 // Tear off a mixin method |
| 712 class Base { |
| 713 m(x) {return x;} |
| 714 }; |
| 715 dart.setSignature(Base, { |
| 716 methods: () => ({ |
| 717 m: dart.definiteFunctionType(core.int, [core.int]), |
| 718 }) |
| 719 }); |
| 720 |
| 721 class M1 { |
| 722 m(x) {return x;} |
| 723 }; |
| 724 dart.setSignature(M1, { |
| 725 methods: () => ({ |
| 726 m: dart.definiteFunctionType(core.num, [core.int]), |
| 727 }) |
| 728 }); |
| 729 |
| 730 class M2 { |
| 731 m(x) {return x;} |
| 732 }; |
| 733 dart.setSignature(M2, { |
| 734 methods: () => ({ |
| 735 m: dart.definiteFunctionType(core.Object, [core.int]), |
| 736 }) |
| 737 }); |
| 738 |
| 739 class O extends dart.mixin(Base, M1, M2) { |
| 740 O() {}; |
| 741 }; |
| 742 dart.setSignature(O, {}); |
| 743 var obj = new O(); |
| 744 var m = dart.bind(obj, 'm'); |
| 745 checkType(m, dart.functionType(core.Object, [core.int])); |
| 746 checkType(m, dart.functionType(core.int, [core.int]), false, true); |
| 747 |
| 748 // Test inherited signatures |
| 749 class P extends O { |
| 750 P() {}; |
| 751 m(x) {return x;}; |
| 752 }; |
| 753 dart.setSignature(P, {}); |
| 754 var obj = new P(); |
| 755 var m = dart.bind(obj, 'm'); |
| 756 checkType(m, dart.functionType(core.Object, [core.int])); |
| 757 checkType(m, dart.functionType(core.int, [core.int]), false, true); |
| 758 }); |
| 759 |
| 760 test('Object members', () => { |
| 761 let nullHash = dart.hashCode(null); |
| 762 assert.equal(nullHash, 0); |
| 763 let nullString = dart.toString(null); |
| 764 assert.equal(nullString, 'null'); |
| 765 |
| 766 let map = new Map(); |
| 767 let mapHash = dart.hashCode(map); |
| 768 checkType(mapHash, core.int); |
| 769 assert.equal(mapHash, map.hashCode); |
| 770 |
| 771 let mapString = dart.toString(map); |
| 772 assert.equal(mapString, map.toString()); |
| 773 checkType(mapString, core.String); |
| 774 |
| 775 let str = "A string"; |
| 776 let strHash = dart.hashCode(str); |
| 777 checkType(strHash, core.int); |
| 778 |
| 779 let strString = dart.toString(str); |
| 780 checkType(strString, core.String); |
| 781 assert.equal(str, strString); |
| 782 |
| 783 let n = 42; |
| 784 let intHash = dart.hashCode(n); |
| 785 checkType(intHash, core.int); |
| 786 |
| 787 let intString = dart.toString(n); |
| 788 assert.equal(intString, '42'); |
| 789 }); |
33 }); | 790 }); |
34 | 791 |
35 test('can throw number', () => { | 792 suite('subtyping', function() { |
36 try { | 793 'use strict'; |
37 dart.throw(42); | 794 |
38 } catch (e) { | 795 let functionType = dart.functionType; |
39 assert.equal(e, 42); | 796 let definiteFunctionType = dart.definiteFunctionType; |
| 797 let typedef = dart.typedef; |
| 798 let isSubtype = dart.isSubtype; |
| 799 let int = core.int; |
| 800 let num = core.num; |
| 801 let dyn = dart.dynamic; |
| 802 |
| 803 function always(t1, t2) { |
| 804 assert.equal(isSubtype(t1, t2), true); |
40 } | 805 } |
| 806 function never(t1, t2) { |
| 807 assert.equal(isSubtype(t1, t2), false); |
| 808 } |
| 809 function maybe(t1, t2) { |
| 810 assert.equal(isSubtype(t1, t2), null); |
| 811 } |
| 812 |
| 813 function always2(t1, t2) { |
| 814 assert.equal(isSubtype(t1, t2), true); |
| 815 always(functionType(t1, [t2]), functionType(t2, [t1])); |
| 816 } |
| 817 function never2(t1, t2) { |
| 818 assert.equal(isSubtype(t1, t2), false); |
| 819 maybe(functionType(t1, [t2]), functionType(t2, [t1])); |
| 820 } |
| 821 function maybe2(t1, t2) { |
| 822 assert.equal(isSubtype(t1, t2), null); |
| 823 maybe(functionType(t1, [t2]), functionType(t2, [t1])); |
| 824 } |
| 825 |
| 826 function run_test(func1, func2, func2opt, func1extra, func2extra) { |
| 827 always2(func2(int, int), func2(int, int)); |
| 828 always2(func2(int, num), func2(int, int)); |
| 829 always2(func2(int, int), func2(num, int)); |
| 830 |
| 831 always2(func2opt(int, int), func2opt(int, int)); |
| 832 always2(func2opt(int, num), func2opt(int, int)); |
| 833 always2(func2opt(int, int), func2opt(num, int)); |
| 834 |
| 835 always2(func2opt(int, int), func2(int, int)); |
| 836 always2(func2opt(int, num), func2(int, int)); |
| 837 always2(func2opt(int, int), func2(num, int)); |
| 838 |
| 839 always2(func2opt(int, int), func1(int)); |
| 840 always2(func2opt(int, num), func1(int)); |
| 841 always2(func2opt(int, int), func1(num)); |
| 842 |
| 843 always2(func2extra(int, int), func2(int, int)); |
| 844 always2(func2extra(int, num), func2(int, int)); |
| 845 always2(func2extra(int, int), func2(num, int)); |
| 846 |
| 847 maybe2(func2(int, int), func2(int, num)); |
| 848 maybe2(func2(num, int), func2(int, int)); |
| 849 |
| 850 maybe2(func2opt(num, num), func1(int)); |
| 851 |
| 852 maybe2(func2opt(int, int), func2opt(int, num)); |
| 853 maybe2(func2opt(num, int), func2opt(int, int)); |
| 854 |
| 855 maybe2(func2opt(int, int), func2(int, num)); |
| 856 maybe2(func2opt(num, int), func2(int, int)); |
| 857 |
| 858 maybe2(func2extra(int, int), func2(int, num)); |
| 859 maybe2(func2extra(num, int), func2(int, int)); |
| 860 |
| 861 never2(func1(int), func2(int, num)); |
| 862 never2(func1(num), func2(int, int)); |
| 863 never2(func1(num), func2(num, num)); |
| 864 |
| 865 never2(func2(int, int), func1(int)); |
| 866 never2(func2(num, int), func1(int)); |
| 867 never2(func2(num, num), func1(num)); |
| 868 |
| 869 never2(func1(int), func2opt(int, num)); |
| 870 never2(func1(num), func2opt(int, int)); |
| 871 never2(func1(num), func2opt(num, num)); |
| 872 |
| 873 never2(func2(int, int), func2opt(int, num)); |
| 874 never2(func2(num, int), func2opt(int, int)); |
| 875 never2(func2(num, num), func2opt(num, num)); |
| 876 |
| 877 never2(func1extra(int), func2(int, num)); |
| 878 never2(func1extra(num), func2(int, int)); |
| 879 never2(func1extra(num), func2(num, num)); |
| 880 |
| 881 never2(func1extra(int), func2opt(int, num)); |
| 882 never2(func1extra(num), func2opt(int, int)); |
| 883 never2(func1extra(num), func2opt(num, num)); |
| 884 |
| 885 never2(func1(int), func1extra(int)); |
| 886 never2(func1(num), func1extra(int)); |
| 887 never2(func1(num), func1extra(num)); |
| 888 |
| 889 never2(func2(int, int), func1extra(int)); |
| 890 never2(func2(num, int), func1extra(int)); |
| 891 never2(func2(num, num), func1extra(num)); |
| 892 |
| 893 never2(func2(int, int), func2extra(int, int)); |
| 894 never2(func2(num, int), func2extra(int, int)); |
| 895 never2(func2(num, num), func2extra(num, num)); |
| 896 }; |
| 897 |
| 898 test('basic function types', () => { |
| 899 function func1(S) { |
| 900 return functionType(S, []); |
| 901 } |
| 902 |
| 903 function func2(S, T) { |
| 904 return functionType(S, [T]); |
| 905 } |
| 906 |
| 907 function func2opt(S, T) { |
| 908 return functionType(S, [], [T]); |
| 909 } |
| 910 |
| 911 function func1extra(S) { |
| 912 return functionType(S, [], {extra: int}); |
| 913 } |
| 914 |
| 915 function func2extra(S, T) { |
| 916 return functionType(S, [T], {extra: int}); |
| 917 } |
| 918 |
| 919 run_test(func1, func2, func2opt, func1extra, func2extra); |
| 920 }); |
| 921 |
| 922 test('basic typedefs', () => { |
| 923 function func1(S) { |
| 924 return dart.typedef('Func1', () => functionType(S, [])) |
| 925 } |
| 926 |
| 927 function func2(S, T) { |
| 928 return dart.typedef('Func2', () => functionType(S, [T])) |
| 929 } |
| 930 |
| 931 function func2opt(S, T) { |
| 932 return dart.typedef('Func2', () => functionType(S, [], [T])) |
| 933 } |
| 934 |
| 935 function func1extra(S) { |
| 936 return dart.typedef('Func1', () => functionType(S, [], {extra: int})) |
| 937 } |
| 938 |
| 939 function func2extra(S, T) { |
| 940 return dart.typedef('Func2', () => functionType(S, [T], {extra: int})) |
| 941 } |
| 942 |
| 943 run_test(func1, func2, func2opt, func1extra, func2extra); |
| 944 }); |
| 945 |
| 946 test('basic generic typedefs', () => { |
| 947 let func1 = dart.generic( |
| 948 (S) => dart.typedef('Func1', () => functionType(S, []))); |
| 949 |
| 950 let func2 = dart.generic( |
| 951 (S, T) => dart.typedef('Func2', () => functionType(S, [T]))); |
| 952 |
| 953 let func2opt = dart.generic( |
| 954 (S, T) => dart.typedef('Func2', () => functionType(S, [], [T]))); |
| 955 |
| 956 let func1extra = dart.generic( |
| 957 (S) => dart.typedef('Func1', () => functionType(S, [], {extra: int}))); |
| 958 |
| 959 let func2extra = dart.generic( |
| 960 (S, T) => dart.typedef('Func2', |
| 961 () => functionType(S, [T], {extra: int}))); |
| 962 |
| 963 run_test(func1, func2, func2opt, func1extra, func2extra); |
| 964 }); |
| 965 |
| 966 test('fuzzy function types', () => { |
| 967 always(functionType(int, [int]), functionType(dyn, [dyn])); |
| 968 always(functionType(int, [], [int]), functionType(dyn, [], [dyn])); |
| 969 always(functionType(int, [], [int]), functionType(dyn, [dyn])); |
| 970 always(functionType(int, [], [int]), functionType(dyn, [])); |
| 971 always(functionType(int, [int], {extra: int}), functionType(dyn, [dyn])); |
| 972 |
| 973 always(functionType(dyn, [dyn]), functionType(dyn, [dyn])); |
| 974 always(functionType(dyn, [], [dyn]), functionType(dyn, [], [dyn])); |
| 975 always(functionType(dyn, [], [dyn]), functionType(dyn, [dyn])); |
| 976 always(functionType(dyn, [], [dyn]), functionType(dyn, [])); |
| 977 always(functionType(dyn, [dyn], {extra: dyn}), functionType(dyn, [dyn])); |
| 978 |
| 979 }); |
| 980 |
| 981 test('void function types', () => { |
| 982 always(functionType(int, [int]), functionType(dart.void, [dyn])); |
| 983 always(functionType(int, [], [int]), functionType(dart.void, [], [dyn])); |
| 984 always(functionType(int, [], [int]), functionType(dart.void, [dyn])); |
| 985 always(functionType(int, [], [int]), functionType(dart.void, [])); |
| 986 always(functionType(int, [int], {extra: int}), functionType(dart.void, [dy
n])); |
| 987 |
| 988 always(functionType(dart.void, [int]), functionType(dart.void, [dyn])); |
| 989 always(functionType(dart.void, [], [int]), functionType(dart.void, [], [dy
n])); |
| 990 always(functionType(dart.void, [], [int]), functionType(dart.void, [dyn]))
; |
| 991 always(functionType(dart.void, [], [int]), functionType(dart.void, [])); |
| 992 always(functionType(dart.void, [int], {extra: int}), functionType(dart.voi
d, [dyn])); |
| 993 |
| 994 always(functionType(dyn, [dyn]), functionType(dart.void, [dyn])); |
| 995 always(functionType(dyn, [], [dyn]), functionType(dart.void, [], [dyn])); |
| 996 always(functionType(dyn, [], [dyn]), functionType(dart.void, [dyn])); |
| 997 always(functionType(dyn, [], [dyn]), functionType(dart.void, [])); |
| 998 always(functionType(dyn, [dyn], {extra: dyn}), functionType(dart.void, [dy
n])); |
| 999 |
| 1000 always(functionType(dart.void, [dyn]), functionType(dart.void, [dyn])); |
| 1001 always(functionType(dart.void, [], [dyn]), functionType(dart.void, [], [dy
n])); |
| 1002 always(functionType(dart.void, [], [dyn]), functionType(dart.void, [dyn]))
; |
| 1003 always(functionType(dart.void, [], [dyn]), functionType(dart.void, [])); |
| 1004 always(functionType(dart.void, [dyn], {extra: dyn}), functionType(dart.voi
d, [dyn])); |
| 1005 |
| 1006 always(functionType(dart.void, [int]), functionType(dyn, [dyn])); |
| 1007 always(functionType(dart.void, [], [int]), functionType(dyn, [], [dyn])); |
| 1008 always(functionType(dart.void, [], [int]), functionType(dyn, [dyn])); |
| 1009 always(functionType(dart.void, [], [int]), functionType(dyn, [])); |
| 1010 always(functionType(dart.void, [int], {extra: int}), functionType(dyn, [dy
n])); |
| 1011 |
| 1012 never(functionType(dart.void, [int]), functionType(int, [dyn])); |
| 1013 never(functionType(dart.void, [], [int]), functionType(int, [], [dyn])); |
| 1014 never(functionType(dart.void, [], [int]), functionType(int, [dyn])); |
| 1015 never(functionType(dart.void, [], [int]), functionType(int, [])); |
| 1016 never(functionType(dart.void, [int], {extra: int}), functionType(int, [dyn
])); |
| 1017 |
| 1018 never(functionType(dart.void, [int]), functionType(int, [int])); |
| 1019 never(functionType(dart.void, [], [int]), functionType(int, [], [int])); |
| 1020 never(functionType(dart.void, [], [int]), functionType(int, [int])); |
| 1021 never(functionType(dart.void, [], [int]), functionType(int, [])); |
| 1022 never(functionType(dart.void, [int], {extra: int}), functionType(int, [int
])); |
| 1023 |
| 1024 }); |
| 1025 |
| 1026 test('higher-order typedef', () => { |
| 1027 let Func$ = dart.generic((S, T) => |
| 1028 dart.typedef('Func', () => |
| 1029 functionType(T, [S]))); |
| 1030 let Func2$ = dart.generic((R, S, T) => |
| 1031 dart.typedef('Func2', () => |
| 1032 functionType(T, [Func$(R, S)]))); |
| 1033 |
| 1034 maybe(functionType(int, [functionType(int, [num])]), |
| 1035 functionType(num, [functionType(int, [int])])); |
| 1036 maybe(functionType(int, [Func$(num, int)]), |
| 1037 functionType(num, [Func$(int, int)])); |
| 1038 maybe(Func2$(num, int, int), Func2$(int, int, num)); |
| 1039 }); |
| 1040 |
| 1041 test('mixed types', () => { |
| 1042 let AA$ = dart.generic((T) => class AA extends core.Object {}); |
| 1043 |
| 1044 always(int, dyn); |
| 1045 maybe(dyn, int); |
| 1046 |
| 1047 never(functionType(int, [int]), int); |
| 1048 |
| 1049 never(int, functionType(int, [int])); |
| 1050 |
| 1051 always(AA$(int), AA$(dyn)); |
| 1052 maybe(AA$(dyn), AA$(int)); |
| 1053 never(AA$(core.Object), AA$(int)); |
| 1054 |
| 1055 always(AA$(functionType(int, [int])), AA$(dyn)); |
| 1056 maybe(AA$(dyn), AA$(functionType(int, [int]))); |
| 1057 never(AA$(core.Object), AA$(functionType(int, [int]))); |
| 1058 |
| 1059 always(AA$(functionType(int, [int])), AA$(functionType(dyn, [dyn]))); |
| 1060 maybe(AA$(functionType(dyn, [dyn])), AA$(functionType(int, [int]))); |
| 1061 maybe(AA$(functionType(core.Object, [core.Object])), |
| 1062 AA$(functionType(int, [int]))); |
| 1063 |
| 1064 |
| 1065 }); |
| 1066 |
41 }); | 1067 }); |
42 | 1068 |
43 test('argument count cannot change', () => { | 1069 suite('canonicalization', function() { |
44 let SomeType = generic(function(x) { return {x: x}; }); | 1070 'use strict'; |
45 assert.throws(() => { SomeType(1,2) }); | 1071 let functionType = dart.functionType; |
46 let obj = {}; | 1072 let definiteFunctionType = dart.definiteFunctionType; |
47 assert.equal(SomeType(obj).x, obj); | 1073 let typedef = dart.typedef; |
48 assert.equal(SomeType(obj).x, obj); | 1074 let generic = dart.generic; |
49 assert.equal(SomeType().x, dart.dynamic); | 1075 |
| 1076 let Object = core.Object; |
| 1077 let String = core.String; |
| 1078 let int = core.int; |
| 1079 let dynamic = dart.dynamic; |
| 1080 let bottom = dart.bottom; |
| 1081 let Map = core.Map; |
| 1082 let Map$ = core.Map$; |
| 1083 |
| 1084 class A {} |
| 1085 |
| 1086 let AA$ = generic((T, U) => class AA extends core.Object {}); |
| 1087 let AA = AA$(); |
| 1088 |
| 1089 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])
); |
| 1090 |
| 1091 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U]
))) |
| 1092 let FuncG = FuncG$(); |
| 1093 |
| 1094 test('base types', () => { |
| 1095 assert.equal(Object, Object); |
| 1096 assert.equal(String, String); |
| 1097 assert.equal(dynamic, dynamic); |
| 1098 }); |
| 1099 |
| 1100 test('class types', () => { |
| 1101 assert.equal(A, A); |
| 1102 }); |
| 1103 |
| 1104 test('generic class types', () => { |
| 1105 assert.equal(AA, AA); |
| 1106 assert.equal(AA, AA$(dynamic, dynamic)); |
| 1107 assert.equal(AA$(dynamic, dynamic), AA$(dynamic, dynamic)); |
| 1108 assert.equal(AA$(AA, Object), AA$(AA, Object)); |
| 1109 assert.equal(Map, Map); |
| 1110 assert.equal(Map$(dynamic, dynamic), Map); |
| 1111 assert.equal(Map$(int, Map$(int, int)), Map$(int, Map$(int, int))); |
| 1112 }); |
| 1113 |
| 1114 test('typedefs', () => { |
| 1115 assert.equal(Func2, Func2); |
| 1116 assert.equal(FuncG, FuncG$(dynamic, dynamic)); |
| 1117 assert.equal(FuncG$(dynamic, dynamic), FuncG$(dynamic, dynamic)); |
| 1118 assert.equal(FuncG$(String, Func2), FuncG$(String, Func2)); |
| 1119 }); |
| 1120 |
| 1121 test('function types', () => { |
| 1122 assert.equal(functionType(dynamic, [dynamic, dynamic]), |
| 1123 functionType(dynamic, [dynamic, dynamic])) |
| 1124 |
| 1125 assert.notEqual(definiteFunctionType(dynamic, [dynamic, dynamic]), |
| 1126 functionType(dynamic, [dynamic, dynamic])) |
| 1127 |
| 1128 assert.equal(functionType(dynamic, [dynamic, dynamic]), |
| 1129 functionType(dynamic, [bottom, bottom])) |
| 1130 |
| 1131 assert.equal(functionType(dynamic, [], [dynamic, dynamic]), |
| 1132 functionType(dynamic, [], [dynamic, dynamic])) |
| 1133 |
| 1134 assert.notEqual(definiteFunctionType(dynamic, [], [dynamic, dynamic]), |
| 1135 functionType(dynamic, [], [dynamic, dynamic])) |
| 1136 |
| 1137 assert.equal(functionType(dynamic, [], [dynamic, dynamic]), |
| 1138 functionType(dynamic, [], [bottom, bottom])) |
| 1139 |
| 1140 assert.equal(functionType(dynamic, [], {extra: dynamic}), |
| 1141 functionType(dynamic, [], {extra: dynamic})) |
| 1142 |
| 1143 assert.notEqual(definiteFunctionType(dynamic, [], {extra: dynamic}), |
| 1144 functionType(dynamic, [], {extra: dynamic})) |
| 1145 |
| 1146 assert.equal(functionType(dynamic, [], {extra: dynamic}), |
| 1147 functionType(dynamic, [], {extra: bottom})) |
| 1148 |
| 1149 assert.equal(functionType(int, [int, int]), |
| 1150 functionType(int, [int, int])) |
| 1151 |
| 1152 assert.equal(functionType(int, [], [int, int]), |
| 1153 functionType(int, [], [int, int])) |
| 1154 |
| 1155 assert.equal(functionType(int, [int, int], {extra: int}), |
| 1156 functionType(int, [int, int], {extra: int})) |
| 1157 |
| 1158 assert.equal(functionType(int, [int, int, int, int, int]), |
| 1159 functionType(int, [int, int, int, int, int])) |
| 1160 |
| 1161 assert.notEqual(functionType(int, [int, int, int, int, int]), |
| 1162 functionType(int, [int, int, int], [int, int])) |
| 1163 |
| 1164 assert.notEqual(functionType(String, [int, int, int, int, int]), |
| 1165 functionType(int, [int, int, int, int, int])) |
| 1166 |
| 1167 assert.notEqual(functionType(String, []), |
| 1168 functionType(int, [])) |
| 1169 }); |
50 }); | 1170 }); |
51 | 1171 |
52 test('undefined/null are not allowed', () => { | 1172 suite('primitives', function() { |
53 let SomeType = generic(function(x) {}); | 1173 'use strict'; |
54 assert.throws(() => { SomeType(void 0) }); | 1174 |
55 SomeType(1); | 1175 test('fixed length list', () => { |
56 assert.throws(() => { SomeType(void 0) }); | 1176 let list = new core.List(10); |
57 SomeType(1); | 1177 list[0] = 42; |
58 assert.throws(() => { SomeType(null) }); | 1178 assert.throws(() => list.add(42)); |
59 }); | 1179 }); |
60 | 1180 |
61 test('result is memoized', () => { | 1181 test('toString on ES Symbol', () => { |
62 let t1 = Object.create(null); | 1182 let sym = Symbol('_foobar'); |
63 let t2 = Object.create(null); | 1183 assert.equal(dart.toString(sym), 'Symbol(_foobar)'); |
64 | 1184 }); |
65 let count = 0; | |
66 let SomeType = generic(function(x, y) { | |
67 count++; | |
68 return Object.create(null); | |
69 }); | |
70 | |
71 let x12 = SomeType(1, 2); | |
72 assert.strictEqual(SomeType(1, 2), x12); | |
73 assert.strictEqual(SomeType(1, 2), x12); | |
74 assert.strictEqual(count, 1); | |
75 let x11 = SomeType(1, 1); | |
76 assert.strictEqual(count, 2); | |
77 assert.strictEqual(SomeType(1, 1), x11); | |
78 assert.strictEqual(count, 2); | |
79 count = 0; | |
80 | |
81 let t1t2 = SomeType(t1, t2); | |
82 assert.strictEqual(count, 1); | |
83 let t2t1 = SomeType(t2, t1); | |
84 assert.strictEqual(count, 2); | |
85 assert.notStrictEqual(t1t2, t2t1); | |
86 assert.strictEqual(SomeType(t1, t2), t1t2); | |
87 assert.strictEqual(SomeType(t2, t1), t2t1); | |
88 assert.strictEqual(SomeType(t1, t2), t1t2); | |
89 count = 0; | |
90 | |
91 // Nothing has been stored on the object | |
92 assert.strictEqual(Object.keys(t1).length, 0); | |
93 assert.strictEqual(Object.keys(t2).length, 0); | |
94 }); | |
95 | |
96 test('type constructor is reflectable', () => { | |
97 let SomeType = generic(function(x, y) { return Object.create(null); }); | |
98 let someValue = SomeType('hi', 123); | |
99 assert.equal(dart.getGenericClass(someValue), SomeType); | |
100 assert.deepEqual(dart.getGenericArgs(someValue), ['hi', 123]); | |
101 }); | |
102 | |
103 test('proper type constructor is called', () => { | |
104 // This tests https://github.com/dart-lang/dev_compiler/issues/178 | |
105 let l = dart.list([1, 2, 3], core.int); | |
106 let s = l[dartx.join](); | |
107 assert.equal(s, '123'); | |
108 }); | 1185 }); |
109 }); | 1186 }); |
110 | |
111 | |
112 suite('instanceOf', () => { | |
113 "use strict"; | |
114 | |
115 let expect = assert.equal; | |
116 let isGroundType = dart.isGroundType; | |
117 let generic = dart.generic; | |
118 let intIsNonNullable = false; | |
119 let cast = dart.as; | |
120 let instanceOf = dart.is; | |
121 let strongInstanceOf = dart.strongInstanceOf; | |
122 let getReifiedType = dart.getReifiedType; | |
123 let functionType = dart.functionType; | |
124 let typedef = dart.typedef; | |
125 let isSubtype = dart.isSubtype; | |
126 | |
127 let Object = core.Object; | |
128 let String = core.String; | |
129 let dynamic = dart.dynamic; | |
130 let List = core.List; | |
131 let Map = core.Map; | |
132 let Map$ = core.Map$; | |
133 let int = core.int; | |
134 let num = core.num; | |
135 let bool = core.bool; | |
136 | |
137 class A {} | |
138 class B extends A {} | |
139 class C extends B {} | |
140 | |
141 let AA$ = generic((T, U) => class AA extends core.Object {}); | |
142 let AA = AA$(); | |
143 let BB$ = generic((T, U) => class BB extends AA$(U, T) {}); | |
144 let BB = BB$(); | |
145 class CC extends BB$(String, List) {} | |
146 | |
147 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])); | |
148 let Foo = typedef('Foo', () => functionType(B, [B, String])); | |
149 | |
150 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U]))
) | |
151 let FuncG = FuncG$(); | |
152 | |
153 // TODO(vsm): Revisit when we encode types on functions properly. | |
154 // A bar1(C c, String s) => null; | |
155 function bar1(c, s) { return null; } | |
156 dart.fn(bar1, dart.definiteFunctionType(A, [C, String])); | |
157 | |
158 // bar2(B b, String s) => null; | |
159 function bar2(b, s) { return null; } | |
160 dart.fn(bar2, dart.definiteFunctionType(dynamic, [B, String])); | |
161 | |
162 // B bar3(B b, Object o) => null; | |
163 function bar3(b, o) { return null; } | |
164 dart.fn(bar3, dart.definiteFunctionType(B, [B, Object])); | |
165 | |
166 // B bar4(B b, o) => null; | |
167 function bar4(b, o) { return null; } | |
168 dart.fn(bar4, dart.definiteFunctionType(B, [B, dynamic])); | |
169 | |
170 // C bar5(A a, Object o) => null; | |
171 function bar5(a, o) { return null; } | |
172 dart.fn(bar5, dart.definiteFunctionType(C, [A, Object])); | |
173 | |
174 // B bar6(B b, String s, String o) => null; | |
175 function bar6(b, s, o) { return null; } | |
176 dart.fn(bar6, dart.definiteFunctionType(B, [B, String, String])); | |
177 | |
178 // B bar7(B b, String s, [Object o]) => null; | |
179 function bar7(b, s, o) { return null; } | |
180 dart.fn(bar7, dart.definiteFunctionType(B, [B, String], [Object])); | |
181 | |
182 // B bar8(B b, String s, {Object p}) => null; | |
183 function bar8(b, s, o) { return null; } | |
184 dart.fn(bar8, dart.definiteFunctionType(B, [B, String], {p: Object})); | |
185 | |
186 let cls1 = dart.fn((c, s) => { return null; }, | |
187 dart.definiteFunctionType(A, [C, String])); | |
188 | |
189 let cls2 = dart.fn((b, s) => { return null; }, | |
190 dart.definiteFunctionType(dynamic, [B, String])); | |
191 | |
192 let cls3 = dart.fn((b, o) => { return null; }, | |
193 dart.definiteFunctionType(B, [B, Object])); | |
194 | |
195 let cls4 = dart.fn((b, o) => { return null; }, | |
196 dart.definiteFunctionType(B, [B, dynamic])); | |
197 | |
198 let cls5 = dart.fn((a, o) => { return null; }, | |
199 dart.definiteFunctionType(C, [A, Object])); | |
200 | |
201 let cls6 = dart.fn((b, s, o) => { return null; }, | |
202 dart.definiteFunctionType(B, [B, String, String])); | |
203 | |
204 let cls7 = dart.fn((b, s, o) => { return null; }, | |
205 dart.definiteFunctionType(B, [B, String], [Object])); | |
206 | |
207 let cls8 = | |
208 dart.fn((b, s, o) => { return null; }, | |
209 dart.definiteFunctionType(B, [B, String], {p: Object})); | |
210 | |
211 function checkType(x, type, expectedTrue, strongOnly) { | |
212 if (expectedTrue === undefined) expectedTrue = true; | |
213 if (strongOnly == undefined) strongOnly = false; | |
214 if (!strongOnly) { | |
215 assert.doesNotThrow(() => instanceOf(x, type)); | |
216 expect(instanceOf(x, type), expectedTrue); | |
217 } else { | |
218 assert.throws(() => instanceOf(x, type), dart.StrongModeError); | |
219 expect(expectedTrue, false); | |
220 expect(strongInstanceOf(x, type), null); | |
221 } | |
222 } | |
223 | |
224 test('int', () => { | |
225 expect(isGroundType(int), true); | |
226 expect(isGroundType(getReifiedType(5)), true); | |
227 | |
228 checkType(5, int); | |
229 checkType(5, dynamic); | |
230 checkType(5, Object); | |
231 checkType(5, num); | |
232 | |
233 checkType(5, bool, false); | |
234 checkType(5, String, false); | |
235 | |
236 expect(cast(5, int), 5); | |
237 if (intIsNonNullable) { | |
238 expect(() => cast(null, int), throws); | |
239 } else { | |
240 expect(cast(null, int), null); | |
241 } | |
242 }); | |
243 | |
244 test('dynamic', () => { | |
245 expect(isGroundType(dynamic), true); | |
246 checkType(new Object(), dynamic); | |
247 checkType(null, dynamic); | |
248 | |
249 expect(cast(null, dynamic), null); | |
250 }); | |
251 | |
252 test('Object', () => { | |
253 expect(isGroundType(Object), true); | |
254 checkType(new Object(), dynamic); | |
255 checkType(null, Object); | |
256 | |
257 expect(cast(null, Object), null); | |
258 }); | |
259 | |
260 test('null', () => { | |
261 // Object, dynamic cases are already handled above. | |
262 checkType(null, core.Null); | |
263 checkType(null, core.String, false); | |
264 checkType(null, core.int, false); | |
265 checkType(null, Map, false); | |
266 checkType(void 0, core.Null); | |
267 checkType(void 0, core.Object); | |
268 checkType(void 0, dart.dynamic); | |
269 }); | |
270 | |
271 test('String', () => { | |
272 expect(isGroundType(String), true); | |
273 expect(isGroundType(getReifiedType("foo")), true); | |
274 checkType("foo", String); | |
275 checkType("foo", Object); | |
276 checkType("foo", dynamic); | |
277 | |
278 expect(cast(null, String), null); | |
279 }); | |
280 | |
281 test('Map', () => { | |
282 let m1 = new (Map$(String, String))(); | |
283 let m2 = new (Map$(Object, Object))(); | |
284 let m3 = new Map(); | |
285 let m4 = new (collection.HashMap$(dart.dynamic, dart.dynamic))(); | |
286 let m5 = new collection.LinkedHashMap(); | |
287 let m6 = new (Map$(String, dart.dynamic))(); | |
288 | |
289 | |
290 expect(isGroundType(Map), true); | |
291 expect(isGroundType(getReifiedType(m1)), false); | |
292 expect(isGroundType(Map$(String, String)), false); | |
293 expect(isGroundType(getReifiedType(m2)), true); | |
294 expect(isGroundType(Map$(Object, Object)), true); | |
295 expect(isGroundType(getReifiedType(m3)), true); | |
296 expect(isGroundType(Map), true); | |
297 expect(isGroundType(getReifiedType(m4)), true); | |
298 expect(isGroundType(collection.HashMap$(dynamic, dynamic)), true); | |
299 expect(isGroundType(getReifiedType(m5)), true); | |
300 expect(isGroundType(collection.LinkedHashMap), true); | |
301 expect(isGroundType(collection.LinkedHashMap), true); | |
302 | |
303 // Map<T1,T2> <: Map | |
304 checkType(m1, Map); | |
305 checkType(m1, Object); | |
306 | |
307 // Instance of self | |
308 checkType(m1, getReifiedType(m1)); | |
309 checkType(m1, Map$(String, String)); | |
310 | |
311 // Covariance on generics | |
312 checkType(m1, getReifiedType(m2)); | |
313 checkType(m1, Map$(Object, Object)); | |
314 | |
315 // No contravariance on generics. | |
316 checkType(m2, getReifiedType(m1), false); | |
317 checkType(m2, Map$(String, String), false); | |
318 | |
319 // null is! Map | |
320 checkType(null, Map, false); | |
321 | |
322 // Raw generic types | |
323 checkType(m5, Map); | |
324 checkType(m4, Map); | |
325 | |
326 // Is checks | |
327 assert.throws(() => dart.is(m3, Map$(String, String)), | |
328 dart.StrongModeError); | |
329 assert.throws(() => dart.is(m6, Map$(String, String)), | |
330 dart.StrongModeError); | |
331 assert.isTrue(dart.is(m1, Map$(String, String))); | |
332 assert.isFalse(dart.is(m2, Map$(String, String))); | |
333 | |
334 // As checks | |
335 // TODO(vsm): Enable these. We're currently only logging warnings on | |
336 // StrongModeErrors. | |
337 // assert.throws(() => dart.as(m3, Map$(String, String)), | |
338 // dart.StrongModeError); | |
339 // assert.throws(() => dart.as(m6, Map$(String, String)), | |
340 // dart.StrongModeError); | |
341 assert.equal(dart.as(m1, Map$(String, String)), m1); | |
342 // assert.throws(() => dart.as(m2, Map$(String, String)), | |
343 // dart.StrongModeError); | |
344 }); | |
345 | |
346 test('constructors', () => { | |
347 class C extends core.Object { | |
348 C(x) {}; | |
349 named(x, y) {}; | |
350 } | |
351 dart.defineNamedConstructor(C, 'named'); | |
352 dart.setSignature(C, { | |
353 constructors: () => ({ | |
354 C: dart.definiteFunctionType(C, [core.int]), | |
355 named: dart.definiteFunctionType(C, [core.int, core.int]) | |
356 }) | |
357 }); | |
358 let getType = dart.classGetConstructorType; | |
359 isSubtype(getType(C), dart.functionType(C, [core.int])); | |
360 isSubtype(getType(C), dart.functionType(C, [core.String]), false); | |
361 isSubtype(getType(C, 'C'), dart.functionType(C, [core.int])); | |
362 isSubtype(getType(C, 'C'), dart.functionType(C, [core.String]), false); | |
363 isSubtype(getType(C, 'named'), dart.functionType(C, [core.int, core.int])); | |
364 isSubtype(getType(C, 'named'), | |
365 dart.functionType(C, [core.int, core.String]), false); | |
366 }); | |
367 | |
368 test('generic and inheritance', () => { | |
369 let aaraw = new AA(); | |
370 let aarawtype = getReifiedType(aaraw); | |
371 let aadynamic = new (AA$(dynamic, dynamic))(); | |
372 let aadynamictype = getReifiedType(aadynamic); | |
373 let aa = new (AA$(String, List))(); | |
374 let aatype = getReifiedType(aa); | |
375 let bb = new (BB$(String, List))(); | |
376 let bbtype = getReifiedType(bb); | |
377 let cc = new CC(); | |
378 let cctype = getReifiedType(cc); | |
379 // We don't allow constructing bad types. | |
380 // This was AA<String> in Dart (wrong number of type args). | |
381 let aabad = new (AA$(dart.dynamic, dart.dynamic))(); | |
382 let aabadtype = getReifiedType(aabad); | |
383 | |
384 expect(isGroundType(aatype), false); | |
385 expect(isGroundType(AA$(String, List)), false); | |
386 expect(isGroundType(bbtype), false); | |
387 expect(isGroundType(BB$(String, List)), false); | |
388 expect(isGroundType(cctype), true); | |
389 expect(isGroundType(CC), true); | |
390 checkType(cc, aatype, false); | |
391 checkType(cc, AA$(String, List), false); | |
392 checkType(cc, bbtype); | |
393 checkType(cc, BB$(String, List)); | |
394 checkType(aa, cctype, false); | |
395 checkType(aa, CC, false); | |
396 checkType(aa, bbtype, false); | |
397 checkType(aa, BB$(String, List), false); | |
398 checkType(bb, cctype, false); | |
399 checkType(bb, CC, false); | |
400 checkType(aa, aabadtype); | |
401 checkType(aa, dynamic); | |
402 checkType(aabad, aatype, false, true); | |
403 checkType(aabad, AA$(String, List), false, true); | |
404 checkType(aabad, aarawtype); | |
405 checkType(aabad, AA); | |
406 checkType(aaraw, aabadtype); | |
407 checkType(aaraw, AA$(dart.dynamic, dart.dynamic)); | |
408 checkType(aaraw, aadynamictype); | |
409 checkType(aaraw, AA$(dynamic, dynamic)); | |
410 checkType(aadynamic, aarawtype); | |
411 checkType(aadynamic, AA); | |
412 }); | |
413 | |
414 test('void', () => { | |
415 //checkType((x) => x, type((void _(x)) {})); | |
416 }); | |
417 | |
418 test('mixins', () => { | |
419 let c = collection; | |
420 var s1 = new (c.SplayTreeSet$(String))(); | |
421 | |
422 checkType(s1, c.IterableMixin); | |
423 checkType(s1, c.IterableMixin$(String)); | |
424 checkType(s1, c.IterableMixin$(int), false); | |
425 | |
426 checkType(s1, c.SetMixin); | |
427 checkType(s1, c.SetMixin$(String)); | |
428 checkType(s1, c.SetMixin$(int), false); | |
429 }); | |
430 | |
431 test('Type', () => { | |
432 checkType(int, core.Type, true); | |
433 checkType(num, core.Type, true); | |
434 checkType(bool, core.Type, true); | |
435 checkType(String, core.Type, true); | |
436 checkType(dynamic, core.Type, true); | |
437 checkType(Object, core.Type, true); | |
438 checkType(List, core.Type, true); | |
439 checkType(Map, core.Type, true); | |
440 checkType(Map$(int, String), core.Type, true); | |
441 checkType(Func2, core.Type, true); | |
442 checkType(functionType(dynamic, [dynamic]), core.Type, true); | |
443 checkType(core.Type, core.Type, true); | |
444 | |
445 checkType(3, core.Type, false); | |
446 checkType("hello", core.Type, false); | |
447 }) | |
448 | |
449 test('Functions', () => { | |
450 // - return type: Dart is bivariant. We're covariant. | |
451 // - param types: Dart is bivariant. We're contravariant. | |
452 expect(isGroundType(Func2), true); | |
453 expect(isGroundType(Foo), false); | |
454 expect(isGroundType(functionType(B, [B, String])), false); | |
455 checkType(bar1, Foo, false, true); | |
456 checkType(cls1, Foo, false, true); | |
457 checkType(bar1, functionType(B, [B, String]), false, true); | |
458 checkType(cls1, functionType(B, [B, String]), false, true); | |
459 checkType(bar2, Foo, false, true); | |
460 checkType(cls2, Foo, false, true); | |
461 checkType(bar2, functionType(B, [B, String]), false, true); | |
462 checkType(cls2, functionType(B, [B, String]), false, true); | |
463 checkType(bar3, Foo); | |
464 checkType(cls3, Foo); | |
465 checkType(bar3, functionType(B, [B, String])); | |
466 checkType(cls3, functionType(B, [B, String])); | |
467 checkType(bar4, Foo, true); | |
468 checkType(cls4, Foo, true); | |
469 checkType(bar4, functionType(B, [B, String]), true); | |
470 checkType(cls4, functionType(B, [B, String]), true); | |
471 checkType(bar5, Foo); | |
472 checkType(cls5, Foo); | |
473 checkType(bar5, functionType(B, [B, String])); | |
474 checkType(cls5, functionType(B, [B, String])); | |
475 checkType(bar6, Foo, false); | |
476 checkType(cls6, Foo, false); | |
477 checkType(bar6, functionType(B, [B, String]), false); | |
478 checkType(cls6, functionType(B, [B, String]), false); | |
479 checkType(bar7, Foo); | |
480 checkType(cls7, Foo); | |
481 checkType(bar7, functionType(B, [B, String])); | |
482 checkType(cls7, functionType(B, [B, String])); | |
483 checkType(bar7, getReifiedType(bar6)); | |
484 checkType(cls7, getReifiedType(bar6)); | |
485 checkType(bar8, Foo); | |
486 checkType(cls8, Foo); | |
487 checkType(bar8, functionType(B, [B, String])); | |
488 checkType(cls8, functionType(B, [B, String])); | |
489 checkType(bar8, getReifiedType(bar6), false); | |
490 checkType(cls8, getReifiedType(bar6), false); | |
491 checkType(bar7, getReifiedType(bar8), false); | |
492 checkType(cls7, getReifiedType(bar8), false); | |
493 checkType(bar8, getReifiedType(bar7), false); | |
494 checkType(cls8, getReifiedType(bar7), false); | |
495 | |
496 // Parameterized typedefs | |
497 expect(isGroundType(FuncG), true); | |
498 expect(isGroundType(FuncG$(B, String)), false); | |
499 checkType(bar1, FuncG$(B, String), false, true); | |
500 checkType(cls1, FuncG$(B, String), false, true); | |
501 checkType(bar3, FuncG$(B, String)); | |
502 checkType(cls3, FuncG$(B, String)); | |
503 }); | |
504 | |
505 test('dcall', () => { | |
506 function dd2d(x, y) {return x}; | |
507 dart.fn(dd2d); | |
508 function ii2i(x, y) {return x}; | |
509 dart.fn(ii2i, dart.definiteFunctionType(core.int, [core.int, core.int])); | |
510 function ii_2i(x, y) {return x}; | |
511 dart.fn(ii_2i, dart.definiteFunctionType(core.int, [core.int], [core.int])); | |
512 function i_i2i(x, opts) {return x}; | |
513 dart.fn(i_i2i, | |
514 dart.definiteFunctionType(core.int, [core.int], {extra: core.int})); | |
515 | |
516 assert.equal(dart.dcall(dd2d, 0, 1), 0); | |
517 assert.equal(dart.dcall(dd2d, "hello", "world"), "hello"); | |
518 assert.throws(() => dart.dcall(dd2d, 0)); | |
519 assert.throws(() => dart.dcall(dd2d, 0, 1, 2)); | |
520 assert.throws(() => dart.dcall(dd2d, 0, 1, {extra : 3})); | |
521 // This should throw but currently doesn't. | |
522 // assert.throws(() => dart.dcall(dd2d, 0, {extra:3})); | |
523 | |
524 assert.equal(dart.dcall(ii2i, 0, 1), 0); | |
525 assert.throws(() => dart.dcall(ii2i, "hello", "world")); | |
526 assert.throws(() => dart.dcall(ii2i, 0)); | |
527 assert.throws(() => dart.dcall(ii2i, 0, 1, 2)); | |
528 | |
529 assert.equal(dart.dcall(ii_2i, 0, 1), 0); | |
530 assert.throws(() => dart.dcall(ii_2i, "hello", "world")); | |
531 assert.equal(dart.dcall(ii_2i, 0), 0); | |
532 assert.throws(() => dart.dcall(ii_2i, 0, 1, 2)); | |
533 | |
534 assert.throws(() => dart.dcall(i_i2i, 0, 1)); | |
535 assert.throws(() => dart.dcall(i_i2i, "hello", "world")); | |
536 assert.equal(dart.dcall(i_i2i, 0), 0); | |
537 assert.throws(() => dart.dcall(i_i2i, 0, 1, 2)); | |
538 assert.equal(dart.dcall(i_i2i, 0, {extra: 3}), 0); | |
539 }); | |
540 | |
541 test('dsend', () => { | |
542 class Tester extends core.Object { | |
543 new() { | |
544 this.f = dart.fn(x => x, | |
545 dart.definiteFunctionType(core.int, [core.int])); | |
546 this.me = this; | |
547 } | |
548 m(x, y) {return x;} | |
549 call(x) {return x;} | |
550 static s(x, y) { return x;} | |
551 } | |
552 dart.setSignature(Tester, { | |
553 methods: () => ({ | |
554 m: dart.definiteFunctionType(core.int, [core.int, core.int]), | |
555 call: dart.definiteFunctionType(core.int, [core.int]) | |
556 }), | |
557 statics: () => ({ | |
558 s: dart.definiteFunctionType(core.String, [core.String]) | |
559 }), | |
560 names: ['s'] | |
561 }) | |
562 let o = new Tester(); | |
563 | |
564 // Method send | |
565 assert.equal(dart.dsend(o, 'm', 3, 4), 3); | |
566 assert.equal(dart.dsend(o, 'm', null, 4), null); | |
567 assert.throws(() => dart.dsend(o, 'm', 3)); | |
568 assert.throws(() => dart.dsend(o, 'm', "hello", "world")); | |
569 assert.throws(() => dart.dsend(o, 'q', 3)); | |
570 | |
571 // Method send through a field | |
572 assert.equal(dart.dsend(o, 'f', 3), 3); | |
573 assert.equal(dart.dsend(o, 'f', null), null); | |
574 assert.throws(() => dart.dsend(o, 'f', "hello")); | |
575 assert.throws(() => dart.dsend(o, 'f', 3, 4)); | |
576 | |
577 // Static method call | |
578 assert.equal(dart.dcall(Tester.s, "hello"), "hello"); | |
579 assert.equal(dart.dcall(Tester.s, null), null); | |
580 assert.throws(() => dart.dcall(Tester.s, "hello", "world")); | |
581 assert.throws(() => dart.dcall(Tester.s, 0, 1)); | |
582 | |
583 // Calling an object with a call method | |
584 assert.equal(dart.dcall(o, 3), 3); | |
585 assert.equal(dart.dcall(o, null), null); | |
586 assert.throws(() => dart.dcall(o, "hello")); | |
587 assert.throws(() => dart.dcall(o, 3, 4)); | |
588 | |
589 // Calling through a field containing an object with a call method | |
590 assert.equal(dart.dsend(o, 'me', 3), 3); | |
591 assert.equal(dart.dsend(o, 'me', null), null); | |
592 assert.throws(() => dart.dsend(o, 'me', "hello")); | |
593 assert.throws(() => dart.dsend(o, 'me', 3, 4)); | |
594 }); | |
595 | |
596 test('Types on top level functions', () => { | |
597 // Test some generated code | |
598 // Test the lazy path | |
599 checkType(core.identityHashCode, | |
600 dart.functionType(core.int, [core.Object])); | |
601 // Test the normal path | |
602 checkType(core.identical, | |
603 dart.functionType(core.bool, | |
604 [core.Object, core.Object])); | |
605 | |
606 // Hand crafted tests | |
607 // All dynamic | |
608 function dd2d(x, y) {return x}; | |
609 dart.fn(dd2d); | |
610 checkType(dd2d, dart.functionType(dart.dynamic, | |
611 [dart.dynamic, dart.dynamic])); | |
612 | |
613 // Set the type eagerly | |
614 function ii2i(x, y) {return x}; | |
615 dart.fn(ii2i, dart.definiteFunctionType(core.int, [core.int, core.int])); | |
616 checkType(ii2i, dart.functionType(core.int, | |
617 [core.int, core.int])); | |
618 | |
619 // Set the type lazily | |
620 function ss2s(x, y) {return x}; | |
621 var coreString; | |
622 dart.lazyFn(ss2s, | |
623 () => dart.definiteFunctionType(coreString, | |
624 [coreString, coreString])); | |
625 coreString = core.String; | |
626 checkType(ss2s, dart.functionType(core.String, | |
627 [core.String, core.String])); | |
628 | |
629 // Optional types | |
630 function ii_2i(x, y) {return x}; | |
631 dart.fn(ii_2i, dart.definiteFunctionType(core.int, [core.int], [core.int])); | |
632 checkType(ii_2i, dart.functionType(core.int, [core.int], | |
633 [core.int])); | |
634 checkType(ii_2i, dart.functionType(core.int, [core.int, | |
635 core.int])); | |
636 checkType(ii_2i, dart.functionType(core.int, [], [core.int, | |
637 core.int]), | |
638 false); | |
639 checkType(ii_2i, dart.functionType(core.int, [core.int], | |
640 {extra: core.int}), false); | |
641 | |
642 // Named types | |
643 function i_i2i(x, opts) {return x}; | |
644 dart.fn(i_i2i, dart.definiteFunctionType(core.int, [core.int], | |
645 {extra: core.int})); | |
646 checkType(i_i2i, dart.functionType(core.int, [core.int], | |
647 {extra: core.int})); | |
648 checkType(i_i2i, dart.functionType(core.int, | |
649 [core.int, core.int]), false); | |
650 checkType(i_i2i, dart.functionType(core.int, [core.int], {})); | |
651 checkType(i_i2i, | |
652 dart.functionType(core.int, [], {extra: core.int, | |
653 also: core.int}), false); | |
654 checkType(i_i2i, | |
655 dart.functionType(core.int, [core.int], [core.int]), false); | |
656 }); | |
657 | |
658 test('Method tearoffs', () => { | |
659 let c = collection; | |
660 // Tear off of an inherited method | |
661 let map = new (Map$(core.int, core.String))(); | |
662 checkType(dart.bind(map, 'toString'), | |
663 dart.functionType(String, [])); | |
664 checkType(dart.bind(map, 'toString'), | |
665 dart.functionType(int, []), false, true); | |
666 | |
667 // Tear off of a method directly on the object | |
668 let smap = new (c.SplayTreeMap$(core.int, core.String))(); | |
669 checkType(dart.bind(smap, 'forEach'), | |
670 dart.functionType(dart.void, | |
671 [dart.functionType(dart.void, [core.int, core.String])
])); | |
672 checkType(dart.bind(smap, 'forEach'), | |
673 dart.functionType(dart.void, | |
674 [dart.functionType(dart.void, | |
675 [core.String, core.String])]), false, true); | |
676 | |
677 // Tear off of a mixed in method | |
678 let mapB = new (c.MapBase$(core.int, core.int))(); | |
679 checkType(dart.bind(mapB, 'forEach'), | |
680 dart.functionType(dart.void, [ | |
681 dart.functionType(dart.void, [core.int, core.int])])); | |
682 checkType(dart.bind(mapB, 'forEach'), | |
683 dart.functionType(dart.void, [ | |
684 dart.functionType(dart.void, [core.int, core.String])]), | |
685 false, true); | |
686 | |
687 // Tear off of a method with a symbol name | |
688 let listB = new (c.ListBase$(core.int))(); | |
689 checkType(dart.bind(listB, dartx.add), | |
690 dart.functionType(dart.void, [core.int])); | |
691 checkType(dart.bind(listB, dartx.add), | |
692 dart.functionType(dart.void, [core.String]), false, true); | |
693 | |
694 // Tear off of a static method | |
695 checkType(c.ListBase.listToString, | |
696 dart.functionType(core.String, [core.List])); | |
697 checkType(c.ListBase.listToString, | |
698 dart.functionType(core.String, [core.String]), false, true); | |
699 | |
700 // Tear-off of extension methods on primitives | |
701 checkType(dart.bind(3.0, dartx.floor), | |
702 dart.functionType(core.int, [])); | |
703 checkType(dart.bind(3.0, dartx.floor), | |
704 dart.functionType(core.String, []), false, true); | |
705 checkType(dart.bind("", dartx.endsWith), | |
706 dart.functionType(core.bool, [core.String])); | |
707 checkType(dart.bind("", dartx.endsWith), | |
708 dart.functionType(core.bool, [core.int]), false, true); | |
709 | |
710 // Tear off a mixin method | |
711 class Base { | |
712 m(x) {return x;} | |
713 }; | |
714 dart.setSignature(Base, { | |
715 methods: () => ({ | |
716 m: dart.definiteFunctionType(core.int, [core.int]), | |
717 }) | |
718 }); | |
719 | |
720 class M1 { | |
721 m(x) {return x;} | |
722 }; | |
723 dart.setSignature(M1, { | |
724 methods: () => ({ | |
725 m: dart.definiteFunctionType(core.num, [core.int]), | |
726 }) | |
727 }); | |
728 | |
729 class M2 { | |
730 m(x) {return x;} | |
731 }; | |
732 dart.setSignature(M2, { | |
733 methods: () => ({ | |
734 m: dart.definiteFunctionType(core.Object, [core.int]), | |
735 }) | |
736 }); | |
737 | |
738 class O extends dart.mixin(Base, M1, M2) { | |
739 O() {}; | |
740 }; | |
741 dart.setSignature(O, {}); | |
742 var obj = new O(); | |
743 var m = dart.bind(obj, 'm'); | |
744 checkType(m, dart.functionType(core.Object, [core.int])); | |
745 checkType(m, dart.functionType(core.int, [core.int]), false, true); | |
746 | |
747 // Test inherited signatures | |
748 class P extends O { | |
749 P() {}; | |
750 m(x) {return x;}; | |
751 }; | |
752 dart.setSignature(P, {}); | |
753 var obj = new P(); | |
754 var m = dart.bind(obj, 'm'); | |
755 checkType(m, dart.functionType(core.Object, [core.int])); | |
756 checkType(m, dart.functionType(core.int, [core.int]), false, true); | |
757 }); | |
758 | |
759 test('Object members', () => { | |
760 let nullHash = dart.hashCode(null); | |
761 assert.equal(nullHash, 0); | |
762 let nullString = dart.toString(null); | |
763 assert.equal(nullString, 'null'); | |
764 | |
765 let map = new Map(); | |
766 let mapHash = dart.hashCode(map); | |
767 checkType(mapHash, core.int); | |
768 assert.equal(mapHash, map.hashCode); | |
769 | |
770 let mapString = dart.toString(map); | |
771 assert.equal(mapString, map.toString()); | |
772 checkType(mapString, core.String); | |
773 | |
774 let str = "A string"; | |
775 let strHash = dart.hashCode(str); | |
776 checkType(strHash, core.int); | |
777 | |
778 let strString = dart.toString(str); | |
779 checkType(strString, core.String); | |
780 assert.equal(str, strString); | |
781 | |
782 let n = 42; | |
783 let intHash = dart.hashCode(n); | |
784 checkType(intHash, core.int); | |
785 | |
786 let intString = dart.toString(n); | |
787 assert.equal(intString, '42'); | |
788 }); | |
789 }); | |
790 | |
791 suite('subtyping', function() { | |
792 'use strict'; | |
793 | |
794 let functionType = dart.functionType; | |
795 let definiteFunctionType = dart.definiteFunctionType; | |
796 let typedef = dart.typedef; | |
797 let isSubtype = dart.isSubtype; | |
798 let int = core.int; | |
799 let num = core.num; | |
800 let dyn = dart.dynamic; | |
801 | |
802 function always(t1, t2) { | |
803 assert.equal(isSubtype(t1, t2), true); | |
804 } | |
805 function never(t1, t2) { | |
806 assert.equal(isSubtype(t1, t2), false); | |
807 } | |
808 function maybe(t1, t2) { | |
809 assert.equal(isSubtype(t1, t2), null); | |
810 } | |
811 | |
812 function always2(t1, t2) { | |
813 assert.equal(isSubtype(t1, t2), true); | |
814 always(functionType(t1, [t2]), functionType(t2, [t1])); | |
815 } | |
816 function never2(t1, t2) { | |
817 assert.equal(isSubtype(t1, t2), false); | |
818 maybe(functionType(t1, [t2]), functionType(t2, [t1])); | |
819 } | |
820 function maybe2(t1, t2) { | |
821 assert.equal(isSubtype(t1, t2), null); | |
822 maybe(functionType(t1, [t2]), functionType(t2, [t1])); | |
823 } | |
824 | |
825 function run_test(func1, func2, func2opt, func1extra, func2extra) { | |
826 always2(func2(int, int), func2(int, int)); | |
827 always2(func2(int, num), func2(int, int)); | |
828 always2(func2(int, int), func2(num, int)); | |
829 | |
830 always2(func2opt(int, int), func2opt(int, int)); | |
831 always2(func2opt(int, num), func2opt(int, int)); | |
832 always2(func2opt(int, int), func2opt(num, int)); | |
833 | |
834 always2(func2opt(int, int), func2(int, int)); | |
835 always2(func2opt(int, num), func2(int, int)); | |
836 always2(func2opt(int, int), func2(num, int)); | |
837 | |
838 always2(func2opt(int, int), func1(int)); | |
839 always2(func2opt(int, num), func1(int)); | |
840 always2(func2opt(int, int), func1(num)); | |
841 | |
842 always2(func2extra(int, int), func2(int, int)); | |
843 always2(func2extra(int, num), func2(int, int)); | |
844 always2(func2extra(int, int), func2(num, int)); | |
845 | |
846 maybe2(func2(int, int), func2(int, num)); | |
847 maybe2(func2(num, int), func2(int, int)); | |
848 | |
849 maybe2(func2opt(num, num), func1(int)); | |
850 | |
851 maybe2(func2opt(int, int), func2opt(int, num)); | |
852 maybe2(func2opt(num, int), func2opt(int, int)); | |
853 | |
854 maybe2(func2opt(int, int), func2(int, num)); | |
855 maybe2(func2opt(num, int), func2(int, int)); | |
856 | |
857 maybe2(func2extra(int, int), func2(int, num)); | |
858 maybe2(func2extra(num, int), func2(int, int)); | |
859 | |
860 never2(func1(int), func2(int, num)); | |
861 never2(func1(num), func2(int, int)); | |
862 never2(func1(num), func2(num, num)); | |
863 | |
864 never2(func2(int, int), func1(int)); | |
865 never2(func2(num, int), func1(int)); | |
866 never2(func2(num, num), func1(num)); | |
867 | |
868 never2(func1(int), func2opt(int, num)); | |
869 never2(func1(num), func2opt(int, int)); | |
870 never2(func1(num), func2opt(num, num)); | |
871 | |
872 never2(func2(int, int), func2opt(int, num)); | |
873 never2(func2(num, int), func2opt(int, int)); | |
874 never2(func2(num, num), func2opt(num, num)); | |
875 | |
876 never2(func1extra(int), func2(int, num)); | |
877 never2(func1extra(num), func2(int, int)); | |
878 never2(func1extra(num), func2(num, num)); | |
879 | |
880 never2(func1extra(int), func2opt(int, num)); | |
881 never2(func1extra(num), func2opt(int, int)); | |
882 never2(func1extra(num), func2opt(num, num)); | |
883 | |
884 never2(func1(int), func1extra(int)); | |
885 never2(func1(num), func1extra(int)); | |
886 never2(func1(num), func1extra(num)); | |
887 | |
888 never2(func2(int, int), func1extra(int)); | |
889 never2(func2(num, int), func1extra(int)); | |
890 never2(func2(num, num), func1extra(num)); | |
891 | |
892 never2(func2(int, int), func2extra(int, int)); | |
893 never2(func2(num, int), func2extra(int, int)); | |
894 never2(func2(num, num), func2extra(num, num)); | |
895 }; | |
896 | |
897 test('basic function types', () => { | |
898 function func1(S) { | |
899 return functionType(S, []); | |
900 } | |
901 | |
902 function func2(S, T) { | |
903 return functionType(S, [T]); | |
904 } | |
905 | |
906 function func2opt(S, T) { | |
907 return functionType(S, [], [T]); | |
908 } | |
909 | |
910 function func1extra(S) { | |
911 return functionType(S, [], {extra: int}); | |
912 } | |
913 | |
914 function func2extra(S, T) { | |
915 return functionType(S, [T], {extra: int}); | |
916 } | |
917 | |
918 run_test(func1, func2, func2opt, func1extra, func2extra); | |
919 }); | |
920 | |
921 test('basic typedefs', () => { | |
922 function func1(S) { | |
923 return dart.typedef('Func1', () => functionType(S, [])) | |
924 } | |
925 | |
926 function func2(S, T) { | |
927 return dart.typedef('Func2', () => functionType(S, [T])) | |
928 } | |
929 | |
930 function func2opt(S, T) { | |
931 return dart.typedef('Func2', () => functionType(S, [], [T])) | |
932 } | |
933 | |
934 function func1extra(S) { | |
935 return dart.typedef('Func1', () => functionType(S, [], {extra: int})) | |
936 } | |
937 | |
938 function func2extra(S, T) { | |
939 return dart.typedef('Func2', () => functionType(S, [T], {extra: int})) | |
940 } | |
941 | |
942 run_test(func1, func2, func2opt, func1extra, func2extra); | |
943 }); | |
944 | |
945 test('basic generic typedefs', () => { | |
946 let func1 = dart.generic( | |
947 (S) => dart.typedef('Func1', () => functionType(S, []))); | |
948 | |
949 let func2 = dart.generic( | |
950 (S, T) => dart.typedef('Func2', () => functionType(S, [T]))); | |
951 | |
952 let func2opt = dart.generic( | |
953 (S, T) => dart.typedef('Func2', () => functionType(S, [], [T]))); | |
954 | |
955 let func1extra = dart.generic( | |
956 (S) => dart.typedef('Func1', () => functionType(S, [], {extra: int}))); | |
957 | |
958 let func2extra = dart.generic( | |
959 (S, T) => dart.typedef('Func2', | |
960 () => functionType(S, [T], {extra: int}))); | |
961 | |
962 run_test(func1, func2, func2opt, func1extra, func2extra); | |
963 }); | |
964 | |
965 test('fuzzy function types', () => { | |
966 always(functionType(int, [int]), functionType(dyn, [dyn])); | |
967 always(functionType(int, [], [int]), functionType(dyn, [], [dyn])); | |
968 always(functionType(int, [], [int]), functionType(dyn, [dyn])); | |
969 always(functionType(int, [], [int]), functionType(dyn, [])); | |
970 always(functionType(int, [int], {extra: int}), functionType(dyn, [dyn])); | |
971 | |
972 always(functionType(dyn, [dyn]), functionType(dyn, [dyn])); | |
973 always(functionType(dyn, [], [dyn]), functionType(dyn, [], [dyn])); | |
974 always(functionType(dyn, [], [dyn]), functionType(dyn, [dyn])); | |
975 always(functionType(dyn, [], [dyn]), functionType(dyn, [])); | |
976 always(functionType(dyn, [dyn], {extra: dyn}), functionType(dyn, [dyn])); | |
977 | |
978 }); | |
979 | |
980 test('void function types', () => { | |
981 always(functionType(int, [int]), functionType(dart.void, [dyn])); | |
982 always(functionType(int, [], [int]), functionType(dart.void, [], [dyn])); | |
983 always(functionType(int, [], [int]), functionType(dart.void, [dyn])); | |
984 always(functionType(int, [], [int]), functionType(dart.void, [])); | |
985 always(functionType(int, [int], {extra: int}), functionType(dart.void, [dyn]
)); | |
986 | |
987 always(functionType(dart.void, [int]), functionType(dart.void, [dyn])); | |
988 always(functionType(dart.void, [], [int]), functionType(dart.void, [], [dyn]
)); | |
989 always(functionType(dart.void, [], [int]), functionType(dart.void, [dyn])); | |
990 always(functionType(dart.void, [], [int]), functionType(dart.void, [])); | |
991 always(functionType(dart.void, [int], {extra: int}), functionType(dart.void,
[dyn])); | |
992 | |
993 always(functionType(dyn, [dyn]), functionType(dart.void, [dyn])); | |
994 always(functionType(dyn, [], [dyn]), functionType(dart.void, [], [dyn])); | |
995 always(functionType(dyn, [], [dyn]), functionType(dart.void, [dyn])); | |
996 always(functionType(dyn, [], [dyn]), functionType(dart.void, [])); | |
997 always(functionType(dyn, [dyn], {extra: dyn}), functionType(dart.void, [dyn]
)); | |
998 | |
999 always(functionType(dart.void, [dyn]), functionType(dart.void, [dyn])); | |
1000 always(functionType(dart.void, [], [dyn]), functionType(dart.void, [], [dyn]
)); | |
1001 always(functionType(dart.void, [], [dyn]), functionType(dart.void, [dyn])); | |
1002 always(functionType(dart.void, [], [dyn]), functionType(dart.void, [])); | |
1003 always(functionType(dart.void, [dyn], {extra: dyn}), functionType(dart.void,
[dyn])); | |
1004 | |
1005 always(functionType(dart.void, [int]), functionType(dyn, [dyn])); | |
1006 always(functionType(dart.void, [], [int]), functionType(dyn, [], [dyn])); | |
1007 always(functionType(dart.void, [], [int]), functionType(dyn, [dyn])); | |
1008 always(functionType(dart.void, [], [int]), functionType(dyn, [])); | |
1009 always(functionType(dart.void, [int], {extra: int}), functionType(dyn, [dyn]
)); | |
1010 | |
1011 never(functionType(dart.void, [int]), functionType(int, [dyn])); | |
1012 never(functionType(dart.void, [], [int]), functionType(int, [], [dyn])); | |
1013 never(functionType(dart.void, [], [int]), functionType(int, [dyn])); | |
1014 never(functionType(dart.void, [], [int]), functionType(int, [])); | |
1015 never(functionType(dart.void, [int], {extra: int}), functionType(int, [dyn])
); | |
1016 | |
1017 never(functionType(dart.void, [int]), functionType(int, [int])); | |
1018 never(functionType(dart.void, [], [int]), functionType(int, [], [int])); | |
1019 never(functionType(dart.void, [], [int]), functionType(int, [int])); | |
1020 never(functionType(dart.void, [], [int]), functionType(int, [])); | |
1021 never(functionType(dart.void, [int], {extra: int}), functionType(int, [int])
); | |
1022 | |
1023 }); | |
1024 | |
1025 test('higher-order typedef', () => { | |
1026 let Func$ = dart.generic((S, T) => | |
1027 dart.typedef('Func', () => | |
1028 functionType(T, [S]))); | |
1029 let Func2$ = dart.generic((R, S, T) => | |
1030 dart.typedef('Func2', () => | |
1031 functionType(T, [Func$(R, S)]))); | |
1032 | |
1033 maybe(functionType(int, [functionType(int, [num])]), | |
1034 functionType(num, [functionType(int, [int])])); | |
1035 maybe(functionType(int, [Func$(num, int)]), | |
1036 functionType(num, [Func$(int, int)])); | |
1037 maybe(Func2$(num, int, int), Func2$(int, int, num)); | |
1038 }); | |
1039 | |
1040 test('mixed types', () => { | |
1041 let AA$ = dart.generic((T) => class AA extends core.Object {}); | |
1042 | |
1043 always(int, dyn); | |
1044 maybe(dyn, int); | |
1045 | |
1046 never(functionType(int, [int]), int); | |
1047 | |
1048 never(int, functionType(int, [int])); | |
1049 | |
1050 always(AA$(int), AA$(dyn)); | |
1051 maybe(AA$(dyn), AA$(int)); | |
1052 never(AA$(core.Object), AA$(int)); | |
1053 | |
1054 always(AA$(functionType(int, [int])), AA$(dyn)); | |
1055 maybe(AA$(dyn), AA$(functionType(int, [int]))); | |
1056 never(AA$(core.Object), AA$(functionType(int, [int]))); | |
1057 | |
1058 always(AA$(functionType(int, [int])), AA$(functionType(dyn, [dyn]))); | |
1059 maybe(AA$(functionType(dyn, [dyn])), AA$(functionType(int, [int]))); | |
1060 maybe(AA$(functionType(core.Object, [core.Object])), | |
1061 AA$(functionType(int, [int]))); | |
1062 | |
1063 | |
1064 }); | |
1065 | |
1066 }); | |
1067 | |
1068 suite('canonicalization', function() { | |
1069 'use strict'; | |
1070 let functionType = dart.functionType; | |
1071 let definiteFunctionType = dart.definiteFunctionType; | |
1072 let typedef = dart.typedef; | |
1073 let generic = dart.generic; | |
1074 | |
1075 let Object = core.Object; | |
1076 let String = core.String; | |
1077 let int = core.int; | |
1078 let dynamic = dart.dynamic; | |
1079 let bottom = dart.bottom; | |
1080 let Map = core.Map; | |
1081 let Map$ = core.Map$; | |
1082 | |
1083 class A {} | |
1084 | |
1085 let AA$ = generic((T, U) => class AA extends core.Object {}); | |
1086 let AA = AA$(); | |
1087 | |
1088 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])); | |
1089 | |
1090 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U]))
) | |
1091 let FuncG = FuncG$(); | |
1092 | |
1093 test('base types', () => { | |
1094 assert.equal(Object, Object); | |
1095 assert.equal(String, String); | |
1096 assert.equal(dynamic, dynamic); | |
1097 }); | |
1098 | |
1099 test('class types', () => { | |
1100 assert.equal(A, A); | |
1101 }); | |
1102 | |
1103 test('generic class types', () => { | |
1104 assert.equal(AA, AA); | |
1105 assert.equal(AA, AA$(dynamic, dynamic)); | |
1106 assert.equal(AA$(dynamic, dynamic), AA$(dynamic, dynamic)); | |
1107 assert.equal(AA$(AA, Object), AA$(AA, Object)); | |
1108 assert.equal(Map, Map); | |
1109 assert.equal(Map$(dynamic, dynamic), Map); | |
1110 assert.equal(Map$(int, Map$(int, int)), Map$(int, Map$(int, int))); | |
1111 }); | |
1112 | |
1113 test('typedefs', () => { | |
1114 assert.equal(Func2, Func2); | |
1115 assert.equal(FuncG, FuncG$(dynamic, dynamic)); | |
1116 assert.equal(FuncG$(dynamic, dynamic), FuncG$(dynamic, dynamic)); | |
1117 assert.equal(FuncG$(String, Func2), FuncG$(String, Func2)); | |
1118 }); | |
1119 | |
1120 test('function types', () => { | |
1121 assert.equal(functionType(dynamic, [dynamic, dynamic]), | |
1122 functionType(dynamic, [dynamic, dynamic])) | |
1123 | |
1124 assert.notEqual(definiteFunctionType(dynamic, [dynamic, dynamic]), | |
1125 functionType(dynamic, [dynamic, dynamic])) | |
1126 | |
1127 assert.equal(functionType(dynamic, [dynamic, dynamic]), | |
1128 functionType(dynamic, [bottom, bottom])) | |
1129 | |
1130 assert.equal(functionType(dynamic, [], [dynamic, dynamic]), | |
1131 functionType(dynamic, [], [dynamic, dynamic])) | |
1132 | |
1133 assert.notEqual(definiteFunctionType(dynamic, [], [dynamic, dynamic]), | |
1134 functionType(dynamic, [], [dynamic, dynamic])) | |
1135 | |
1136 assert.equal(functionType(dynamic, [], [dynamic, dynamic]), | |
1137 functionType(dynamic, [], [bottom, bottom])) | |
1138 | |
1139 assert.equal(functionType(dynamic, [], {extra: dynamic}), | |
1140 functionType(dynamic, [], {extra: dynamic})) | |
1141 | |
1142 assert.notEqual(definiteFunctionType(dynamic, [], {extra: dynamic}), | |
1143 functionType(dynamic, [], {extra: dynamic})) | |
1144 | |
1145 assert.equal(functionType(dynamic, [], {extra: dynamic}), | |
1146 functionType(dynamic, [], {extra: bottom})) | |
1147 | |
1148 assert.equal(functionType(int, [int, int]), | |
1149 functionType(int, [int, int])) | |
1150 | |
1151 assert.equal(functionType(int, [], [int, int]), | |
1152 functionType(int, [], [int, int])) | |
1153 | |
1154 assert.equal(functionType(int, [int, int], {extra: int}), | |
1155 functionType(int, [int, int], {extra: int})) | |
1156 | |
1157 assert.equal(functionType(int, [int, int, int, int, int]), | |
1158 functionType(int, [int, int, int, int, int])) | |
1159 | |
1160 assert.notEqual(functionType(int, [int, int, int, int, int]), | |
1161 functionType(int, [int, int, int], [int, int])) | |
1162 | |
1163 assert.notEqual(functionType(String, [int, int, int, int, int]), | |
1164 functionType(int, [int, int, int, int, int])) | |
1165 | |
1166 assert.notEqual(functionType(String, []), | |
1167 functionType(int, [])) | |
1168 }); | |
1169 }); | |
1170 | |
1171 suite('primitives', function() { | |
1172 'use strict'; | |
1173 | |
1174 test('fixed length list', () => { | |
1175 let list = new core.List(10); | |
1176 list[0] = 42; | |
1177 assert.throws(() => list.add(42)); | |
1178 }); | |
1179 | |
1180 test('toString on ES Symbol', () => { | |
1181 let sym = Symbol('_foobar'); | |
1182 assert.equal(dart.toString(sym), 'Symbol(_foobar)'); | |
1183 }); | |
1184 }); | |
OLD | NEW |