Chromium Code Reviews| 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 var assert = chai.assert; |
| 6 | 6 |
| 7 suite('generic', () => { | 7 suite('generic', () => { |
| 8 "use strict"; | 8 "use strict"; |
| 9 | 9 |
| 10 let generic = dart.generic; | 10 let generic = dart.generic; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 | 118 |
| 119 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])); | 119 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])); |
| 120 let Foo = typedef('Foo', () => functionType(B, [B, String])); | 120 let Foo = typedef('Foo', () => functionType(B, [B, String])); |
| 121 | 121 |
| 122 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U])) ) | 122 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U])) ) |
| 123 let FuncG = FuncG$(); | 123 let FuncG = FuncG$(); |
| 124 | 124 |
| 125 // TODO(vsm): Revisit when we encode types on functions properly. | 125 // TODO(vsm): Revisit when we encode types on functions properly. |
| 126 // A bar1(C c, String s) => null; | 126 // A bar1(C c, String s) => null; |
| 127 function bar1(c, s) { return null; } | 127 function bar1(c, s) { return null; } |
| 128 setRuntimeType(bar1, functionType(A, [C, String])); | 128 setRuntimeType(bar1, functionType(A, [C, String])); |
|
vsm
2015/05/18 17:35:11
Can we replace all 'setRuntimeType' with your new
Leaf
2015/05/19 00:02:21
Done.
| |
| 129 | 129 |
| 130 // bar2(B b, String s) => null; | 130 // bar2(B b, String s) => null; |
| 131 function bar2(b, s) { return null; } | 131 function bar2(b, s) { return null; } |
| 132 setRuntimeType(bar2, functionType(dynamic, [B, String])); | 132 setRuntimeType(bar2, functionType(dynamic, [B, String])); |
| 133 | 133 |
| 134 // B bar3(B b, Object o) => null; | 134 // B bar3(B b, Object o) => null; |
| 135 function bar3(b, o) { return null; } | 135 function bar3(b, o) { return null; } |
| 136 setRuntimeType(bar3, functionType(B, [B, Object])); | 136 setRuntimeType(bar3, functionType(B, [B, Object])); |
| 137 | 137 |
| 138 // B bar4(B b, o) => null; | 138 // B bar4(B b, o) => null; |
| 139 function bar4(b, o) { return null; } | 139 function bar4(b, o) { return null; } |
| 140 setRuntimeType(bar4, functionType(B, [B, dynamic])); | 140 setRuntimeType(bar4, functionType(B, [B, dynamic])); |
| 141 | 141 |
| 142 // C bar5(A a, Object o) => null; | 142 // C bar5(A a, Object o) => null; |
| 143 function bar5(a, o) { return null; } | 143 function bar5(a, o) { return null; } |
| 144 setRuntimeType(bar5, functionType(C, [A, Object])); | 144 setRuntimeType(bar5, functionType(C, [A, Object])); |
| 145 | 145 |
| 146 // B bar6(B b, String s, String o) => null; | 146 // B bar6(B b, String s, String o) => null; |
| 147 function bar6(b, s, o) { return null; } | 147 function bar6(b, s, o) { return null; } |
| 148 setRuntimeType(bar6, functionType(B, [B, String, String])); | 148 setRuntimeType(bar6, functionType(B, [B, String, String])); |
| 149 | 149 |
| 150 // B bar7(B b, String s, [Object o]) => null; | 150 // B bar7(B b, String s, [Object o]) => null; |
| 151 function bar7(b, s, o) { return null; } | 151 function bar7(b, s, o) { return null; } |
| 152 setRuntimeType(bar7, functionType(B, [B, String], [Object])); | 152 setRuntimeType(bar7, functionType(B, [B, String], [Object])); |
| 153 | 153 |
| 154 // B bar8(B b, String s, {Object p}) => null; | 154 // B bar8(B b, String s, {Object p}) => null; |
| 155 function bar8(b, s, o) { return null; } | 155 function bar8(b, s, o) { return null; } |
| 156 setRuntimeType(bar8, functionType(B, [B, String], {p: Object})); | 156 setRuntimeType(bar8, functionType(B, [B, String], {p: Object})); |
| 157 | 157 |
| 158 let cls1 = dart.fn((c, s) => { return null; }, A, [C, String]); | |
| 159 | |
| 160 let cls2 = dart.fn((b, s) => { return null; }, dynamic, [B, String]); | |
| 161 | |
| 162 let cls3 = dart.fn((b, o) => { return null; }, B, [B, Object]); | |
| 163 | |
| 164 let cls4 = dart.fn((b, o) => { return null; }, B, [B, dynamic]); | |
| 165 | |
| 166 let cls5 = dart.fn((a, o) => { return null; }, C, [A, Object]); | |
| 167 | |
| 168 let cls6 = dart.fn((b, s, o) => { return null; }, B, [B, String, String]); | |
| 169 | |
| 170 let cls7 = dart.fn((b, s, o) => { return null; }, B, [B, String], [Object]); | |
| 171 | |
| 172 let cls8 = dart.fn((b, s, o) => { return null; }, B, [B, String], {p: Object}) ; | |
| 173 | |
| 158 function checkType(x, type, expectedTrue) { | 174 function checkType(x, type, expectedTrue) { |
| 159 if (expectedTrue === undefined) expectedTrue = true; | 175 if (expectedTrue === undefined) expectedTrue = true; |
| 160 expect(instanceOf(x, type), expectedTrue); | 176 expect(instanceOf(x, type), expectedTrue); |
| 161 } | 177 } |
| 162 | 178 |
| 163 test('int', () => { | 179 test('int', () => { |
| 164 expect(isGroundType(int), true); | 180 expect(isGroundType(int), true); |
| 165 expect(isGroundType(runtimeType(5)), true); | 181 expect(isGroundType(runtimeType(5)), true); |
| 166 | 182 |
| 167 checkType(5, int); | 183 checkType(5, int); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 checkType(s1, c.SetMixin$(int), false); | 340 checkType(s1, c.SetMixin$(int), false); |
| 325 }); | 341 }); |
| 326 | 342 |
| 327 test('Functions', () => { | 343 test('Functions', () => { |
| 328 // - return type: Dart is bivariant. We're covariant. | 344 // - return type: Dart is bivariant. We're covariant. |
| 329 // - param types: Dart is bivariant. We're contravariant. | 345 // - param types: Dart is bivariant. We're contravariant. |
| 330 expect(isGroundType(Func2), true); | 346 expect(isGroundType(Func2), true); |
| 331 expect(isGroundType(Foo), false); | 347 expect(isGroundType(Foo), false); |
| 332 expect(isGroundType(functionType(B, [B, String])), false); | 348 expect(isGroundType(functionType(B, [B, String])), false); |
| 333 checkType(bar1, Foo, false); | 349 checkType(bar1, Foo, false); |
| 350 checkType(cls1, Foo, false); | |
| 334 checkType(bar1, functionType(B, [B, String]), false); | 351 checkType(bar1, functionType(B, [B, String]), false); |
| 352 checkType(cls1, functionType(B, [B, String]), false); | |
| 335 checkType(bar2, Foo, false); | 353 checkType(bar2, Foo, false); |
| 354 checkType(cls2, Foo, false); | |
| 336 checkType(bar2, functionType(B, [B, String]), false); | 355 checkType(bar2, functionType(B, [B, String]), false); |
| 356 checkType(cls2, functionType(B, [B, String]), false); | |
| 337 checkType(bar3, Foo); | 357 checkType(bar3, Foo); |
| 358 checkType(cls3, Foo); | |
| 338 checkType(bar3, functionType(B, [B, String])); | 359 checkType(bar3, functionType(B, [B, String])); |
| 360 checkType(cls3, functionType(B, [B, String])); | |
| 339 checkType(bar4, Foo, false); | 361 checkType(bar4, Foo, false); |
| 362 checkType(cls4, Foo, false); | |
| 340 // TODO(vsm): Revisit. bar4 is (B, *) -> B. Perhaps it should be treated a s top for a reified object. | 363 // TODO(vsm): Revisit. bar4 is (B, *) -> B. Perhaps it should be treated a s top for a reified object. |
| 341 checkType(bar4, functionType(B, [B, String]), false); | 364 checkType(bar4, functionType(B, [B, String]), false); |
| 365 checkType(cls4, functionType(B, [B, String]), false); | |
| 342 checkType(bar5, Foo); | 366 checkType(bar5, Foo); |
| 367 checkType(cls5, Foo); | |
| 343 checkType(bar5, functionType(B, [B, String])); | 368 checkType(bar5, functionType(B, [B, String])); |
| 369 checkType(cls5, functionType(B, [B, String])); | |
| 344 checkType(bar6, Foo, false); | 370 checkType(bar6, Foo, false); |
| 371 checkType(cls6, Foo, false); | |
| 345 checkType(bar6, functionType(B, [B, String]), false); | 372 checkType(bar6, functionType(B, [B, String]), false); |
| 373 checkType(cls6, functionType(B, [B, String]), false); | |
| 346 checkType(bar7, Foo); | 374 checkType(bar7, Foo); |
| 375 checkType(cls7, Foo); | |
| 347 checkType(bar7, functionType(B, [B, String])); | 376 checkType(bar7, functionType(B, [B, String])); |
| 377 checkType(cls7, functionType(B, [B, String])); | |
| 348 checkType(bar7, runtimeType(bar6)); | 378 checkType(bar7, runtimeType(bar6)); |
| 379 checkType(cls7, runtimeType(bar6)); | |
| 349 checkType(bar8, Foo); | 380 checkType(bar8, Foo); |
| 381 checkType(cls8, Foo); | |
| 350 checkType(bar8, functionType(B, [B, String])); | 382 checkType(bar8, functionType(B, [B, String])); |
| 383 checkType(cls8, functionType(B, [B, String])); | |
| 351 checkType(bar8, runtimeType(bar6), false); | 384 checkType(bar8, runtimeType(bar6), false); |
| 385 checkType(cls8, runtimeType(bar6), false); | |
| 352 checkType(bar7, runtimeType(bar8), false); | 386 checkType(bar7, runtimeType(bar8), false); |
| 387 checkType(cls7, runtimeType(bar8), false); | |
| 353 checkType(bar8, runtimeType(bar7), false); | 388 checkType(bar8, runtimeType(bar7), false); |
| 389 checkType(cls8, runtimeType(bar7), false); | |
| 354 | 390 |
| 355 // Parameterized typedefs | 391 // Parameterized typedefs |
| 356 expect(isGroundType(FuncG), true); | 392 expect(isGroundType(FuncG), true); |
| 357 expect(isGroundType(FuncG$(B, String)), false); | 393 expect(isGroundType(FuncG$(B, String)), false); |
| 358 checkType(bar1, FuncG$(B, String), false); | 394 checkType(bar1, FuncG$(B, String), false); |
| 395 checkType(cls1, FuncG$(B, String), false); | |
| 359 checkType(bar3, FuncG$(B, String)); | 396 checkType(bar3, FuncG$(B, String)); |
| 397 checkType(cls3, FuncG$(B, String)); | |
| 398 }); | |
| 399 | |
| 400 test('dcall', () => { | |
| 401 function dd2d(x, y) {return x}; | |
| 402 dart.fn(dd2d); | |
| 403 function ii2i(x, y) {return x}; | |
| 404 dart.fn(ii2i, core.int, [core.int, core.int]); | |
| 405 function ii_2i(x, y) {return x}; | |
| 406 dart.fn(ii_2i, core.int, [core.int], [core.int]); | |
| 407 function i_i2i(x, opts) {return x}; | |
| 408 dart.fn(i_i2i, core.int, [core.int], {extra: core.int}); | |
| 409 | |
| 410 assert.equal(dart.dcall(dd2d, 0, 1), 0); | |
| 411 assert.equal(dart.dcall(dd2d, "hello", "world"), "hello"); | |
| 412 assert.throws(() => dart.dcall(dd2d, 0)); | |
| 413 assert.throws(() => dart.dcall(dd2d, 0, 1, 2)); | |
| 414 assert.throws(() => dart.dcall(dd2d, 0, 1, {extra : 3})); | |
| 415 // This should throw but currently doesn't. | |
|
vsm
2015/05/18 17:35:10
Nit: indent
Leaf
2015/05/19 00:02:21
Done.
| |
| 416 // assert.throws(() => dart.dcall(dd2d, 0, {extra:3})); | |
| 417 | |
| 418 assert.equal(dart.dcall(ii2i, 0, 1), 0); | |
| 419 assert.throws(() => dart.dcall(ii2i, "hello", "world")); | |
| 420 assert.throws(() => dart.dcall(ii2i, 0)); | |
| 421 assert.throws(() => dart.dcall(ii2i, 0, 1, 2)); | |
| 422 | |
| 423 assert.equal(dart.dcall(ii_2i, 0, 1), 0); | |
| 424 assert.throws(() => dart.dcall(ii_2i, "hello", "world")); | |
| 425 assert.equal(dart.dcall(ii_2i, 0), 0); | |
| 426 assert.throws(() => dart.dcall(ii_2i, 0, 1, 2)); | |
| 427 | |
| 428 assert.throws(() => dart.dcall(i_i2i, 0, 1)); | |
| 429 assert.throws(() => dart.dcall(i_i2i, "hello", "world")); | |
| 430 assert.equal(dart.dcall(i_i2i, 0), 0); | |
| 431 assert.throws(() => dart.dcall(i_i2i, 0, 1, 2)); | |
| 432 assert.equal(dart.dcall(i_i2i, 0, {extra: 3}), 0); | |
| 433 }); | |
| 434 | |
| 435 test('Types on top level functions', () => { | |
| 436 // Test some generated code | |
| 437 // Test the lazy path | |
| 438 checkType(core.identityHashCode, dart.functionType(core.int, [core.Object])) ; | |
| 439 // Test the normal path | |
| 440 checkType(core.identical, dart.functionType(core.bool, [core.Object, core.Ob ject])); | |
|
vsm
2015/05/18 17:35:10
line len
Leaf
2015/05/19 00:02:21
Done.
| |
| 441 | |
| 442 // Hand crafted tests | |
| 443 // All dynamic | |
| 444 function dd2d(x, y) {return x}; | |
| 445 dart.fn(dd2d); | |
| 446 checkType(dd2d, dart.functionType(dart.dynamic, [dart.dynamic, dart.dynamic] )); | |
| 447 | |
| 448 // Set the type eagerly | |
| 449 function ii2i(x, y) {return x}; | |
| 450 dart.fn(ii2i, core.int, [core.int, core.int]); | |
| 451 checkType(ii2i, dart.functionType(core.int, [core.int, core.int])); | |
| 452 | |
| 453 // Set the type lazily | |
| 454 function ss2s(x, y) {return x}; | |
| 455 var coreString; | |
| 456 dart.fn(ss2s, () => dart.functionType(coreString, [coreString, coreString])) ; | |
| 457 coreString = core.String; | |
| 458 checkType(ss2s, dart.functionType(core.String, [core.String, core.String])); | |
| 459 | |
| 460 // Optional types | |
| 461 function ii_2i(x, y) {return x}; | |
| 462 dart.fn(ii_2i, core.int, [core.int], [core.int]); | |
| 463 checkType(ii_2i, dart.functionType(core.int, [core.int], [core.int])); | |
| 464 checkType(ii_2i, dart.functionType(core.int, [core.int, core.int])); | |
| 465 checkType(ii_2i, dart.functionType(core.int, [], [core.int, core.int]), fals e); | |
| 466 checkType(ii_2i, dart.functionType(core.int, [core.int], {extra: core.int}), false); | |
| 467 | |
| 468 // Named types | |
| 469 function i_i2i(x, opts) {return x}; | |
| 470 dart.fn(i_i2i, core.int, [core.int], {extra: core.int}); | |
| 471 checkType(i_i2i, dart.functionType(core.int, [core.int], {extra: core.int})) ; | |
| 472 checkType(i_i2i, dart.functionType(core.int, [core.int, core.int]), false); | |
| 473 checkType(i_i2i, dart.functionType(core.int, [core.int], {})); | |
| 474 checkType(i_i2i, dart.functionType(core.int, [], {extra: core.int, also: cor e.int}), false); | |
| 475 checkType(i_i2i, dart.functionType(core.int, [core.int], [core.int]), false) ; | |
| 476 }); | |
| 477 | |
| 478 test('Method tearoffs', () => { | |
| 479 let c = collection; | |
| 480 // Tear off of an inherited method | |
| 481 let map = new (Map$(core.int, core.String))(); | |
| 482 checkType(dart.tearoff(map, 'toString'), dart.functionType(String, [])); | |
| 483 checkType(dart.tearoff(map, 'toString'), dart.functionType(int, []), false); | |
| 484 | |
| 485 // Tear off of a method directly on the object | |
| 486 let smap = new (c.SplayTreeMap$(core.int, core.String))(); | |
| 487 checkType(dart.tearoff(smap, 'forEach'), dart.functionType(dart.void, [dart. functionType(dart.void, [core.int, core.String])])); | |
| 488 checkType(dart.tearoff(smap, 'forEach'), dart.functionType(dart.void, [dart. functionType(dart.void, [core.String, core.String])]), false); | |
| 489 | |
| 490 // Tear off of a mixed in method | |
| 491 let mapB = new (c.MapBase$(core.int, core.int))(); | |
| 492 checkType(dart.tearoff(mapB, 'forEach'), dart.functionType(dart.void, [dart. functionType(dart.void, [core.int, core.int])])); | |
| 493 checkType(dart.tearoff(mapB, 'forEach'), dart.functionType(dart.void, [dart. functionType(dart.void, [core.int, core.String])]), false); | |
| 494 | |
| 495 // Tear off of a method with a symbol name | |
| 496 let listB = new (c.ListBase$(core.int))(); | |
| 497 checkType(dart.tearoff(listB, core.$add), dart.functionType(dart.void, [core .int])); | |
| 498 checkType(dart.tearoff(listB, core.$add), dart.functionType(dart.void, [core .String]), false); | |
| 499 | |
| 500 // Tear off of a static method | |
| 501 checkType(c.ListBase.listToString, dart.functionType(core.String, [core.List ])); | |
| 502 checkType(c.ListBase.listToString, dart.functionType(core.String, [core.Stri ng]), false); | |
| 503 | |
| 360 }); | 504 }); |
| 361 | 505 |
| 362 test('Object members', () => { | 506 test('Object members', () => { |
| 363 let nullHash = dart.hashCode(null); | 507 let nullHash = dart.hashCode(null); |
| 364 assert.equal(nullHash, 0); | 508 assert.equal(nullHash, 0); |
| 365 let nullString = dart.toString(null); | 509 let nullString = dart.toString(null); |
| 366 assert.equal(nullString, 'null'); | 510 assert.equal(nullString, 'null'); |
| 367 let nullType = dart.runtimeType(null); | 511 let nullType = dart.runtimeType(null); |
| 368 assert.equal(nullType, core.Null); | 512 assert.equal(nullType, core.Null); |
| 369 | 513 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 | 545 |
| 402 suite('primitives', function() { | 546 suite('primitives', function() { |
| 403 'use strict'; | 547 'use strict'; |
| 404 | 548 |
| 405 test('fixed length list', () => { | 549 test('fixed length list', () => { |
| 406 let list = new core.List(10); | 550 let list = new core.List(10); |
| 407 list[0] = 42; | 551 list[0] = 42; |
| 408 assert.throws(() => list.add(42)); | 552 assert.throws(() => list.add(42)); |
| 409 }); | 553 }); |
| 410 }); | 554 }); |
| OLD | NEW |