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 'dart:js' as js; |
| 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 getOwnPropertyDescriptorJson: function(array, property) { |
| 134 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); |
| 135 }, |
| 136 |
| 137 setLength: function(array, len) { |
| 138 return array.length = len; |
| 139 }, |
| 140 |
| 141 getValue: function(obj, index) { |
| 142 return obj[index]; |
| 143 }, |
| 144 |
| 145 setValue: function(obj, index, value) { |
| 146 return obj[index] = value; |
| 147 }, |
| 148 |
| 149 // Calling a method from Dart List on an arbitrary target object. |
| 150 callListMethodOnTarget: function(dartArray, target, methodName, args) { |
| 151 return dartArray[methodName].apply(target, args); |
| 152 }, |
| 153 |
| 154 newArray: function() { return []; }, |
| 155 |
| 156 newLiteral: function() { return {}; }, |
| 157 |
| 158 }; |
| 159 """); |
19 } | 160 } |
20 | 161 |
21 function jsEnumerateIndices(obj) { | 162 @Js() |
22 var ret = []; | 163 class SimpleJsLiteralClass { |
23 for(var i in obj) { | 164 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 } | 165 } |
159 | 166 |
160 class Foo {} | 167 class Foo {} |
161 | 168 |
162 callJsMethod(List array, String methodName, List args) => | 169 @Js() |
163 context.callMethod("callJsMethod", [array, methodName, args]); | 170 external callJsMethod(List array, String methodName, List args); |
164 | 171 |
165 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); | 172 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); |
166 callLastIndexOf(List array, value) => | 173 callLastIndexOf(List array, value) => |
167 callJsMethod(array, "lastIndexOf", [value]); | 174 callJsMethod(array, "lastIndexOf", [value]); |
168 | 175 |
169 callPop(List array) => callJsMethod(array, "pop", []); | 176 callPop(List array) => callJsMethod(array, "pop", []); |
170 callPush(List array, element) => callJsMethod(array, "push", [element]); | 177 callPush(List array, element) => callJsMethod(array, "push", [element]); |
171 callShift(List array) => callJsMethod(array, "shift", []); | 178 callShift(List array) => callJsMethod(array, "shift", []); |
172 callReverse(List array) => callJsMethod(array, "reverse", []); | 179 callReverse(List array) => callJsMethod(array, "reverse", []); |
173 callSetLength(List array, length) => | |
174 context.callMethod("setLength", [array, length]); | |
175 | 180 |
176 callListMethodOnObject(JsObject object, String methodName, List args) => context | 181 callListMethodOnObject(object, String methodName, List args) => |
177 .callMethod("callListMethodOnTarget", [[], object, methodName, args]); | 182 callListMethodOnTarget([], object, methodName, args); |
178 | 183 |
179 jsonStringify(JsObject object) => context.callMethod("jsonStringify", [object]); | 184 @Js() |
| 185 external jsEnumerateIndices(obj); |
| 186 @Js() |
| 187 external bool checkIsArray(obj); |
| 188 @Js() |
| 189 external concatValues(obj); |
| 190 |
| 191 @Js() |
| 192 external concatOntoArray(obj); |
| 193 |
| 194 @Js() |
| 195 external repeatedConcatOntoArray(obj); |
| 196 @Js() |
| 197 external bool everyGreaterThanZero(obj); |
| 198 @Js() |
| 199 external bool everyGreaterThanZeroCheckThisArg(obj); |
| 200 |
| 201 @Js() |
| 202 external filterGreater42(obj); |
| 203 |
| 204 @Js() |
| 205 external forEachCollectResult(List array); |
| 206 @Js() |
| 207 external someEqual42(List array); |
| 208 @Js() |
| 209 external sortNumbersBackwards(List array); |
| 210 |
| 211 @Js() |
| 212 external List spliceDummyItems(List array); |
| 213 |
| 214 @Js() |
| 215 external List spliceTestStringArgs(List array); |
| 216 |
| 217 @Js() |
| 218 external List splicePastEnd(List array); |
| 219 |
| 220 @Js() |
| 221 external String callJsToString(List array); |
| 222 |
| 223 @Js() |
| 224 external mapAddIndexToEachElement(List array); |
| 225 @Js() |
| 226 external reduceSumDoubledElements(List array); |
| 227 |
| 228 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
| 229 @Js() |
| 230 external reduceRightSumDoubledElements(List array); |
| 231 |
| 232 @Js() |
| 233 external getOwnPropertyDescriptorJson(List array, property); |
| 234 |
| 235 @Js("setLength") |
| 236 external callSetLength(List array, length); |
| 237 |
| 238 @Js() |
| 239 external getValue(obj, index); |
| 240 |
| 241 @Js() |
| 242 external setValue(obj, index, value); |
| 243 |
| 244 @Js() |
| 245 external callListMethodOnTarget(List target, object, String methodName, List arg
s); |
| 246 |
| 247 @Js() |
| 248 external newArray(); |
| 249 |
| 250 @Js() |
| 251 external newLiteral(); |
180 | 252 |
181 main() { | 253 main() { |
182 _injectJs(); | 254 _injectJs(); |
183 useHtmlConfiguration(); | 255 useHtmlConfiguration(); |
184 | 256 |
185 group('indexOf', () { | 257 group('indexOf', () { |
186 var div = new DivElement(); | 258 var div = new DivElement(); |
187 var list = [3, 42, "foo", 42, div]; | 259 var list = [3, 42, "foo", 42, div]; |
188 test('found', () { | 260 test('found', () { |
189 expect(callIndexOf(list, 3), equals(0)); | 261 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)); | 303 expect(list.length, equals(2)); |
232 }); | 304 }); |
233 }); | 305 }); |
234 | 306 |
235 group('join', () { | 307 group('join', () { |
236 var list = [3, 42, "foo"]; | 308 var list = [3, 42, "foo"]; |
237 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()]; | 309 var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()]; |
238 test('default', () { | 310 test('default', () { |
239 expect(callJsMethod(list, "join", []), equals("3,42,foo")); | 311 expect(callJsMethod(list, "join", []), equals("3,42,foo")); |
240 expect(callJsMethod(listWithDartClasses, "join", []), | 312 expect(callJsMethod(listWithDartClasses, "join", []), |
241 equals("3,Instance of 'Foo',42,foo,Instance of 'Object'")); | 313 equals("3,${new Foo()},42,foo,${new Object()}")); |
242 }); | 314 }); |
243 | 315 |
244 test('custom separator', () { | 316 test('custom separator', () { |
245 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo")); | 317 expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo")); |
246 }); | 318 }); |
247 }); | 319 }); |
248 | 320 |
249 group('lastIndexOf', () { | 321 group('lastIndexOf', () { |
250 var list = [3, 42, "foo", 42]; | 322 var list = [3, 42, "foo", 42]; |
251 test('found', () { | 323 test('found', () { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 expect(callJsMethod(list, "slice", [-2, 3]), equals([92])); | 440 expect(callJsMethod(list, "slice", [-2, 3]), equals([92])); |
369 | 441 |
370 // Past the end of the front of the array. | 442 // Past the end of the front of the array. |
371 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42])); | 443 expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42])); |
372 }); | 444 }); |
373 }); | 445 }); |
374 | 446 |
375 group("js snippet tests", () { | 447 group("js snippet tests", () { |
376 test("enumerate indices", () { | 448 test("enumerate indices", () { |
377 var list = ["a", "b", "c", "d"]; | 449 var list = ["a", "b", "c", "d"]; |
378 var indices = context.callMethod('jsEnumerateIndices', [list]); | 450 var indices = |
| 451 jsEnumerateIndices(list); |
379 expect(indices.length, equals(4)); | 452 expect(indices.length, equals(4)); |
380 for (int i = 0; i < 4; i++) { | 453 for (int i = 0; i < 4; i++) { |
381 expect(indices[i], equals('$i')); | 454 expect(indices[i], equals('$i')); |
382 } | 455 } |
383 }); | 456 }); |
384 | 457 |
385 test("set element", () { | 458 test("set element", () { |
386 var list = ["a", "b", "c", "d"]; | 459 var list = ["a", "b", "c", "d"]; |
387 context.callMethod('setValue', [list, 0, 42]); | 460 setValue(list, 0, 42); |
388 expect(list[0], equals(42)); | 461 expect(list[0], equals(42)); |
389 context.callMethod('setValue', [list, 1, 84]); | 462 setValue(list, 1, 84); |
390 expect(list[1], equals(84)); | 463 expect(list[1], equals(84)); |
391 context.callMethod( | 464 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)); | 465 expect(list.length, equals(7)); |
394 expect(list[4], equals(null)); | 466 expect(list[4], equals(null)); |
395 expect(list[6], equals(100)); | 467 expect(list[6], equals(100)); |
396 | 468 |
397 // These tests have to be commented out because we don't persist | 469 // These tests have to be commented out because we don't persist |
398 // JS proxies for Dart objects like we could/should. | 470 // JS proxies for Dart objects like we could/should. |
399 // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid array
index | 471 // setValue(list, -1, "foo"); // Not a valid array index |
400 // expect(context.callMethod('getValue', [list, -1]), equals("foo")); | 472 // expect(getValue(list, -1), equals("foo")); |
401 // expect(context.callMethod('getValue', [list, "-1"]), equals("foo")); | 473 // expect(getValue(list, "-1"), equals("foo")); |
402 }); | 474 }); |
403 | 475 |
404 test("get element", () { | 476 test("get element", () { |
405 var list = ["a", "b", "c", "d"]; | 477 var list = ["a", "b", "c", "d"]; |
406 expect(context.callMethod('getValue', [list, 0]), equals("a")); | 478 expect(getValue(list, 0), |
407 expect(context.callMethod('getValue', [list, 1]), equals("b")); | 479 equals("a")); |
408 expect(context.callMethod('getValue', [list, 6]), equals(null)); | 480 expect(getValue(list, 1), |
409 expect(context.callMethod('getValue', [list, -1]), equals(null)); | 481 equals("b")); |
| 482 expect(getValue(list, 6), |
| 483 equals(null)); |
| 484 expect(getValue(list, -1), |
| 485 equals(null)); |
410 | 486 |
411 expect(context.callMethod('getValue', [list, "0"]), equals("a")); | 487 expect(getValue(list, "0"), |
412 expect(context.callMethod('getValue', [list, "1"]), equals("b")); | 488 equals("a")); |
| 489 expect(getValue(list, "1"), |
| 490 equals("b")); |
413 }); | 491 }); |
414 | 492 |
415 test("is array", () { | 493 test("is array", () { |
416 var list = ["a", "b"]; | 494 var list = ["a", "b"]; |
417 expect(context.callMethod("checkIsArray", [list]), isTrue); | 495 expect(checkIsArray(list), isTrue); |
418 }); | 496 }); |
419 | 497 |
420 test("property descriptors", () { | 498 test("property descriptors", () { |
421 // This test matters to make behavior consistent with JS native arrays | 499 // This test matters to make behavior consistent with JS native arrays |
422 // and to make devtools integration work well. | 500 // and to make devtools integration work well. |
423 var list = ["a", "b"]; | 501 var list = ["a", "b"]; |
424 expect(context.callMethod("getOwnPropertyDescriptorJson", [list, 0]), | 502 expect(getOwnPropertyDescriptorJson(list, 0), |
425 equals('{"value":"a",' | 503 equals('{"value":"a",' |
426 '"writable":true,' | 504 '"writable":true,' |
427 '"enumerable":true,' | 505 '"enumerable":true,' |
428 '"configurable":true}')); | 506 '"configurable":true}')); |
429 | 507 |
430 expect( | 508 expect( |
431 context.callMethod("getOwnPropertyDescriptorJson", [list, "length"]), | 509 getOwnPropertyDescriptorJson(list, "length"), |
432 equals('{"value":2,' | 510 equals('{"value":2,' |
433 '"writable":true,' | 511 '"writable":true,' |
434 '"enumerable":false,' | 512 '"enumerable":false,' |
435 '"configurable":false}')); | 513 '"configurable":false}')); |
436 }); | 514 }); |
437 | 515 |
438 test("concat js arrays", () { | 516 test("concat js arrays", () { |
439 var list = ["1", "2"]; | 517 var list = ["1", "2"]; |
440 // Tests that calling the concat method from JS will flatten out JS arrays | 518 // 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} | 519 // 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}] | 520 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] |
443 var ret = context.callMethod("concatValues", [list]); | 521 var ret = concatValues(list); |
444 expect(list.length, equals(2)); | 522 expect(list.length, equals(2)); |
445 expect(ret.length, equals(8)); | 523 expect(ret.length, equals(8)); |
446 expect(ret[0], equals("1")); | 524 expect(ret[0], equals("1")); |
447 expect(ret[3], equals("b")); | 525 expect(ret[3], equals("b")); |
448 expect(ret[5], equals("d")); | 526 expect(ret[5], equals("d")); |
449 expect(ret[6], equals(42)); | 527 expect(ret[6], equals(42)); |
450 expect(ret[7]['foo'], equals(10)); | 528 SimpleJsLiteralClass item = ret[7]; |
| 529 expect(item.foo, equals(10)); |
451 }); | 530 }); |
452 | 531 |
453 test("concat onto arrays", () { | 532 test("concat onto arrays", () { |
454 // This test only passes if we have monkey patched the core Array object | 533 // This test only passes if we have monkey patched the core Array object |
455 // prototype to handle Dart Lists. | 534 // prototype to handle Dart Lists. |
456 var list = ["a", "b"]; | 535 var list = ["a", "b"]; |
457 var ret = context.callMethod("concatOntoArray", [list]); | 536 var ret = concatOntoArray(list); |
458 expect(list.length, equals(2)); | 537 expect(list.length, equals(2)); |
459 expect(ret, equals([1, 2, 3, "a", "b", "foo"])); | 538 expect(ret, equals([1, 2, 3, "a", "b", "foo"])); |
460 }); | 539 }); |
461 | 540 |
462 test("dart arrays on dart arrays", () { | 541 test("dart arrays on dart arrays", () { |
463 // This test only passes if we have monkey patched the core Array object | 542 // This test only passes if we have monkey patched the core Array object |
464 // prototype to handle Dart Lists. | 543 // prototype to handle Dart Lists. |
465 var list = ["a", "b"]; | 544 var list = ["a", "b"]; |
466 var ret = callJsMethod(list, "concat", [["c", "d"], "e", ["f", "g"]]); | 545 var ret = callJsMethod(list, "concat", [ |
| 546 ["c", "d"], |
| 547 "e", |
| 548 ["f", "g"] |
| 549 ]); |
467 expect(list.length, equals(2)); | 550 expect(list.length, equals(2)); |
468 expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"])); | 551 expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"])); |
469 }); | 552 }); |
470 | 553 |
471 test("every greater than zero", () { | 554 test("every greater than zero", () { |
472 expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), isTrue); | 555 expect( |
473 expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]), | 556 everyGreaterThanZero([1, 5]), |
474 isTrue); | 557 isTrue); |
475 expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), isFalse); | 558 expect( |
476 expect(context.callMethod("everyGreaterThanZero", [[]]), isTrue); | 559 everyGreaterThanZeroCheckThisArg([1, 5]), |
| 560 isTrue); |
| 561 expect( |
| 562 everyGreaterThanZero([1, 0]), |
| 563 isFalse); |
| 564 expect(everyGreaterThanZero([]), |
| 565 isTrue); |
477 }); | 566 }); |
478 | 567 |
479 test("filter greater than 42", () { | 568 test("filter greater than 42", () { |
480 expect(context.callMethod("filterGreater42", [[1, 5]]), equals([])); | 569 expect(filterGreater42([1, 5]), equals([])); |
481 expect(context.callMethod("filterGreater42", [[43, 5, 49]]), | 570 expect( |
| 571 filterGreater42([43, 5, 49]), |
482 equals([43, 49])); | 572 equals([43, 49])); |
483 expect(context.callMethod("filterGreater42", [["43", "5", "49"]]), | 573 expect( |
| 574 filterGreater42(["43", "5", "49"]), |
484 equals(["43", "49"])); | 575 equals(["43", "49"])); |
485 }); | 576 }); |
486 | 577 |
487 test("for each collect result", () { | 578 test("for each collect result", () { |
488 expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]), | 579 expect( |
| 580 forEachCollectResult([1, 5, 7]), |
489 equals([2, 10, 14])); | 581 equals([2, 10, 14])); |
490 }); | 582 }); |
491 | 583 |
492 test("some", () { | 584 test("some", () { |
493 expect(context.callMethod("someEqual42", [[1, 5, 9]]), isFalse); | 585 expect(someEqual42([1, 5, 9]), |
494 expect(context.callMethod("someEqual42", [[1, 42, 9]]), isTrue); | 586 isFalse); |
| 587 expect(someEqual42([1, 42, 9]), |
| 588 isTrue); |
495 }); | 589 }); |
496 | 590 |
497 test("sort backwards", () { | 591 test("sort backwards", () { |
498 var arr = [1, 5, 9]; | 592 var arr = [1, 5, 9]; |
499 var ret = context.callMethod("sortNumbersBackwards", [arr]); | 593 var ret = sortNumbersBackwards(arr); |
500 expect(identical(arr, ret), isTrue); | 594 expect(identical(arr, ret), isTrue); |
501 expect(ret, equals([9, 5, 1])); | 595 expect(ret, equals([9, 5, 1])); |
502 }); | 596 }); |
503 | 597 |
504 test("splice dummy items", () { | 598 test("splice dummy items", () { |
505 var list = [1, 2, 3, 4]; | 599 var list = [1, 2, 3, 4]; |
506 var removed = context.callMethod("spliceDummyItems", [list]); | 600 var removed = spliceDummyItems(list); |
507 expect(removed.length, equals(2)); | 601 expect(removed.length, equals(2)); |
508 expect(removed[0], equals(2)); | 602 expect(removed[0], equals(2)); |
509 expect(removed[1], equals(3)); | 603 expect(removed[1], equals(3)); |
510 expect(list.first, equals(1)); | 604 expect(list.first, equals(1)); |
511 expect(list[1], equals("quick")); | 605 expect(list[1], equals("quick")); |
512 expect(list[2], equals("brown")); | 606 expect(list[2], equals("brown")); |
513 expect(list[3], equals("fox")); | 607 expect(list[3], equals("fox")); |
514 expect(list.last, equals(4)); | 608 expect(list.last, equals(4)); |
515 }); | 609 }); |
516 | 610 |
517 test("splice string args", () { | 611 test("splice string args", () { |
518 var list = [1, 2, 3, 4]; | 612 var list = [1, 2, 3, 4]; |
519 var removed = context.callMethod("spliceTestStringArgs", [list]); | 613 var removed = spliceTestStringArgs(list); |
520 expect(removed.length, equals(2)); | 614 expect(removed.length, equals(2)); |
521 expect(removed[0], equals(2)); | 615 expect(removed[0], equals(2)); |
522 expect(removed[1], equals(3)); | 616 expect(removed[1], equals(3)); |
523 expect(list.first, equals(1)); | 617 expect(list.first, equals(1)); |
524 expect(list[1], equals("quick")); | 618 expect(list[1], equals("quick")); |
525 expect(list[2], equals("brown")); | 619 expect(list[2], equals("brown")); |
526 expect(list[3], equals("fox")); | 620 expect(list[3], equals("fox")); |
527 expect(list.last, equals(4)); | 621 expect(list.last, equals(4)); |
528 }); | 622 }); |
529 | 623 |
530 test("splice pastEndOfArray", () { | 624 test("splice pastEndOfArray", () { |
531 var list = [1, 2, 3, 4]; | 625 var list = [1, 2, 3, 4]; |
532 var removed = context.callMethod("splicePastEnd", [list]); | 626 var removed = splicePastEnd(list); |
533 expect(removed.length, equals(3)); | 627 expect(removed.length, equals(3)); |
534 expect(list.first, equals(1)); | 628 expect(list.first, equals(1)); |
535 expect(list.length, equals(4)); | 629 expect(list.length, equals(4)); |
536 expect(list[1], equals("quick")); | 630 expect(list[1], equals("quick")); |
537 expect(list[2], equals("brown")); | 631 expect(list[2], equals("brown")); |
538 expect(list[3], equals("fox")); | 632 expect(list[3], equals("fox")); |
539 }); | 633 }); |
540 | 634 |
541 test("splice both bounds past end of array", () { | 635 test("splice both bounds past end of array", () { |
542 var list = [1]; | 636 var list = [1]; |
543 var removed = context.callMethod("splicePastEnd", [list]); | 637 var removed = splicePastEnd(list); |
544 expect(removed.length, equals(0)); | 638 expect(removed.length, equals(0)); |
545 expect(list.first, equals(1)); | 639 expect(list.first, equals(1)); |
546 expect(list.length, equals(4)); | 640 expect(list.length, equals(4)); |
547 expect(list[1], equals("quick")); | 641 expect(list[1], equals("quick")); |
548 expect(list[2], equals("brown")); | 642 expect(list[2], equals("brown")); |
549 expect(list[3], equals("fox")); | 643 expect(list[3], equals("fox")); |
550 }); | 644 }); |
551 | 645 |
552 test("call List method on JavaScript object", () { | 646 test("call List method on JavaScript object", () { |
553 var jsObject = new JsObject.jsify({}); | 647 var jsObject = newLiteral(); |
554 callListMethodOnObject(jsObject, 'push', ["a"]); | 648 callListMethodOnObject(jsObject, 'push', ["a"]); |
555 callListMethodOnObject(jsObject, 'push', ["b"]); | 649 callListMethodOnObject(jsObject, 'push', ["b"]); |
556 callListMethodOnObject(jsObject, 'push', ["c", "d"]); | 650 callListMethodOnObject(jsObject, 'push', ["c", "d"]); |
557 callListMethodOnObject(jsObject, 'push', []); | 651 callListMethodOnObject(jsObject, 'push', []); |
558 | 652 |
559 expect(jsonStringify(jsObject), | 653 expect(json_helper.stringify(jsObject), |
560 equals('{"0":"a","1":"b","2":"c","3":"d","length":4}')); | 654 equals('{"0":"a","1":"b","2":"c","3":"d","length":4}')); |
561 | 655 |
562 expect(callListMethodOnObject(jsObject, 'pop', []), equals("d")); | 656 expect(callListMethodOnObject(jsObject, 'pop', []), equals("d")); |
563 expect(callListMethodOnObject(jsObject, 'join', ["#"]), equals("a#b#c")); | 657 expect(callListMethodOnObject(jsObject, 'join', ["#"]), equals("a#b#c")); |
564 | 658 |
565 var jsArray = new JsObject.jsify([]); | 659 var jsArray = newArray(); |
566 callListMethodOnObject(jsArray, 'push', ["a"]); | 660 callListMethodOnObject(jsArray, 'push', ["a"]); |
567 callListMethodOnObject(jsArray, 'push', ["b"]); | 661 callListMethodOnObject(jsArray, 'push', ["b"]); |
568 callListMethodOnObject(jsArray, 'push', ["c", "d"]); | 662 callListMethodOnObject(jsArray, 'push', ["c", "d"]); |
569 callListMethodOnObject(jsArray, 'push', []); | 663 callListMethodOnObject(jsArray, 'push', []); |
570 | 664 |
571 expect(jsonStringify(jsArray), equals('["a","b","c","d"]')); | 665 expect(json_helper.stringify(jsArray), equals('["a","b","c","d"]')); |
572 }); | 666 }); |
573 }); | 667 }); |
574 | 668 |
575 // This test group is disabled until we figure out an efficient way to | 669 // 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. | 670 // distinguish between "array" Dart List types and non-array Dart list types. |
577 /* | 671 /* |
578 group('Non-array Lists', () { | 672 group('Non-array Lists', () { |
579 test('opaque proxy', () { | 673 test('opaque proxy', () { |
580 // Dartium could easily support making LinkedList and all other classes | 674 // Dartium could easily support making LinkedList and all other classes |
581 // implementing List behave like a JavaScript array but that would | 675 // implementing List behave like a JavaScript array but that would |
582 // be challenging to implement in dart2js until browsers support ES6. | 676 // be challenging to implement in dart2js until browsers support ES6. |
583 var list = ["a", "b", "c", "d"]; | 677 var list = ["a", "b", "c", "d"]; |
584 var listView = new UnmodifiableListView(list.getRange(1,3)); | 678 var listView = new UnmodifiableListView(list.getRange(1,3)); |
585 expect(listView is List, isTrue); | 679 expect(listView is List, isTrue); |
586 expect(listView.length, equals(2)); | 680 expect(listView.length, equals(2)); |
587 expect(context.callMethod("checkIsArray", [listView]), isFalse); | 681 expect(checkIsArray(listView), isFalse); |
588 expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue); | 682 expect(checkIsArray(listView.toList()), isTrue); |
589 expect(context.callMethod("getOwnPropertyDescriptorJson", | 683 expect(getOwnPropertyDescriptorJson( |
590 [listView, "length"]), equals("null")); | 684 listView, "length"), equals("null")); |
591 }); | 685 }); |
592 }); | 686 }); |
593 */ | 687 */ |
594 } | 688 } |
OLD | NEW |