Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: tests/html/js_array_test.dart

Issue 1209093005: Revert "Enhance dart:js interop in a backwards compatible manner." (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/html/html.status ('k') | tests/html/js_typed_interop_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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) {
123 return previousValue + currentValue*2;
124 },
125 0);
126 }
127
128 // TODO(jacobr): add a test that distinguishes reduce from reduceRight.
129 function reduceRightSumDoubledElements(array) {
130 return array.reduceRight(
131 function(previousValue, currentValue) {
132 return previousValue + currentValue*2;
133 },
134 0);
135 }
136
137 function identical(o1, o2) {
138 return o1 === o2;
139 }
140
141 function getOwnPropertyDescriptorJson(array, property) {
142 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property));
143 }
144
145 function setLength(array, len) {
146 return array.length = len;
147 }
148
149 """);
150 }
151
152 class Foo {}
153
154 callJsMethod(List array, String methodName, List args) =>
155 context.callMethod("callJsMethod", [array, methodName, args]);
156
157 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]);
158 callLastIndexOf(List array, value) =>
159 callJsMethod(array, "lastIndexOf", [value]);
160
161 callPop(List array) => callJsMethod(array, "pop", []);
162 callPush(List array, element) => callJsMethod(array, "push", [element]);
163 callShift(List array) => callJsMethod(array, "shift", []);
164 callReverse(List array) => callJsMethod(array, "reverse", []);
165 callSetLength(List array, length) =>
166 context.callMethod("setLength", [array, length]);
167
168 main() {
169 _injectJs();
170 useHtmlConfiguration();
171
172 group('indexOf', () {
173 var div = new DivElement();
174 var list = [3, 42, "foo", 42, div];
175 test('found', () {
176 expect(callIndexOf(list, 3), equals(0));
177 expect(callIndexOf(list, 42), equals(1));
178 expect(callIndexOf(list, "foo"), equals(2));
179 expect(callIndexOf(list, div), equals(4));
180 });
181
182 test('missing', () {
183 expect(callIndexOf(list, 31), equals(-1));
184 expect(callIndexOf(list, "42"), equals(-1));
185 expect(callIndexOf(list, null), equals(-1));
186 });
187 });
188
189 group('set length', () {
190 test('larger', () {
191 var list = ["a", "b", "c", "d"];
192 expect(callSetLength(list, 10), equals(10));
193 expect(list.length, equals(10));
194 expect(list.last, equals(null));
195 expect(list[3], equals("d"));
196 });
197
198 test('smaller', () {
199 var list = ["a", "b", "c", "d"];
200 expect(callSetLength(list, 2), equals(2));
201 expect(list.first, equals("a"));
202 expect(list.last, equals("b"));
203 expect(list.length, equals(2));
204 expect(callSetLength(list, 0), equals(0));
205 expect(list.length, equals(0));
206 expect(callSetLength(list, 2), equals(2));
207 expect(list.first, equals(null));
208 });
209
210 test('invalid', () {
211 var list = ["a", "b", "c", "d"];
212 expect(() => callSetLength(list, 2.3), throws);
213 expect(list.length, equals(4));
214 expect(() => callSetLength(list, -1), throws);
215 expect(list.length, equals(4));
216 // Make sure we are coercing to a JS number.
217 expect(callSetLength(list, "2"), equals("2"));
218 expect(list.length, equals(2));
219 });
220 });
221
222 group('join', () {
223 var list = [3, 42, "foo"];
224 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()];
225 test('default', () {
226 expect(callJsMethod(list, "join", []), equals("3,42,foo"));
227 expect(callJsMethod(listWithDartClasses, "join", []),
228 equals("3,Instance of 'Foo',42,foo,Instance of 'Object'"));
229 });
230
231 test('custom separator', () {
232 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo"));
233 });
234 });
235
236 group('lastIndexOf', () {
237 var list = [3, 42, "foo", 42];
238 test('found', () {
239 expect(callLastIndexOf(list, 3), equals(0));
240 expect(callLastIndexOf(list, 42), equals(3));
241 expect(callLastIndexOf(list, "foo"), equals(2));
242 });
243
244 test('missing', () {
245 expect(callLastIndexOf(list, 31), equals(-1));
246 expect(callLastIndexOf(list, "42"), equals(-1));
247 expect(callLastIndexOf(list, null), equals(-1));
248 });
249 });
250
251 group('pop', () {
252 test('all', () {
253 var foo = new Foo();
254 var div = new DivElement();
255 var list = [3, 42, "foo", foo, div];
256 expect(callPop(list), equals(div));
257 expect(list.length, equals(4));
258 expect(callPop(list), equals(foo));
259 expect(list.length, equals(3));
260 expect(callPop(list), equals("foo"));
261 expect(list.length, equals(2));
262 expect(callPop(list), equals(42));
263 expect(list.length, equals(1));
264 expect(callPop(list), equals(3));
265 expect(list.length, equals(0));
266 expect(callPop(list), equals(null));
267 expect(list.length, equals(0));
268 });
269 });
270
271 group('push', () {
272 test('strings', () {
273 var list = [];
274 var div = new DivElement();
275 expect(callPush(list, "foo"), equals(1));
276 expect(callPush(list, "bar"), equals(2));
277 expect(callPush(list, "baz"), equals(3));
278 expect(callPush(list, div), equals(4));
279 expect(list, equals(["foo", "bar", "baz", div]));
280 });
281 });
282
283 group('shift', () {
284 test('all', () {
285 var foo = new Foo();
286 var div = new DivElement();
287 var list = [3, 42, "foo", foo, div];
288 expect(callShift(list), equals(3));
289 expect(list.length, equals(4));
290 expect(callShift(list), equals(42));
291 expect(list.length, equals(3));
292 expect(callShift(list), equals("foo"));
293 expect(list.length, equals(2));
294 expect(callShift(list), equals(foo));
295 expect(list.length, equals(1));
296 expect(callShift(list), equals(div));
297 expect(list.length, equals(0));
298 expect(callShift(list), equals(null));
299 expect(list.length, equals(0));
300 });
301 });
302
303 group('reverse', () {
304 test('simple', () {
305 var foo = new Foo();
306 var div = new DivElement();
307 var list = [div, 42, foo];
308 callReverse(list);
309 expect(list, equals([foo, 42, div]));
310 list = [3, 42];
311 callReverse(list);
312 expect(list, equals([42, 3]));
313 });
314 });
315
316 group('slice', () {
317 test('copy', () {
318 var foo = new Foo();
319 var div = new DivElement();
320 var list = [3, 42, "foo", foo, div];
321 var copy = callJsMethod(list, "slice", []);
322 expect(identical(list, copy), isFalse);
323 expect(copy.length, equals(list.length));
324 for (var i = 0; i < list.length; i++) {
325 expect(list[i], equals(copy[i]));
326 }
327 expect(identical(list[3], copy[3]), isTrue);
328 expect(identical(list[4], copy[4]), isTrue);
329
330 copy.add("dummy");
331 expect(list.length + 1, equals(copy.length));
332 });
333
334 test('specify start', () {
335 var list = [3, 42, "foo"];
336 var copy = callJsMethod(list, "slice", [1]);
337 expect(copy.first, equals(42));
338 });
339
340 test('specify start and end', () {
341 var list = [3, 42, 92, "foo"];
342 var copy = callJsMethod(list, "slice", [1, 3]);
343 expect(copy.first, equals(42));
344 expect(copy.last, equals(92));
345 });
346
347 test('from end', () {
348 var list = [3, 42, 92, "foo"];
349 expect(callJsMethod(list, "slice", [-2]), equals([92, "foo"]));
350
351 // Past the end of the front of the array.
352 expect(callJsMethod(list, "slice", [-2, 3]), equals([92]));
353
354 // Past the end of the front of the array.
355 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42]));
356 });
357 });
358
359 group("js snippet tests", () {
360 test("enumerate indices", () {
361 var list = ["a", "b", "c", "d"];
362 var indices = context.callMethod('jsEnumerateIndices', [list]);
363 expect(indices.length, equals(4));
364 for (int i = 0; i < 4; i++) {
365 expect(indices[i], equals('$i'));
366 }
367 });
368
369 test("set element", () {
370 var list = ["a", "b", "c", "d"];
371 context.callMethod('setValue', [list, 0, 42]);
372 expect(list[0], equals(42));
373 context.callMethod('setValue', [list, 1, 84]);
374 expect(list[1], equals(84));
375 context.callMethod(
376 'setValue', [list, 6, 100]); // Off the end of the list.
377 expect(list.length, equals(7));
378 expect(list[4], equals(null));
379 expect(list[6], equals(100));
380
381 // These tests have to be commented out because we don't persist
382 // JS proxies for Dart objects like we could/should.
383 // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid index
384 // expect(context.callMethod('getValue', [list, -1]), equals("foo"));
385 // expect(context.callMethod('getValue', [list, "-1"]), equals("foo"));
386 });
387
388 test("get element", () {
389 var list = ["a", "b", "c", "d"];
390 expect(context.callMethod('getValue', [list, 0]), equals("a"));
391 expect(context.callMethod('getValue', [list, 1]), equals("b"));
392 expect(context.callMethod('getValue', [list, 6]), equals(null));
393 expect(context.callMethod('getValue', [list, -1]), equals(null));
394
395 expect(context.callMethod('getValue', [list, "0"]), equals("a"));
396 expect(context.callMethod('getValue', [list, "1"]), equals("b"));
397 });
398
399 test("is array", () {
400 var list = ["a", "b"];
401 expect(context.callMethod("checkIsArray", [list]), isTrue);
402 });
403
404 test("property descriptors", () {
405 // This test matters to make behavior consistent with JS native arrays
406 // and to make devtools integration work well.
407 var list = ["a", "b"];
408 expect(context.callMethod("getOwnPropertyDescriptorJson", [list, 0]),
409 equals('{"value":"a",'
410 '"writable":true,'
411 '"enumerable":true,'
412 '"configurable":true}'));
413
414 expect(
415 context.callMethod("getOwnPropertyDescriptorJson", [list, "length"]),
416 equals('{"value":2,'
417 '"writable":true,'
418 '"enumerable":false,'
419 '"configurable":false}'));
420 });
421
422 test("concat js arrays", () {
423 var list = ["1", "2"];
424 // Tests that calling the concat method from JS will flatten out JS arrays
425 // We concat the array with "a", "b", ["c", "d"], 42, {foo: 10}
426 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}]
427 var ret = context.callMethod("concatValues", [list]);
428 expect(list.length, equals(2));
429 expect(ret.length, equals(8));
430 expect(ret[0], equals("1"));
431 expect(ret[3], equals("b"));
432 expect(ret[5], equals("d"));
433 expect(ret[6], equals(42));
434 expect(ret[7]['foo'], equals(10));
435 });
436
437 test("concat onto arrays", () {
438 // This test only passes if we have monkey patched the core Array object
439 // prototype to handle Dart Lists.
440 var list = ["a", "b"];
441 var ret = context.callMethod("concatOntoArray", [list]);
442 expect(list.length, equals(2));
443 expect(ret, equals([1, 2, 3, "a", "b", "foo"]));
444 });
445
446 test("dart arrays on dart arrays", () {
447 // This test only passes if we have monkey patched the core Array object
448 // prototype to handle Dart Lists.
449 var list = ["a", "b"];
450 var ret = callJsMethod(list, "concat", [["c", "d"], "e", ["f", "g"]]);
451 expect(list.length, equals(2));
452 expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"]));
453 });
454
455 test("every greater than zero", () {
456 expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), isTrue);
457 expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]),
458 isTrue);
459 expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), isFalse);
460 expect(context.callMethod("everyGreaterThanZero", [[]]), isTrue);
461 });
462
463 test("filter greater than 42", () {
464 expect(context.callMethod("filterGreater42", [[1, 5]]), equals([]));
465 expect(context.callMethod("filterGreater42", [[43, 5, 49]]),
466 equals([43, 49]));
467 expect(context.callMethod("filterGreater42", [["43", "5", "49"]]),
468 equals(["43", "49"]));
469 });
470
471 test("for each collect result", () {
472 expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]),
473 equals([2, 10, 14]));
474 });
475
476 test("some", () {
477 expect(context.callMethod("someEqual42", [[1, 5, 9]]), isFalse);
478 expect(context.callMethod("someEqual42", [[1, 42, 9]]), isTrue);
479 });
480
481 test("sort backwards", () {
482 var arr = [1, 5, 9];
483 var ret = context.callMethod("sortNumbersBackwards", [arr]);
484 expect(identical(arr, ret), isTrue);
485 expect(ret, equals([9, 5, 1]));
486 });
487
488 test("splice dummy items", () {
489 var list = [1, 2, 3, 4];
490 var removed = context.callMethod("spliceDummyItems", [list]);
491 expect(removed.length, equals(2));
492 expect(removed[0], equals(2));
493 expect(removed[1], equals(3));
494 expect(list.first, equals(1));
495 expect(list[1], equals("quick"));
496 expect(list[2], equals("brown"));
497 expect(list[3], equals("fox"));
498 expect(list.last, equals(4));
499 });
500
501 test("splice string args", () {
502 var list = [1, 2, 3, 4];
503 var removed = context.callMethod("spliceTestStringArgs", [list]);
504 expect(removed.length, equals(2));
505 expect(removed[0], equals(2));
506 expect(removed[1], equals(3));
507 expect(list.first, equals(1));
508 expect(list[1], equals("quick"));
509 expect(list[2], equals("brown"));
510 expect(list[3], equals("fox"));
511 expect(list.last, equals(4));
512 });
513
514 test("splice pastEndOfArray", () {
515 var list = [1, 2, 3, 4];
516 var removed = context.callMethod("splicePastEnd", [list]);
517 expect(removed.length, equals(3));
518 expect(list.first, equals(1));
519 expect(list.length, equals(4));
520 expect(list[1], equals("quick"));
521 expect(list[2], equals("brown"));
522 expect(list[3], equals("fox"));
523 });
524
525 test("splice both bounds past end of array", () {
526 var list = [1];
527 var removed = context.callMethod("splicePastEnd", [list]);
528 expect(removed.length, equals(0));
529 expect(list.first, equals(1));
530 expect(list.length, equals(4));
531 expect(list[1], equals("quick"));
532 expect(list[2], equals("brown"));
533 expect(list[3], equals("fox"));
534 });
535 });
536
537 // This test group is disabled until we figure out an efficient way to
538 // distinguish between "array" Dart List types and non-array Dart list types.
539 /*
540 group('Non-array Lists', () {
541 test('opaque proxy', () {
542 // Dartium could easily support making LinkedList and all other classes
543 // implementing List behave like a JavaScript array but that would
544 // be challenging to implement in dart2js until browsers support ES6.
545 var list = ["a", "b", "c", "d"];
546 var listView = new UnmodifiableListView(list.getRange(1,3));
547 expect(listView is List, isTrue);
548 expect(listView.length, equals(2));
549 expect(context.callMethod("checkIsArray", [listView]), isFalse);
550 expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue);
551 expect(context.callMethod("getOwnPropertyDescriptorJson",
552 [listView, "length"]), equals("null"));
553 });
554 });
555 */
556 }
OLDNEW
« no previous file with comments | « tests/html/html.status ('k') | tests/html/js_typed_interop_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698