Index: tests/html/js_array_test.dart |
diff --git a/tests/html/js_array_test.dart b/tests/html/js_array_test.dart |
index 4ede8ed70fb50a20e1b2c8adb3413eebb02b6151..039535eac1ac6b76156f25295e8861078b82b839 100644 |
--- a/tests/html/js_array_test.dart |
+++ b/tests/html/js_array_test.dart |
@@ -2,165 +2,172 @@ |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
-library jsArrayTest; |
+@Js("ArrayTest.Util") |
+library js_array_test; |
import 'dart:html'; |
-import 'dart:js'; |
+import 'dart:js' as js; |
+import 'package:js/js.dart'; |
import 'package:unittest/unittest.dart'; |
import 'package:unittest/html_config.dart'; |
+import 'json_helper.dart' as json_helper; |
_injectJs() { |
document.body.append(new ScriptElement() |
..type = 'text/javascript' |
..innerHtml = r""" |
-function callJsMethod(jsObj, jsMethodName, args) { |
- return jsObj[jsMethodName].apply(jsObj, args); |
-} |
+ArrayTest = {}; |
+ArrayTest.Util = { |
+ callJsMethod: function(jsObj, jsMethodName, args) { |
+ return jsObj[jsMethodName].apply(jsObj, args); |
+ }, |
+ |
+ jsEnumerateIndices: function(obj) { |
+ var ret = []; |
+ for(var i in obj) { |
+ ret.push(i); |
+ } |
+ return ret; |
+ }, |
-function jsEnumerateIndices(obj) { |
- var ret = []; |
- for(var i in obj) { |
- ret.push(i); |
- } |
- return ret; |
-} |
+ checkIsArray: function(obj) { |
+ return Array.isArray(obj); |
+ }, |
-function setValue(obj, index, value) { |
- return obj[index] = value; |
-} |
+ concatValues: function(obj) { |
+ return obj.concat("a", "b", ["c", "d"], 42, {foo: 10}); |
+ }, |
-function getValue(obj, index) { |
- return obj[index]; |
-} |
+ concatOntoArray: function(obj) { |
+ return [1,2,3].concat(obj, "foo"); |
+ }, |
-function checkIsArray(obj) { |
- return Array.isArray(obj); |
-} |
+ repeatedConcatOntoArray: function(obj) { |
+ return [1,2,3].concat(obj, obj); |
+ }, |
-function concatValues(obj) { |
- return obj.concat("a", "b", ["c", "d"], 42, {foo: 10}); |
-} |
+ everyGreaterThanZero: function(obj) { |
+ return obj.every(function(currentValue, index, array) { |
+ return currentValue > 0; |
+ }); |
+ }, |
-function concatOntoArray(obj) { |
- return [1,2,3].concat(obj, "foo"); |
-} |
+ everyGreaterThanZeroCheckThisArg: function(obj) { |
+ var j = 0; |
+ return obj.every(function(currentValue, index, array) { |
+ if (j != index) { |
+ throw "Unxpected index"; |
+ } |
+ j++; |
+ if (array !== obj) { |
+ throw "Array argument doesn't match obj"; |
+ } |
+ return currentValue > 0; |
+ }); |
+ }, |
-function repeatedConcatOntoArray(obj) { |
- return [1,2,3].concat(obj, obj); |
-} |
+ filterGreater42: function(obj) { |
+ return obj.filter(function(currentValue, index, array) { |
+ return currentValue > 42; |
+ }); |
+ }, |
-function everyGreaterThanZero(obj) { |
- return obj.every(function(currentValue, index, array) { |
- return currentValue > 0; |
- }); |
-} |
+ forEachCollectResult: function(array) { |
+ var result = []; |
+ array.forEach(function(currentValue) { |
+ result.push(currentValue * 2); |
+ }); |
+ return result; |
+ }, |
-function everyGreaterThanZeroCheckThisArg(obj) { |
- var j = 0; |
- return obj.every(function(currentValue, index, array) { |
- if (j != index) { |
- throw "Unxpected index"; |
- } |
- j++; |
- if (array !== obj) { |
- throw "Array argument doesn't match obj"; |
- } |
- return currentValue > 0; |
- }); |
-} |
+ someEqual42: function(array) { |
+ return array.some(function(currentValue) { |
+ return currentValue == 42; |
+ }); |
+ }, |
-function filterGreater42(obj) { |
- return obj.filter(function(currentValue, index, array) { |
- return currentValue > 42; |
- }); |
-} |
+ sortNumbersBackwards: function(array) { |
+ return array.sort(function(a, b) { |
+ return b - a; |
+ }); |
+ }, |
-function forEachCollectResult(array, callback) { |
- var result = []; |
- array.forEach(function(currentValue) { |
- result.push(currentValue * 2); |
- }); |
- return result; |
-} |
+ spliceDummyItems: function(array) { |
+ return array.splice(1, 2, "quick" ,"brown", "fox"); |
+ }, |
-function someEqual42(array, callback) { |
- return array.some(function(currentValue) { |
- return currentValue == 42; |
- }); |
-} |
+ spliceTestStringArgs: function(array) { |
+ return array.splice("1.2", "2.01", "quick" ,"brown", "fox"); |
+ }, |
-function sortNumbersBackwards(array) { |
- return array.sort(function(a, b) { |
- return b - a; |
- }); |
-} |
+ splicePastEnd: function(array) { |
+ return array.splice(1, 5332, "quick" ,"brown", "fox"); |
+ }, |
-function spliceDummyItems(array) { |
- return array.splice(1, 2, "quick" ,"brown", "fox"); |
-} |
+ callJsToString: function(array) { |
+ return array.toString(); |
+ }, |
-function spliceTestStringArgs(array) { |
- return array.splice("1.2", "2.01", "quick" ,"brown", "fox"); |
-} |
+ mapAddIndexToEachElement: function(array) { |
+ return array.map(function(currentValue, index) { |
+ return currentValue + index; |
+ }); |
+ }, |
-function splicePastEnd(array) { |
- return array.splice(1, 5332, "quick" ,"brown", "fox"); |
-} |
+ reduceSumDoubledElements: function(array) { |
+ return array.reduce(function(previousValue, currentValue) { |
+ return previousValue + currentValue*2; |
+ }, |
+ 0); |
+ }, |
-function callJsToString(array) { |
- return array.toString(); |
-} |
+ // TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
+ reduceRightSumDoubledElements: function(array) { |
+ return array.reduceRight(function(previousValue, currentValue) { |
+ return previousValue + currentValue*2; |
+ }, |
+ 0); |
+ }, |
-function mapAddIndexToEachElement(array) { |
- return array.map(function(currentValue, index) { |
- return currentValue + index; |
- }); |
-} |
+ getOwnPropertyDescriptorJson: function(array, property) { |
+ return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); |
+ }, |
-function reduceSumDoubledElements(array) { |
- return array.reduce(function(previousValue, currentValue) { |
- return previousValue + currentValue*2; |
- }, |
- 0); |
-} |
+ setLength: function(array, len) { |
+ return array.length = len; |
+ }, |
-// TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
-function reduceRightSumDoubledElements(array) { |
- return array.reduceRight(function(previousValue, currentValue) { |
- return previousValue + currentValue*2; |
- }, |
- 0); |
-} |
+ getValue: function(obj, index) { |
+ return obj[index]; |
+ }, |
-function identical(o1, o2) { |
- return o1 === o2; |
-} |
+ setValue: function(obj, index, value) { |
+ return obj[index] = value; |
+ }, |
-function getOwnPropertyDescriptorJson(array, property) { |
- return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); |
-} |
+ // Calling a method from Dart List on an arbitrary target object. |
+ callListMethodOnTarget: function(dartArray, target, methodName, args) { |
+ return dartArray[methodName].apply(target, args); |
+ }, |
-function setLength(array, len) { |
- return array.length = len; |
-} |
+ newArray: function() { return []; }, |
-function jsonStringify(o) { |
- return JSON.stringify(o); |
-} |
+ newLiteral: function() { return {}; }, |
-// Calling a method from Dart List on an arbitrary target object. |
-function callListMethodOnTarget(dartArray, target, methodName, args) { |
- return dartArray[methodName].apply(target, args); |
+}; |
+"""); |
} |
-"""); |
+@Js() |
+class SimpleJsLiteralClass { |
+ external get foo; |
} |
class Foo {} |
-callJsMethod(List array, String methodName, List args) => |
- context.callMethod("callJsMethod", [array, methodName, args]); |
+@Js() |
+external callJsMethod(List array, String methodName, List args); |
callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); |
callLastIndexOf(List array, value) => |
@@ -170,13 +177,78 @@ callPop(List array) => callJsMethod(array, "pop", []); |
callPush(List array, element) => callJsMethod(array, "push", [element]); |
callShift(List array) => callJsMethod(array, "shift", []); |
callReverse(List array) => callJsMethod(array, "reverse", []); |
-callSetLength(List array, length) => |
- context.callMethod("setLength", [array, length]); |
-callListMethodOnObject(JsObject object, String methodName, List args) => context |
- .callMethod("callListMethodOnTarget", [[], object, methodName, args]); |
+callListMethodOnObject(object, String methodName, List args) => |
+ callListMethodOnTarget([], object, methodName, args); |
+ |
+@Js() |
+external jsEnumerateIndices(obj); |
+@Js() |
+external bool checkIsArray(obj); |
+@Js() |
+external concatValues(obj); |
+ |
+@Js() |
+external concatOntoArray(obj); |
+ |
+@Js() |
+external repeatedConcatOntoArray(obj); |
+@Js() |
+external bool everyGreaterThanZero(obj); |
+@Js() |
+external bool everyGreaterThanZeroCheckThisArg(obj); |
+ |
+@Js() |
+external filterGreater42(obj); |
+ |
+@Js() |
+external forEachCollectResult(List array); |
+@Js() |
+external someEqual42(List array); |
+@Js() |
+external sortNumbersBackwards(List array); |
+ |
+@Js() |
+external List spliceDummyItems(List array); |
+ |
+@Js() |
+external List spliceTestStringArgs(List array); |
+ |
+@Js() |
+external List splicePastEnd(List array); |
+ |
+@Js() |
+external String callJsToString(List array); |
-jsonStringify(JsObject object) => context.callMethod("jsonStringify", [object]); |
+@Js() |
+external mapAddIndexToEachElement(List array); |
+@Js() |
+external reduceSumDoubledElements(List array); |
+ |
+// TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
+@Js() |
+external reduceRightSumDoubledElements(List array); |
+ |
+@Js() |
+external getOwnPropertyDescriptorJson(List array, property); |
+ |
+@Js("setLength") |
+external callSetLength(List array, length); |
+ |
+@Js() |
+external getValue(obj, index); |
+ |
+@Js() |
+external setValue(obj, index, value); |
+ |
+@Js() |
+external callListMethodOnTarget(List target, object, String methodName, List args); |
+ |
+@Js() |
+external newArray(); |
+ |
+@Js() |
+external newLiteral(); |
main() { |
_injectJs(); |
@@ -238,7 +310,7 @@ main() { |
test('default', () { |
expect(callJsMethod(list, "join", []), equals("3,42,foo")); |
expect(callJsMethod(listWithDartClasses, "join", []), |
- equals("3,Instance of 'Foo',42,foo,Instance of 'Object'")); |
+ equals("3,${new Foo()},42,foo,${new Object()}")); |
}); |
test('custom separator', () { |
@@ -375,7 +447,8 @@ main() { |
group("js snippet tests", () { |
test("enumerate indices", () { |
var list = ["a", "b", "c", "d"]; |
- var indices = context.callMethod('jsEnumerateIndices', [list]); |
+ var indices = |
+ jsEnumerateIndices(list); |
expect(indices.length, equals(4)); |
for (int i = 0; i < 4; i++) { |
expect(indices[i], equals('$i')); |
@@ -384,51 +457,56 @@ main() { |
test("set element", () { |
var list = ["a", "b", "c", "d"]; |
- context.callMethod('setValue', [list, 0, 42]); |
+ setValue(list, 0, 42); |
expect(list[0], equals(42)); |
- context.callMethod('setValue', [list, 1, 84]); |
+ setValue(list, 1, 84); |
expect(list[1], equals(84)); |
- context.callMethod( |
- 'setValue', [list, 6, 100]); // Off the end of the list. |
+ setValue(list, 6, 100); // Off the end of the list. |
expect(list.length, equals(7)); |
expect(list[4], equals(null)); |
expect(list[6], equals(100)); |
// These tests have to be commented out because we don't persist |
// JS proxies for Dart objects like we could/should. |
- // context.callMethod('setValue', [list, -1, "foo"]); // Not a valid array index |
- // expect(context.callMethod('getValue', [list, -1]), equals("foo")); |
- // expect(context.callMethod('getValue', [list, "-1"]), equals("foo")); |
+ // setValue(list, -1, "foo"); // Not a valid array index |
+ // expect(getValue(list, -1), equals("foo")); |
+ // expect(getValue(list, "-1"), equals("foo")); |
}); |
test("get element", () { |
var list = ["a", "b", "c", "d"]; |
- expect(context.callMethod('getValue', [list, 0]), equals("a")); |
- expect(context.callMethod('getValue', [list, 1]), equals("b")); |
- expect(context.callMethod('getValue', [list, 6]), equals(null)); |
- expect(context.callMethod('getValue', [list, -1]), equals(null)); |
+ expect(getValue(list, 0), |
+ equals("a")); |
+ expect(getValue(list, 1), |
+ equals("b")); |
+ expect(getValue(list, 6), |
+ equals(null)); |
+ expect(getValue(list, -1), |
+ equals(null)); |
- expect(context.callMethod('getValue', [list, "0"]), equals("a")); |
- expect(context.callMethod('getValue', [list, "1"]), equals("b")); |
+ expect(getValue(list, "0"), |
+ equals("a")); |
+ expect(getValue(list, "1"), |
+ equals("b")); |
}); |
test("is array", () { |
var list = ["a", "b"]; |
- expect(context.callMethod("checkIsArray", [list]), isTrue); |
+ expect(checkIsArray(list), isTrue); |
}); |
test("property descriptors", () { |
// This test matters to make behavior consistent with JS native arrays |
// and to make devtools integration work well. |
var list = ["a", "b"]; |
- expect(context.callMethod("getOwnPropertyDescriptorJson", [list, 0]), |
+ expect(getOwnPropertyDescriptorJson(list, 0), |
equals('{"value":"a",' |
'"writable":true,' |
'"enumerable":true,' |
'"configurable":true}')); |
expect( |
- context.callMethod("getOwnPropertyDescriptorJson", [list, "length"]), |
+ getOwnPropertyDescriptorJson(list, "length"), |
equals('{"value":2,' |
'"writable":true,' |
'"enumerable":false,' |
@@ -440,21 +518,22 @@ main() { |
// Tests that calling the concat method from JS will flatten out JS arrays |
// We concat the array with "a", "b", ["c", "d"], 42, {foo: 10} |
// which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] |
- var ret = context.callMethod("concatValues", [list]); |
+ var ret = concatValues(list); |
expect(list.length, equals(2)); |
expect(ret.length, equals(8)); |
expect(ret[0], equals("1")); |
expect(ret[3], equals("b")); |
expect(ret[5], equals("d")); |
expect(ret[6], equals(42)); |
- expect(ret[7]['foo'], equals(10)); |
+ SimpleJsLiteralClass item = ret[7]; |
+ expect(item.foo, equals(10)); |
}); |
test("concat onto arrays", () { |
// This test only passes if we have monkey patched the core Array object |
// prototype to handle Dart Lists. |
var list = ["a", "b"]; |
- var ret = context.callMethod("concatOntoArray", [list]); |
+ var ret = concatOntoArray(list); |
expect(list.length, equals(2)); |
expect(ret, equals([1, 2, 3, "a", "b", "foo"])); |
}); |
@@ -463,47 +542,62 @@ main() { |
// This test only passes if we have monkey patched the core Array object |
// prototype to handle Dart Lists. |
var list = ["a", "b"]; |
- var ret = callJsMethod(list, "concat", [["c", "d"], "e", ["f", "g"]]); |
+ var ret = callJsMethod(list, "concat", [ |
+ ["c", "d"], |
+ "e", |
+ ["f", "g"] |
+ ]); |
expect(list.length, equals(2)); |
expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"])); |
}); |
test("every greater than zero", () { |
- expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), isTrue); |
- expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]), |
+ expect( |
+ everyGreaterThanZero([1, 5]), |
+ isTrue); |
+ expect( |
+ everyGreaterThanZeroCheckThisArg([1, 5]), |
+ isTrue); |
+ expect( |
+ everyGreaterThanZero([1, 0]), |
+ isFalse); |
+ expect(everyGreaterThanZero([]), |
isTrue); |
- expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), isFalse); |
- expect(context.callMethod("everyGreaterThanZero", [[]]), isTrue); |
}); |
test("filter greater than 42", () { |
- expect(context.callMethod("filterGreater42", [[1, 5]]), equals([])); |
- expect(context.callMethod("filterGreater42", [[43, 5, 49]]), |
+ expect(filterGreater42([1, 5]), equals([])); |
+ expect( |
+ filterGreater42([43, 5, 49]), |
equals([43, 49])); |
- expect(context.callMethod("filterGreater42", [["43", "5", "49"]]), |
+ expect( |
+ filterGreater42(["43", "5", "49"]), |
equals(["43", "49"])); |
}); |
test("for each collect result", () { |
- expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]), |
+ expect( |
+ forEachCollectResult([1, 5, 7]), |
equals([2, 10, 14])); |
}); |
test("some", () { |
- expect(context.callMethod("someEqual42", [[1, 5, 9]]), isFalse); |
- expect(context.callMethod("someEqual42", [[1, 42, 9]]), isTrue); |
+ expect(someEqual42([1, 5, 9]), |
+ isFalse); |
+ expect(someEqual42([1, 42, 9]), |
+ isTrue); |
}); |
test("sort backwards", () { |
var arr = [1, 5, 9]; |
- var ret = context.callMethod("sortNumbersBackwards", [arr]); |
+ var ret = sortNumbersBackwards(arr); |
expect(identical(arr, ret), isTrue); |
expect(ret, equals([9, 5, 1])); |
}); |
test("splice dummy items", () { |
var list = [1, 2, 3, 4]; |
- var removed = context.callMethod("spliceDummyItems", [list]); |
+ var removed = spliceDummyItems(list); |
expect(removed.length, equals(2)); |
expect(removed[0], equals(2)); |
expect(removed[1], equals(3)); |
@@ -516,7 +610,7 @@ main() { |
test("splice string args", () { |
var list = [1, 2, 3, 4]; |
- var removed = context.callMethod("spliceTestStringArgs", [list]); |
+ var removed = spliceTestStringArgs(list); |
expect(removed.length, equals(2)); |
expect(removed[0], equals(2)); |
expect(removed[1], equals(3)); |
@@ -529,7 +623,7 @@ main() { |
test("splice pastEndOfArray", () { |
var list = [1, 2, 3, 4]; |
- var removed = context.callMethod("splicePastEnd", [list]); |
+ var removed = splicePastEnd(list); |
expect(removed.length, equals(3)); |
expect(list.first, equals(1)); |
expect(list.length, equals(4)); |
@@ -540,7 +634,7 @@ main() { |
test("splice both bounds past end of array", () { |
var list = [1]; |
- var removed = context.callMethod("splicePastEnd", [list]); |
+ var removed = splicePastEnd(list); |
expect(removed.length, equals(0)); |
expect(list.first, equals(1)); |
expect(list.length, equals(4)); |
@@ -550,25 +644,25 @@ main() { |
}); |
test("call List method on JavaScript object", () { |
- var jsObject = new JsObject.jsify({}); |
+ var jsObject = newLiteral(); |
callListMethodOnObject(jsObject, 'push', ["a"]); |
callListMethodOnObject(jsObject, 'push', ["b"]); |
callListMethodOnObject(jsObject, 'push', ["c", "d"]); |
callListMethodOnObject(jsObject, 'push', []); |
- expect(jsonStringify(jsObject), |
+ expect(json_helper.stringify(jsObject), |
equals('{"0":"a","1":"b","2":"c","3":"d","length":4}')); |
expect(callListMethodOnObject(jsObject, 'pop', []), equals("d")); |
expect(callListMethodOnObject(jsObject, 'join', ["#"]), equals("a#b#c")); |
- var jsArray = new JsObject.jsify([]); |
+ var jsArray = newArray(); |
callListMethodOnObject(jsArray, 'push', ["a"]); |
callListMethodOnObject(jsArray, 'push', ["b"]); |
callListMethodOnObject(jsArray, 'push', ["c", "d"]); |
callListMethodOnObject(jsArray, 'push', []); |
- expect(jsonStringify(jsArray), equals('["a","b","c","d"]')); |
+ expect(json_helper.stringify(jsArray), equals('["a","b","c","d"]')); |
}); |
}); |
@@ -584,10 +678,10 @@ main() { |
var listView = new UnmodifiableListView(list.getRange(1,3)); |
expect(listView is List, isTrue); |
expect(listView.length, equals(2)); |
- expect(context.callMethod("checkIsArray", [listView]), isFalse); |
- expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue); |
- expect(context.callMethod("getOwnPropertyDescriptorJson", |
- [listView, "length"]), equals("null")); |
+ expect(checkIsArray(listView), isFalse); |
+ expect(checkIsArray(listView.toList()), isTrue); |
+ expect(getOwnPropertyDescriptorJson( |
+ listView, "length"), equals("null")); |
}); |
}); |
*/ |