OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library jsArrayTest; | |
6 | |
7 import 'dart:html'; | |
8 import 'dart:js'; | |
9 | |
10 import 'package:unittest/unittest.dart'; | |
11 import 'package:unittest/html_config.dart'; | |
12 | |
13 _injectJs() { | |
14 document.body.append(new ScriptElement() | |
15 ..type = 'text/javascript' | |
16 ..innerHtml = r""" | |
17 function callJsMethod(jsObj, jsMethodName, args) { | |
18 return jsObj[jsMethodName].apply(jsObj, args); | |
19 } | |
20 | |
21 function jsEnumerateIndices(obj) { | |
22 var ret = []; | |
23 for(var i in obj) { | |
24 ret.push(i); | |
25 } | |
26 return ret; | |
27 } | |
28 | |
29 function setValue(obj, index, value) { | |
30 return obj[index] = value; | |
31 } | |
32 | |
33 function getValue(obj, index) { | |
34 return obj[index]; | |
35 } | |
36 | |
37 function checkIsArray(obj) { | |
38 return Array.isArray(obj); | |
39 } | |
40 | |
41 function concatValues(obj) { | |
42 return obj.concat("a", "b", ["c", "d"], 42, {foo: 10}); | |
43 } | |
44 | |
45 function concatOntoArray(obj) { | |
46 return [1,2,3].concat(obj, "foo"); | |
47 } | |
48 | |
49 function repeatedConcatOntoArray(obj) { | |
50 return [1,2,3].concat(obj, obj); | |
51 } | |
52 | |
53 function everyGreaterThanZero(obj) { | |
54 return obj.every(function(currentValue, index, array) { | |
55 return currentValue > 0; | |
56 }); | |
57 } | |
58 | |
59 function everyGreaterThanZeroCheckThisArg(obj) { | |
60 var j = 0; | |
61 return obj.every(function(currentValue, index, array) { | |
62 if (j != index) { | |
63 throw "Unxpected index"; | |
64 } | |
65 j++; | |
66 if (array !== obj) { | |
67 throw "Array argument doesn't match obj"; | |
68 } | |
69 return currentValue > 0; | |
70 }); | |
71 } | |
72 | |
73 function filterGreater42(obj) { | |
74 return obj.filter(function(currentValue, index, array) { | |
75 return currentValue > 42; | |
76 }); | |
77 } | |
78 | |
79 function forEachCollectResult(array, callback) { | |
80 var result = []; | |
81 array.forEach(function(currentValue) { | |
82 result.push(currentValue * 2); | |
83 }); | |
84 return result; | |
85 } | |
86 | |
87 function someEqual42(array, callback) { | |
88 return array.some(function(currentValue) { | |
89 return currentValue == 42; | |
90 }); | |
91 } | |
92 | |
93 function sortNumbersBackwards(array) { | |
94 return array.sort(function(a, b) { | |
95 return b - a; | |
96 }); | |
97 } | |
98 | |
99 function spliceDummyItems(array) { | |
100 return array.splice(1, 2, "quick" ,"brown", "fox"); | |
101 } | |
102 | |
103 function spliceTestStringArgs(array) { | |
104 return array.splice("1.2", "2.01", "quick" ,"brown", "fox"); | |
105 } | |
106 | |
107 function splicePastEnd(array) { | |
108 return array.splice(1, 5332, "quick" ,"brown", "fox"); | |
109 } | |
110 | |
111 function callJsToString(array) { | |
112 return array.toString(); | |
113 } | |
114 | |
115 function mapAddIndexToEachElement(array) { | |
116 return array.map(function(currentValue, index) { | |
117 return currentValue + index; | |
118 }); | |
119 } | |
120 | |
121 function reduceSumDoubledElements(array) { | |
122 return array.reduce(function(previousValue, currentValue) { return previousVal ue + currentValue*2; }, 0); | |
vsm
2015/06/26 15:59:36
line len here and a few places below
| |
123 } | |
124 | |
125 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. | |
126 function reduceRightSumDoubledElements(array) { | |
127 return array.reduceRight(function(previousValue, currentValue) { return previo usValue + currentValue*2; }, 0); | |
128 } | |
129 | |
130 function identical(o1, o2) { | |
131 return o1 === o2; | |
132 } | |
133 | |
134 function getOwnPropertyDescriptorJson(array, property) { | |
135 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); | |
136 } | |
137 | |
138 function setLength(array, len) { | |
139 return array.length = len; | |
140 } | |
141 | |
142 """); | |
143 } | |
144 | |
145 class Foo {} | |
146 | |
147 callJsMethod(List array, String methodName, List args) => | |
148 context.callMethod("callJsMethod", [array, methodName, args]); | |
149 | |
150 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); | |
151 callLastIndexOf(List array, value) => | |
152 callJsMethod(array, "lastIndexOf", [value]); | |
153 | |
154 callPop(List array) => callJsMethod(array, "pop", []); | |
155 callPush(List array, element) => callJsMethod(array, "push", [element]); | |
156 callShift(List array) => callJsMethod(array, "shift", []); | |
157 callReverse(List array) => callJsMethod(array, "reverse", []); | |
158 callSetLength(List array, length) => | |
159 context.callMethod("setLength", [array, length]); | |
160 | |
161 main() { | |
162 _injectJs(); | |
163 useHtmlConfiguration(); | |
164 | |
165 group('indexOf', () { | |
166 var div = new DivElement(); | |
167 var list = [3, 42, "foo", 42, div]; | |
168 test('found', () { | |
169 expect(callIndexOf(list, 3), equals(0)); | |
170 expect(callIndexOf(list, 42), equals(1)); | |
171 expect(callIndexOf(list, "foo"), equals(2)); | |
172 expect(callIndexOf(list, div), equals(4)); | |
173 }); | |
174 | |
175 test('missing', () { | |
176 expect(callIndexOf(list, 31), equals(-1)); | |
177 expect(callIndexOf(list, "42"), equals(-1)); | |
178 expect(callIndexOf(list, null), equals(-1)); | |
179 }); | |
180 }); | |
181 | |
182 group('set length', () { | |
183 test('larger', () { | |
184 var list = ["a", "b", "c", "d"]; | |
185 expect(callSetLength(list, 10), equals(10)); | |
186 expect(list.length, equals(10)); | |
187 expect(list.last, equals(null)); | |
188 expect(list[3], equals("d")); | |
189 }); | |
190 | |
191 test('smaller', () { | |
192 var list = ["a", "b", "c", "d"]; | |
193 expect(callSetLength(list, 2), equals(2)); | |
194 expect(list.first, equals("a")); | |
195 expect(list.last, equals("b")); | |
196 expect(list.length, equals(2)); | |
197 expect(callSetLength(list, 0), equals(0)); | |
198 expect(list.length, equals(0)); | |
199 expect(callSetLength(list, 2), equals(2)); | |
200 expect(list.first, equals(null)); | |
201 }); | |
202 | |
203 test('invalid', () { | |
204 var list = ["a", "b", "c", "d"]; | |
205 expect(() => callSetLength(list, 2.3), throws); | |
206 expect(list.length, equals(4)); | |
207 expect(() => callSetLength(list, -1), throws); | |
208 expect(list.length, equals(4)); | |
209 // Make sure we are coercing to a JS number. | |
210 expect(callSetLength(list, "2"), equals("2")); | |
211 expect(list.length, equals(2)); | |
212 }); | |
213 }); | |
214 | |
215 group('join', () { | |
216 var list = [3, 42, "foo"]; | |
217 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()]; | |
218 test('default', () { | |
219 expect(callJsMethod(list, "join", []), equals("3,42,foo")); | |
220 expect(callJsMethod(listWithDartClasses, "join", []), | |
221 equals("3,Instance of 'Foo',42,foo,Instance of 'Object'")); | |
222 }); | |
223 | |
224 test('custom separator', () { | |
225 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo")); | |
226 }); | |
227 }); | |
228 | |
229 group('lastIndexOf', () { | |
230 var list = [3, 42, "foo", 42]; | |
231 test('found', () { | |
232 expect(callLastIndexOf(list, 3), equals(0)); | |
233 expect(callLastIndexOf(list, 42), equals(3)); | |
234 expect(callLastIndexOf(list, "foo"), equals(2)); | |
235 }); | |
236 | |
237 test('missing', () { | |
238 expect(callLastIndexOf(list, 31), equals(-1)); | |
239 expect(callLastIndexOf(list, "42"), equals(-1)); | |
240 expect(callLastIndexOf(list, null), equals(-1)); | |
241 }); | |
242 }); | |
243 | |
244 group('pop', () { | |
245 test('all', () { | |
246 var foo = new Foo(); | |
247 var div = new DivElement(); | |
248 var list = [3, 42, "foo", foo, div]; | |
249 expect(callPop(list), equals(div)); | |
250 expect(list.length, equals(4)); | |
251 expect(callPop(list), equals(foo)); | |
252 expect(list.length, equals(3)); | |
253 expect(callPop(list), equals("foo")); | |
254 expect(list.length, equals(2)); | |
255 expect(callPop(list), equals(42)); | |
256 expect(list.length, equals(1)); | |
257 expect(callPop(list), equals(3)); | |
258 expect(list.length, equals(0)); | |
259 expect(callPop(list), equals(null)); | |
260 expect(list.length, equals(0)); | |
261 }); | |
262 }); | |
263 | |
264 group('push', () { | |
265 test('strings', () { | |
266 var list = []; | |
267 var div = new DivElement(); | |
268 expect(callPush(list, "foo"), equals(1)); | |
269 expect(callPush(list, "bar"), equals(2)); | |
270 expect(callPush(list, "baz"), equals(3)); | |
271 expect(callPush(list, div), equals(4)); | |
272 expect(list, equals(["foo", "bar", "baz", div])); | |
273 }); | |
274 }); | |
275 | |
276 group('shift', () { | |
277 test('all', () { | |
278 var foo = new Foo(); | |
279 var div = new DivElement(); | |
280 var list = [3, 42, "foo", foo, div]; | |
281 expect(callShift(list), equals(3)); | |
282 expect(list.length, equals(4)); | |
283 expect(callShift(list), equals(42)); | |
284 expect(list.length, equals(3)); | |
285 expect(callShift(list), equals("foo")); | |
286 expect(list.length, equals(2)); | |
287 expect(callShift(list), equals(foo)); | |
288 expect(list.length, equals(1)); | |
289 expect(callShift(list), equals(div)); | |
290 expect(list.length, equals(0)); | |
291 expect(callShift(list), equals(null)); | |
292 expect(list.length, equals(0)); | |
293 }); | |
294 }); | |
295 | |
296 group('reverse', () { | |
297 test('simple', () { | |
298 var foo = new Foo(); | |
299 var div = new DivElement(); | |
300 var list = [div, 42, foo]; | |
301 callReverse(list); | |
302 expect(list, equals([foo, 42, div])); | |
303 list = [3, 42]; | |
304 callReverse(list); | |
305 expect(list, equals([42, 3])); | |
306 }); | |
307 }); | |
308 | |
309 group('slice', () { | |
310 test('copy', () { | |
311 var foo = new Foo(); | |
312 var div = new DivElement(); | |
313 var list = [3, 42, "foo", foo, div]; | |
314 var copy = callJsMethod(list, "slice", []); | |
315 expect(identical(list, copy), isFalse); | |
316 expect(copy.length, equals(list.length)); | |
317 for (var i = 0; i < list.length; i++) { | |
318 expect(list[i], equals(copy[i])); | |
319 } | |
320 expect(identical(list[3], copy[3]), isTrue); | |
321 expect(identical(list[4], copy[4]), isTrue); | |
322 | |
323 copy.add("dummy"); | |
324 expect(list.length + 1, equals(copy.length)); | |
325 }); | |
326 | |
327 test('specify start', () { | |
328 var list = [3, 42, "foo"]; | |
329 var copy = callJsMethod(list, "slice", [1]); | |
330 expect(copy.first, equals(42)); | |
331 }); | |
332 | |
333 test('specify start and end', () { | |
334 var list = [3, 42, 92, "foo"]; | |
335 var copy = callJsMethod(list, "slice", [1, 3]); | |
336 expect(copy.first, equals(42)); | |
337 expect(copy.last, equals(92)); | |
338 }); | |
339 | |
340 test('from end', () { | |
341 var list = [3, 42, 92, "foo"]; | |
342 expect(callJsMethod(list, "slice", [-2]), equals([92, "foo"])); | |
343 | |
344 // Past the end of the front of the array. | |
345 expect(callJsMethod(list, "slice", [-2, 3]), equals([92])); | |
346 | |
347 // Past the end of the front of the array. | |
348 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42])); | |
349 }); | |
350 }); | |
351 | |
352 group("js snippet tests", () { | |
353 test("enumerate indices", () { | |
354 var list = ["a", "b", "c", "d"]; | |
355 var indices = context.callMethod('jsEnumerateIndices', [list]); | |
356 expect(indices.length, equals(4)); | |
357 for (int i = 0; i < 4; i++) { | |
358 expect(indices[i], equals('$i')); | |
359 } | |
360 }); | |
361 | |
362 test("set element", () { | |
363 var list = ["a", "b", "c", "d"]; | |
364 context.callMethod('setValue', [list, 0, 42]); | |
365 expect(list[0], equals(42)); | |
366 context.callMethod('setValue', [list, 1, 84]); | |
367 expect(list[1], equals(84)); | |
368 context.callMethod( | |
369 'setValue', [list, 6, 100]); // Off the end of the list. | |
370 expect(list.length, equals(7)); | |
371 expect(list[4], equals(null)); | |
372 expect(list[6], equals(100)); | |
373 | |
374 // These tests have to be commented out because we don't persist | |
375 // JS proxies for Dart objects like we could/should. | |
376 // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid array index | |
377 // expect(context.callMethod('getValue', [list, -1]), equals("foo")); | |
378 // expect(context.callMethod('getValue', [list, "-1"]), equals("foo")); | |
379 }); | |
380 | |
381 test("get element", () { | |
382 var list = ["a", "b", "c", "d"]; | |
383 expect(context.callMethod('getValue', [list, 0]), equals("a")); | |
384 expect(context.callMethod('getValue', [list, 1]), equals("b")); | |
385 expect(context.callMethod('getValue', [list, 6]), equals(null)); | |
386 expect(context.callMethod('getValue', [list, -1]), equals(null)); | |
387 | |
388 expect(context.callMethod('getValue', [list, "0"]), equals("a")); | |
389 expect(context.callMethod('getValue', [list, "1"]), equals("b")); | |
390 }); | |
391 | |
392 test("is array", () { | |
393 var list = ["a", "b"]; | |
394 expect(context.callMethod("checkIsArray", [list]), isTrue); | |
395 }); | |
396 | |
397 test("property descriptors", () { | |
398 // This test matters to make behavior consistent with JS native arrays | |
399 // and to make devtools integration work well. | |
400 var list = ["a", "b"]; | |
401 expect(context.callMethod( | |
402 "getOwnPropertyDescriptorJson", [list, 0]), equals( | |
403 '{"value":"a","writable":true,"enumerable":true,"configurable":true}') ); | |
404 | |
405 expect(context.callMethod( | |
406 "getOwnPropertyDescriptorJson", [list, "length"]), equals( | |
407 '{"value":2,"writable":true,"enumerable":false,"configurable":false}') ); | |
408 }); | |
409 | |
410 test("concat js arrays", () { | |
411 var list = ["1", "2"]; | |
412 // Tests that calling the concat method from JS will flatten out JS arrays | |
413 // We concat the array with "a", "b", ["c", "d"], 42, {foo: 10} | |
414 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] | |
415 var ret = context.callMethod("concatValues", [list]); | |
416 expect(list.length, equals(2)); | |
417 expect(ret.length, equals(8)); | |
418 expect(ret[0], equals("1")); | |
419 expect(ret[3], equals("b")); | |
420 expect(ret[5], equals("d")); | |
421 expect(ret[6], equals(42)); | |
422 expect(ret[7]['foo'], equals(10)); | |
423 }); | |
424 | |
425 test("concat onto arrays", () { | |
426 // This test only passes if we have monkey patched the core Array object | |
427 // prototype to handle Dart Lists. | |
428 var list = ["a", "b"]; | |
429 var ret = context.callMethod("concatOntoArray", [list]); | |
430 expect(list.length, equals(2)); | |
431 expect(ret, equals([1, 2, 3, "a", "b", "foo"])); | |
432 }); | |
433 | |
434 test("dart arrays on dart arrays", () { | |
435 // This test only passes if we have monkey patched the core Array object | |
436 // prototype to handle Dart Lists. | |
437 var list = ["a", "b"]; | |
438 var ret = callJsMethod(list, "concat", [["c", "d"], "e", ["f", "g"]]); | |
439 expect(list.length, equals(2)); | |
440 expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"])); | |
441 }); | |
442 | |
443 test("every greater than zero", () { | |
444 expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), isTrue); | |
445 expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]), | |
446 isTrue); | |
447 expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), isFalse); | |
448 expect(context.callMethod("everyGreaterThanZero", [[]]), isTrue); | |
449 }); | |
450 | |
451 test("filter greater than 42", () { | |
452 expect(context.callMethod("filterGreater42", [[1, 5]]), equals([])); | |
453 expect(context.callMethod("filterGreater42", [[43, 5, 49]]), | |
454 equals([43, 49])); | |
455 expect(context.callMethod("filterGreater42", [["43", "5", "49"]]), | |
456 equals(["43", "49"])); | |
457 }); | |
458 | |
459 test("for each collect result", () { | |
460 expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]), | |
461 equals([2, 10, 14])); | |
462 }); | |
463 | |
464 test("some", () { | |
465 expect(context.callMethod("someEqual42", [[1, 5, 9]]), isFalse); | |
466 expect(context.callMethod("someEqual42", [[1, 42, 9]]), isTrue); | |
467 }); | |
468 | |
469 test("sort backwards", () { | |
470 var arr = [1, 5, 9]; | |
471 var ret = context.callMethod("sortNumbersBackwards", [arr]); | |
472 expect(identical(arr, ret), isTrue); | |
473 expect(ret, equals([9, 5, 1])); | |
474 }); | |
475 | |
476 test("splice dummy items", () { | |
477 var list = [1, 2, 3, 4]; | |
478 var removed = context.callMethod("spliceDummyItems", [list]); | |
479 expect(removed.length, equals(2)); | |
480 expect(removed[0], equals(2)); | |
481 expect(removed[1], equals(3)); | |
482 expect(list.first, equals(1)); | |
483 expect(list[1], equals("quick")); | |
484 expect(list[2], equals("brown")); | |
485 expect(list[3], equals("fox")); | |
486 expect(list.last, equals(4)); | |
487 }); | |
488 | |
489 test("splice string args", () { | |
490 var list = [1, 2, 3, 4]; | |
491 var removed = context.callMethod("spliceTestStringArgs", [list]); | |
492 expect(removed.length, equals(2)); | |
493 expect(removed[0], equals(2)); | |
494 expect(removed[1], equals(3)); | |
495 expect(list.first, equals(1)); | |
496 expect(list[1], equals("quick")); | |
497 expect(list[2], equals("brown")); | |
498 expect(list[3], equals("fox")); | |
499 expect(list.last, equals(4)); | |
500 }); | |
501 | |
502 test("splice pastEndOfArray", () { | |
503 var list = [1, 2, 3, 4]; | |
504 var removed = context.callMethod("splicePastEnd", [list]); | |
505 expect(removed.length, equals(3)); | |
506 expect(list.first, equals(1)); | |
507 expect(list.length, equals(4)); | |
508 expect(list[1], equals("quick")); | |
509 expect(list[2], equals("brown")); | |
510 expect(list[3], equals("fox")); | |
511 }); | |
512 | |
513 test("splice both bounds past end of array", () { | |
514 var list = [1]; | |
515 var removed = context.callMethod("splicePastEnd", [list]); | |
516 expect(removed.length, equals(0)); | |
517 expect(list.first, equals(1)); | |
518 expect(list.length, equals(4)); | |
519 expect(list[1], equals("quick")); | |
520 expect(list[2], equals("brown")); | |
521 expect(list[3], equals("fox")); | |
522 }); | |
523 }); | |
524 | |
525 // This test group is disabled until we figure out an efficient way to | |
526 // distinguish between "array" Dart List types and non-array Dart list types. | |
527 /* | |
528 group('Non-array Lists', () { | |
529 test('opaque proxy', () { | |
530 // Dartium could easily support making LinkedList and all other classes | |
531 // implementing List behave like a JavaScript array but that would | |
532 // be challenging to implement in dart2js until browsers support ES6. | |
533 var list = ["a", "b", "c", "d"]; | |
534 var listView = new UnmodifiableListView(list.getRange(1,3)); | |
535 expect(listView is List, isTrue); | |
536 expect(listView.length, equals(2)); | |
537 expect(context.callMethod("checkIsArray", [listView]), isFalse); | |
538 expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue); | |
539 expect(context.callMethod("getOwnPropertyDescriptorJson", [listView, "leng th"]), equals("null")); | |
vsm
2015/06/26 15:59:37
line len
| |
540 }); | |
541 }); | |
542 */ | |
543 } | |
OLD | NEW |