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..7b1b9302cf458f84a0be843fcad96f9b498c13b5 100644 |
--- a/tests/html/js_array_test.dart |
+++ b/tests/html/js_array_test.dart |
@@ -157,10 +157,20 @@ function callListMethodOnTarget(dartArray, target, methodName, args) { |
"""); |
} |
+// TODO(jacobr): depend on package:js instead of defining this annotation here. |
+class JsName { |
+ const JsName(); |
+} |
+ |
+@JsName() |
+class SimpleJsLiteralClass extends JavaScriptObject { |
+ get foo => JsNative.getProperty(this, 'foo'); |
+} |
+ |
class Foo {} |
-callJsMethod(List array, String methodName, List args) => |
- context.callMethod("callJsMethod", [array, methodName, args]); |
+callJsMethod(List array, String methodName, List args) => JsNative.callMethod3( |
+ nativeContext, "callJsMethod", array, methodName, args); |
callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); |
callLastIndexOf(List array, value) => |
@@ -171,12 +181,14 @@ 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]); |
+ JsNative.callMethod2(nativeContext, "setLength", array, length); |
-callListMethodOnObject(JsObject object, String methodName, List args) => context |
- .callMethod("callListMethodOnTarget", [[], object, methodName, args]); |
+callListMethodOnObject(object, String methodName, List args) => |
+ JsNative.callMethod4( |
+ nativeContext, "callListMethodOnTarget", [], object, methodName, args); |
-jsonStringify(JsObject object) => context.callMethod("jsonStringify", [object]); |
+jsonStringify(object) => |
+ JsNative.callMethod1(nativeContext, "jsonStringify", object); |
main() { |
_injectJs(); |
@@ -238,7 +250,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 +387,8 @@ main() { |
group("js snippet tests", () { |
test("enumerate indices", () { |
var list = ["a", "b", "c", "d"]; |
- var indices = context.callMethod('jsEnumerateIndices', [list]); |
+ var indices = |
+ JsNative.callMethod1(nativeContext, 'jsEnumerateIndices', list); |
expect(indices.length, equals(4)); |
for (int i = 0; i < 4; i++) { |
expect(indices[i], equals('$i')); |
@@ -384,51 +397,60 @@ main() { |
test("set element", () { |
var list = ["a", "b", "c", "d"]; |
- context.callMethod('setValue', [list, 0, 42]); |
+ JsNative.callMethod3(nativeContext, 'setValue', list, 0, 42); |
expect(list[0], equals(42)); |
- context.callMethod('setValue', [list, 1, 84]); |
+ JsNative.callMethod3(nativeContext, 'setValue', list, 1, 84); |
expect(list[1], equals(84)); |
- context.callMethod( |
- 'setValue', [list, 6, 100]); // Off the end of the list. |
+ JsNative.callMethod3( |
+ nativeContext, '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")); |
+ // JsNative.callMethod3(nativeContext, 'setValue', list, -1, "foo"); // Not a valid array index |
+ // expect(JsNative.callMethod2(nativeContext, 'getValue', list, -1), equals("foo")); |
+ // expect(JsNative.callMethod2(nativeContext, '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(JsNative.callMethod2(nativeContext, 'getValue', list, 0), |
+ equals("a")); |
+ expect(JsNative.callMethod2(nativeContext, 'getValue', list, 1), |
+ equals("b")); |
+ expect(JsNative.callMethod2(nativeContext, 'getValue', list, 6), |
+ equals(null)); |
+ expect(JsNative.callMethod2(nativeContext, 'getValue', list, -1), |
+ equals(null)); |
- expect(context.callMethod('getValue', [list, "0"]), equals("a")); |
- expect(context.callMethod('getValue', [list, "1"]), equals("b")); |
+ expect(JsNative.callMethod2(nativeContext, 'getValue', list, "0"), |
+ equals("a")); |
+ expect(JsNative.callMethod2(nativeContext, 'getValue', list, "1"), |
+ equals("b")); |
}); |
test("is array", () { |
var list = ["a", "b"]; |
- expect(context.callMethod("checkIsArray", [list]), isTrue); |
+ expect(JsNative.callMethod1(nativeContext, "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( |
+ JsNative.callMethod2( |
+ nativeContext, "getOwnPropertyDescriptorJson", list, 0), |
equals('{"value":"a",' |
'"writable":true,' |
'"enumerable":true,' |
'"configurable":true}')); |
expect( |
- context.callMethod("getOwnPropertyDescriptorJson", [list, "length"]), |
+ JsNative.callMethod2( |
+ nativeContext, "getOwnPropertyDescriptorJson", list, "length"), |
equals('{"value":2,' |
'"writable":true,' |
'"enumerable":false,' |
@@ -440,21 +462,21 @@ 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 = JsNative.callMethod1(nativeContext, "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)); |
+ expect(ret[7].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 = JsNative.callMethod1(nativeContext, "concatOntoArray", list); |
expect(list.length, equals(2)); |
expect(ret, equals([1, 2, 3, "a", "b", "foo"])); |
}); |
@@ -463,47 +485,68 @@ 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( |
+ JsNative.callMethod1(nativeContext, "everyGreaterThanZero", [1, 5]), |
+ isTrue); |
+ expect( |
+ JsNative.callMethod1( |
+ nativeContext, "everyGreaterThanZeroCheckThisArg", [1, 5]), |
+ isTrue); |
+ expect( |
+ JsNative.callMethod1(nativeContext, "everyGreaterThanZero", [1, 0]), |
+ isFalse); |
+ expect(JsNative.callMethod1(nativeContext, "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(JsNative.callMethod1(nativeContext, "filterGreater42", [1, 5]), |
+ equals([])); |
+ expect( |
+ JsNative.callMethod1(nativeContext, "filterGreater42", [43, 5, 49]), |
equals([43, 49])); |
- expect(context.callMethod("filterGreater42", [["43", "5", "49"]]), |
+ expect( |
+ JsNative.callMethod1( |
+ nativeContext, "filterGreater42", ["43", "5", "49"]), |
equals(["43", "49"])); |
}); |
test("for each collect result", () { |
- expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]), |
+ expect( |
+ JsNative.callMethod1( |
+ nativeContext, "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(JsNative.callMethod1(nativeContext, "someEqual42", [1, 5, 9]), |
+ isFalse); |
+ expect(JsNative.callMethod1(nativeContext, "someEqual42", [1, 42, 9]), |
+ isTrue); |
}); |
test("sort backwards", () { |
var arr = [1, 5, 9]; |
- var ret = context.callMethod("sortNumbersBackwards", [arr]); |
+ var ret = |
+ JsNative.callMethod1(nativeContext, "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 = |
+ JsNative.callMethod1(nativeContext, "spliceDummyItems", list); |
expect(removed.length, equals(2)); |
expect(removed[0], equals(2)); |
expect(removed[1], equals(3)); |
@@ -516,7 +559,8 @@ main() { |
test("splice string args", () { |
var list = [1, 2, 3, 4]; |
- var removed = context.callMethod("spliceTestStringArgs", [list]); |
+ var removed = |
+ JsNative.callMethod1(nativeContext, "spliceTestStringArgs", list); |
expect(removed.length, equals(2)); |
expect(removed[0], equals(2)); |
expect(removed[1], equals(3)); |
@@ -529,7 +573,7 @@ main() { |
test("splice pastEndOfArray", () { |
var list = [1, 2, 3, 4]; |
- var removed = context.callMethod("splicePastEnd", [list]); |
+ var removed = JsNative.callMethod1(nativeContext, "splicePastEnd", list); |
expect(removed.length, equals(3)); |
expect(list.first, equals(1)); |
expect(list.length, equals(4)); |
@@ -540,7 +584,7 @@ main() { |
test("splice both bounds past end of array", () { |
var list = [1]; |
- var removed = context.callMethod("splicePastEnd", [list]); |
+ var removed = JsNative.callMethod1(nativeContext, "splicePastEnd", list); |
expect(removed.length, equals(0)); |
expect(list.first, equals(1)); |
expect(list.length, equals(4)); |
@@ -550,7 +594,7 @@ main() { |
}); |
test("call List method on JavaScript object", () { |
- var jsObject = new JsObject.jsify({}); |
+ var jsObject = JsNative.newLiteral(); |
callListMethodOnObject(jsObject, 'push', ["a"]); |
callListMethodOnObject(jsObject, 'push', ["b"]); |
callListMethodOnObject(jsObject, 'push', ["c", "d"]); |
@@ -562,7 +606,7 @@ main() { |
expect(callListMethodOnObject(jsObject, 'pop', []), equals("d")); |
expect(callListMethodOnObject(jsObject, 'join', ["#"]), equals("a#b#c")); |
- var jsArray = new JsObject.jsify([]); |
+ var jsArray = JsNative.newArray(); |
callListMethodOnObject(jsArray, 'push', ["a"]); |
callListMethodOnObject(jsArray, 'push', ["b"]); |
callListMethodOnObject(jsArray, 'push', ["c", "d"]); |
@@ -584,10 +628,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(JsNative.callMethod1(nativeContext, "checkIsArray", listView), isFalse); |
+ expect(JsNative.callMethod1(nativeContext, "checkIsArray", listView.toList()), isTrue); |
+ expect(JsNative.callMethod2(nativeContext, "getOwnPropertyDescriptorJson", |
+ listView, "length"), equals("null")); |
}); |
}); |
*/ |