OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library jsTest; | |
6 | |
7 import 'dart:js'; | |
8 | |
9 // TODO(jmesserly): get this from a package. | |
10 import 'dom.dart'; | |
11 | |
12 // TODO(jmesserly): use the real package:test. It's not possible yet because | |
13 // it depends on async/await which we don't support. | |
14 // | |
15 // import 'package:test/test.dart'; | |
vsm
2015/06/24 23:16:15
Do the tests here run yet?
Are these tests the s
Jennifer Messerly
2015/06/25 20:11:27
basically, yes & yes
I may have removed some comm
| |
16 | |
17 class Foo { | |
18 final JsObject _proxy; | |
19 | |
20 Foo(num a) : this._proxy = new JsObject(context['Foo'], [a]); | |
21 | |
22 JsObject toJs() => _proxy; | |
23 | |
24 num get a => _proxy['a']; | |
25 num bar() => _proxy.callMethod('bar'); | |
26 } | |
27 | |
28 class Color { | |
29 static final RED = new Color._("red"); | |
30 static final BLUE = new Color._("blue"); | |
31 String _value; | |
32 Color._(this._value); | |
33 String toJs() => this._value; | |
34 } | |
35 | |
36 class TestDartObject {} | |
37 | |
38 class Callable { | |
39 call() => 'called'; | |
40 } | |
41 | |
42 main() { | |
43 group('identity', () { | |
44 | |
45 test('context instances should be identical', () { | |
46 var c1 = context; | |
47 var c2 = context; | |
48 expect(identical(c1, c2), true); | |
49 }); | |
50 | |
51 // TODO(jacobr): switch from equals to identical when dartium supports | |
52 // maintaining proxy equality. | |
53 test('identical JS functions should have equal proxies', () { | |
54 var f1 = context['Object']; | |
55 var f2 = context['Object']; | |
56 expect(f1, equals(f2)); | |
57 }); | |
58 | |
59 // TODO(justinfagnani): old tests duplicate checks above, remove | |
60 // on test next cleanup pass | |
61 test('test proxy equality', () { | |
62 var foo1 = new JsObject(context['Foo'], [1]); | |
63 var foo2 = new JsObject(context['Foo'], [2]); | |
64 context['foo1'] = foo1; | |
65 context['foo2'] = foo2; | |
66 expect(foo1, isNot(context['foo2'])); | |
67 expect(foo2, context['foo2']); | |
68 context.deleteProperty('foo1'); | |
69 context.deleteProperty('foo2'); | |
70 }); | |
71 | |
72 test('retrieve same dart Object', () { | |
73 final obj = new Object(); | |
74 context['obj'] = obj; | |
75 expect(context['obj'], same(obj)); | |
76 context.deleteProperty('obj'); | |
77 }); | |
78 | |
79 group('caching', () { | |
80 test('JS->Dart', () { | |
81 // Test that we are not pulling cached proxy from the prototype | |
82 // when asking for a proxy for the object. | |
83 final proto = context['someProto']; | |
84 expect(proto['role'], 'proto'); | |
85 final obj = context['someObject']; | |
86 expect(obj['role'], 'object'); | |
87 }); | |
88 }); | |
89 | |
90 }); | |
91 | |
92 group('context', () { | |
93 | |
94 test('read global field', () { | |
95 expect(context['x'], 42); | |
96 expect(context['y'], null); | |
97 }); | |
98 | |
99 test('read global field with underscore', () { | |
100 expect(context['_x'], 123); | |
101 expect(context['y'], null); | |
102 }); | |
103 | |
104 test('write global field', () { | |
105 context['y'] = 42; | |
106 expect(context['y'], 42); | |
107 }); | |
108 | |
109 }); | |
110 | |
111 group('new JsObject()', () { | |
112 | |
113 test('new Foo()', () { | |
114 var foo = new JsObject(context['Foo'], [42]); | |
115 expect(foo['a'], 42); | |
116 expect(foo.callMethod('bar'), 42); | |
117 expect(() => foo.callMethod('baz'), throwsA(isNoSuchMethodError)); | |
118 }); | |
119 | |
120 test('new container.Foo()', () { | |
121 final Foo2 = context['container']['Foo']; | |
122 final foo = new JsObject(Foo2, [42]); | |
123 expect(foo['a'], 42); | |
124 expect(Foo2['b'], 38); | |
125 }); | |
126 | |
127 test('new Array()', () { | |
128 var a = new JsObject(context['Array']); | |
129 expect(a, (a) => a is JsArray); | |
130 | |
131 // Test that the object still behaves via the base JsObject interface. | |
132 // JsArray specific tests are below. | |
133 expect(a['length'], 0); | |
134 | |
135 a.callMethod('push', ["value 1"]); | |
136 expect(a['length'], 1); | |
137 expect(a[0], "value 1"); | |
138 | |
139 a.callMethod('pop'); | |
140 expect(a['length'], 0); | |
141 }); | |
142 | |
143 test('new Date()', () { | |
144 final a = new JsObject(context['Date']); | |
145 expect(a.callMethod('getTime'), isNotNull); | |
146 }); | |
147 | |
148 test('new Date(12345678)', () { | |
149 final a = new JsObject(context['Date'], [12345678]); | |
150 expect(a.callMethod('getTime'), 12345678); | |
151 }); | |
152 | |
153 test('new Date("December 17, 1995 03:24:00 GMT")', () { | |
154 final a = new JsObject(context['Date'], | |
155 ["December 17, 1995 03:24:00 GMT"]); | |
156 expect(a.callMethod('getTime'), 819170640000); | |
157 }); | |
158 | |
159 test('new Date(1995,11,17)', () { | |
160 // Note: JS Date counts months from 0 while Dart counts from 1. | |
161 final a = new JsObject(context['Date'], [1995, 11, 17]); | |
162 final b = new DateTime(1995, 12, 17); | |
163 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); | |
164 }); | |
165 | |
166 test('new Date(1995,11,17,3,24,0)', () { | |
167 // Note: JS Date counts months from 0 while Dart counts from 1. | |
168 final a = new JsObject(context['Date'], | |
169 [1995, 11, 17, 3, 24, 0]); | |
170 final b = new DateTime(1995, 12, 17, 3, 24, 0); | |
171 expect(a.callMethod('getTime'), b.millisecondsSinceEpoch); | |
172 }); | |
173 | |
174 test('new Object()', () { | |
175 final a = new JsObject(context['Object']); | |
176 expect(a, isNotNull); | |
177 | |
178 a['attr'] = "value"; | |
179 expect(a['attr'], "value"); | |
180 }); | |
181 | |
182 test(r'new RegExp("^\w+$")', () { | |
183 final a = new JsObject(context['RegExp'], [r'^\w+$']); | |
184 expect(a, isNotNull); | |
185 expect(a.callMethod('test', ['true']), true); | |
186 expect(a.callMethod('test', [' false']), false); | |
187 }); | |
188 | |
189 test('js instantiation via map notation : new Array()', () { | |
190 final a = new JsObject(context['Array']); | |
191 expect(a, isNotNull); | |
192 expect(a['length'], 0); | |
193 | |
194 a.callMethod('push', ["value 1"]); | |
195 expect(a['length'], 1); | |
196 expect(a[0], "value 1"); | |
197 | |
198 a.callMethod('pop'); | |
199 expect(a['length'], 0); | |
200 }); | |
201 | |
202 test('js instantiation via map notation : new Date()', () { | |
203 final a = new JsObject(context['Date']); | |
204 expect(a.callMethod('getTime'), isNotNull); | |
205 }); | |
206 | |
207 test('>10 parameters', () { | |
208 final o = new JsObject(context['Baz'], [1,2,3,4,5,6,7,8,9,10,11]); | |
209 for (var i = 1; i <= 11; i++) { | |
210 expect(o["f$i"], i); | |
211 } | |
212 expect(o['constructor'], context['Baz']); | |
213 }); | |
214 }); | |
215 | |
216 group('JsFunction and callMethod', () { | |
217 | |
218 test('new JsObject can return a JsFunction', () { | |
219 var f = new JsObject(context['Function']); | |
220 expect(f, (a) => a is JsFunction); | |
221 }); | |
222 | |
223 test('JsFunction.apply on a function defined in JS', () { | |
224 expect(context['razzle'].apply([]), 42); | |
225 }); | |
226 | |
227 test('JsFunction.apply on a function that uses "this"', () { | |
228 var object = new Object(); | |
229 expect(context['returnThis'].apply([], thisArg: object), same(object)); | |
230 }); | |
231 | |
232 test('JsObject.callMethod on a function defined in JS', () { | |
233 expect(context.callMethod('razzle'), 42); | |
234 expect(() => context.callMethod('dazzle'), throwsA(isNoSuchMethodError)); | |
235 }); | |
236 | |
237 test('callMethod with many arguments', () { | |
238 expect(context.callMethod('varArgs', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), | |
239 55); | |
240 }); | |
241 | |
242 test('access a property of a function', () { | |
243 expect(context.callMethod('Bar'), "ret_value"); | |
244 expect(context['Bar']['foo'], "property_value"); | |
245 }); | |
246 | |
247 }); | |
248 | |
249 group('JsArray', () { | |
250 | |
251 test('new JsArray()', () { | |
252 var array = new JsArray(); | |
253 var arrayType = context['Array']; | |
254 expect(array.instanceof(arrayType), true); | |
255 expect(array, []); | |
256 // basic check that it behaves like a List | |
257 array.addAll([1, 2, 3]); | |
258 expect(array, [1, 2, 3]); | |
259 }); | |
260 | |
261 test('new JsArray.from()', () { | |
262 var array = new JsArray.from([1, 2, 3]); | |
263 var arrayType = context['Array']; | |
264 expect(array.instanceof(arrayType), true); | |
265 expect(array, [1, 2, 3]); | |
266 }); | |
267 | |
268 test('get Array from JS', () { | |
269 context['a'] = new JsObject(context['Array'], [1, 2, 3]); | |
270 expect(context.callMethod('isPropertyInstanceOf', | |
271 ['a', context['Array']]), true); | |
272 var a = context['a']; | |
273 expect(a, (a) => a is JsArray); | |
274 expect(a, [1, 2, 3]); | |
275 context.deleteProperty('a'); | |
276 }); | |
277 | |
278 test('pass Array to JS', () { | |
279 context['a'] = [1, 2, 3]; | |
280 expect(context.callMethod('isPropertyInstanceOf', | |
281 ['a', context['Array']]), false); | |
282 var a = context['a']; | |
283 expect(a, (a) => a is List); | |
284 expect(a, isNot((a) => a is JsArray)); | |
285 expect(a, [1, 2, 3]); | |
286 context.deleteProperty('a'); | |
287 }); | |
288 | |
289 test('[]', () { | |
290 var array = new JsArray.from([1, 2]); | |
291 expect(array[0], 1); | |
292 expect(array[1], 2); | |
293 expect(() => array[-1], throwsA(isRangeError)); | |
294 expect(() => array[2], throwsA(isRangeError)); | |
295 }); | |
296 | |
297 test('[]=', () { | |
298 var array = new JsArray.from([1, 2]); | |
299 array[0] = 'd'; | |
300 array[1] = 'e'; | |
301 expect(array, ['d', 'e']); | |
302 expect(() => array[-1] = 3, throwsA(isRangeError)); | |
303 expect(() => array[2] = 3, throwsA(isRangeError)); | |
304 }); | |
305 | |
306 test('length', () { | |
307 var array = new JsArray.from([1, 2, 3]); | |
308 expect(array.length, 3); | |
309 array.add(4); | |
310 expect(array.length, 4); | |
311 array.length = 2; | |
312 expect(array, [1, 2]); | |
313 array.length = 3; | |
314 expect(array, [1, 2, null]); | |
315 }); | |
316 | |
317 test('add', () { | |
318 var array = new JsArray(); | |
319 array.add('a'); | |
320 expect(array, ['a']); | |
321 array.add('b'); | |
322 expect(array, ['a', 'b']); | |
323 }); | |
324 | |
325 test('addAll', () { | |
326 var array = new JsArray(); | |
327 array.addAll(['a', 'b']); | |
328 expect(array, ['a', 'b']); | |
329 // make sure addAll can handle Iterables | |
330 array.addAll(new Set.from(['c'])); | |
331 expect(array, ['a', 'b', 'c']); | |
332 }); | |
333 | |
334 test('insert', () { | |
335 var array = new JsArray.from([]); | |
336 array.insert(0, 'b'); | |
337 expect(array, ['b']); | |
338 array.insert(0, 'a'); | |
339 expect(array, ['a', 'b']); | |
340 array.insert(2, 'c'); | |
341 expect(array, ['a', 'b', 'c']); | |
342 expect(() => array.insert(4, 'e'), throwsA(isRangeError)); | |
343 expect(() => array.insert(-1, 'e'), throwsA(isRangeError)); | |
344 }); | |
345 | |
346 test('removeAt', () { | |
347 var array = new JsArray.from(['a', 'b', 'c']); | |
348 expect(array.removeAt(1), 'b'); | |
349 expect(array, ['a', 'c']); | |
350 expect(() => array.removeAt(2), throwsA(isRangeError)); | |
351 expect(() => array.removeAt(-1), throwsA(isRangeError)); | |
352 }); | |
353 | |
354 test('removeLast', () { | |
355 var array = new JsArray.from(['a', 'b', 'c']); | |
356 expect(array.removeLast(), 'c'); | |
357 expect(array, ['a', 'b']); | |
358 array.length = 0; | |
359 expect(() => array.removeLast(), throwsA(isRangeError)); | |
360 }); | |
361 | |
362 test('removeRange', () { | |
363 var array = new JsArray.from(['a', 'b', 'c', 'd']); | |
364 array.removeRange(1, 3); | |
365 expect(array, ['a', 'd']); | |
366 expect(() => array.removeRange(-1, 2), throwsA(isRangeError)); | |
367 expect(() => array.removeRange(0, 3), throwsA(isRangeError)); | |
368 expect(() => array.removeRange(2, 1), throwsA(isRangeError)); | |
369 }); | |
370 | |
371 test('setRange', () { | |
372 var array = new JsArray.from(['a', 'b', 'c', 'd']); | |
373 array.setRange(1, 3, ['e', 'f']); | |
374 expect(array, ['a', 'e', 'f', 'd']); | |
375 array.setRange(3, 4, ['g', 'h', 'i'], 1); | |
376 expect(array, ['a', 'e', 'f', 'h']); | |
377 }); | |
378 | |
379 test('sort', () { | |
380 var array = new JsArray.from(['c', 'a', 'b']); | |
381 array.sort(); | |
382 expect(array, ['a', 'b', 'c']); | |
383 }); | |
384 | |
385 test('sort with a Comparator', () { | |
386 var array = new JsArray.from(['c', 'a', 'b']); | |
387 array.sort((a, b) => -(a.compareTo(b))); | |
388 expect(array, ['c', 'b', 'a']); | |
389 }); | |
390 | |
391 }); | |
392 | |
393 group('JsObject.fromBrowserObject()', () { | |
394 | |
395 test('Nodes are proxied', () { | |
396 var node = new JsObject.fromBrowserObject(document.createElement('div')); | |
397 context.callMethod('addTestProperty', [node]); | |
398 expect(node is JsObject, true); | |
399 expect(node.instanceof(context['HTMLDivElement']), true); | |
400 expect(node['testProperty'], 'test'); | |
401 }); | |
402 | |
403 test('primitives and null throw ArgumentError', () { | |
404 for (var v in ['a', 1, 2.0, true, null]) { | |
405 expect(() => new JsObject.fromBrowserObject(v), | |
406 throwsA((a) => a is ArgumentError)); | |
407 } | |
408 }); | |
409 | |
410 }); | |
411 | |
412 group('Dart functions', () { | |
413 test('invoke Dart callback from JS', () { | |
414 expect(() => context.callMethod('invokeCallback'), throws); | |
415 | |
416 context['callback'] = () => 42; | |
417 expect(context.callMethod('invokeCallback'), 42); | |
418 | |
419 context.deleteProperty('callback'); | |
420 }); | |
421 | |
422 test('callback as parameter', () { | |
423 expect(context.callMethod('getTypeOf', [context['razzle']]), | |
424 "function"); | |
425 }); | |
426 | |
427 test('invoke Dart callback from JS with this', () { | |
428 // A JavaScript constructor implemented in Dart using 'this' | |
429 final constructor = new JsFunction.withThis(($this, arg1) { | |
430 $this['a'] = 42; | |
431 }); | |
432 var o = new JsObject(constructor, ["b"]); | |
433 expect(o['a'], 42); | |
434 }); | |
435 | |
436 test('invoke Dart callback from JS with 11 parameters', () { | |
437 context['callbackWith11params'] = (p1, p2, p3, p4, p5, p6, p7, | |
438 p8, p9, p10, p11) => '$p1$p2$p3$p4$p5$p6$p7$p8$p9$p10$p11'; | |
439 expect(context.callMethod('invokeCallbackWith11params'), | |
440 '1234567891011'); | |
441 }); | |
442 | |
443 test('return a JS proxy to JavaScript', () { | |
444 var result = context.callMethod('testJsMap', | |
445 [() => new JsObject.jsify({'value': 42})]); | |
446 expect(result, 42); | |
447 }); | |
448 | |
449 test('emulated functions should be callable in JS', () { | |
450 context['callable'] = new Callable(); | |
451 var result = context.callMethod('callable'); | |
452 expect(result, 'called'); | |
453 context.deleteProperty('callable'); | |
454 }, skip: "https://github.com/dart-lang/dev_compiler/issues/244"); | |
455 | |
456 }); | |
457 | |
458 group('JsObject.jsify()', () { | |
459 | |
460 test('convert a List', () { | |
461 final list = [1, 2, 3, 4, 5, 6, 7, 8]; | |
462 var array = new JsObject.jsify(list); | |
463 expect(context.callMethod('isArray', [array]), true); | |
464 expect(array['length'], list.length); | |
465 for (var i = 0; i < list.length ; i++) { | |
466 expect(array[i], list[i]); | |
467 } | |
468 }); | |
469 | |
470 test('convert an Iterable', () { | |
471 final set = new Set.from([1, 2, 3, 4, 5, 6, 7, 8]); | |
472 var array = new JsObject.jsify(set); | |
473 expect(context.callMethod('isArray', [array]), true); | |
474 expect(array['length'], set.length); | |
475 for (var i = 0; i < array['length'] ; i++) { | |
476 expect(set.contains(array[i]), true); | |
477 } | |
478 }); | |
479 | |
480 test('convert a Map', () { | |
481 var map = {'a': 1, 'b': 2, 'c': 3}; | |
482 var jsMap = new JsObject.jsify(map); | |
483 expect(!context.callMethod('isArray', [jsMap]), true); | |
484 for (final key in map.keys) { | |
485 expect(context.callMethod('checkMap', [jsMap, key, map[key]]), true); | |
486 } | |
487 }); | |
488 | |
489 test('deep convert a complex object', () { | |
490 final object = { | |
491 'a': [1, [2, 3]], | |
492 'b': { | |
493 'c': 3, | |
494 'd': new JsObject(context['Foo'], [42]) | |
495 }, | |
496 'e': null | |
497 }; | |
498 var jsObject = new JsObject.jsify(object); | |
499 expect(jsObject['a'][0], object['a'][0]); | |
500 expect(jsObject['a'][1][0], object['a'][1][0]); | |
501 expect(jsObject['a'][1][1], object['a'][1][1]); | |
502 expect(jsObject['b']['c'], object['b']['c']); | |
503 expect(jsObject['b']['d'], object['b']['d']); | |
504 expect(jsObject['b']['d'].callMethod('bar'), 42); | |
505 expect(jsObject['e'], null); | |
506 }); | |
507 | |
508 test('throws if object is not a Map or Iterable', () { | |
509 expect(() => new JsObject.jsify('a'), | |
510 throwsA((a) => a is ArgumentError)); | |
511 }); | |
512 }); | |
513 | |
514 group('JsObject methods', () { | |
515 | |
516 test('hashCode and ==', () { | |
517 final o1 = context['Object']; | |
518 final o2 = context['Object']; | |
519 expect(o1 == o2, true); | |
520 expect(o1.hashCode == o2.hashCode, true); | |
521 final d = context['document']; | |
522 expect(o1 == d, false); | |
523 }); | |
524 | |
525 test('toString', () { | |
526 var foo = new JsObject(context['Foo'], [42]); | |
527 expect(foo.toString(), "I'm a Foo a=42"); | |
528 var container = context['container']; | |
529 expect(container.toString(), "[object Object]"); | |
530 }); | |
531 | |
532 test('toString returns a String even if the JS object does not', () { | |
533 var foo = new JsObject(context['Liar']); | |
534 expect(foo.callMethod('toString'), 1); | |
535 expect(foo.toString(), '1'); | |
536 }); | |
537 | |
538 test('instanceof', () { | |
539 var foo = new JsObject(context['Foo'], [1]); | |
540 expect(foo.instanceof(context['Foo']), true); | |
541 expect(foo.instanceof(context['Object']), true); | |
542 expect(foo.instanceof(context['String']), false); | |
543 }); | |
544 | |
545 test('deleteProperty', () { | |
546 var object = new JsObject.jsify({}); | |
547 object['a'] = 1; | |
548 expect(context['Object'].callMethod('keys', [object])['length'], 1); | |
549 expect(context['Object'].callMethod('keys', [object])[0], "a"); | |
550 object.deleteProperty("a"); | |
551 expect(context['Object'].callMethod('keys', [object])['length'], 0); | |
552 }); | |
553 | |
554 test('hasProperty', () { | |
555 var object = new JsObject.jsify({}); | |
556 object['a'] = 1; | |
557 expect(object.hasProperty('a'), true); | |
558 expect(object.hasProperty('b'), false); | |
559 }); | |
560 | |
561 test('[] and []=', () { | |
562 final myArray = context['myArray']; | |
563 expect(myArray['length'], 1); | |
564 expect(myArray[0], "value1"); | |
565 myArray[0] = "value2"; | |
566 expect(myArray['length'], 1); | |
567 expect(myArray[0], "value2"); | |
568 | |
569 final foo = new JsObject(context['Foo'], [1]); | |
570 foo["getAge"] = () => 10; | |
571 expect(foo.callMethod('getAge'), 10); | |
572 }); | |
573 | |
574 }); | |
575 | |
576 group('transferrables', () { | |
577 | |
578 group('JS->Dart', () { | |
579 | |
580 test('DateTime', () { | |
581 var date = context.callMethod('getNewDate'); | |
582 expect(date is DateTime, true); | |
583 }); | |
584 | |
585 test('window', () { | |
586 expect(context['window'] is Window, true); | |
587 }); | |
588 | |
589 test('foreign browser objects should be proxied', () { | |
590 var iframe = document.createElement('iframe'); | |
591 document.body.appendChild(iframe); | |
592 var proxy = new JsObject.fromBrowserObject(iframe); | |
593 | |
594 // Window | |
595 var contentWindow = proxy['contentWindow']; | |
596 expect(contentWindow, isNot((a) => a is Window)); | |
597 expect(contentWindow, (a) => a is JsObject); | |
598 | |
599 // Node | |
600 var foreignDoc = contentWindow['document']; | |
601 expect(foreignDoc, isNot((a) => a is Node)); | |
602 expect(foreignDoc, (a) => a is JsObject); | |
603 | |
604 // Event | |
605 var clicked = false; | |
606 foreignDoc['onclick'] = (e) { | |
607 expect(e, isNot((a) => a is Event)); | |
608 expect(e, (a) => a is JsObject); | |
609 clicked = true; | |
610 }; | |
611 | |
612 context.callMethod('fireClickEvent', [contentWindow]); | |
613 expect(clicked, true); | |
614 }); | |
615 | |
616 test('document', () { | |
617 expect(context['document'] is Document, true); | |
618 }); | |
619 | |
620 test('Blob', () { | |
621 var blob = context.callMethod('getNewBlob'); | |
622 expect(blob is Blob, true); | |
623 expect(blob.type, 'text/html'); | |
624 }); | |
625 | |
626 test('unattached DivElement', () { | |
627 var node = context.callMethod('getNewDivElement'); | |
628 expect(node is DivElement, true); | |
629 }); | |
630 | |
631 test('Event', () { | |
632 var event = context.callMethod('getNewEvent'); | |
633 expect(event is Event, true); | |
634 }); | |
635 | |
636 test('ImageData', () { | |
637 var node = context.callMethod('getNewImageData'); | |
638 expect(node is ImageData, true); | |
639 }); | |
640 | |
641 }); | |
642 | |
643 group('Dart->JS', () { | |
644 | |
645 test('Date', () { | |
646 context['o'] = new DateTime(1995, 12, 17); | |
647 var dateType = context['Date']; | |
648 expect(context.callMethod('isPropertyInstanceOf', ['o', dateType]), | |
649 true); | |
650 context.deleteProperty('o'); | |
651 }); | |
652 | |
653 test('window', () { | |
654 context['o'] = window; | |
655 var windowType = context['Window']; | |
656 expect(context.callMethod('isPropertyInstanceOf', ['o', windowType]), | |
657 true); | |
658 context.deleteProperty('o'); | |
659 }); | |
660 | |
661 test('document', () { | |
662 context['o'] = document; | |
663 var documentType = context['Document']; | |
664 expect(context.callMethod('isPropertyInstanceOf', ['o', documentType]), | |
665 true); | |
666 context.deleteProperty('o'); | |
667 }); | |
668 | |
669 test('Blob', () { | |
670 var fileParts = ['<a id="a"><b id="b">hey!</b></a>']; | |
671 context['o'] = new Blob(fileParts, type: 'text/html'); | |
672 var blobType = context['Blob']; | |
673 expect(context.callMethod('isPropertyInstanceOf', ['o', blobType]), | |
674 true); | |
675 context.deleteProperty('o'); | |
676 }); | |
677 | |
678 test('unattached DivElement', () { | |
679 context['o'] = document.createElement('div'); | |
680 var divType = context['HTMLDivElement']; | |
681 expect(context.callMethod('isPropertyInstanceOf', ['o', divType]), | |
682 true); | |
683 context.deleteProperty('o'); | |
684 }); | |
685 | |
686 test('Event', () { | |
687 context['o'] = new CustomEvent('test'); | |
688 var eventType = context['Event']; | |
689 expect(context.callMethod('isPropertyInstanceOf', ['o', eventType]), | |
690 true); | |
691 context.deleteProperty('o'); | |
692 }); | |
693 | |
694 // this test fails in IE9 for very weird, but unknown, reasons | |
695 // the expression context['ImageData'] fails if useHtmlConfiguration() | |
696 // is called, or if the other tests in this file are enabled | |
697 test('ImageData', () { | |
698 CanvasElement canvas = document.createElement('canvas'); | |
699 var ctx = canvas.getContext('2d') as CanvasRenderingContext2D; | |
700 context['o'] = ctx.createImageData(1, 1); | |
701 var imageDataType = context['ImageData']; | |
702 expect(context.callMethod('isPropertyInstanceOf', ['o', imageDataType]), | |
703 true); | |
704 context.deleteProperty('o'); | |
705 }); | |
706 | |
707 }); | |
708 }); | |
709 | |
710 } | |
711 | |
712 final console = (window as dynamic).console; | |
vsm
2015/06/24 23:16:15
Move the code below to a separate 'unittest' file?
Jennifer Messerly
2015/06/25 20:11:27
sure. I hope it doesn't stick around long, though.
| |
713 | |
714 void group(String name, void body()) { | |
715 console.group(name); | |
716 body(); | |
717 console.groupEnd(name); | |
718 } | |
719 | |
720 void test(String name, void body(), {String skip}) { | |
721 if (skip != null) { | |
722 console.warn('SKIP $name: $skip'); | |
723 return; | |
724 } | |
725 console.log(name); | |
726 try { | |
727 body(); | |
728 } catch(e) { | |
729 console.error(e); | |
730 } | |
731 } | |
732 | |
733 void expect(Object actual, matcher) { | |
734 if (matcher is! Matcher) matcher = equals(matcher); | |
735 if (!matcher(actual)) { | |
736 throw 'Expect failed to match $actual with $matcher'; | |
737 } | |
738 } | |
739 | |
740 Matcher equals(Object expected) { | |
741 return (actual) { | |
742 if (expected is List && actual is List) { | |
743 int len = expected.length; | |
744 if (len != actual.length) return false; | |
745 for (int i = 0; i < len; i++) { | |
746 if (!equals(expected[i])(actual[i])) return false; | |
747 } | |
748 return true; | |
749 } else { | |
750 return expected == actual; | |
751 } | |
752 }; | |
753 } | |
754 | |
755 Matcher same(Object expected) => (actual) => identical(expected, actual); | |
756 Matcher isNot(matcher) { | |
757 if (matcher is! Matcher) matcher = equals(matcher); | |
758 return (actual) => !matcher(actual); | |
759 } | |
760 | |
761 bool isNull(actual) => actual == null; | |
762 final Matcher isNotNull = isNot(isNull); | |
763 bool isRangeError(actual) => actual is RangeError; | |
764 bool isNoSuchMethodError(actual) => actual is NoSuchMethodError; | |
765 | |
766 Matcher throwsA(matcher) { | |
767 if (matcher is! Matcher) matcher = equals(matcher); | |
768 return (actual) { | |
769 try { | |
770 actual(); | |
771 return false; | |
772 } catch(e) { | |
773 return matcher(e); | |
774 } | |
775 }; | |
776 } | |
777 | |
778 final Matcher throws = throwsA((a) => true); | |
779 | |
780 typedef Matcher(actual); | |
OLD | NEW |