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 | 9 |
| 10 import 'package:js/js.dart'; |
10 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
11 import 'package:unittest/html_config.dart'; | 12 import 'package:unittest/html_config.dart'; |
| 13 import 'json_helper.dart' as json_helper; |
12 | 14 |
13 _injectJs() { | 15 _injectJs() { |
14 document.body.append(new ScriptElement() | 16 document.body.append(new ScriptElement() |
15 ..type = 'text/javascript' | 17 ..type = 'text/javascript' |
16 ..innerHtml = r""" | 18 ..innerHtml = r""" |
17 function callJsMethod(jsObj, jsMethodName, args) { | 19 ArrayTest = {}; |
18 return jsObj[jsMethodName].apply(jsObj, args); | 20 ArrayTest.Util = { |
| 21 callJsMethod: function(jsObj, jsMethodName, args) { |
| 22 return jsObj[jsMethodName].apply(jsObj, args); |
| 23 }, |
| 24 |
| 25 jsEnumerateIndices: function(obj) { |
| 26 var ret = []; |
| 27 for(var i in obj) { |
| 28 ret.push(i); |
| 29 } |
| 30 return ret; |
| 31 }, |
| 32 |
| 33 checkIsArray: function(obj) { |
| 34 return Array.isArray(obj); |
| 35 }, |
| 36 |
| 37 concatValues: function(obj) { |
| 38 return obj.concat("a", "b", ["c", "d"], 42, {foo: 10}); |
| 39 }, |
| 40 |
| 41 concatOntoArray: function(obj) { |
| 42 return [1,2,3].concat(obj, "foo"); |
| 43 }, |
| 44 |
| 45 repeatedConcatOntoArray: function(obj) { |
| 46 return [1,2,3].concat(obj, obj); |
| 47 }, |
| 48 |
| 49 everyGreaterThanZero: function(obj) { |
| 50 return obj.every(function(currentValue, index, array) { |
| 51 return currentValue > 0; |
| 52 }); |
| 53 }, |
| 54 |
| 55 everyGreaterThanZeroCheckThisArg: function(obj) { |
| 56 var j = 0; |
| 57 return obj.every(function(currentValue, index, array) { |
| 58 if (j != index) { |
| 59 throw "Unxpected index"; |
| 60 } |
| 61 j++; |
| 62 if (array !== obj) { |
| 63 throw "Array argument doesn't match obj"; |
| 64 } |
| 65 return currentValue > 0; |
| 66 }); |
| 67 }, |
| 68 |
| 69 filterGreater42: function(obj) { |
| 70 return obj.filter(function(currentValue, index, array) { |
| 71 return currentValue > 42; |
| 72 }); |
| 73 }, |
| 74 |
| 75 forEachCollectResult: function(array) { |
| 76 var result = []; |
| 77 array.forEach(function(currentValue) { |
| 78 result.push(currentValue * 2); |
| 79 }); |
| 80 return result; |
| 81 }, |
| 82 |
| 83 someEqual42: function(array) { |
| 84 return array.some(function(currentValue) { |
| 85 return currentValue == 42; |
| 86 }); |
| 87 }, |
| 88 |
| 89 sortNumbersBackwards: function(array) { |
| 90 return array.sort(function(a, b) { |
| 91 return b - a; |
| 92 }); |
| 93 }, |
| 94 |
| 95 spliceDummyItems: function(array) { |
| 96 return array.splice(1, 2, "quick" ,"brown", "fox"); |
| 97 }, |
| 98 |
| 99 spliceTestStringArgs: function(array) { |
| 100 return array.splice("1.2", "2.01", "quick" ,"brown", "fox"); |
| 101 }, |
| 102 |
| 103 splicePastEnd: function(array) { |
| 104 return array.splice(1, 5332, "quick" ,"brown", "fox"); |
| 105 }, |
| 106 |
| 107 callJsToString: function(array) { |
| 108 return array.toString(); |
| 109 }, |
| 110 |
| 111 mapAddIndexToEachElement: function(array) { |
| 112 return array.map(function(currentValue, index) { |
| 113 return currentValue + index; |
| 114 }); |
| 115 }, |
| 116 |
| 117 reduceSumDoubledElements: function(array) { |
| 118 return array.reduce(function(previousValue, currentValue) { |
| 119 return previousValue + currentValue*2; |
| 120 }, |
| 121 0); |
| 122 }, |
| 123 |
| 124 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
| 125 reduceRightSumDoubledElements: function(array) { |
| 126 return array.reduceRight(function(previousValue, currentValue) { |
| 127 return previousValue + currentValue*2; |
| 128 }, |
| 129 0); |
| 130 }, |
| 131 |
| 132 identical: function(o1, o2) { |
| 133 return o1 === o2; |
| 134 }, |
| 135 |
| 136 getOwnPropertyDescriptorJson: function(array, property) { |
| 137 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); |
| 138 }, |
| 139 |
| 140 setLength: function(array, len) { |
| 141 return array.length = len; |
| 142 }, |
| 143 |
| 144 getValue: function(obj, index) { |
| 145 return obj[index]; |
| 146 }, |
| 147 |
| 148 setValue: function(obj, index, value) { |
| 149 return obj[index] = value; |
| 150 }, |
| 151 |
| 152 // Calling a method from Dart List on an arbitrary target object. |
| 153 callListMethodOnTarget: function(dartArray, target, methodName, args) { |
| 154 return dartArray[methodName].apply(target, args); |
| 155 }, |
| 156 |
| 157 newArray: function() { return []; }, |
| 158 |
| 159 newLiteral: function() { return {}; }, |
| 160 |
| 161 }; |
| 162 """); |
19 } | 163 } |
20 | 164 |
21 function jsEnumerateIndices(obj) { | 165 @Js() |
22 var ret = []; | 166 class SimpleJsLiteralClass { |
23 for(var i in obj) { | 167 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 } | 168 } |
159 | 169 |
160 class Foo {} | 170 class Foo {} |
161 | 171 |
162 callJsMethod(List array, String methodName, List args) => | 172 @Js() |
163 context.callMethod("callJsMethod", [array, methodName, args]); | 173 external callJsMethod(List array, String methodName, List args); |
164 | 174 |
165 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); | 175 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); |
166 callLastIndexOf(List array, value) => | 176 callLastIndexOf(List array, value) => |
167 callJsMethod(array, "lastIndexOf", [value]); | 177 callJsMethod(array, "lastIndexOf", [value]); |
168 | 178 |
169 callPop(List array) => callJsMethod(array, "pop", []); | 179 callPop(List array) => callJsMethod(array, "pop", []); |
170 callPush(List array, element) => callJsMethod(array, "push", [element]); | 180 callPush(List array, element) => callJsMethod(array, "push", [element]); |
171 callShift(List array) => callJsMethod(array, "shift", []); | 181 callShift(List array) => callJsMethod(array, "shift", []); |
172 callReverse(List array) => callJsMethod(array, "reverse", []); | 182 callReverse(List array) => callJsMethod(array, "reverse", []); |
173 callSetLength(List array, length) => | |
174 context.callMethod("setLength", [array, length]); | |
175 | 183 |
176 callListMethodOnObject(JsObject object, String methodName, List args) => context | 184 callListMethodOnObject(object, String methodName, List args) => |
177 .callMethod("callListMethodOnTarget", [[], object, methodName, args]); | 185 callListMethodOnTarget([], object, methodName, args); |
178 | 186 |
179 jsonStringify(JsObject object) => context.callMethod("jsonStringify", [object]); | 187 @Js() |
| 188 external jsEnumerateIndices(obj); |
| 189 @Js() |
| 190 external bool checkIsArray(obj); |
| 191 @Js() |
| 192 external concatValues(obj); |
| 193 |
| 194 @Js() |
| 195 external concatOntoArray(obj); |
| 196 |
| 197 @Js() |
| 198 external repeatedConcatOntoArray(obj); |
| 199 @Js() |
| 200 external bool everyGreaterThanZero(obj); |
| 201 @Js() |
| 202 external bool everyGreaterThanZeroCheckThisArg(obj); |
| 203 |
| 204 @Js() |
| 205 external filterGreater42(obj); |
| 206 |
| 207 @Js() |
| 208 external forEachCollectResult(List array); |
| 209 @Js() |
| 210 external someEqual42(List array); |
| 211 @Js() |
| 212 external sortNumbersBackwards(List array); |
| 213 |
| 214 @Js() |
| 215 external List spliceDummyItems(List array); |
| 216 |
| 217 @Js() |
| 218 external List spliceTestStringArgs(List array); |
| 219 |
| 220 @Js() |
| 221 external List splicePastEnd(List array); |
| 222 |
| 223 @Js() |
| 224 external String callJsToString(List array); |
| 225 |
| 226 @Js() |
| 227 external mapAddIndexToEachElement(List array); |
| 228 @Js() |
| 229 external reduceSumDoubledElements(List array); |
| 230 |
| 231 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
| 232 @Js() |
| 233 external reduceRightSumDoubledElements(List array); |
| 234 @Js() |
| 235 external identical(o1, o2); |
| 236 |
| 237 @Js() |
| 238 external getOwnPropertyDescriptorJson(List array, property); |
| 239 |
| 240 @Js("setLength") |
| 241 external callSetLength(List array, length); |
| 242 |
| 243 @Js() |
| 244 external getValue(obj, index); |
| 245 |
| 246 @Js() |
| 247 external setValue(obj, index, value); |
| 248 |
| 249 @Js() |
| 250 external callListMethodOnTarget(List target, object, String methodName, List arg
s); |
| 251 |
| 252 @Js() |
| 253 external newArray(); |
| 254 |
| 255 @Js() |
| 256 external newLiteral(); |
180 | 257 |
181 main() { | 258 main() { |
182 _injectJs(); | 259 _injectJs(); |
183 useHtmlConfiguration(); | 260 useHtmlConfiguration(); |
184 | 261 |
185 group('indexOf', () { | 262 group('indexOf', () { |
186 var div = new DivElement(); | 263 var div = new DivElement(); |
187 var list = [3, 42, "foo", 42, div]; | 264 var list = [3, 42, "foo", 42, div]; |
188 test('found', () { | 265 test('found', () { |
189 expect(callIndexOf(list, 3), equals(0)); | 266 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)); | 308 expect(list.length, equals(2)); |
232 }); | 309 }); |
233 }); | 310 }); |
234 | 311 |
235 group('join', () { | 312 group('join', () { |
236 var list = [3, 42, "foo"]; | 313 var list = [3, 42, "foo"]; |
237 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()]; | 314 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()]; |
238 test('default', () { | 315 test('default', () { |
239 expect(callJsMethod(list, "join", []), equals("3,42,foo")); | 316 expect(callJsMethod(list, "join", []), equals("3,42,foo")); |
240 expect(callJsMethod(listWithDartClasses, "join", []), | 317 expect(callJsMethod(listWithDartClasses, "join", []), |
241 equals("3,Instance of 'Foo',42,foo,Instance of 'Object'")); | 318 equals("3,${new Foo()},42,foo,${new Object()}")); |
242 }); | 319 }); |
243 | 320 |
244 test('custom separator', () { | 321 test('custom separator', () { |
245 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo")); | 322 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo")); |
246 }); | 323 }); |
247 }); | 324 }); |
248 | 325 |
249 group('lastIndexOf', () { | 326 group('lastIndexOf', () { |
250 var list = [3, 42, "foo", 42]; | 327 var list = [3, 42, "foo", 42]; |
251 test('found', () { | 328 test('found', () { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 expect(callJsMethod(list, "slice", [-2, 3]), equals([92])); | 445 expect(callJsMethod(list, "slice", [-2, 3]), equals([92])); |
369 | 446 |
370 // Past the end of the front of the array. | 447 // Past the end of the front of the array. |
371 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42])); | 448 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42])); |
372 }); | 449 }); |
373 }); | 450 }); |
374 | 451 |
375 group("js snippet tests", () { | 452 group("js snippet tests", () { |
376 test("enumerate indices", () { | 453 test("enumerate indices", () { |
377 var list = ["a", "b", "c", "d"]; | 454 var list = ["a", "b", "c", "d"]; |
378 var indices = context.callMethod('jsEnumerateIndices', [list]); | 455 var indices = |
| 456 jsEnumerateIndices(list); |
379 expect(indices.length, equals(4)); | 457 expect(indices.length, equals(4)); |
380 for (int i = 0; i < 4; i++) { | 458 for (int i = 0; i < 4; i++) { |
381 expect(indices[i], equals('$i')); | 459 expect(indices[i], equals('$i')); |
382 } | 460 } |
383 }); | 461 }); |
384 | 462 |
385 test("set element", () { | 463 test("set element", () { |
386 var list = ["a", "b", "c", "d"]; | 464 var list = ["a", "b", "c", "d"]; |
387 context.callMethod('setValue', [list, 0, 42]); | 465 setValue(list, 0, 42); |
388 expect(list[0], equals(42)); | 466 expect(list[0], equals(42)); |
389 context.callMethod('setValue', [list, 1, 84]); | 467 setValue(list, 1, 84); |
390 expect(list[1], equals(84)); | 468 expect(list[1], equals(84)); |
391 context.callMethod( | 469 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)); | 470 expect(list.length, equals(7)); |
394 expect(list[4], equals(null)); | 471 expect(list[4], equals(null)); |
395 expect(list[6], equals(100)); | 472 expect(list[6], equals(100)); |
396 | 473 |
397 // These tests have to be commented out because we don't persist | 474 // These tests have to be commented out because we don't persist |
398 // JS proxies for Dart objects like we could/should. | 475 // JS proxies for Dart objects like we could/should. |
399 // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid array
index | 476 // setValue(list, -1, "foo"); // Not a valid array index |
400 // expect(context.callMethod('getValue', [list, -1]), equals("foo")); | 477 // expect(getValue(list, -1), equals("foo")); |
401 // expect(context.callMethod('getValue', [list, "-1"]), equals("foo")); | 478 // expect(getValue(list, "-1"), equals("foo")); |
402 }); | 479 }); |
403 | 480 |
404 test("get element", () { | 481 test("get element", () { |
405 var list = ["a", "b", "c", "d"]; | 482 var list = ["a", "b", "c", "d"]; |
406 expect(context.callMethod('getValue', [list, 0]), equals("a")); | 483 expect(getValue(list, 0), |
407 expect(context.callMethod('getValue', [list, 1]), equals("b")); | 484 equals("a")); |
408 expect(context.callMethod('getValue', [list, 6]), equals(null)); | 485 expect(getValue(list, 1), |
409 expect(context.callMethod('getValue', [list, -1]), equals(null)); | 486 equals("b")); |
| 487 expect(getValue(list, 6), |
| 488 equals(null)); |
| 489 expect(getValue(list, -1), |
| 490 equals(null)); |
410 | 491 |
411 expect(context.callMethod('getValue', [list, "0"]), equals("a")); | 492 expect(getValue(list, "0"), |
412 expect(context.callMethod('getValue', [list, "1"]), equals("b")); | 493 equals("a")); |
| 494 expect(getValue(list, "1"), |
| 495 equals("b")); |
413 }); | 496 }); |
414 | 497 |
415 test("is array", () { | 498 test("is array", () { |
416 var list = ["a", "b"]; | 499 var list = ["a", "b"]; |
417 expect(context.callMethod("checkIsArray", [list]), isTrue); | 500 expect(checkIsArray(list), isTrue); |
418 }); | 501 }); |
419 | 502 |
420 test("property descriptors", () { | 503 test("property descriptors", () { |
421 // This test matters to make behavior consistent with JS native arrays | 504 // This test matters to make behavior consistent with JS native arrays |
422 // and to make devtools integration work well. | 505 // and to make devtools integration work well. |
423 var list = ["a", "b"]; | 506 var list = ["a", "b"]; |
424 expect(context.callMethod("getOwnPropertyDescriptorJson", [list, 0]), | 507 expect(getOwnPropertyDescriptorJson(list, 0), |
425 equals('{"value":"a",' | 508 equals('{"value":"a",' |
426 '"writable":true,' | 509 '"writable":true,' |
427 '"enumerable":true,' | 510 '"enumerable":true,' |
428 '"configurable":true}')); | 511 '"configurable":true}')); |
429 | 512 |
430 expect( | 513 expect( |
431 context.callMethod("getOwnPropertyDescriptorJson", [list, "length"]), | 514 getOwnPropertyDescriptorJson(list, "length"), |
432 equals('{"value":2,' | 515 equals('{"value":2,' |
433 '"writable":true,' | 516 '"writable":true,' |
434 '"enumerable":false,' | 517 '"enumerable":false,' |
435 '"configurable":false}')); | 518 '"configurable":false}')); |
436 }); | 519 }); |
437 | 520 |
438 test("concat js arrays", () { | 521 test("concat js arrays", () { |
439 var list = ["1", "2"]; | 522 var list = ["1", "2"]; |
440 // Tests that calling the concat method from JS will flatten out JS arrays | 523 // 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} | 524 // 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}] | 525 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] |
443 var ret = context.callMethod("concatValues", [list]); | 526 var ret = concatValues(list); |
444 expect(list.length, equals(2)); | 527 expect(list.length, equals(2)); |
445 expect(ret.length, equals(8)); | 528 expect(ret.length, equals(8)); |
446 expect(ret[0], equals("1")); | 529 expect(ret[0], equals("1")); |
447 expect(ret[3], equals("b")); | 530 expect(ret[3], equals("b")); |
448 expect(ret[5], equals("d")); | 531 expect(ret[5], equals("d")); |
449 expect(ret[6], equals(42)); | 532 expect(ret[6], equals(42)); |
450 expect(ret[7]['foo'], equals(10)); | 533 SimpleJsLiteralClass item = ret[7]; |
| 534 expect(item.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 |