| 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 suite('instanceOf', () => { | 78 suite('instanceOf', () => { |
| 79 "use strict"; | 79 "use strict"; |
| 80 | 80 |
| 81 let expect = assert.equal; | 81 let expect = assert.equal; |
| 82 let isGroundType = dart.isGroundType; | 82 let isGroundType = dart.isGroundType; |
| 83 let generic = dart.generic; | 83 let generic = dart.generic; |
| 84 let intIsNonNullable = false; | 84 let intIsNonNullable = false; |
| 85 let cast = dart.as; | 85 let cast = dart.as; |
| 86 let instanceOf = dart.is; | 86 let instanceOf = dart.is; |
| 87 let getRuntimeType = dart.getRuntimeType; | 87 let getRuntimeType = dart.getRuntimeType; |
| 88 let runtimeType = dart.runtimeType; |
| 89 let functionType = dart.functionType; |
| 90 let typedef = dart.typedef; |
| 88 | 91 |
| 89 let Object = core.Object; | 92 let Object = core.Object; |
| 90 let String = core.String; | 93 let String = core.String; |
| 91 let dynamic = dart.dynamic; | 94 let dynamic = dart.dynamic; |
| 92 let List = core.List; | 95 let List = core.List; |
| 93 let Map = core.Map; | 96 let Map = core.Map; |
| 94 let Map$ = core.Map$; | 97 let Map$ = core.Map$; |
| 95 let int = core.int; | 98 let int = core.int; |
| 96 let num = core.num; | 99 let num = core.num; |
| 97 let bool = core.bool; | 100 let bool = core.bool; |
| 98 | 101 |
| 99 class A {} | 102 class A {} |
| 100 class B extends A {} | 103 class B extends A {} |
| 101 class C extends B {} | 104 class C extends B {} |
| 102 | 105 |
| 103 let AA$ = generic((T, U) => class AA extends core.Object {}); | 106 let AA$ = generic((T, U) => class AA extends core.Object {}); |
| 104 let AA = AA$(); | 107 let AA = AA$(); |
| 105 let BB$ = generic((T, U) => class BB extends AA$(U, T) {}); | 108 let BB$ = generic((T, U) => class BB extends AA$(U, T) {}); |
| 106 let BB = BB$(); | 109 let BB = BB$(); |
| 107 class CC extends BB$(String, List) {} | 110 class CC extends BB$(String, List) {} |
| 108 | 111 |
| 112 let Func2 = typedef('Func2', () => functionType(dynamic, [dynamic, dynamic])); |
| 113 let Foo = typedef('Foo', () => functionType(B, [B, String])); |
| 114 |
| 115 let FuncG$ = generic((T, U) => typedef('FuncG', () => functionType(T, [T, U]))
) |
| 116 let FuncG = FuncG$(); |
| 117 |
| 118 // TODO(vsm): Revisit when we encode types on functions properly. |
| 119 // A bar1(C c, String s) => null; |
| 120 function bar1(c, s) { return null; } |
| 121 bar1[runtimeType] = functionType(A, [C, String]); |
| 122 |
| 123 // bar2(B b, String s) => null; |
| 124 function bar2(b, s) { return null; } |
| 125 bar2[runtimeType] = functionType(dynamic, [B, String]); |
| 126 |
| 127 // B bar3(B b, Object o) => null; |
| 128 function bar3(b, o) { return null; } |
| 129 bar3[runtimeType] = functionType(B, [B, Object]); |
| 130 |
| 131 // B bar4(B b, o) => null; |
| 132 function bar4(b, o) { return null; } |
| 133 bar4[runtimeType] = functionType(B, [B, dynamic]); |
| 134 |
| 135 // C bar5(A a, Object o) => null; |
| 136 function bar5(a, o) { return null; } |
| 137 bar5[runtimeType] = functionType(C, [A, Object]); |
| 138 |
| 139 // B bar6(B b, String s, String o) => null; |
| 140 function bar6(b, s, o) { return null; } |
| 141 bar6[runtimeType] = functionType(B, [B, String, String]); |
| 142 |
| 143 // B bar7(B b, String s, [Object o]) => null; |
| 144 function bar7(b, s, o) { return null; } |
| 145 bar7[runtimeType] = functionType(B, [B, String], [Object]); |
| 146 |
| 147 // B bar8(B b, String s, {Object p}) => null; |
| 148 function bar8(b, s, o) { return null; } |
| 149 bar8[runtimeType] = functionType(B, [B, String], {p: Object}); |
| 150 |
| 109 function checkType(x, type, expectedTrue) { | 151 function checkType(x, type, expectedTrue) { |
| 110 if (expectedTrue === undefined) expectedTrue = true; | 152 if (expectedTrue === undefined) expectedTrue = true; |
| 111 expect(instanceOf(x, type), expectedTrue); | 153 expect(instanceOf(x, type), expectedTrue); |
| 112 } | 154 } |
| 113 | 155 |
| 114 test('int', () => { | 156 test('int', () => { |
| 115 expect(isGroundType(int), true); | 157 expect(isGroundType(int), true); |
| 116 expect(isGroundType(getRuntimeType(5)), true); | 158 expect(isGroundType(getRuntimeType(5)), true); |
| 117 | 159 |
| 118 checkType(5, int); | 160 checkType(5, int); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 var s1 = new (c.SplayTreeSet$(String))(); | 309 var s1 = new (c.SplayTreeSet$(String))(); |
| 268 | 310 |
| 269 checkType(s1, c.IterableMixin); | 311 checkType(s1, c.IterableMixin); |
| 270 checkType(s1, c.IterableMixin$(String)); | 312 checkType(s1, c.IterableMixin$(String)); |
| 271 checkType(s1, c.IterableMixin$(int), false); | 313 checkType(s1, c.IterableMixin$(int), false); |
| 272 | 314 |
| 273 checkType(s1, c.SetMixin); | 315 checkType(s1, c.SetMixin); |
| 274 checkType(s1, c.SetMixin$(String)); | 316 checkType(s1, c.SetMixin$(String)); |
| 275 checkType(s1, c.SetMixin$(int), false); | 317 checkType(s1, c.SetMixin$(int), false); |
| 276 }); | 318 }); |
| 319 |
| 320 test('Functions', () => { |
| 321 // - return type: Dart is bivariant. We're covariant. |
| 322 // - param types: Dart is bivariant. We're contravariant. |
| 323 expect(isGroundType(Func2), true); |
| 324 expect(isGroundType(Foo), false); |
| 325 expect(isGroundType(functionType(B, [B, String])), false); |
| 326 checkType(bar1, Foo, false); |
| 327 checkType(bar1, functionType(B, [B, String]), false); |
| 328 checkType(bar2, Foo, false); |
| 329 checkType(bar2, functionType(B, [B, String]), false); |
| 330 checkType(bar3, Foo); |
| 331 checkType(bar3, functionType(B, [B, String])); |
| 332 checkType(bar4, Foo, false); |
| 333 // TODO(vsm): Revisit. bar4 is (B, *) -> B. Perhaps it should be treated a
s top for a reified object. |
| 334 checkType(bar4, functionType(B, [B, String]), false); |
| 335 checkType(bar5, Foo); |
| 336 checkType(bar5, functionType(B, [B, String])); |
| 337 checkType(bar6, Foo, false); |
| 338 checkType(bar6, functionType(B, [B, String]), false); |
| 339 checkType(bar7, Foo); |
| 340 checkType(bar7, functionType(B, [B, String])); |
| 341 checkType(bar7, bar6[runtimeType]); |
| 342 checkType(bar8, Foo); |
| 343 checkType(bar8, functionType(B, [B, String])); |
| 344 checkType(bar8, bar6[runtimeType], false); |
| 345 checkType(bar7, bar8[runtimeType], false); |
| 346 checkType(bar8, bar7[runtimeType], false); |
| 347 |
| 348 // Parameterized typedefs |
| 349 expect(isGroundType(FuncG), true); |
| 350 expect(isGroundType(FuncG$(B, String)), false); |
| 351 checkType(bar1, FuncG$(B, String), false); |
| 352 checkType(bar3, FuncG$(B, String)); |
| 353 }); |
| 277 }); | 354 }); |
| OLD | NEW |