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 var dart_sdk = dart_library.import('dart_sdk'); | 6 var dart_sdk = dart_library.import('dart_sdk'); |
7 var core = dart_sdk.core; | 7 var core = dart_sdk.core; |
8 var collection = dart_sdk.collection; | 8 var collection = dart_sdk.collection; |
9 var dart = dart_sdk.dart; | 9 var dart = dart_sdk.dart; |
10 var dartx = dart.dartx; | 10 var dartx = dart.dartx; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 146 |
147 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])); | 147 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])); |
148 let Foo = typedef('Foo', () => functionType(B, [B, String])); | 148 let Foo = typedef('Foo', () => functionType(B, [B, String])); |
149 | 149 |
150 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U]))
) | 150 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U]))
) |
151 let FuncG = FuncG$(); | 151 let FuncG = FuncG$(); |
152 | 152 |
153 // TODO(vsm): Revisit when we encode types on functions properly. | 153 // TODO(vsm): Revisit when we encode types on functions properly. |
154 // A bar1(C c, String s) => null; | 154 // A bar1(C c, String s) => null; |
155 function bar1(c, s) { return null; } | 155 function bar1(c, s) { return null; } |
156 dart.fn(bar1, A, [C, String]); | 156 dart.fn(bar1, dart.definiteFunctionType(A, [C, String])); |
157 | 157 |
158 // bar2(B b, String s) => null; | 158 // bar2(B b, String s) => null; |
159 function bar2(b, s) { return null; } | 159 function bar2(b, s) { return null; } |
160 dart.fn(bar2, dynamic, [B, String]); | 160 dart.fn(bar2, dart.definiteFunctionType(dynamic, [B, String])); |
161 | 161 |
162 // B bar3(B b, Object o) => null; | 162 // B bar3(B b, Object o) => null; |
163 function bar3(b, o) { return null; } | 163 function bar3(b, o) { return null; } |
164 dart.fn(bar3, B, [B, Object]); | 164 dart.fn(bar3, dart.definiteFunctionType(B, [B, Object])); |
165 | 165 |
166 // B bar4(B b, o) => null; | 166 // B bar4(B b, o) => null; |
167 function bar4(b, o) { return null; } | 167 function bar4(b, o) { return null; } |
168 dart.fn(bar4, B, [B, dynamic]); | 168 dart.fn(bar4, dart.definiteFunctionType(B, [B, dynamic])); |
169 | 169 |
170 // C bar5(A a, Object o) => null; | 170 // C bar5(A a, Object o) => null; |
171 function bar5(a, o) { return null; } | 171 function bar5(a, o) { return null; } |
172 dart.fn(bar5, C, [A, Object]); | 172 dart.fn(bar5, dart.definiteFunctionType(C, [A, Object])); |
173 | 173 |
174 // B bar6(B b, String s, String o) => null; | 174 // B bar6(B b, String s, String o) => null; |
175 function bar6(b, s, o) { return null; } | 175 function bar6(b, s, o) { return null; } |
176 dart.fn(bar6, B, [B, String, String]); | 176 dart.fn(bar6, dart.definiteFunctionType(B, [B, String, String])); |
177 | 177 |
178 // B bar7(B b, String s, [Object o]) => null; | 178 // B bar7(B b, String s, [Object o]) => null; |
179 function bar7(b, s, o) { return null; } | 179 function bar7(b, s, o) { return null; } |
180 dart.fn(bar7, B, [B, String], [Object]); | 180 dart.fn(bar7, dart.definiteFunctionType(B, [B, String], [Object])); |
181 | 181 |
182 // B bar8(B b, String s, {Object p}) => null; | 182 // B bar8(B b, String s, {Object p}) => null; |
183 function bar8(b, s, o) { return null; } | 183 function bar8(b, s, o) { return null; } |
184 dart.fn(bar8, B, [B, String], {p: Object}); | 184 dart.fn(bar8, dart.definiteFunctionType(B, [B, String], {p: Object})); |
185 | 185 |
186 let cls1 = dart.fn((c, s) => { return null; }, A, [C, String]); | 186 let cls1 = dart.fn((c, s) => { return null; }, |
| 187 dart.definiteFunctionType(A, [C, String])); |
187 | 188 |
188 let cls2 = dart.fn((b, s) => { return null; }, dynamic, [B, String]); | 189 let cls2 = dart.fn((b, s) => { return null; }, |
| 190 dart.definiteFunctionType(dynamic, [B, String])); |
189 | 191 |
190 let cls3 = dart.fn((b, o) => { return null; }, B, [B, Object]); | 192 let cls3 = dart.fn((b, o) => { return null; }, |
| 193 dart.definiteFunctionType(B, [B, Object])); |
191 | 194 |
192 let cls4 = dart.fn((b, o) => { return null; }, B, [B, dynamic]); | 195 let cls4 = dart.fn((b, o) => { return null; }, |
| 196 dart.definiteFunctionType(B, [B, dynamic])); |
193 | 197 |
194 let cls5 = dart.fn((a, o) => { return null; }, C, [A, Object]); | 198 let cls5 = dart.fn((a, o) => { return null; }, |
| 199 dart.definiteFunctionType(C, [A, Object])); |
195 | 200 |
196 let cls6 = dart.fn((b, s, o) => { return null; }, B, [B, String, String]); | 201 let cls6 = dart.fn((b, s, o) => { return null; }, |
| 202 dart.definiteFunctionType(B, [B, String, String])); |
197 | 203 |
198 let cls7 = dart.fn((b, s, o) => { return null; }, B, [B, String], [Object]); | 204 let cls7 = dart.fn((b, s, o) => { return null; }, |
| 205 dart.definiteFunctionType(B, [B, String], [Object])); |
199 | 206 |
200 let cls8 = | 207 let cls8 = |
201 dart.fn((b, s, o) => { return null; }, B, [B, String], {p: Object}); | 208 dart.fn((b, s, o) => { return null; }, |
| 209 dart.definiteFunctionType(B, [B, String], {p: Object})); |
202 | 210 |
203 function checkType(x, type, expectedTrue, strongOnly) { | 211 function checkType(x, type, expectedTrue, strongOnly) { |
204 if (expectedTrue === undefined) expectedTrue = true; | 212 if (expectedTrue === undefined) expectedTrue = true; |
205 if (strongOnly == undefined) strongOnly = false; | 213 if (strongOnly == undefined) strongOnly = false; |
206 if (!strongOnly) { | 214 if (!strongOnly) { |
207 assert.doesNotThrow(() => instanceOf(x, type)); | 215 assert.doesNotThrow(() => instanceOf(x, type)); |
208 expect(instanceOf(x, type), expectedTrue); | 216 expect(instanceOf(x, type), expectedTrue); |
209 } else { | 217 } else { |
210 assert.throws(() => instanceOf(x, type), dart.StrongModeError); | 218 assert.throws(() => instanceOf(x, type), dart.StrongModeError); |
211 expect(expectedTrue, false); | 219 expect(expectedTrue, false); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 }); | 344 }); |
337 | 345 |
338 test('constructors', () => { | 346 test('constructors', () => { |
339 class C extends core.Object { | 347 class C extends core.Object { |
340 C(x) {}; | 348 C(x) {}; |
341 named(x, y) {}; | 349 named(x, y) {}; |
342 } | 350 } |
343 dart.defineNamedConstructor(C, 'named'); | 351 dart.defineNamedConstructor(C, 'named'); |
344 dart.setSignature(C, { | 352 dart.setSignature(C, { |
345 constructors: () => ({ | 353 constructors: () => ({ |
346 C: [C, [core.int]], | 354 C: dart.definiteFunctionType(C, [core.int]), |
347 named: [C, [core.int, core.int]] | 355 named: dart.definiteFunctionType(C, [core.int, core.int]) |
348 }) | 356 }) |
349 }); | 357 }); |
350 let getType = dart.classGetConstructorType; | 358 let getType = dart.classGetConstructorType; |
351 isSubtype(getType(C), dart.functionType(C, [core.int])); | 359 isSubtype(getType(C), dart.functionType(C, [core.int])); |
352 isSubtype(getType(C), dart.functionType(C, [core.String]), false); | 360 isSubtype(getType(C), dart.functionType(C, [core.String]), false); |
353 isSubtype(getType(C, 'C'), dart.functionType(C, [core.int])); | 361 isSubtype(getType(C, 'C'), dart.functionType(C, [core.int])); |
354 isSubtype(getType(C, 'C'), dart.functionType(C, [core.String]), false); | 362 isSubtype(getType(C, 'C'), dart.functionType(C, [core.String]), false); |
355 isSubtype(getType(C, 'named'), dart.functionType(C, [core.int, core.int])); | 363 isSubtype(getType(C, 'named'), dart.functionType(C, [core.int, core.int])); |
356 isSubtype(getType(C, 'named'), | 364 isSubtype(getType(C, 'named'), |
357 dart.functionType(C, [core.int, core.String]), false); | 365 dart.functionType(C, [core.int, core.String]), false); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 checkType(bar1, FuncG$(B, String), false, true); | 499 checkType(bar1, FuncG$(B, String), false, true); |
492 checkType(cls1, FuncG$(B, String), false, true); | 500 checkType(cls1, FuncG$(B, String), false, true); |
493 checkType(bar3, FuncG$(B, String)); | 501 checkType(bar3, FuncG$(B, String)); |
494 checkType(cls3, FuncG$(B, String)); | 502 checkType(cls3, FuncG$(B, String)); |
495 }); | 503 }); |
496 | 504 |
497 test('dcall', () => { | 505 test('dcall', () => { |
498 function dd2d(x, y) {return x}; | 506 function dd2d(x, y) {return x}; |
499 dart.fn(dd2d); | 507 dart.fn(dd2d); |
500 function ii2i(x, y) {return x}; | 508 function ii2i(x, y) {return x}; |
501 dart.fn(ii2i, core.int, [core.int, core.int]); | 509 dart.fn(ii2i, dart.definiteFunctionType(core.int, [core.int, core.int])); |
502 function ii_2i(x, y) {return x}; | 510 function ii_2i(x, y) {return x}; |
503 dart.fn(ii_2i, core.int, [core.int], [core.int]); | 511 dart.fn(ii_2i, dart.definiteFunctionType(core.int, [core.int], [core.int])); |
504 function i_i2i(x, opts) {return x}; | 512 function i_i2i(x, opts) {return x}; |
505 dart.fn(i_i2i, core.int, [core.int], {extra: core.int}); | 513 dart.fn(i_i2i, |
| 514 dart.definiteFunctionType(core.int, [core.int], {extra: core.int})); |
506 | 515 |
507 assert.equal(dart.dcall(dd2d, 0, 1), 0); | 516 assert.equal(dart.dcall(dd2d, 0, 1), 0); |
508 assert.equal(dart.dcall(dd2d, "hello", "world"), "hello"); | 517 assert.equal(dart.dcall(dd2d, "hello", "world"), "hello"); |
509 assert.throws(() => dart.dcall(dd2d, 0)); | 518 assert.throws(() => dart.dcall(dd2d, 0)); |
510 assert.throws(() => dart.dcall(dd2d, 0, 1, 2)); | 519 assert.throws(() => dart.dcall(dd2d, 0, 1, 2)); |
511 assert.throws(() => dart.dcall(dd2d, 0, 1, {extra : 3})); | 520 assert.throws(() => dart.dcall(dd2d, 0, 1, {extra : 3})); |
512 // This should throw but currently doesn't. | 521 // This should throw but currently doesn't. |
513 // assert.throws(() => dart.dcall(dd2d, 0, {extra:3})); | 522 // assert.throws(() => dart.dcall(dd2d, 0, {extra:3})); |
514 | 523 |
515 assert.equal(dart.dcall(ii2i, 0, 1), 0); | 524 assert.equal(dart.dcall(ii2i, 0, 1), 0); |
516 assert.throws(() => dart.dcall(ii2i, "hello", "world")); | 525 assert.throws(() => dart.dcall(ii2i, "hello", "world")); |
517 assert.throws(() => dart.dcall(ii2i, 0)); | 526 assert.throws(() => dart.dcall(ii2i, 0)); |
518 assert.throws(() => dart.dcall(ii2i, 0, 1, 2)); | 527 assert.throws(() => dart.dcall(ii2i, 0, 1, 2)); |
519 | 528 |
520 assert.equal(dart.dcall(ii_2i, 0, 1), 0); | 529 assert.equal(dart.dcall(ii_2i, 0, 1), 0); |
521 assert.throws(() => dart.dcall(ii_2i, "hello", "world")); | 530 assert.throws(() => dart.dcall(ii_2i, "hello", "world")); |
522 assert.equal(dart.dcall(ii_2i, 0), 0); | 531 assert.equal(dart.dcall(ii_2i, 0), 0); |
523 assert.throws(() => dart.dcall(ii_2i, 0, 1, 2)); | 532 assert.throws(() => dart.dcall(ii_2i, 0, 1, 2)); |
524 | 533 |
525 assert.throws(() => dart.dcall(i_i2i, 0, 1)); | 534 assert.throws(() => dart.dcall(i_i2i, 0, 1)); |
526 assert.throws(() => dart.dcall(i_i2i, "hello", "world")); | 535 assert.throws(() => dart.dcall(i_i2i, "hello", "world")); |
527 assert.equal(dart.dcall(i_i2i, 0), 0); | 536 assert.equal(dart.dcall(i_i2i, 0), 0); |
528 assert.throws(() => dart.dcall(i_i2i, 0, 1, 2)); | 537 assert.throws(() => dart.dcall(i_i2i, 0, 1, 2)); |
529 assert.equal(dart.dcall(i_i2i, 0, {extra: 3}), 0); | 538 assert.equal(dart.dcall(i_i2i, 0, {extra: 3}), 0); |
530 }); | 539 }); |
531 | 540 |
532 test('dsend', () => { | 541 test('dsend', () => { |
533 class Tester extends core.Object { | 542 class Tester extends core.Object { |
534 new() { | 543 new() { |
535 this.f = dart.fn(x => x, core.int, [core.int]); | 544 this.f = dart.fn(x => x, |
| 545 dart.definiteFunctionType(core.int, [core.int])); |
536 this.me = this; | 546 this.me = this; |
537 } | 547 } |
538 m(x, y) {return x;} | 548 m(x, y) {return x;} |
539 call(x) {return x;} | 549 call(x) {return x;} |
540 static s(x, y) { return x;} | 550 static s(x, y) { return x;} |
541 } | 551 } |
542 dart.setSignature(Tester, { | 552 dart.setSignature(Tester, { |
543 methods: () => ({ | 553 methods: () => ({ |
544 m: [core.int, [core.int, core.int]], | 554 m: dart.definiteFunctionType(core.int, [core.int, core.int]), |
545 call: [core.int, [core.int]] | 555 call: dart.definiteFunctionType(core.int, [core.int]) |
546 }), | 556 }), |
547 statics: () => ({ | 557 statics: () => ({ |
548 s: [core.String, [core.String]] | 558 s: dart.definiteFunctionType(core.String, [core.String]) |
549 }), | 559 }), |
550 names: ['s'] | 560 names: ['s'] |
551 }) | 561 }) |
552 let o = new Tester(); | 562 let o = new Tester(); |
553 | 563 |
554 // Method send | 564 // Method send |
555 assert.equal(dart.dsend(o, 'm', 3, 4), 3); | 565 assert.equal(dart.dsend(o, 'm', 3, 4), 3); |
556 assert.equal(dart.dsend(o, 'm', null, 4), null); | 566 assert.equal(dart.dsend(o, 'm', null, 4), null); |
557 assert.throws(() => dart.dsend(o, 'm', 3)); | 567 assert.throws(() => dart.dsend(o, 'm', 3)); |
558 assert.throws(() => dart.dsend(o, 'm', "hello", "world")); | 568 assert.throws(() => dart.dsend(o, 'm', "hello", "world")); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 | 605 |
596 // Hand crafted tests | 606 // Hand crafted tests |
597 // All dynamic | 607 // All dynamic |
598 function dd2d(x, y) {return x}; | 608 function dd2d(x, y) {return x}; |
599 dart.fn(dd2d); | 609 dart.fn(dd2d); |
600 checkType(dd2d, dart.functionType(dart.dynamic, | 610 checkType(dd2d, dart.functionType(dart.dynamic, |
601 [dart.dynamic, dart.dynamic])); | 611 [dart.dynamic, dart.dynamic])); |
602 | 612 |
603 // Set the type eagerly | 613 // Set the type eagerly |
604 function ii2i(x, y) {return x}; | 614 function ii2i(x, y) {return x}; |
605 dart.fn(ii2i, core.int, [core.int, core.int]); | 615 dart.fn(ii2i, dart.definiteFunctionType(core.int, [core.int, core.int])); |
606 checkType(ii2i, dart.functionType(core.int, | 616 checkType(ii2i, dart.functionType(core.int, |
607 [core.int, core.int])); | 617 [core.int, core.int])); |
608 | 618 |
609 // Set the type lazily | 619 // Set the type lazily |
610 function ss2s(x, y) {return x}; | 620 function ss2s(x, y) {return x}; |
611 var coreString; | 621 var coreString; |
612 dart.lazyFn(ss2s, () => [coreString, [coreString, coreString]]); | 622 dart.lazyFn(ss2s, |
| 623 () => dart.definiteFunctionType(coreString, |
| 624 [coreString, coreString])); |
613 coreString = core.String; | 625 coreString = core.String; |
614 checkType(ss2s, dart.functionType(core.String, | 626 checkType(ss2s, dart.functionType(core.String, |
615 [core.String, core.String])); | 627 [core.String, core.String])); |
616 | 628 |
617 // Optional types | 629 // Optional types |
618 function ii_2i(x, y) {return x}; | 630 function ii_2i(x, y) {return x}; |
619 dart.fn(ii_2i, core.int, [core.int], [core.int]); | 631 dart.fn(ii_2i, dart.definiteFunctionType(core.int, [core.int], [core.int])); |
620 checkType(ii_2i, dart.functionType(core.int, [core.int], | 632 checkType(ii_2i, dart.functionType(core.int, [core.int], |
621 [core.int])); | 633 [core.int])); |
622 checkType(ii_2i, dart.functionType(core.int, [core.int, | 634 checkType(ii_2i, dart.functionType(core.int, [core.int, |
623 core.int])); | 635 core.int])); |
624 checkType(ii_2i, dart.functionType(core.int, [], [core.int, | 636 checkType(ii_2i, dart.functionType(core.int, [], [core.int, |
625 core.int]), | 637 core.int]), |
626 false); | 638 false); |
627 checkType(ii_2i, dart.functionType(core.int, [core.int], | 639 checkType(ii_2i, dart.functionType(core.int, [core.int], |
628 {extra: core.int}), false); | 640 {extra: core.int}), false); |
629 | 641 |
630 // Named types | 642 // Named types |
631 function i_i2i(x, opts) {return x}; | 643 function i_i2i(x, opts) {return x}; |
632 dart.fn(i_i2i, core.int, [core.int], {extra: core.int}); | 644 dart.fn(i_i2i, dart.definiteFunctionType(core.int, [core.int], |
| 645 {extra: core.int})); |
633 checkType(i_i2i, dart.functionType(core.int, [core.int], | 646 checkType(i_i2i, dart.functionType(core.int, [core.int], |
634 {extra: core.int})); | 647 {extra: core.int})); |
635 checkType(i_i2i, dart.functionType(core.int, | 648 checkType(i_i2i, dart.functionType(core.int, |
636 [core.int, core.int]), false); | 649 [core.int, core.int]), false); |
637 checkType(i_i2i, dart.functionType(core.int, [core.int], {})); | 650 checkType(i_i2i, dart.functionType(core.int, [core.int], {})); |
638 checkType(i_i2i, | 651 checkType(i_i2i, |
639 dart.functionType(core.int, [], {extra: core.int, | 652 dart.functionType(core.int, [], {extra: core.int, |
640 also: core.int}), false); | 653 also: core.int}), false); |
641 checkType(i_i2i, | 654 checkType(i_i2i, |
642 dart.functionType(core.int, [core.int], [core.int]), false); | 655 dart.functionType(core.int, [core.int], [core.int]), false); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
693 dart.functionType(core.bool, [core.String])); | 706 dart.functionType(core.bool, [core.String])); |
694 checkType(dart.bind("", dartx.endsWith), | 707 checkType(dart.bind("", dartx.endsWith), |
695 dart.functionType(core.bool, [core.int]), false, true); | 708 dart.functionType(core.bool, [core.int]), false, true); |
696 | 709 |
697 // Tear off a mixin method | 710 // Tear off a mixin method |
698 class Base { | 711 class Base { |
699 m(x) {return x;} | 712 m(x) {return x;} |
700 }; | 713 }; |
701 dart.setSignature(Base, { | 714 dart.setSignature(Base, { |
702 methods: () => ({ | 715 methods: () => ({ |
703 m: [core.int, [core.int]], | 716 m: dart.definiteFunctionType(core.int, [core.int]), |
704 }) | 717 }) |
705 }); | 718 }); |
706 | 719 |
707 class M1 { | 720 class M1 { |
708 m(x) {return x;} | 721 m(x) {return x;} |
709 }; | 722 }; |
710 dart.setSignature(M1, { | 723 dart.setSignature(M1, { |
711 methods: () => ({ | 724 methods: () => ({ |
712 m: [core.num, [core.int]], | 725 m: dart.definiteFunctionType(core.num, [core.int]), |
713 }) | 726 }) |
714 }); | 727 }); |
715 | 728 |
716 class M2 { | 729 class M2 { |
717 m(x) {return x;} | 730 m(x) {return x;} |
718 }; | 731 }; |
719 dart.setSignature(M2, { | 732 dart.setSignature(M2, { |
720 methods: () => ({ | 733 methods: () => ({ |
721 m: [core.Object, [core.int]], | 734 m: dart.definiteFunctionType(core.Object, [core.int]), |
722 }) | 735 }) |
723 }); | 736 }); |
724 | 737 |
725 class O extends dart.mixin(Base, M1, M2) { | 738 class O extends dart.mixin(Base, M1, M2) { |
726 O() {}; | 739 O() {}; |
727 }; | 740 }; |
728 dart.setSignature(O, {}); | 741 dart.setSignature(O, {}); |
729 var obj = new O(); | 742 var obj = new O(); |
730 var m = dart.bind(obj, 'm'); | 743 var m = dart.bind(obj, 'm'); |
731 checkType(m, dart.functionType(core.Object, [core.int])); | 744 checkType(m, dart.functionType(core.Object, [core.int])); |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1162 let list = new core.List(10); | 1175 let list = new core.List(10); |
1163 list[0] = 42; | 1176 list[0] = 42; |
1164 assert.throws(() => list.add(42)); | 1177 assert.throws(() => list.add(42)); |
1165 }); | 1178 }); |
1166 | 1179 |
1167 test('toString on ES Symbol', () => { | 1180 test('toString on ES Symbol', () => { |
1168 let sym = Symbol('_foobar'); | 1181 let sym = Symbol('_foobar'); |
1169 assert.equal(dart.toString(sym), 'Symbol(_foobar)'); | 1182 assert.equal(dart.toString(sym), 'Symbol(_foobar)'); |
1170 }); | 1183 }); |
1171 }); | 1184 }); |
OLD | NEW |