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

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

Issue 1194643002: Enhance dart:js interop in a backwards compatible manner. List objects can now be passed back and f… (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: ready for review Created 5 years, 6 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
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 this.dartArray = obj;
terry 2015/06/19 14:57:04 What does this do?
Jacob 2015/06/19 20:39:45 good catch... nothing! I was debugging and forgot
39 return Array.isArray(obj);
40 }
41
42 function concatValues(obj) {
43 return obj.concat("a", "b", ["c", "d"], 42);
44 }
45
46 function concatOntoArray(obj) {
47 return [1,2,3].concat(obj, "foo");
48 }
49
50 function repeatedConcatOntoArray(obj) {
51 return [1,2,3].concat(obj, obj);
52 }
53
54 function everyGreaterThanZero(obj) {
55 return obj.every(function(currentValue, index, array) {
56 return currentValue > 0;
57 });
58 }
59
60 function everyGreaterThanZeroCheckThisArg(obj) {
61 var j = 0;
62 return obj.every(function(currentValue, index, array) {
63 if (j != index) {
64 throw "Unxpected index";
65 }
66 j++;
67 if (array !== obj) {
68 throw "Array argument doesn't match obj";
69 }
70 return currentValue > 0;
71 });
72 }
73
74 function filterGreater42(obj) {
75 return obj.filter(function(currentValue, index, array) {
76 return currentValue > 42;
77 });
78 }
79
80 function forEachCollectResult(array, callback) {
81 var result = [];
82 array.forEach(function(currentValue) {
83 result.push(currentValue * 2);
84 });
85 return result;
86 }
87
88 function someEqual42(array, callback) {
89 return array.some(function(currentValue) {
90 return currentValue == 42;
91 });
92 }
93
94 function sortNumbersBackwards(array) {
95 return array.sort(function(a, b) {
96 return b - a;
97 });
98 }
99
100 function spliceDummyItems(array) {
101 return array.splice(1, 2, "quick" ,"brown", "fox");
102 }
103
104 function spliceTestStringArgs(array) {
105 return array.splice("1.2", "2.01", "quick" ,"brown", "fox");
106 }
107
108 function splicePastEnd(array) {
109 return array.splice(1, 5332, "quick" ,"brown", "fox");
110 }
111
112 function callJsToString(array) {
113 return array.toString();
114 }
115
116 function mapAddIndexToEachElement(array) {
117 return array.map(function(currentValue, index) {
118 return currentValue + index;
119 });
120 }
121
122 function reduceSumDoubledElements(array) {
123 return array.reduce(function(previousValue, currentValue) { return previousVal ue + currentValue*2; }, 0);
124 }
125
126 // TODO(jacobr): add a test that distinguishes reduce from reduceRight.
127 function reduceRightSumDoubledElements(array) {
128 return array.reduceRight(function(previousValue, currentValue) { return previo usValue + currentValue*2; }, 0);
129 }
130
131 function identical(o1, o2) {
132 return o1 === o2;
133 }
134
135 function getOwnPropertyDescriptorJson(array, property) {
136 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property));
137 }
138
139 function setLength(array, len) {
140 return array.length = len;
141 }
142
143 """);
144 }
145
146 class Foo {}
147
148 callJsMethod(List array, String methodName, List args) =>
149 context.callMethod("callJsMethod", [array, methodName, args]);
150
151 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]);
152 callLastIndexOf(List array, value) =>
153 callJsMethod(array, "lastIndexOf", [value]);
154
155 callPop(List array) => callJsMethod(array, "pop", []);
156 callPush(List array, element) => callJsMethod(array, "push", [element]);
157 callShift(List array) => callJsMethod(array, "shift", []);
158 callReverse(List array) => callJsMethod(array, "reverse", []);
159 callSetLength(List array, length) =>
160 context.callMethod("setLength", [array, length]);
161
162 main() {
163 _injectJs();
164 useHtmlConfiguration();
165
166 group('indexOf', () {
167 var div = new DivElement();
168 var list = [3, 42, "foo", 42, div];
169 test('found', () {
170 expect(callIndexOf(list, 3), equals(0));
171 expect(callIndexOf(list, 42), equals(1));
172 expect(callIndexOf(list, "foo"), equals(2));
173 expect(callIndexOf(list, div), equals(4));
174 });
175
176 test('missing', () {
177 expect(callIndexOf(list, 31), equals(-1));
178 expect(callIndexOf(list, "42"), equals(-1));
179 expect(callIndexOf(list, null), equals(-1));
180 });
181 });
182
183 group('set length', () {
184 test('larger', () {
185 var list = ["a", "b", "c", "d"];
186 expect(callSetLength(list, 10), equals(10));
187 expect(list.length, equals(10));
188 expect(list.last, equals(null));
189 expect(list[3], equals("d"));
190 });
191
192 test('smaller', () {
193 var list = ["a", "b", "c", "d"];
194 expect(callSetLength(list, 2), equals(2));
195 expect(list.length, equals(2));
terry 2015/06/19 14:57:04 I know these tests aren't enabled yet. What about
Jacob 2015/06/19 20:39:45 added thta test. btw these tests are actually runn
196 expect(callSetLength(list, 0), equals(0));
197 expect(list.length, equals(0));
198 expect(callSetLength(list, 2), equals(2));
199 expect(list.first, equals(null));
200 });
201
202 test('invalid', () {
203 var list = ["a", "b", "c", "d"];
204 expect(() => callSetLength(list, 2.3), throws);
205 expect(() => callSetLength(list, -1), throws);
206 // Make sure we are coercing to a JS number.
terry 2015/06/19 14:57:04 After the setLength failures should list.length be
Jacob 2015/06/19 20:39:45 good idea. done.
207 expect(callSetLength(list, "2"), equals("2"));
208 expect(list.length, equals(2));
209 });
210 });
211
212 group('join', () {
213 var list = [3, 42, "foo"];
214 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()];
215 test('default', () {
216 expect(callJsMethod(list, "join", []), equals("3,42,foo"));
217 expect(callJsMethod(listWithDartClasses, "join", []),
218 equals("3,Instance of 'Foo',42,foo,Instance of 'Object'"));
219 });
220
221 test('custom separator', () {
222 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo"));
223 });
224 });
terry 2015/06/19 14:57:04 What about joining 2 real lists e.g., expect(call
Jacob 2015/06/19 20:39:44 Are you thinking about the concat method? join on
terry 2015/06/25 13:40:22 Your right join is not concat.
225
226 group('lastIndexOf', () {
227 var list = [3, 42, "foo", 42];
228 test('found', () {
229 expect(callLastIndexOf(list, 3), equals(0));
230 expect(callLastIndexOf(list, 42), equals(3));
231 expect(callLastIndexOf(list, "foo"), equals(2));
232 });
233
234 test('missing', () {
235 expect(callLastIndexOf(list, 31), equals(-1));
236 expect(callLastIndexOf(list, "42"), equals(-1));
237 expect(callLastIndexOf(list, null), equals(-1));
238 });
239 });
240
241 group('pop', () {
242 test('all', () {
243 var foo = new Foo();
244 var div = new DivElement();
245 var list = [3, 42, "foo", foo, div];
246 expect(callPop(list), equals(div));
247 expect(list.length, equals(4));
248 expect(callPop(list), equals(foo));
249 expect(list.length, equals(3));
250 expect(callPop(list), equals("foo"));
251 expect(list.length, equals(2));
252 expect(callPop(list), equals(42));
253 expect(list.length, equals(1));
254 expect(callPop(list), equals(3));
255 expect(list.length, equals(0));
terry 2015/06/19 14:57:04 Pop once more when there's no items in the list?
Jacob 2015/06/19 20:39:45 added and fixed behavior for that case to match JS
Jacob 2015/06/19 20:39:45 good idea. done.
256 });
257 });
258
259 group('push', () {
260 test('strings', () {
261 var list = [];
262 expect(callPush(list, "foo"), equals(1));
263 expect(callPush(list, "bar"), equals(2));
terry 2015/06/19 14:57:04 May push a 'new Foo()' or 'new DivElement()' inste
Jacob 2015/06/19 20:39:45 Done.
264 expect(callPush(list, "baz"), equals(3));
265 expect(list.toString(), equals("[foo, bar, baz]"));
266 });
267 });
268
269 group('shift', () {
270 test('all', () {
271 var foo = new Foo();
272 var div = new DivElement();
273 var list = [3, 42, "foo", foo, div];
274 expect(callShift(list), equals(3));
275 expect(list.length, equals(4));
276 expect(callShift(list), equals(42));
277 expect(list.length, equals(3));
278 expect(callShift(list), equals("foo"));
279 expect(list.length, equals(2));
280 expect(callShift(list), equals(foo));
281 expect(list.length, equals(1));
282 expect(callShift(list), equals(div));
283 expect(list.length, equals(0));
terry 2015/06/19 14:57:04 One more shift with nothing in the array?
Jacob 2015/06/19 20:39:43 Done.
284 });
285 });
286
287 group('reverse', () {
288 test('simple', () {
289 var foo = new Foo();
290 var div = new DivElement();
291 var list = [3, 42, "foo"];
terry 2015/06/19 14:57:04 Did you mean to have foo or div object in the list
Jacob 2015/06/19 20:39:45 Done.
292 callReverse(list);
293 expect(list.toString(), equals("[foo, 42, 3]"));
294 list = [3, 42];
terry 2015/06/19 14:57:04 is this test redundant?
Jacob 2015/06/19 20:39:44 testing with both an odd and even # of elements be
295 callReverse(list);
296 expect(list.toString(), equals("[42, 3]"));
297 });
298 });
299
300 group('slice', () {
301 test('copy', () {
302 var foo = new Foo();
303 var div = new DivElement();
304 var list = [3, 42, "foo", foo, div];
305 var copy = callJsMethod(list, "slice", []);
306 expect(identical(list, copy), isFalse);
307 expect(copy.length, equals(list.length));
308 for (var i = 0; i < list.length; i++) {
309 expect(list[i], equals(copy[i]));
terry 2015/06/19 14:57:04 Should test for list[3] === copy[3] and list[4] ==
Jacob 2015/06/19 20:39:44 Done.
310 }
311 copy.add("dummy");
312 expect(list.length + 1, equals(copy.length));
313 });
314 test('specify start', () {
315 var list = [3, 42, "foo"];
316 var copy = callJsMethod(list, "slice", [1]);
317 expect(copy.first, equals(42));
318 });
319 test('specify start and end', () {
320 var list = [3, 42, 92, "foo"];
321 var copy = callJsMethod(list, "slice", [1, 3]);
322 expect(copy.first, equals(42));
323 expect(copy.last, equals(92));
324 });
terry 2015/06/19 14:57:04 What about testing slicing from end e.g., slice[-2
Jacob 2015/06/19 20:39:44 Done.
325 });
326
327 group("js snippet tests", () {
328 test("enumerate indices", () {
329 var list = ["a", "b", "c", "d"];
330 var indices = context.callMethod('jsEnumerateIndices', [list]);
331 expect(indices.length, equals(4));
332 for (int i = 0; i < 4; i++) {
333 expect(indices[i], equals('$i'));
334 }
335 });
336
337 test("set element", () {
338 var list = ["a", "b", "c", "d"];
339 context.callMethod('setValue', [list, 0, 42]);
340 expect(list[0], equals(42));
341 context.callMethod('setValue', [list, 1, 84]);
342 expect(list[1], equals(84));
343 context.callMethod(
344 'setValue', [list, 6, 100]); // Off the end of the list.
345 expect(list.length, equals(7));
346 expect(list[4], equals(null));
347 expect(list[6], equals(100));
348
349 // These tests have to be commented out because we don't persist
350 // JS proxies for Dart objects like we could/should.
351 // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid array index
terry 2015/06/19 14:57:04 Would callJsMethod(list, 'splice', [-1, 0, "foo"])
Jacob 2015/06/19 20:39:43 that would work fine. the issue is just that we ar
352 // expect(context.callMethod('getValue', [list, -1]), equals("foo"));
353 // expect(context.callMethod('getValue', [list, "-1"]), equals("foo"));
354 });
355
356 test("get element", () {
357 var list = ["a", "b", "c", "d"];
358 expect(context.callMethod('getValue', [list, 0]), equals("a"));
359 expect(context.callMethod('getValue', [list, 1]), equals("b"));
360 expect(context.callMethod('getValue', [list, 6]), equals(null));
361 expect(context.callMethod('getValue', [list, -1]), equals(null));
362
363 expect(context.callMethod('getValue', [list, "0"]), equals("a"));
364 expect(context.callMethod('getValue', [list, "1"]), equals("b"));
365 });
366
367 test("is array", () {
368 var list = ["a", "b"];
369 expect(context.callMethod("checkIsArray", [list]), isTrue);
370 });
371
372 test("property descriptors", () {
373 // This test matters to make behavior consistent with JS native arrays
374 // and to make devtools integration work well.
375 var list = ["a", "b"];
376 expect(context.callMethod(
377 "getOwnPropertyDescriptorJson", [list, 0]), equals(
378 '{"value":"a","writable":true,"enumerable":true,"configurable":true}') );
379
380 expect(context.callMethod(
381 "getOwnPropertyDescriptorJson", [list, "length"]), equals(
382 '{"value":2,"writable":true,"enumerable":false,"configurable":false}') );
383 });
384
385 test("concat js arrays", () {
386 var list = ["1", "2"];
387 var ret = context.callMethod("concatValues", [list]);
terry 2015/06/19 14:57:04 Should comment like expected ret is ["1", "2", "a
Jacob 2015/06/19 20:39:44 Done.
388 expect(list.length, equals(2));
389 expect(ret.length, equals(7));
390 expect(ret[0], equals("1"));
391 expect(ret[3], equals("b"));
392 expect(ret[5], equals("d"));
393 expect(ret[6], equals(42));
394 });
395
396 test("concat onto arrays", () {
397 // This test only passes if we have monkey patched the core Array object
398 // prototype to handle Dart Lists.
399 var list = ["a", "b"];
400 var ret = context.callMethod("concatOntoArray", [list]);
terry 2015/06/19 14:57:04 Should comment like expected ret to be [1,2,3, "a
Jacob 2015/06/19 20:39:45 Done.
401 expect(list.length, equals(2));
402 expect(ret.length, equals(6));
403 expect(ret[0], equals(1));
404 expect(ret[3], equals("a"));
405 expect(ret[4], equals("b"));
406 expect(ret[5], equals("foo"));
407 });
408
409 test("every greater than zero", () {
410 expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), isTrue);
411 expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]),
412 isTrue);
413 expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), isFalse);
terry 2015/06/19 14:57:04 What if empty list?
Jacob 2015/06/19 20:39:45 Done.
414 });
415
416 test("filter greater than 42", () {
417 expect(context.callMethod("filterGreater42", [[1, 5]]), equals([]));
418 expect(context.callMethod("filterGreater42", [[43, 5, 49]]),
419 equals([43, 49]));
420 expect(context.callMethod("filterGreater42", [["43", "5", "49"]]),
421 equals(["43", "49"]));
422 });
423
424 test("for each collect result", () {
425 expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]),
426 equals([2, 10, 14]));
427 });
428
429 test("some", () {
430 expect(context.callMethod("someEqual42", [[1, 5, 9]]), isFalse);
431 expect(context.callMethod("someEqual42", [[1, 42, 9]]), isTrue);
432 });
433
434 test("sort backwards", () {
435 var arr = [1, 5, 9];
436 var ret = context.callMethod("sortNumbersBackwards", [arr]);
437 expect(identical(arr, ret), isTrue);
438 expect(ret, equals([9, 5, 1]));
439 });
440
441 test("splice dummy items", () {
442 var list = [1, 2, 3, 4];
443 var removed = context.callMethod("spliceDummyItems", [list]);
444 expect(removed.length, equals(2));
445 expect(removed[0], equals(2));
446 expect(removed[1], equals(3));
447 expect(list.first, equals(1));
448 expect(list[1], equals("quick"));
449 expect(list[2], equals("brown"));
450 expect(list[3], equals("fox"));
451 expect(list.last, equals(4));
452 });
453
454 test("splice string args", () {
455 var list = [1, 2, 3, 4];
456 var removed = context.callMethod("spliceTestStringArgs", [list]);
457 expect(removed.length, equals(2));
458 expect(removed[0], equals(2));
459 expect(removed[1], equals(3));
460 expect(list.first, equals(1));
461 expect(list[1], equals("quick"));
462 expect(list[2], equals("brown"));
463 expect(list[3], equals("fox"));
464 expect(list.last, equals(4));
465 });
466
467 test("splice pastEndOfArray", () {
468 var list = [1, 2, 3, 4];
469 var removed = context.callMethod("splicePastEnd", [list]);
470 expect(removed.length, equals(3));
471 expect(list.first, equals(1));
472 expect(list.length, equals(4));
473 expect(list[1], equals("quick"));
474 expect(list[2], equals("brown"));
475 expect(list[3], equals("fox"));
476 });
477
478 test("splice both bounds past end of array", () {
479 var list = [1];
480 var removed = context.callMethod("splicePastEnd", [list]);
481 expect(removed.length, equals(0));
482 expect(list.first, equals(1));
483 expect(list.length, equals(4));
484 expect(list[1], equals("quick"));
485 expect(list[2], equals("brown"));
486 expect(list[3], equals("fox"));
487 });
488 });
489
490 // This test group is disabled until we figure out an efficient way to
491 // distinguish between "array" Dart List types and non-array Dart list types.
492 /*
493 group('Non-array Lists', () {
494 test('opaque proxy', () {
495 // Dartium could easily support making LinkedList and all other classes
496 // implementing List behave like a JavaScript array but that would
497 // be challenging to implement in dart2js until browsers support ES6.
498 var list = ["a", "b", "c", "d"];
499 var listView = new UnmodifiableListView(list.getRange(1,3));
500 expect(listView is List, isTrue);
501 expect(listView.length, equals(2));
502 expect(context.callMethod("checkIsArray", [listView]), isFalse);
503 expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue);
504 expect(context.callMethod("getOwnPropertyDescriptorJson", [listView, "leng th"]), equals("null"));
505 });
506 });
507 */
508 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698