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 library jsArrayTest; | 5 @Js("ArrayTest.Util") |
6 library js_array_test; | |
6 | 7 |
7 import 'dart:html'; | 8 import 'dart:html'; |
8 import 'dart:js'; | 9 import 'dart:js'; |
9 | 10 |
11 import 'package:js/js.dart'; | |
10 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
11 import 'package:unittest/html_config.dart'; | 13 import 'package:unittest/html_config.dart'; |
14 import 'json_helper.dart' as json_helper; | |
12 | 15 |
13 _injectJs() { | 16 _injectJs() { |
14 document.body.append(new ScriptElement() | 17 document.body.append(new ScriptElement() |
15 ..type = 'text/javascript' | 18 ..type = 'text/javascript' |
16 ..innerHtml = r""" | 19 ..innerHtml = r""" |
17 function callJsMethod(jsObj, jsMethodName, args) { | 20 ArrayTest = {}; |
18 return jsObj[jsMethodName].apply(jsObj, args); | 21 ArrayTest.Util = { |
22 callJsMethod: function(jsObj, jsMethodName, args) { | |
23 return jsObj[jsMethodName].apply(jsObj, args); | |
24 }, | |
25 | |
26 jsEnumerateIndices: function(obj) { | |
27 var ret = []; | |
28 for(var i in obj) { | |
29 ret.push(i); | |
30 } | |
31 return ret; | |
32 }, | |
33 | |
34 checkIsArray: function(obj) { | |
35 return Array.isArray(obj); | |
36 }, | |
37 | |
38 concatValues: function(obj) { | |
39 return obj.concat("a", "b", ["c", "d"], 42, {foo: 10}); | |
40 }, | |
41 | |
42 concatOntoArray: function(obj) { | |
43 return [1,2,3].concat(obj, "foo"); | |
44 }, | |
45 | |
46 repeatedConcatOntoArray: function(obj) { | |
47 return [1,2,3].concat(obj, obj); | |
48 }, | |
49 | |
50 everyGreaterThanZero: function(obj) { | |
51 return obj.every(function(currentValue, index, array) { | |
52 return currentValue > 0; | |
53 }); | |
54 }, | |
55 | |
56 everyGreaterThanZeroCheckThisArg: function(obj) { | |
57 var j = 0; | |
58 return obj.every(function(currentValue, index, array) { | |
59 if (j != index) { | |
60 throw "Unxpected index"; | |
61 } | |
62 j++; | |
63 if (array !== obj) { | |
64 throw "Array argument doesn't match obj"; | |
65 } | |
66 return currentValue > 0; | |
67 }); | |
68 }, | |
69 | |
70 filterGreater42: function(obj) { | |
71 return obj.filter(function(currentValue, index, array) { | |
72 return currentValue > 42; | |
73 }); | |
74 }, | |
75 | |
76 forEachCollectResult: function(array) { | |
77 var result = []; | |
78 array.forEach(function(currentValue) { | |
79 result.push(currentValue * 2); | |
80 }); | |
81 return result; | |
82 }, | |
83 | |
84 someEqual42: function(array) { | |
85 return array.some(function(currentValue) { | |
86 return currentValue == 42; | |
87 }); | |
88 }, | |
89 | |
90 sortNumbersBackwards: function(array) { | |
91 return array.sort(function(a, b) { | |
92 return b - a; | |
93 }); | |
94 }, | |
95 | |
96 spliceDummyItems: function(array) { | |
97 return array.splice(1, 2, "quick" ,"brown", "fox"); | |
98 }, | |
99 | |
100 spliceTestStringArgs: function(array) { | |
101 return array.splice("1.2", "2.01", "quick" ,"brown", "fox"); | |
102 }, | |
103 | |
104 splicePastEnd: function(array) { | |
105 return array.splice(1, 5332, "quick" ,"brown", "fox"); | |
106 }, | |
107 | |
108 callJsToString: function(array) { | |
109 return array.toString(); | |
110 }, | |
111 | |
112 mapAddIndexToEachElement: function(array) { | |
113 return array.map(function(currentValue, index) { | |
114 return currentValue + index; | |
115 }); | |
116 }, | |
117 | |
118 reduceSumDoubledElements: function(array) { | |
119 return array.reduce(function(previousValue, currentValue) { | |
120 return previousValue + currentValue*2; | |
121 }, | |
122 0); | |
123 }, | |
124 | |
125 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. | |
126 reduceRightSumDoubledElements: function(array) { | |
127 return array.reduceRight(function(previousValue, currentValue) { | |
128 return previousValue + currentValue*2; | |
129 }, | |
130 0); | |
131 }, | |
132 | |
133 identical: function(o1, o2) { | |
134 return o1 === o2; | |
135 }, | |
136 | |
137 getOwnPropertyDescriptorJson: function(array, property) { | |
138 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); | |
139 }, | |
140 | |
141 setLength: function(array, len) { | |
142 return array.length = len; | |
143 }, | |
144 | |
145 getValue: function(obj, index) { | |
146 return obj[index]; | |
147 }, | |
148 | |
149 setValue: function(obj, index, value) { | |
150 return obj[index] = value; | |
151 }, | |
152 | |
153 // Calling a method from Dart List on an arbitrary target object. | |
154 callListMethodOnTarget: function(dartArray, target, methodName, args) { | |
155 return dartArray[methodName].apply(target, args); | |
156 }, | |
157 | |
158 newArray: function() { return []; }, | |
159 | |
160 newLiteral: function() { return {}; }, | |
161 | |
162 }; | |
163 """); | |
19 } | 164 } |
20 | 165 |
21 function jsEnumerateIndices(obj) { | 166 @Js() |
22 var ret = []; | 167 class SimpleJsLiteralClass extends JavaScriptObject { |
alexandre.ardhuin
2015/10/01 08:51:05
I can't find where this class is used.
Jacob
2015/10/02 20:08:16
There is a JS literal with a getter foo
see Line
alexandre.ardhuin
2015/10/02 20:29:56
I saw this JS line but on the dart side there's on
| |
23 for(var i in obj) { | 168 external get foo; |
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(function(previousValue, currentValue) { | |
131 return previousValue + currentValue*2; | |
132 }, | |
133 0); | |
134 } | |
135 | |
136 function identical(o1, o2) { | |
137 return o1 === o2; | |
138 } | |
139 | |
140 function getOwnPropertyDescriptorJson(array, property) { | |
141 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); | |
142 } | |
143 | |
144 function setLength(array, len) { | |
145 return array.length = len; | |
146 } | |
147 | |
148 function jsonStringify(o) { | |
149 return JSON.stringify(o); | |
150 } | |
151 | |
152 // Calling a method from Dart List on an arbitrary target object. | |
153 function callListMethodOnTarget(dartArray, target, methodName, args) { | |
154 return dartArray[methodName].apply(target, args); | |
155 } | |
156 | |
157 """); | |
158 } | 169 } |
159 | 170 |
160 class Foo {} | 171 class Foo {} |
161 | 172 |
162 callJsMethod(List array, String methodName, List args) => | 173 @Js() |
163 context.callMethod("callJsMethod", [array, methodName, args]); | 174 external callJsMethod(List array, String methodName, List args); |
164 | 175 |
165 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); | 176 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); |
166 callLastIndexOf(List array, value) => | 177 callLastIndexOf(List array, value) => |
167 callJsMethod(array, "lastIndexOf", [value]); | 178 callJsMethod(array, "lastIndexOf", [value]); |
168 | 179 |
169 callPop(List array) => callJsMethod(array, "pop", []); | 180 callPop(List array) => callJsMethod(array, "pop", []); |
170 callPush(List array, element) => callJsMethod(array, "push", [element]); | 181 callPush(List array, element) => callJsMethod(array, "push", [element]); |
171 callShift(List array) => callJsMethod(array, "shift", []); | 182 callShift(List array) => callJsMethod(array, "shift", []); |
172 callReverse(List array) => callJsMethod(array, "reverse", []); | 183 callReverse(List array) => callJsMethod(array, "reverse", []); |
173 callSetLength(List array, length) => | |
174 context.callMethod("setLength", [array, length]); | |
175 | 184 |
176 callListMethodOnObject(JsObject object, String methodName, List args) => context | 185 callListMethodOnObject(object, String methodName, List args) => |
177 .callMethod("callListMethodOnTarget", [[], object, methodName, args]); | 186 callListMethodOnTarget([], object, methodName, args); |
178 | 187 |
179 jsonStringify(JsObject object) => context.callMethod("jsonStringify", [object]); | 188 @Js() |
189 external jsEnumerateIndices(obj); | |
190 @Js() | |
191 external bool checkIsArray(obj); | |
192 @Js() | |
193 external concatValues(obj); | |
194 | |
195 @Js() | |
196 external concatOntoArray(obj); | |
197 | |
198 @Js() | |
199 external repeatedConcatOntoArray(obj); | |
200 @Js() | |
201 external bool everyGreaterThanZero(obj); | |
202 @Js() | |
203 external bool everyGreaterThanZeroCheckThisArg(obj); | |
204 | |
205 @Js() | |
206 external filterGreater42(obj); | |
207 | |
208 @Js() | |
209 external forEachCollectResult(List array); | |
210 @Js() | |
211 external someEqual42(List array); | |
212 @Js() | |
213 external sortNumbersBackwards(List array); | |
214 | |
215 @Js() | |
216 external List spliceDummyItems(List array); | |
217 | |
218 @Js() | |
219 external List spliceTestStringArgs(List array); | |
220 | |
221 @Js() | |
222 external List splicePastEnd(List array); | |
223 | |
224 @Js() | |
225 external String callJsToString(List array); | |
226 | |
227 @Js() | |
228 external mapAddIndexToEachElement(List array); | |
229 @Js() | |
230 external reduceSumDoubledElements(List array); | |
231 | |
232 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. | |
233 @Js() | |
234 external reduceRightSumDoubledElements(List array); | |
235 @Js() | |
236 external identical(o1, o2); | |
237 | |
238 @Js() | |
239 external getOwnPropertyDescriptorJson(List array, property); | |
240 | |
241 @Js("setLength") | |
242 external callSetLength(List array, length); | |
243 | |
244 @Js() | |
245 external getValue(obj, index); | |
246 | |
247 @Js() | |
248 external setValue(obj, index, value); | |
249 | |
250 @Js() | |
251 external callListMethodOnTarget(List target, object, String methodName, List arg s); | |
252 | |
253 @Js() | |
254 external newArray(); | |
255 | |
256 @Js() | |
257 external newLiteral(); | |
180 | 258 |
181 main() { | 259 main() { |
182 _injectJs(); | 260 _injectJs(); |
183 useHtmlConfiguration(); | 261 useHtmlConfiguration(); |
184 | 262 |
185 group('indexOf', () { | 263 group('indexOf', () { |
186 var div = new DivElement(); | 264 var div = new DivElement(); |
187 var list = [3, 42, "foo", 42, div]; | 265 var list = [3, 42, "foo", 42, div]; |
188 test('found', () { | 266 test('found', () { |
189 expect(callIndexOf(list, 3), equals(0)); | 267 expect(callIndexOf(list, 3), equals(0)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 expect(list.length, equals(2)); | 309 expect(list.length, equals(2)); |
232 }); | 310 }); |
233 }); | 311 }); |
234 | 312 |
235 group('join', () { | 313 group('join', () { |
236 var list = [3, 42, "foo"]; | 314 var list = [3, 42, "foo"]; |
237 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()]; | 315 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()]; |
238 test('default', () { | 316 test('default', () { |
239 expect(callJsMethod(list, "join", []), equals("3,42,foo")); | 317 expect(callJsMethod(list, "join", []), equals("3,42,foo")); |
240 expect(callJsMethod(listWithDartClasses, "join", []), | 318 expect(callJsMethod(listWithDartClasses, "join", []), |
241 equals("3,Instance of 'Foo',42,foo,Instance of 'Object'")); | 319 equals("3,${new Foo()},42,foo,${new Object()}")); |
242 }); | 320 }); |
243 | 321 |
244 test('custom separator', () { | 322 test('custom separator', () { |
245 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo")); | 323 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo")); |
246 }); | 324 }); |
247 }); | 325 }); |
248 | 326 |
249 group('lastIndexOf', () { | 327 group('lastIndexOf', () { |
250 var list = [3, 42, "foo", 42]; | 328 var list = [3, 42, "foo", 42]; |
251 test('found', () { | 329 test('found', () { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 expect(callJsMethod(list, "slice", [-2, 3]), equals([92])); | 446 expect(callJsMethod(list, "slice", [-2, 3]), equals([92])); |
369 | 447 |
370 // Past the end of the front of the array. | 448 // Past the end of the front of the array. |
371 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42])); | 449 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42])); |
372 }); | 450 }); |
373 }); | 451 }); |
374 | 452 |
375 group("js snippet tests", () { | 453 group("js snippet tests", () { |
376 test("enumerate indices", () { | 454 test("enumerate indices", () { |
377 var list = ["a", "b", "c", "d"]; | 455 var list = ["a", "b", "c", "d"]; |
378 var indices = context.callMethod('jsEnumerateIndices', [list]); | 456 var indices = |
457 jsEnumerateIndices(list); | |
379 expect(indices.length, equals(4)); | 458 expect(indices.length, equals(4)); |
380 for (int i = 0; i < 4; i++) { | 459 for (int i = 0; i < 4; i++) { |
381 expect(indices[i], equals('$i')); | 460 expect(indices[i], equals('$i')); |
382 } | 461 } |
383 }); | 462 }); |
384 | 463 |
385 test("set element", () { | 464 test("set element", () { |
386 var list = ["a", "b", "c", "d"]; | 465 var list = ["a", "b", "c", "d"]; |
387 context.callMethod('setValue', [list, 0, 42]); | 466 setValue(list, 0, 42); |
388 expect(list[0], equals(42)); | 467 expect(list[0], equals(42)); |
389 context.callMethod('setValue', [list, 1, 84]); | 468 setValue(list, 1, 84); |
390 expect(list[1], equals(84)); | 469 expect(list[1], equals(84)); |
391 context.callMethod( | 470 setValue(list, 6, 100); // Off the end of the list. |
392 'setValue', [list, 6, 100]); // Off the end of the list. | |
393 expect(list.length, equals(7)); | 471 expect(list.length, equals(7)); |
394 expect(list[4], equals(null)); | 472 expect(list[4], equals(null)); |
395 expect(list[6], equals(100)); | 473 expect(list[6], equals(100)); |
396 | 474 |
397 // These tests have to be commented out because we don't persist | 475 // These tests have to be commented out because we don't persist |
398 // JS proxies for Dart objects like we could/should. | 476 // JS proxies for Dart objects like we could/should. |
399 // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid array index | 477 // setValue(list, -1, "foo"); // Not a valid array index |
400 // expect(context.callMethod('getValue', [list, -1]), equals("foo")); | 478 // expect(getValue(list, -1), equals("foo")); |
401 // expect(context.callMethod('getValue', [list, "-1"]), equals("foo")); | 479 // expect(getValue(list, "-1"), equals("foo")); |
402 }); | 480 }); |
403 | 481 |
404 test("get element", () { | 482 test("get element", () { |
405 var list = ["a", "b", "c", "d"]; | 483 var list = ["a", "b", "c", "d"]; |
406 expect(context.callMethod('getValue', [list, 0]), equals("a")); | 484 expect(getValue(list, 0), |
407 expect(context.callMethod('getValue', [list, 1]), equals("b")); | 485 equals("a")); |
408 expect(context.callMethod('getValue', [list, 6]), equals(null)); | 486 expect(getValue(list, 1), |
409 expect(context.callMethod('getValue', [list, -1]), equals(null)); | 487 equals("b")); |
488 expect(getValue(list, 6), | |
489 equals(null)); | |
490 expect(getValue(list, -1), | |
491 equals(null)); | |
410 | 492 |
411 expect(context.callMethod('getValue', [list, "0"]), equals("a")); | 493 expect(getValue(list, "0"), |
412 expect(context.callMethod('getValue', [list, "1"]), equals("b")); | 494 equals("a")); |
495 expect(getValue(list, "1"), | |
496 equals("b")); | |
413 }); | 497 }); |
414 | 498 |
415 test("is array", () { | 499 test("is array", () { |
416 var list = ["a", "b"]; | 500 var list = ["a", "b"]; |
417 expect(context.callMethod("checkIsArray", [list]), isTrue); | 501 expect(checkIsArray(list), isTrue); |
418 }); | 502 }); |
419 | 503 |
420 test("property descriptors", () { | 504 test("property descriptors", () { |
421 // This test matters to make behavior consistent with JS native arrays | 505 // This test matters to make behavior consistent with JS native arrays |
422 // and to make devtools integration work well. | 506 // and to make devtools integration work well. |
423 var list = ["a", "b"]; | 507 var list = ["a", "b"]; |
424 expect(context.callMethod("getOwnPropertyDescriptorJson", [list, 0]), | 508 expect(getOwnPropertyDescriptorJson(list, 0), |
425 equals('{"value":"a",' | 509 equals('{"value":"a",' |
426 '"writable":true,' | 510 '"writable":true,' |
427 '"enumerable":true,' | 511 '"enumerable":true,' |
428 '"configurable":true}')); | 512 '"configurable":true}')); |
429 | 513 |
430 expect( | 514 expect( |
431 context.callMethod("getOwnPropertyDescriptorJson", [list, "length"]), | 515 getOwnPropertyDescriptorJson(list, "length"), |
432 equals('{"value":2,' | 516 equals('{"value":2,' |
433 '"writable":true,' | 517 '"writable":true,' |
434 '"enumerable":false,' | 518 '"enumerable":false,' |
435 '"configurable":false}')); | 519 '"configurable":false}')); |
436 }); | 520 }); |
437 | 521 |
438 test("concat js arrays", () { | 522 test("concat js arrays", () { |
439 var list = ["1", "2"]; | 523 var list = ["1", "2"]; |
440 // Tests that calling the concat method from JS will flatten out JS arrays | 524 // Tests that calling the concat method from JS will flatten out JS arrays |
441 // We concat the array with "a", "b", ["c", "d"], 42, {foo: 10} | 525 // We concat the array with "a", "b", ["c", "d"], 42, {foo: 10} |
442 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] | 526 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] |
443 var ret = context.callMethod("concatValues", [list]); | 527 var ret = concatValues(list); |
444 expect(list.length, equals(2)); | 528 expect(list.length, equals(2)); |
445 expect(ret.length, equals(8)); | 529 expect(ret.length, equals(8)); |
446 expect(ret[0], equals("1")); | 530 expect(ret[0], equals("1")); |
447 expect(ret[3], equals("b")); | 531 expect(ret[3], equals("b")); |
448 expect(ret[5], equals("d")); | 532 expect(ret[5], equals("d")); |
449 expect(ret[6], equals(42)); | 533 expect(ret[6], equals(42)); |
450 expect(ret[7]['foo'], equals(10)); | 534 expect(ret[7].foo, equals(10)); |
451 }); | 535 }); |
452 | 536 |
453 test("concat onto arrays", () { | 537 test("concat onto arrays", () { |
454 // This test only passes if we have monkey patched the core Array object | 538 // This test only passes if we have monkey patched the core Array object |
455 // prototype to handle Dart Lists. | 539 // prototype to handle Dart Lists. |
456 var list = ["a", "b"]; | 540 var list = ["a", "b"]; |
457 var ret = context.callMethod("concatOntoArray", [list]); | 541 var ret = concatOntoArray(list); |
458 expect(list.length, equals(2)); | 542 expect(list.length, equals(2)); |
459 expect(ret, equals([1, 2, 3, "a", "b", "foo"])); | 543 expect(ret, equals([1, 2, 3, "a", "b", "foo"])); |
460 }); | 544 }); |
461 | 545 |
462 test("dart arrays on dart arrays", () { | 546 test("dart arrays on dart arrays", () { |
463 // This test only passes if we have monkey patched the core Array object | 547 // This test only passes if we have monkey patched the core Array object |
464 // prototype to handle Dart Lists. | 548 // prototype to handle Dart Lists. |
465 var list = ["a", "b"]; | 549 var list = ["a", "b"]; |
466 var ret = callJsMethod(list, "concat", [["c", "d"], "e", ["f", "g"]]); | 550 var ret = callJsMethod(list, "concat", [ |
551 ["c", "d"], | |
552 "e", | |
553 ["f", "g"] | |
554 ]); | |
467 expect(list.length, equals(2)); | 555 expect(list.length, equals(2)); |
468 expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"])); | 556 expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"])); |
469 }); | 557 }); |
470 | 558 |
471 test("every greater than zero", () { | 559 test("every greater than zero", () { |
472 expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), isTrue); | 560 expect( |
473 expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]), | 561 everyGreaterThanZero([1, 5]), |
474 isTrue); | 562 isTrue); |
475 expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), isFalse); | 563 expect( |
476 expect(context.callMethod("everyGreaterThanZero", [[]]), isTrue); | 564 everyGreaterThanZeroCheckThisArg([1, 5]), |
565 isTrue); | |
566 expect( | |
567 everyGreaterThanZero([1, 0]), | |
568 isFalse); | |
569 expect(everyGreaterThanZero([]), | |
570 isTrue); | |
477 }); | 571 }); |
478 | 572 |
479 test("filter greater than 42", () { | 573 test("filter greater than 42", () { |
480 expect(context.callMethod("filterGreater42", [[1, 5]]), equals([])); | 574 expect(filterGreater42([1, 5]), equals([])); |
481 expect(context.callMethod("filterGreater42", [[43, 5, 49]]), | 575 expect( |
576 filterGreater42([43, 5, 49]), | |
482 equals([43, 49])); | 577 equals([43, 49])); |
483 expect(context.callMethod("filterGreater42", [["43", "5", "49"]]), | 578 expect( |
579 filterGreater42(["43", "5", "49"]), | |
484 equals(["43", "49"])); | 580 equals(["43", "49"])); |
485 }); | 581 }); |
486 | 582 |
487 test("for each collect result", () { | 583 test("for each collect result", () { |
488 expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]), | 584 expect( |
585 forEachCollectResult([1, 5, 7]), | |
489 equals([2, 10, 14])); | 586 equals([2, 10, 14])); |
490 }); | 587 }); |
491 | 588 |
492 test("some", () { | 589 test("some", () { |
493 expect(context.callMethod("someEqual42", [[1, 5, 9]]), isFalse); | 590 expect(someEqual42([1, 5, 9]), |
494 expect(context.callMethod("someEqual42", [[1, 42, 9]]), isTrue); | 591 isFalse); |
592 expect(someEqual42([1, 42, 9]), | |
593 isTrue); | |
495 }); | 594 }); |
496 | 595 |
497 test("sort backwards", () { | 596 test("sort backwards", () { |
498 var arr = [1, 5, 9]; | 597 var arr = [1, 5, 9]; |
499 var ret = context.callMethod("sortNumbersBackwards", [arr]); | 598 var ret = sortNumbersBackwards(arr); |
500 expect(identical(arr, ret), isTrue); | 599 expect(identical(arr, ret), isTrue); |
501 expect(ret, equals([9, 5, 1])); | 600 expect(ret, equals([9, 5, 1])); |
502 }); | 601 }); |
503 | 602 |
504 test("splice dummy items", () { | 603 test("splice dummy items", () { |
505 var list = [1, 2, 3, 4]; | 604 var list = [1, 2, 3, 4]; |
506 var removed = context.callMethod("spliceDummyItems", [list]); | 605 var removed = spliceDummyItems(list); |
507 expect(removed.length, equals(2)); | 606 expect(removed.length, equals(2)); |
508 expect(removed[0], equals(2)); | 607 expect(removed[0], equals(2)); |
509 expect(removed[1], equals(3)); | 608 expect(removed[1], equals(3)); |
510 expect(list.first, equals(1)); | 609 expect(list.first, equals(1)); |
511 expect(list[1], equals("quick")); | 610 expect(list[1], equals("quick")); |
512 expect(list[2], equals("brown")); | 611 expect(list[2], equals("brown")); |
513 expect(list[3], equals("fox")); | 612 expect(list[3], equals("fox")); |
514 expect(list.last, equals(4)); | 613 expect(list.last, equals(4)); |
515 }); | 614 }); |
516 | 615 |
517 test("splice string args", () { | 616 test("splice string args", () { |
518 var list = [1, 2, 3, 4]; | 617 var list = [1, 2, 3, 4]; |
519 var removed = context.callMethod("spliceTestStringArgs", [list]); | 618 var removed = spliceTestStringArgs(list); |
520 expect(removed.length, equals(2)); | 619 expect(removed.length, equals(2)); |
521 expect(removed[0], equals(2)); | 620 expect(removed[0], equals(2)); |
522 expect(removed[1], equals(3)); | 621 expect(removed[1], equals(3)); |
523 expect(list.first, equals(1)); | 622 expect(list.first, equals(1)); |
524 expect(list[1], equals("quick")); | 623 expect(list[1], equals("quick")); |
525 expect(list[2], equals("brown")); | 624 expect(list[2], equals("brown")); |
526 expect(list[3], equals("fox")); | 625 expect(list[3], equals("fox")); |
527 expect(list.last, equals(4)); | 626 expect(list.last, equals(4)); |
528 }); | 627 }); |
529 | 628 |
530 test("splice pastEndOfArray", () { | 629 test("splice pastEndOfArray", () { |
531 var list = [1, 2, 3, 4]; | 630 var list = [1, 2, 3, 4]; |
532 var removed = context.callMethod("splicePastEnd", [list]); | 631 var removed = splicePastEnd(list); |
533 expect(removed.length, equals(3)); | 632 expect(removed.length, equals(3)); |
534 expect(list.first, equals(1)); | 633 expect(list.first, equals(1)); |
535 expect(list.length, equals(4)); | 634 expect(list.length, equals(4)); |
536 expect(list[1], equals("quick")); | 635 expect(list[1], equals("quick")); |
537 expect(list[2], equals("brown")); | 636 expect(list[2], equals("brown")); |
538 expect(list[3], equals("fox")); | 637 expect(list[3], equals("fox")); |
539 }); | 638 }); |
540 | 639 |
541 test("splice both bounds past end of array", () { | 640 test("splice both bounds past end of array", () { |
542 var list = [1]; | 641 var list = [1]; |
543 var removed = context.callMethod("splicePastEnd", [list]); | 642 var removed = splicePastEnd(list); |
544 expect(removed.length, equals(0)); | 643 expect(removed.length, equals(0)); |
545 expect(list.first, equals(1)); | 644 expect(list.first, equals(1)); |
546 expect(list.length, equals(4)); | 645 expect(list.length, equals(4)); |
547 expect(list[1], equals("quick")); | 646 expect(list[1], equals("quick")); |
548 expect(list[2], equals("brown")); | 647 expect(list[2], equals("brown")); |
549 expect(list[3], equals("fox")); | 648 expect(list[3], equals("fox")); |
550 }); | 649 }); |
551 | 650 |
552 test("call List method on JavaScript object", () { | 651 test("call List method on JavaScript object", () { |
553 var jsObject = new JsObject.jsify({}); | 652 var jsObject = newLiteral(); |
554 callListMethodOnObject(jsObject, 'push', ["a"]); | 653 callListMethodOnObject(jsObject, 'push', ["a"]); |
555 callListMethodOnObject(jsObject, 'push', ["b"]); | 654 callListMethodOnObject(jsObject, 'push', ["b"]); |
556 callListMethodOnObject(jsObject, 'push', ["c", "d"]); | 655 callListMethodOnObject(jsObject, 'push', ["c", "d"]); |
557 callListMethodOnObject(jsObject, 'push', []); | 656 callListMethodOnObject(jsObject, 'push', []); |
558 | 657 |
559 expect(jsonStringify(jsObject), | 658 expect(json_helper.stringify(jsObject), |
560 equals('{"0":"a","1":"b","2":"c","3":"d","length":4}')); | 659 equals('{"0":"a","1":"b","2":"c","3":"d","length":4}')); |
561 | 660 |
562 expect(callListMethodOnObject(jsObject, 'pop', []), equals("d")); | 661 expect(callListMethodOnObject(jsObject, 'pop', []), equals("d")); |
563 expect(callListMethodOnObject(jsObject, 'join', ["#"]), equals("a#b#c")); | 662 expect(callListMethodOnObject(jsObject, 'join', ["#"]), equals("a#b#c")); |
564 | 663 |
565 var jsArray = new JsObject.jsify([]); | 664 var jsArray = newArray(); |
566 callListMethodOnObject(jsArray, 'push', ["a"]); | 665 callListMethodOnObject(jsArray, 'push', ["a"]); |
567 callListMethodOnObject(jsArray, 'push', ["b"]); | 666 callListMethodOnObject(jsArray, 'push', ["b"]); |
568 callListMethodOnObject(jsArray, 'push', ["c", "d"]); | 667 callListMethodOnObject(jsArray, 'push', ["c", "d"]); |
569 callListMethodOnObject(jsArray, 'push', []); | 668 callListMethodOnObject(jsArray, 'push', []); |
570 | 669 |
571 expect(jsonStringify(jsArray), equals('["a","b","c","d"]')); | 670 expect(json_helper.stringify(jsArray), equals('["a","b","c","d"]')); |
572 }); | 671 }); |
573 }); | 672 }); |
574 | 673 |
575 // This test group is disabled until we figure out an efficient way to | 674 // This test group is disabled until we figure out an efficient way to |
576 // distinguish between "array" Dart List types and non-array Dart list types. | 675 // distinguish between "array" Dart List types and non-array Dart list types. |
577 /* | 676 /* |
578 group('Non-array Lists', () { | 677 group('Non-array Lists', () { |
579 test('opaque proxy', () { | 678 test('opaque proxy', () { |
580 // Dartium could easily support making LinkedList and all other classes | 679 // Dartium could easily support making LinkedList and all other classes |
581 // implementing List behave like a JavaScript array but that would | 680 // implementing List behave like a JavaScript array but that would |
582 // be challenging to implement in dart2js until browsers support ES6. | 681 // be challenging to implement in dart2js until browsers support ES6. |
583 var list = ["a", "b", "c", "d"]; | 682 var list = ["a", "b", "c", "d"]; |
584 var listView = new UnmodifiableListView(list.getRange(1,3)); | 683 var listView = new UnmodifiableListView(list.getRange(1,3)); |
585 expect(listView is List, isTrue); | 684 expect(listView is List, isTrue); |
586 expect(listView.length, equals(2)); | 685 expect(listView.length, equals(2)); |
587 expect(context.callMethod("checkIsArray", [listView]), isFalse); | 686 expect(checkIsArray(listView), isFalse); |
588 expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue); | 687 expect(checkIsArray(listView.toList()), isTrue); |
589 expect(context.callMethod("getOwnPropertyDescriptorJson", | 688 expect(getOwnPropertyDescriptorJson( |
590 [listView, "length"]), equals("null")); | 689 listView, "length"), equals("null")); |
591 }); | 690 }); |
592 }); | 691 }); |
593 */ | 692 */ |
594 } | 693 } |
OLD | NEW |