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