Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: test/mjsunit/harmony/destructuring-assignment.js

Issue 1309813007: [es6] implement destructuring assignment (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Cache te right scope in DeclareAndInitializeVariables() Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/preparser.h ('K') | « test/cctest/test-parsing.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --harmony-destructuring-assignment --harmony-destructuring-bind
6
7 // script-level tests
8 var ox, oy = {}, oz;
9 ({
10 x: ox,
11 y: oy.value,
12 y2: oy["value2"],
13 z: ({ set v(val) { oz = val; } }).v
14 } = {
15 x: "value of x",
16 y: "value of y1",
17 y2: "value of y2",
18 z: "value of z"
19 });
20 assertEquals("value of x", ox);
21 assertEquals("value of y1", oy.value);
22 assertEquals("value of y2", oy.value2);
23 assertEquals("value of z", oz);
24
25 [ox, oy.value, oy["value2"], ...{ set v(val) { oz = val; } }.v] = [
26 1007,
27 798432,
28 555,
29 1, 2, 3, 4, 5
30 ];
31 assertEquals(ox, 1007);
32 assertEquals(oy.value, 798432);
33 assertEquals(oy.value2, 555);
34 assertEquals(oz, [1, 2, 3, 4, 5]);
35
36
37 (function testInFunction() {
38 var x, y = {}, z;
39 ({
40 x: x,
41 y: y.value,
42 y2: y["value2"],
43 z: ({ set v(val) { z = val; } }).v
44 } = {
45 x: "value of x",
46 y: "value of y1",
47 y2: "value of y2",
48 z: "value of z"
49 });
50 assertEquals("value of x", x);
51 assertEquals("value of y1", y.value);
52 assertEquals("value of y2", y.value2);
53 assertEquals("value of z", z);
54
55 [x, y.value, y["value2"], ...{ set v(val) { z = val; } }.v] = [
56 1007,
57 798432,
58 555,
59 1, 2, 3, 4, 5
60 ];
61 assertEquals(x, 1007);
62 assertEquals(y.value, 798432);
63 assertEquals(y.value2, 555);
64 assertEquals(z, [1, 2, 3, 4, 5]);
65 })();
66
67
68 (function testArrowFunctionInitializers() {
69 var fn = (config = { value: defaults.value } = { value: "BLAH" }) => config;
70 var defaults = {};
71 assertEquals({ value: "BLAH" }, fn());
72 assertEquals("BLAH", defaults.value);
73 })();
74
75
76 (function testArrowFunctionInitializers2() {
77 var fn = (config = [defaults.value] = ["BLAH"]) => config;
78 var defaults = {};
79 assertEquals(["BLAH"], fn());
80 assertEquals("BLAH", defaults.value);
81 })();
82
83
84 (function testFunctionInitializers() {
85 function fn(config = { value: defaults.value } = { value: "BLAH" }) {
86 return config;
87 }
88 var defaults = {};
89 assertEquals({ value: "BLAH" }, fn());
90 assertEquals("BLAH", defaults.value);
91 })();
92
93
94 (function testFunctionInitializers2() {
95 function fn(config = [defaults.value] = ["BLAH"]) { return config; }
96 var defaults = {};
97 assertEquals(["BLAH"], fn());
98 assertEquals("BLAH", defaults.value);
99 })();
100
101
102 (function testDeclarationInitializers() {
103 var defaults = {};
104 var { value } = { value: defaults.value } = { value: "BLAH" };
105 assertEquals("BLAH", value);
106 assertEquals("BLAH", defaults.value);
107 })();
108
109
110 (function testDeclarationInitializers2() {
111 var defaults = {};
112 var [value] = [defaults.value] = ["BLAH"];
113 assertEquals("BLAH", value);
114 assertEquals("BLAH", defaults.value);
115 })();
116
117
118 (function testObjectLiteralProperty() {
119 var ext = {};
120 var obj = {
121 a: { b: ext.b, c: ext["c"], d: { set v(val) { ext.d = val; } }.v } = {
122 b: "b", c: "c", d: "d" }
123 };
124 assertEquals({ b: "b", c: "c", d: "d" }, ext);
125 assertEquals({ a: { b: "b", c: "c", d: "d" } }, obj);
126 })();
127
128
129 (function testArrayLiteralProperty() {
130 var ext = {};
131 var obj = [
132 ...[ ext.b, ext["c"], { set v(val) { ext.d = val; } }.v ] = [
133 "b", "c", "d" ]
134 ];
135 assertEquals({ b: "b", c: "c", d: "d" }, ext);
136 assertEquals([ "b", "c", "d" ], obj);
137 })();
138
139
140 // TODO(caitp): add similar test for ArrayPatterns, once Proxies support
141 // delegating symbol-keyed get/set.
142 (function testObjectPatternOperationOrder() {
143 var steps = [];
144 var store = {};
145 function computePropertyName(name) {
146 steps.push("compute name: " + name);
147 return name;
148 }
149 function loadValue(descr, value) {
150 steps.push("load: " + descr + " > " + value);
151 return value;
152 }
153 function storeValue(descr, name, value) {
154 steps.push("store: " + descr + " = " + value);
155 store[name] = value;
156 }
157 var result = {
158 get a() { assertUnreachable(); },
159 set a(value) { storeValue("result.a", "a", value); },
160 get b() { assertUnreachable(); },
161 set b(value) { storeValue("result.b", "b", value); }
162 };
163
164 ({
165 obj: {
166 x: result.a = 10,
167 [computePropertyName("y")]: result.b = false,
168 } = {}
169 } = { obj: {
170 get x() { return loadValue(".temp.obj.x", undefined); },
171 set x(value) { assertUnreachable(); },
172 get y() { return loadValue(".temp.obj.y", undefined); },
173 set y(value) { assertUnreachable(); }
174 }});
175
176 assertPropertiesEqual({
177 a: 10,
178 b: false
179 }, store);
180
181 assertArrayEquals([
182 "load: .temp.obj.x > undefined",
183 "store: result.a = 10",
184
185 "compute name: y",
186 "load: .temp.obj.y > undefined",
187 "store: result.b = false"
188 ], steps);
189
190 steps = [];
191
192 ({
193 obj: {
194 x: result.a = 50,
195 [computePropertyName("y")]: result.b = "hello",
196 } = {}
197 } = { obj: {
198 get x() { return loadValue(".temp.obj.x", 20); },
199 set x(value) { assertUnreachable(); },
200 get y() { return loadValue(".temp.obj.y", true); },
201 set y(value) { assertUnreachable(); }
202 }});
203
204 assertPropertiesEqual({
205 a: 20,
206 b: true
207 }, store);
208
209 assertArrayEquals([
210 "load: .temp.obj.x > 20",
211 "store: result.a = 20",
212 "compute name: y",
213 "load: .temp.obj.y > true",
214 "store: result.b = true",
215 ], steps);
216 })();
OLDNEW
« src/preparser.h ('K') | « test/cctest/test-parsing.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698