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 core = dart_library.import('dart/core'); | 6 var core = dart_library.import('dart/core'); |
7 var collection = dart_library.import('dart/collection'); | 7 var collection = dart_library.import('dart/collection'); |
8 var dart = dart_library.import('dart/_runtime'); | 8 var dart = dart_library.import('dart/_runtime'); |
9 var dartx = dart.dartx; | 9 var dartx = dart.dartx; |
10 | 10 |
11 // TODO(leafp): These are here to test some things not | |
12 // currently exposed through the main dart entry point. | |
13 // If we decide to expose them, this can go away. | |
14 var classes = dart_library.import('dart/_classes'); | |
15 var types = dart_library.import('dart/_types'); | |
16 var dart_utils = dart_library.import('dart/_utils'); | |
17 | |
18 suite('generic', () => { | 11 suite('generic', () => { |
19 "use strict"; | 12 "use strict"; |
20 | 13 |
21 let generic = dart.generic; | 14 let generic = dart.generic; |
22 | 15 |
23 test('zero arguments is not allowed', () => { | 16 test('zero arguments is not allowed', () => { |
24 assert.throws(() => { generic(function(){}); }); | 17 assert.throws(() => { generic(function(){}); }); |
25 }); | 18 }); |
26 | 19 |
| 20 test('dcall noSuchMethod has correct error target', () => { |
| 21 assert.throws(() => dart.dcall(42), |
| 22 new RegExp('NoSuchMethodError.*\nReceiver: 42', 'm'), |
| 23 'Calls with non-function receiver should throw a NoSuchMethodError' + |
| 24 ' with correct target'); |
| 25 |
| 26 // TODO(jmesserly): we should show the name "print" in there somewhere. |
| 27 assert.throws(() => dart.dcall(core.print, 1, 2, 3), |
| 28 new RegExp('NoSuchMethodError.*\n' + |
| 29 "Receiver: Instance of '\\(Object\\) -> void'", 'm'), |
| 30 'Calls with incorrect argument types should throw a NoSuchMethodError' + |
| 31 ' with correct target'); |
| 32 }); |
| 33 |
27 test('can throw number', () => { | 34 test('can throw number', () => { |
28 try { | 35 try { |
29 dart.throw(42); | 36 dart.throw(42); |
30 } catch (e) { | 37 } catch (e) { |
31 assert.equal(e, 42); | 38 assert.equal(e, 42); |
32 } | 39 } |
33 }); | 40 }); |
34 | 41 |
35 test('argument count cannot change', () => { | 42 test('argument count cannot change', () => { |
36 let SomeType = generic(function(x) { return {x: x}; }); | 43 let SomeType = generic(function(x) { return {x: x}; }); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 count = 0; | 88 count = 0; |
82 | 89 |
83 // Nothing has been stored on the object | 90 // Nothing has been stored on the object |
84 assert.strictEqual(Object.keys(t1).length, 0); | 91 assert.strictEqual(Object.keys(t1).length, 0); |
85 assert.strictEqual(Object.keys(t2).length, 0); | 92 assert.strictEqual(Object.keys(t2).length, 0); |
86 }); | 93 }); |
87 | 94 |
88 test('type constructor is reflectable', () => { | 95 test('type constructor is reflectable', () => { |
89 let SomeType = generic(function(x, y) { return Object.create(null); }); | 96 let SomeType = generic(function(x, y) { return Object.create(null); }); |
90 let someValue = SomeType('hi', 123); | 97 let someValue = SomeType('hi', 123); |
91 assert.equal(classes.getGenericClass(someValue), SomeType); | 98 assert.equal(dart.getGenericClass(someValue), SomeType); |
92 assert.deepEqual(classes.getGenericArgs(someValue), ['hi', 123]); | 99 assert.deepEqual(dart.getGenericArgs(someValue), ['hi', 123]); |
93 }); | 100 }); |
94 | 101 |
95 test('proper type constructor is called', () => { | 102 test('proper type constructor is called', () => { |
96 // This tests https://github.com/dart-lang/dev_compiler/issues/178 | 103 // This tests https://github.com/dart-lang/dev_compiler/issues/178 |
97 let l = dart.list([1, 2, 3], core.int); | 104 let l = dart.list([1, 2, 3], core.int); |
98 let s = l[dartx.join](); | 105 let s = l[dartx.join](); |
99 assert.equal(s, '123'); | 106 assert.equal(s, '123'); |
100 }); | 107 }); |
101 }); | 108 }); |
102 | 109 |
103 | 110 |
104 suite('instanceOf', () => { | 111 suite('instanceOf', () => { |
105 "use strict"; | 112 "use strict"; |
106 | 113 |
107 let expect = assert.equal; | 114 let expect = assert.equal; |
108 let isGroundType = types.isGroundType; | 115 let isGroundType = dart.isGroundType; |
109 let generic = dart.generic; | 116 let generic = dart.generic; |
110 let intIsNonNullable = false; | 117 let intIsNonNullable = false; |
111 let cast = dart.as; | 118 let cast = dart.as; |
112 let instanceOf = dart.is; | 119 let instanceOf = dart.is; |
113 let strongInstanceOf = dart.strongInstanceOf; | 120 let strongInstanceOf = dart.strongInstanceOf; |
114 let runtimeType = dart.realRuntimeType; | 121 let runtimeType = dart.realRuntimeType; |
115 let functionType = dart.functionType; | 122 let functionType = dart.functionType; |
116 let typedef = dart.typedef; | 123 let typedef = dart.typedef; |
117 let isSubtype = types.isSubtype; | 124 let isSubtype = dart.isSubtype; |
118 | 125 |
119 let Object = core.Object; | 126 let Object = core.Object; |
120 let String = core.String; | 127 let String = core.String; |
121 let dynamic = dart.dynamic; | 128 let dynamic = dart.dynamic; |
122 let List = core.List; | 129 let List = core.List; |
123 let Map = core.Map; | 130 let Map = core.Map; |
124 let Map$ = core.Map$; | 131 let Map$ = core.Map$; |
125 let int = core.int; | 132 let int = core.int; |
126 let num = core.num; | 133 let num = core.num; |
127 let bool = core.bool; | 134 let bool = core.bool; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 | 198 |
192 let cls8 = | 199 let cls8 = |
193 dart.fn((b, s, o) => { return null; }, B, [B, String], {p: Object}); | 200 dart.fn((b, s, o) => { return null; }, B, [B, String], {p: Object}); |
194 | 201 |
195 function checkType(x, type, expectedTrue, strongOnly) { | 202 function checkType(x, type, expectedTrue, strongOnly) { |
196 if (expectedTrue === undefined) expectedTrue = true; | 203 if (expectedTrue === undefined) expectedTrue = true; |
197 if (strongOnly == undefined) strongOnly = false; | 204 if (strongOnly == undefined) strongOnly = false; |
198 if (!strongOnly) { | 205 if (!strongOnly) { |
199 expect(instanceOf(x, type), expectedTrue); | 206 expect(instanceOf(x, type), expectedTrue); |
200 } else { | 207 } else { |
201 assert.throws(() => instanceOf(x, type), dart_utils.StrongModeError); | 208 assert.throws(() => instanceOf(x, type), dart.StrongModeError); |
202 expect(strongInstanceOf(x, type), expectedTrue); | 209 expect(strongInstanceOf(x, type), expectedTrue); |
203 } | 210 } |
204 } | 211 } |
205 | 212 |
206 test('int', () => { | 213 test('int', () => { |
207 expect(isGroundType(int), true); | 214 expect(isGroundType(int), true); |
208 expect(isGroundType(runtimeType(5)), true); | 215 expect(isGroundType(runtimeType(5)), true); |
209 | 216 |
210 checkType(5, int); | 217 checkType(5, int); |
211 checkType(5, dynamic); | 218 checkType(5, dynamic); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 | 307 |
301 // null is! Map | 308 // null is! Map |
302 checkType(null, Map, false); | 309 checkType(null, Map, false); |
303 | 310 |
304 // Raw generic types | 311 // Raw generic types |
305 checkType(m5, Map); | 312 checkType(m5, Map); |
306 checkType(m4, Map); | 313 checkType(m4, Map); |
307 | 314 |
308 // Is checks | 315 // Is checks |
309 assert.throws(() => dart.is(m3, Map$(String, String)), | 316 assert.throws(() => dart.is(m3, Map$(String, String)), |
310 dart_utils.StrongModeError); | 317 dart.StrongModeError); |
311 assert.throws(() => dart.is(m6, Map$(String, String)), | 318 assert.throws(() => dart.is(m6, Map$(String, String)), |
312 dart_utils.StrongModeError); | 319 dart.StrongModeError); |
313 assert.isTrue(dart.is(m1, Map$(String, String))); | 320 assert.isTrue(dart.is(m1, Map$(String, String))); |
314 assert.throws(() => dart.is(m2, Map$(String, String)), | 321 assert.throws(() => dart.is(m2, Map$(String, String)), |
315 dart_utils.StrongModeError); | 322 dart.StrongModeError); |
316 | 323 |
317 // As checks | 324 // As checks |
318 // TODO(vsm): Enable these. We're currently only logging warnings on | 325 // TODO(vsm): Enable these. We're currently only logging warnings on |
319 // StrongModeErrors. | 326 // StrongModeErrors. |
320 // assert.throws(() => dart.as(m3, Map$(String, String)), | 327 // assert.throws(() => dart.as(m3, Map$(String, String)), |
321 // dart_utils.StrongModeError); | 328 // dart.StrongModeError); |
322 // assert.throws(() => dart.as(m6, Map$(String, String)), | 329 // assert.throws(() => dart.as(m6, Map$(String, String)), |
323 // dart_utils.StrongModeError); | 330 // dart.StrongModeError); |
324 assert.equal(dart.as(m1, Map$(String, String)), m1); | 331 assert.equal(dart.as(m1, Map$(String, String)), m1); |
325 // assert.throws(() => dart.as(m2, Map$(String, String)), | 332 // assert.throws(() => dart.as(m2, Map$(String, String)), |
326 // dart_utils.StrongModeError); | 333 // dart.StrongModeError); |
327 }); | 334 }); |
328 | 335 |
329 test('constructors', () => { | 336 test('constructors', () => { |
330 class C extends core.Object { | 337 class C extends core.Object { |
331 C(x) {}; | 338 C(x) {}; |
332 named(x, y) {}; | 339 named(x, y) {}; |
333 } | 340 } |
334 dart.defineNamedConstructor(C, 'named'); | 341 dart.defineNamedConstructor(C, 'named'); |
335 dart.setSignature(C, { | 342 dart.setSignature(C, { |
336 constructors: () => ({ | 343 constructors: () => ({ |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 | 784 |
778 suite('primitives', function() { | 785 suite('primitives', function() { |
779 'use strict'; | 786 'use strict'; |
780 | 787 |
781 test('fixed length list', () => { | 788 test('fixed length list', () => { |
782 let list = new core.List(10); | 789 let list = new core.List(10); |
783 list[0] = 42; | 790 list[0] = 42; |
784 assert.throws(() => list.add(42)); | 791 assert.throws(() => list.add(42)); |
785 }); | 792 }); |
786 }); | 793 }); |
OLD | NEW |