OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Flags: --harmony-destructuring | 5 // Flags: --harmony-destructuring |
6 | 6 |
7 (function TestObjectLiteralPattern() { | 7 (function TestObjectLiteralPattern() { |
8 var { x : x, y : y } = { x : 1, y : 2 }; | 8 var { x : x, y : y } = { x : 1, y : 2 }; |
9 assertEquals(1, x); | 9 assertEquals(1, x); |
10 assertEquals(2, y); | 10 assertEquals(2, y); |
11 | 11 |
12 var {z} = { z : 3 }; | 12 var {z} = { z : 3 }; |
13 assertEquals(3, z); | 13 assertEquals(3, z); |
14 | 14 |
15 | 15 |
16 var sum = 0; | 16 var sum = 0; |
17 for(var {z} = { z : 3 }; z != 0; z--) { | 17 for(var {z} = { z : 3 }; z != 0; z--) { |
18 sum += z; | 18 sum += z; |
19 } | 19 } |
20 assertEquals(6, sum); | 20 assertEquals(6, sum); |
21 | 21 |
22 | 22 |
23 var countX = 0; | 23 var countX = 0; |
24 var countY = 0; | 24 var countY = 0; |
25 var countZ = 0; | 25 var countZ = 0; |
26 var o = { get x() { | 26 var o = { get x() { |
27 countX++; | 27 countX++; |
28 return 0; | 28 return 0; |
29 }, | 29 }, |
30 get y() { | 30 get y() { |
31 countY++; | 31 countY++; |
32 return { | 32 return { |
33 get z() { countZ++; return 1; } | 33 get z() { countZ++; return 1; } |
34 } | 34 } |
35 } | 35 } |
36 }; | 36 }; |
37 var { x : x0, y : { z : z1 }, x : x1 } = o; | 37 var { x : x0, y : { z : z1 }, x : x1 } = o; |
38 assertSame(0, x0); | 38 assertSame(0, x0); |
39 assertSame(1, z1); | 39 assertSame(1, z1); |
40 assertSame(0, x1); | 40 assertSame(0, x1); |
41 assertEquals(2, countX); | 41 assertEquals(2, countX); |
42 assertEquals(1, countY); | 42 assertEquals(1, countY); |
43 assertEquals(1, countZ); | 43 assertEquals(1, countZ); |
44 }()); | |
45 | |
46 | |
47 (function TestObjectLiteralPatternInitializers() { | |
48 var { x : x, y : y = 2 } = { x : 1 }; | |
49 assertEquals(1, x); | |
50 assertEquals(2, y); | |
51 | |
52 var {z = 3} = {}; | |
53 assertEquals(3, z); | |
54 | |
arv (Not doing code reviews)
2015/05/18 14:36:20
-1 newline
Dmitry Lomov (no reviews)
2015/05/18 17:31:23
Done.
| |
55 | |
56 var sum = 0; | |
57 for(var {z = 3} = {}; z != 0; z--) { | |
arv (Not doing code reviews)
2015/05/18 14:36:20
ws
Dmitry Lomov (no reviews)
2015/05/18 17:31:23
Done.
| |
58 sum += z; | |
59 } | |
60 assertEquals(6, sum); | |
61 | |
62 | |
rossberg
2015/05/18 16:17:20
Nit: newline
Dmitry Lomov (no reviews)
2015/05/18 17:31:23
Done.
| |
63 var countX = 0; | |
64 var countY = 0; | |
65 var countZ = 0; | |
66 var o = { get x() { | |
rossberg
2015/05/18 16:17:19
Can we modify this test (and below) such that it a
Dmitry Lomov (no reviews)
2015/05/18 17:31:23
Done.
| |
67 countX++; | |
68 return undefined; | |
69 }, | |
70 get y() { | |
71 countY++; | |
72 return { | |
73 get z() { countZ++; return undefined; } | |
74 } | |
75 } | |
76 }; | |
77 var { x : x0 = 0, y : { z : z1 = 1}, x : x1 = 0} = o; | |
78 assertSame(0, x0); | |
79 assertSame(1, z1); | |
80 assertSame(0, x1); | |
81 assertEquals(2, countX); | |
82 assertEquals(1, countY); | |
83 assertEquals(1, countZ); | |
84 }()); | |
85 | |
86 | |
87 (function TestObjectLiteralPatternLexicalInitializers() { | |
88 'use strict'; | |
89 let { x : x, y : y = 2 } = { x : 1 }; | |
90 assertEquals(1, x); | |
91 assertEquals(2, y); | |
92 | |
93 let {z = 3} = {}; | |
94 assertEquals(3, z); | |
95 | |
96 let countX = 0; | |
97 let countY = 0; | |
98 let countZ = 0; | |
99 let o = { get x() { | |
100 countX++; | |
101 return undefined; | |
102 }, | |
103 get y() { | |
104 countY++; | |
105 return { | |
106 get z() { countZ++; return undefined; } | |
107 } | |
108 } | |
109 }; | |
110 let { x : x0 = 0, y : { z : z1 = 1 }, x : x1 = 5} = o; | |
111 assertSame(0, x0); | |
112 assertSame(1, z1); | |
113 assertSame(5, x1); | |
114 assertEquals(2, countX); | |
115 assertEquals(1, countY); | |
116 assertEquals(1, countZ); | |
44 | 117 |
45 | 118 |
119 let sum = 0; | |
120 for(let {x = 0, z = 3} = {}; z != 0; z--) { | |
121 assertEquals(0, x); | |
122 sum += z; | |
123 } | |
124 assertEquals(6, sum); | |
46 }()); | 125 }()); |
47 | 126 |
48 | 127 |
49 (function TestObjectLiteralPatternLexical() { | 128 (function TestObjectLiteralPatternLexical() { |
50 'use strict'; | 129 'use strict'; |
51 let { x : x, y : y } = { x : 1, y : 2 }; | 130 let { x : x, y : y } = { x : 1, y : 2 }; |
52 assertEquals(1, x); | 131 assertEquals(1, x); |
53 assertEquals(2, y); | 132 assertEquals(2, y); |
54 | 133 |
55 let {z} = { z : 3 }; | 134 let {z} = { z : 3 }; |
56 assertEquals(3, z); | 135 assertEquals(3, z); |
57 | 136 |
58 let countX = 0; | 137 let countX = 0; |
59 let countY = 0; | 138 let countY = 0; |
60 let countZ = 0; | 139 let countZ = 0; |
61 let o = { get x() { | 140 let o = { get x() { |
62 countX++; | 141 countX++; |
63 return 0; | 142 return 0; |
64 }, | 143 }, |
65 get y() { | 144 get y() { |
66 countY++; | 145 countY++; |
67 return { | 146 return { |
68 get z() { countZ++; return 1; } | 147 get z() { countZ++; return 1; } |
69 } | 148 } |
70 } | 149 } |
71 }; | 150 }; |
72 let { x : x0, y : { z : z1 }, x : x1 } = o; | 151 let { x : x0, y : { z : z1 }, x : x1 } = o; |
73 assertSame(0, x0); | 152 assertSame(0, x0); |
74 assertSame(1, z1); | 153 assertSame(1, z1); |
75 assertSame(0, x1); | 154 assertSame(0, x1); |
76 assertEquals(2, countX); | 155 assertEquals(2, countX); |
(...skipping 21 matching lines...) Expand all Loading... | |
98 | 177 |
99 const {z} = { z : 3 }; | 178 const {z} = { z : 3 }; |
100 assertEquals(3, z); | 179 assertEquals(3, z); |
101 | 180 |
102 | 181 |
103 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { | 182 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { |
104 assertTrue(false); | 183 assertTrue(false); |
105 } | 184 } |
106 }()); | 185 }()); |
107 | 186 |
187 (function TestFailingMatchesSloppy() { | |
188 var {x, y} = {}; | |
189 assertSame(undefined, x); | |
190 assertSame(undefined, y); | |
191 | |
192 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
193 assertSame(undefined, z1); | |
194 assertSame(42, y2); | |
195 }()); | |
196 | |
197 | |
198 (function TestFailingMatchesStrict() { | |
199 'use strict'; | |
200 var {x, y} = {}; | |
201 assertSame(undefined, x); | |
202 assertSame(undefined, y); | |
203 | |
204 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
205 assertSame(undefined, z1); | |
206 assertSame(42, y2); | |
207 | |
208 { | |
209 let {x1,y1} = {}; | |
210 assertSame(undefined, x1); | |
211 assertSame(undefined, y1); | |
212 | |
213 let { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
214 assertSame(undefined, z1); | |
215 assertSame(42, y2); | |
216 } | |
217 }()); | |
218 | |
219 | |
220 (function TestTDZInIntializers() { | |
rossberg
2015/05/18 16:17:19
Add tests like
let {x, y = eval("x")} = {x:42}
le
Dmitry Lomov (no reviews)
2015/05/18 17:31:23
Done.
| |
221 'use strict'; | |
222 { | |
223 let {x, y = x} = {x : 42, y : 27}; | |
224 assertSame(42, x); | |
225 assertSame(27, y); | |
226 } | |
227 | |
228 { | |
229 let {x, y = x + 1} = { x : 42 }; | |
230 assertSame(42, x); | |
231 assertSame(43, y); | |
232 } | |
233 assertThrows(function() { | |
234 let {x = y, y} = { y : 42 }; | |
235 }, ReferenceError); | |
236 }()); | |
237 | |
238 | |
239 (function TestSideEffectsInInitializers() { | |
240 var callCount = 0; | |
241 function f(v) { callCount++; return v; } | |
242 | |
243 callCount = 0; | |
244 var { x = f(42) } = { x : 27 }; | |
245 assertSame(27, x); | |
246 assertEquals(0, callCount); | |
247 | |
248 callCount = 0; | |
249 var { x = f(42) } = {}; | |
250 assertSame(42, x); | |
251 assertEquals(1, callCount); | |
252 }()); | |
253 | |
108 | 254 |
arv (Not doing code reviews)
2015/05/18 14:36:20
Did you want to add tests for
let {x, x} = {x: 1
Dmitry Lomov (no reviews)
2015/05/18 17:31:23
Done.
| |
109 (function TestExceptions() { | 255 (function TestExceptions() { |
110 assertThrows(function() { var {} = null; }, TypeError); | 256 assertThrows(function() { var {} = null; }, TypeError); |
111 assertThrows(function() { 'use strict'; let {} = null; }, TypeError); | 257 assertThrows(function() { 'use strict'; let {} = null; }, TypeError); |
112 }()); | 258 }()); |
OLD | NEW |