Index: test/mjsunit/harmony/destructuring-assignment.js |
diff --git a/test/mjsunit/harmony/destructuring-assignment.js b/test/mjsunit/harmony/destructuring-assignment.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..07df92fa5a9d1766358f31a93ff6fd4b1917626c |
--- /dev/null |
+++ b/test/mjsunit/harmony/destructuring-assignment.js |
@@ -0,0 +1,216 @@ |
+// Copyright 2015 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Flags: --harmony-destructuring-assignment --harmony-destructuring-bind |
+ |
+// script-level tests |
+var ox, oy = {}, oz; |
+({ |
+ x: ox, |
+ y: oy.value, |
+ y2: oy["value2"], |
+ z: ({ set v(val) { oz = val; } }).v |
+} = { |
+ x: "value of x", |
+ y: "value of y1", |
+ y2: "value of y2", |
+ z: "value of z" |
+}); |
+assertEquals("value of x", ox); |
+assertEquals("value of y1", oy.value); |
+assertEquals("value of y2", oy.value2); |
+assertEquals("value of z", oz); |
+ |
+[ox, oy.value, oy["value2"], ...{ set v(val) { oz = val; } }.v] = [ |
+ 1007, |
+ 798432, |
+ 555, |
+ 1, 2, 3, 4, 5 |
+]; |
+assertEquals(ox, 1007); |
+assertEquals(oy.value, 798432); |
+assertEquals(oy.value2, 555); |
+assertEquals(oz, [1, 2, 3, 4, 5]); |
+ |
+ |
+(function testInFunction() { |
+ var x, y = {}, z; |
+ ({ |
+ x: x, |
+ y: y.value, |
+ y2: y["value2"], |
+ z: ({ set v(val) { z = val; } }).v |
+ } = { |
+ x: "value of x", |
+ y: "value of y1", |
+ y2: "value of y2", |
+ z: "value of z" |
+ }); |
+ assertEquals("value of x", x); |
+ assertEquals("value of y1", y.value); |
+ assertEquals("value of y2", y.value2); |
+ assertEquals("value of z", z); |
+ |
+ [x, y.value, y["value2"], ...{ set v(val) { z = val; } }.v] = [ |
+ 1007, |
+ 798432, |
+ 555, |
+ 1, 2, 3, 4, 5 |
+ ]; |
+ assertEquals(x, 1007); |
+ assertEquals(y.value, 798432); |
+ assertEquals(y.value2, 555); |
+ assertEquals(z, [1, 2, 3, 4, 5]); |
+})(); |
+ |
+ |
+(function testArrowFunctionInitializers() { |
+ var fn = (config = { value: defaults.value } = { value: "BLAH" }) => config; |
+ var defaults = {}; |
+ assertEquals({ value: "BLAH" }, fn()); |
+ assertEquals("BLAH", defaults.value); |
+})(); |
+ |
+ |
+(function testArrowFunctionInitializers2() { |
+ var fn = (config = [defaults.value] = ["BLAH"]) => config; |
+ var defaults = {}; |
+ assertEquals(["BLAH"], fn()); |
+ assertEquals("BLAH", defaults.value); |
+})(); |
+ |
+ |
+(function testFunctionInitializers() { |
+ function fn(config = { value: defaults.value } = { value: "BLAH" }) { |
+ return config; |
+ } |
+ var defaults = {}; |
+ assertEquals({ value: "BLAH" }, fn()); |
+ assertEquals("BLAH", defaults.value); |
+})(); |
+ |
+ |
+(function testFunctionInitializers2() { |
+ function fn(config = [defaults.value] = ["BLAH"]) { return config; } |
+ var defaults = {}; |
+ assertEquals(["BLAH"], fn()); |
+ assertEquals("BLAH", defaults.value); |
+})(); |
+ |
+ |
+(function testDeclarationInitializers() { |
+ var defaults = {}; |
+ var { value } = { value: defaults.value } = { value: "BLAH" }; |
+ assertEquals("BLAH", value); |
+ assertEquals("BLAH", defaults.value); |
+})(); |
+ |
+ |
+(function testDeclarationInitializers2() { |
+ var defaults = {}; |
+ var [value] = [defaults.value] = ["BLAH"]; |
+ assertEquals("BLAH", value); |
+ assertEquals("BLAH", defaults.value); |
+})(); |
+ |
+ |
+(function testObjectLiteralProperty() { |
+ var ext = {}; |
+ var obj = { |
+ a: { b: ext.b, c: ext["c"], d: { set v(val) { ext.d = val; } }.v } = { |
+ b: "b", c: "c", d: "d" } |
+ }; |
+ assertEquals({ b: "b", c: "c", d: "d" }, ext); |
+ assertEquals({ a: { b: "b", c: "c", d: "d" } }, obj); |
+})(); |
+ |
+ |
+(function testArrayLiteralProperty() { |
+ var ext = {}; |
+ var obj = [ |
+ ...[ ext.b, ext["c"], { set v(val) { ext.d = val; } }.v ] = [ |
+ "b", "c", "d" ] |
+ ]; |
+ assertEquals({ b: "b", c: "c", d: "d" }, ext); |
+ assertEquals([ "b", "c", "d" ], obj); |
+})(); |
+ |
+ |
+// TODO(caitp): add similar test for ArrayPatterns, once Proxies support |
+// delegating symbol-keyed get/set. |
+(function testObjectPatternOperationOrder() { |
+ var steps = []; |
+ var store = {}; |
+ function computePropertyName(name) { |
+ steps.push("compute name: " + name); |
+ return name; |
+ } |
+ function loadValue(descr, value) { |
+ steps.push("load: " + descr + " > " + value); |
+ return value; |
+ } |
+ function storeValue(descr, name, value) { |
+ steps.push("store: " + descr + " = " + value); |
+ store[name] = value; |
+ } |
+ var result = { |
+ get a() { assertUnreachable(); }, |
+ set a(value) { storeValue("result.a", "a", value); }, |
+ get b() { assertUnreachable(); }, |
+ set b(value) { storeValue("result.b", "b", value); } |
+ }; |
+ |
+ ({ |
+ obj: { |
+ x: result.a = 10, |
+ [computePropertyName("y")]: result.b = false, |
+ } = {} |
+ } = { obj: { |
+ get x() { return loadValue(".temp.obj.x", undefined); }, |
+ set x(value) { assertUnreachable(); }, |
+ get y() { return loadValue(".temp.obj.y", undefined); }, |
+ set y(value) { assertUnreachable(); } |
+ }}); |
+ |
+ assertPropertiesEqual({ |
+ a: 10, |
+ b: false |
+ }, store); |
+ |
+ assertArrayEquals([ |
+ "load: .temp.obj.x > undefined", |
+ "store: result.a = 10", |
+ |
+ "compute name: y", |
+ "load: .temp.obj.y > undefined", |
+ "store: result.b = false" |
+ ], steps); |
+ |
+ steps = []; |
+ |
+ ({ |
+ obj: { |
+ x: result.a = 50, |
+ [computePropertyName("y")]: result.b = "hello", |
+ } = {} |
+ } = { obj: { |
+ get x() { return loadValue(".temp.obj.x", 20); }, |
+ set x(value) { assertUnreachable(); }, |
+ get y() { return loadValue(".temp.obj.y", true); }, |
+ set y(value) { assertUnreachable(); } |
+ }}); |
+ |
+ assertPropertiesEqual({ |
+ a: 20, |
+ b: true |
+ }, store); |
+ |
+ assertArrayEquals([ |
+ "load: .temp.obj.x > 20", |
+ "store: result.a = 20", |
+ "compute name: y", |
+ "load: .temp.obj.y > true", |
+ "store: result.b = true", |
+ ], steps); |
+})(); |