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

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

Issue 1139773005: [destructuring] More tests for object literal pattern (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Comments addressed, landing Created 5 years, 7 months 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
OLDNEW
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);
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);
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
99
100 (function TestFailingMatchesSloppy() {
101 var {x, y} = {};
102 assertSame(undefined, x);
103 assertSame(undefined, y);
104
105 var { x : { z1 }, y2} = { x : {}, y2 : 42 }
106 assertSame(undefined, z1);
107 assertSame(42, y2);
108 }());
109
110
111 (function TestFailingMatchesStrict() {
112 'use strict';
113 var {x, y} = {};
114 assertSame(undefined, x);
115 assertSame(undefined, y);
116
117 var { x : { z1 }, y2} = { x : {}, y2 : 42 }
118 assertSame(undefined, z1);
119 assertSame(42, y2);
120
121 {
122 let {x1,y1} = {};
123 assertSame(undefined, x1);
124 assertSame(undefined, y1);
125
126 let { x : { z1 }, y2} = { x : {}, y2 : 42 }
127 assertSame(undefined, z1);
128 assertSame(42, y2);
129 }
130 }());
131
132
133 (function TestExceptions() {
134 for (var val of [null, undefined]) {
135 assertThrows(function() { var {} = val; }, TypeError);
136 assertThrows(function() { var {x} = val; }, TypeError);
137 assertThrows(function() { var { x : {} } = { x : val }; }, TypeError);
138 assertThrows(function() { 'use strict'; let {} = val; }, TypeError);
139 assertThrows(function() { 'use strict'; let {x} = val; }, TypeError);
140 assertThrows(function() { 'use strict'; let { x : {} } = { x : val }; },
141 TypeError);
142 }
143 }());
OLDNEW
« test/message/destructuring-modify-const.js ('K') | « test/message/destructuring-modify-const.out ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698