blob: 9d251f8291e1e2527d5b64b0b2fb2c2171c4c991 [file] [log] [blame]
utatane.tea@gmail.com412d7612015-06-26 03:13:25 +00001function shouldBe(actual, expected) {
2 if (actual !== expected)
3 throw new Error('bad value: ' + actual);
4}
5
6function shouldThrow(func, errorMessage) {
7 var errorThrown = false;
8 var error = null;
9 try {
10 func();
11 } catch (e) {
12 errorThrown = true;
13 error = e;
14 }
15 if (!errorThrown)
16 throw new Error('not thrown');
17 if (String(error) !== errorMessage)
18 throw new Error(`bad error: ${String(error)}`);
19}
20
21function testSyntaxError(script, message) {
22 var error = null;
23 try {
24 eval(script);
25 } catch (e) {
26 error = e;
27 }
28 if (!error)
29 throw new Error("Expected syntax error not thrown");
30
31 if (String(error) !== message)
32 throw new Error("Bad error: " + String(error));
33}
34
35(function () {
36 var [a, b, ...c] = "Cocoa";
37 shouldBe(a, 'C');
38 shouldBe(b, 'o');
39 shouldBe(JSON.stringify(c), String.raw`["c","o","a"]`);
40}());
41
42(function () {
43 var [a, b, ...c] = "Co";
44 shouldBe(a, 'C');
45 shouldBe(b, 'o');
46 shouldBe(JSON.stringify(c), String.raw`[]`);
47}());
48
49(function () {
50 var [a, b, ...c] = "C";
51 shouldBe(a, 'C');
52 shouldBe(b, undefined);
53 shouldBe(JSON.stringify(c), String.raw`[]`);
54}());
55
56(function () {
57 var a, b, c;
58 [a, b, ...c] = "Cocoa";
59 shouldBe(a, 'C');
60 shouldBe(b, 'o');
61 shouldBe(JSON.stringify(c), String.raw`["c","o","a"]`);
62}());
63
64(function () {
65 var a, b, c;
66 [a, b, ...c] = "Co";
67 shouldBe(a, 'C');
68 shouldBe(b, 'o');
69 shouldBe(JSON.stringify(c), String.raw`[]`);
70}());
71
72(function () {
73 var a, b, c;
74 [a, b, ...c] = "C";
75 shouldBe(a, 'C');
76 shouldBe(b, undefined);
77 shouldBe(JSON.stringify(c), String.raw`[]`);
78}());
79
80(function ([a, b, ...c]) {
81 shouldBe(a, 'C');
82 shouldBe(b, 'o');
83 shouldBe(JSON.stringify(c), String.raw`["c","o","a"]`);
84}("Cocoa"));
85
86(function ([a, b, ...c]) {
87 shouldBe(a, 'C');
88 shouldBe(b, 'o');
89 shouldBe(JSON.stringify(c), String.raw`[]`);
90}("Co"));
91
92(function ([a, b, ...c]) {
93 shouldBe(a, 'C');
94 shouldBe(b, undefined);
95 shouldBe(JSON.stringify(c), String.raw`[]`);
96}("C"));
97
saambarati1@gmail.comcc3bcb62015-07-02 23:53:10 +000098testSyntaxError(String.raw`var [a, ...b, c] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
99testSyntaxError(String.raw`var [a, ...b,] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
100testSyntaxError(String.raw`var [a, ...b,,] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
101testSyntaxError(String.raw`var [a, ...b = 20] = 20`, String.raw`SyntaxError: Unexpected token '='. Expected a closing ']' following a rest element destructuring pattern.`);
102testSyntaxError(String.raw`var [a, ...[b, c]] = 20`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
103testSyntaxError(String.raw`var [a, ...{ b, c }] = 20`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
utatane.tea@gmail.com412d7612015-06-26 03:13:25 +0000104
saambarati1@gmail.comcc3bcb62015-07-02 23:53:10 +0000105testSyntaxError(String.raw`(function ([a, ...b, c]) { })`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
106testSyntaxError(String.raw`(function ([a, ...b,]) { })`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
107testSyntaxError(String.raw`(function ([a, ...b,,]) { })`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
108testSyntaxError(String.raw`(function ([a, ...b = 20,,]) { })`, String.raw`SyntaxError: Unexpected token '='. Expected a closing ']' following a rest element destructuring pattern.`);
109testSyntaxError(String.raw`(function ([a, ...[b, c]]) { })`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
110testSyntaxError(String.raw`(function ([a, ...{ b, c }]) { })`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
utatane.tea@gmail.com412d7612015-06-26 03:13:25 +0000111
utatane.tea@gmail.com412d7612015-06-26 03:13:25 +0000112
commit-queue@webkit.org88a74762015-11-19 22:54:46 +0000113testSyntaxError(String.raw`[a, ...b, c] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
114testSyntaxError(String.raw`[a, ...b,] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
115testSyntaxError(String.raw`[a, ...b,,] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
116testSyntaxError(String.raw`[a, ...b = 20] = 20`, String.raw`SyntaxError: Unexpected token '='. Expected a closing ']' following a rest element destructuring pattern.`);
utatane.tea@gmail.com412d7612015-06-26 03:13:25 +0000117
118(function () {
119 var a, b, c;
120 [a, b, ...[...c]] = "Cocoa";
121 shouldBe(a, 'C');
122 shouldBe(b, 'o');
123 shouldBe(JSON.stringify(c), String.raw`["c","o","a"]`);
124}());
125
126(function () {
127 var a, b, c, d, e, f;
128 [a, b, ...{ 0: c, 1: d, 2: e, 3: f }] = "Cocoa";
129 shouldBe(a, 'C');
130 shouldBe(b, 'o');
131 shouldBe(c, 'c');
132 shouldBe(d, 'o');
133 shouldBe(f, undefined);
134}());
135
136(function () {
137 var a, b, c, d, e;
138 [a, b, ...[c, d, ...e]] = "Cocoa";
139 shouldBe(a, 'C');
140 shouldBe(b, 'o');
141 shouldBe(c, 'c');
142 shouldBe(d, 'o');
143 shouldBe(JSON.stringify(e), String.raw`["a"]`);
144}());
145
146function iterator(array) {
147 var nextCount = 0;
148 var returnCount = 0;
149 var original = array.values();
150 return {
151 [Symbol.iterator]() {
152 return this;
153 },
154
155 next() {
156 ++nextCount;
157 return original.next();
158 },
159
160 return() {
161 ++returnCount;
162 return { done: true };
163 },
164
165 reportNext() {
166 return nextCount;
167 },
168
169 reportReturn() {
170 return returnCount;
171 }
172 };
173};
174
175(function () {
176 var iter = iterator([1, 2, 3]);
177 var [...a] = iter;
178 shouldBe(iter.reportNext(), 4);
179 shouldBe(iter.reportReturn(), 0);
180 shouldBe(JSON.stringify(a), String.raw`[1,2,3]`);
181}());
182
183(function () {
184 var iter = iterator([1, 2, 3]);
185 var [a, b, ...c] = iter;
186 shouldBe(iter.reportNext(), 4);
187 shouldBe(iter.reportReturn(), 0);
188 shouldBe(a, 1);
189 shouldBe(b, 2);
190 shouldBe(JSON.stringify(c), String.raw`[3]`);
191}());
192
193(function () {
194 var iter = iterator([1, 2, 3]);
195 var [a, b, c, d, ...e] = iter;
196 shouldBe(iter.reportNext(), 4);
197 shouldBe(iter.reportReturn(), 0);
198 shouldBe(a, 1);
199 shouldBe(b, 2);
200 shouldBe(c, 3);
201 shouldBe(d, undefined);
202 shouldBe(JSON.stringify(e), String.raw`[]`);
203}());
204
205(function () {
206 var iter = iterator([1, 2, 3]);
207 var a, b;
208 [...[a, b]] = iter;
209 shouldBe(iter.reportNext(), 4);
210 shouldBe(iter.reportReturn(), 0);
211 shouldBe(a, 1);
212 shouldBe(b, 2);
213}());