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 | |
22 | |
23 var log = []; | |
24 var o = { | |
25 get x() { | |
26 log.push("x"); | |
27 return 0; | |
28 }, | |
29 get y() { | |
30 log.push("y"); | |
31 return { | |
32 get z() { log.push("z"); return 1; } | |
33 } | |
34 } | |
35 }; | |
36 var { x : x0, y : { z : z1 }, x : x1 } = o; | |
37 assertSame(0, x0); | |
38 assertSame(1, z1); | |
39 assertSame(0, x1); | |
40 assertArrayEquals(["x", "y", "z", "x"], log); | |
arv (Not doing code reviews)
2015/05/18 14:43:42
thanks
Dmitry Lomov (no reviews)
2015/05/18 16:28:23
Acknowledged.
| |
21 }()); | 41 }()); |
22 | 42 |
23 | 43 |
24 (function TestObjectLiteralPatternLexical() { | 44 (function TestObjectLiteralPatternLexical() { |
25 'use strict'; | 45 'use strict'; |
26 let { x : x, y : y } = { x : 1, y : 2 }; | 46 let { x : x, y : y } = { x : 1, y : 2 }; |
27 assertEquals(1, x); | 47 assertEquals(1, x); |
28 assertEquals(2, y); | 48 assertEquals(2, y); |
29 | 49 |
30 let {z} = { z : 3 }; | 50 let {z} = { z : 3 }; |
31 assertEquals(3, z); | 51 assertEquals(3, z); |
32 | 52 |
53 let log = []; | |
54 let o = { | |
55 get x() { | |
56 log.push("x"); | |
57 return 0; | |
58 }, | |
59 get y() { | |
60 log.push("y"); | |
61 return { | |
62 get z() { log.push("z"); return 1; } | |
63 } | |
64 } | |
65 }; | |
66 let { x : x0, y : { z : z1 }, x : x1 } = o; | |
67 assertSame(0, x0); | |
68 assertSame(1, z1); | |
69 assertSame(0, x1); | |
70 assertArrayEquals(["x", "y", "z", "x"], log); | |
33 | 71 |
34 let sum = 0; | 72 let sum = 0; |
35 for(let {x, z} = { x : 0, z : 3 }; z != 0; z--) { | 73 for(let {x, z} = { x : 0, z : 3 }; z != 0; z--) { |
36 assertEquals(0, x); | 74 assertEquals(0, x); |
37 sum += z; | 75 sum += z; |
38 } | 76 } |
39 assertEquals(6, sum); | 77 assertEquals(6, sum); |
40 }()); | 78 }()); |
41 | 79 |
42 | 80 |
43 (function TestObjectLiteralPatternLexicalConst() { | 81 (function TestObjectLiteralPatternLexicalConst() { |
44 'use strict'; | 82 'use strict'; |
45 const { x : x, y : y } = { x : 1, y : 2 }; | 83 const { x : x, y : y } = { x : 1, y : 2 }; |
46 assertEquals(1, x); | 84 assertEquals(1, x); |
47 assertEquals(2, y); | 85 assertEquals(2, y); |
48 | 86 |
87 assertThrows(function() { x++; }, TypeError); | |
arv (Not doing code reviews)
2015/05/18 14:43:42
Can you add a test/message/ test too?
Dmitry Lomov (no reviews)
2015/05/18 16:28:23
Done.
| |
88 assertThrows(function() { y++; }, TypeError); | |
89 | |
49 const {z} = { z : 3 }; | 90 const {z} = { z : 3 }; |
50 assertEquals(3, z); | 91 assertEquals(3, z); |
51 | 92 |
52 | 93 |
53 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { | 94 for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { |
54 assertTrue(false); | 95 assertTrue(false); |
55 } | 96 } |
56 }()); | 97 }()); |
98 | |
arv (Not doing code reviews)
2015/05/18 14:43:42
+1 newline for consistency
Dmitry Lomov (no reviews)
2015/05/18 16:28:23
Done.
| |
99 (function TestFailingMatchesSloppy() { | |
100 var {x, y} = {}; | |
101 assertSame(undefined, x); | |
102 assertSame(undefined, y); | |
103 | |
104 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
105 assertSame(undefined, z1); | |
106 assertSame(42, y2); | |
107 }()); | |
108 | |
109 | |
110 (function TestFailingMatchesStrict() { | |
111 'use strict'; | |
112 var {x, y} = {}; | |
113 assertSame(undefined, x); | |
114 assertSame(undefined, y); | |
115 | |
116 var { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
117 assertSame(undefined, z1); | |
118 assertSame(42, y2); | |
119 | |
120 { | |
121 let {x1,y1} = {}; | |
122 assertSame(undefined, x1); | |
123 assertSame(undefined, y1); | |
124 | |
125 let { x : { z1 }, y2} = { x : {}, y2 : 42 } | |
126 assertSame(undefined, z1); | |
127 assertSame(42, y2); | |
128 } | |
129 }()); | |
130 | |
131 | |
132 (function TestExceptions() { | |
arv (Not doing code reviews)
2015/05/18 14:43:42
How about:
(function TestExceptions() {
for (va
Dmitry Lomov (no reviews)
2015/05/18 16:28:23
Done.
| |
133 assertThrows(function() { var {} = null; }, TypeError); | |
134 assertThrows(function() { var {x} = null; }, TypeError); | |
135 assertThrows(function() { var { x : {} } = { x : null }; }, TypeError); | |
136 assertThrows(function() { 'use strict'; let {} = null; }, TypeError); | |
137 assertThrows(function() { 'use strict'; let {x} = null; }, TypeError); | |
138 assertThrows(function() { 'use strict'; let { x : {} } = { x : null }; }, Type Error); | |
139 }()); | |
OLD | NEW |