]> git.saurik.com Git - apple/javascriptcore.git/blob - tests/stress/rest-elements.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / rest-elements.js
1 function shouldBe(actual, expected) {
2 if (actual !== expected)
3 throw new Error('bad value: ' + actual);
4 }
5
6 function 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
21 function 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
98 testSyntaxError(String.raw`var [a, ...b, c] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
99 testSyntaxError(String.raw`var [a, ...b,] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
100 testSyntaxError(String.raw`var [a, ...b,,] = 20`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
101 testSyntaxError(String.raw`var [a, ...b = 20] = 20`, String.raw`SyntaxError: Unexpected token '='. Expected a closing ']' following a rest element destructuring pattern.`);
102 testSyntaxError(String.raw`var [a, ...[b, c]] = 20`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
103 testSyntaxError(String.raw`var [a, ...{ b, c }] = 20`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
104
105 testSyntaxError(String.raw`(function ([a, ...b, c]) { })`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
106 testSyntaxError(String.raw`(function ([a, ...b,]) { })`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
107 testSyntaxError(String.raw`(function ([a, ...b,,]) { })`, String.raw`SyntaxError: Unexpected token ','. Expected a closing ']' following a rest element destructuring pattern.`);
108 testSyntaxError(String.raw`(function ([a, ...b = 20,,]) { })`, String.raw`SyntaxError: Unexpected token '='. Expected a closing ']' following a rest element destructuring pattern.`);
109 testSyntaxError(String.raw`(function ([a, ...[b, c]]) { })`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
110 testSyntaxError(String.raw`(function ([a, ...{ b, c }]) { })`, String.raw`SyntaxError: Unexpected token ']'. Expected identifier for a rest element destructuring pattern.`);
111
112 shouldThrow(function () {
113 [a, ...b, c] = 20;
114 }, "ReferenceError: Left side of assignment is not a reference.");
115
116 shouldThrow(function () {
117 [a, ...b,] = 20
118 }, "ReferenceError: Left side of assignment is not a reference.");
119
120 shouldThrow(function () {
121 [a, ...b,,] = 20
122 }, "ReferenceError: Left side of assignment is not a reference.");
123
124 shouldThrow(function () {
125 [a, ...b = 20] = 20
126 }, "ReferenceError: Left side of assignment is not a reference.");
127
128 (function () {
129 var a, b, c;
130 [a, b, ...[...c]] = "Cocoa";
131 shouldBe(a, 'C');
132 shouldBe(b, 'o');
133 shouldBe(JSON.stringify(c), String.raw`["c","o","a"]`);
134 }());
135
136 (function () {
137 var a, b, c, d, e, f;
138 [a, b, ...{ 0: c, 1: d, 2: e, 3: f }] = "Cocoa";
139 shouldBe(a, 'C');
140 shouldBe(b, 'o');
141 shouldBe(c, 'c');
142 shouldBe(d, 'o');
143 shouldBe(f, undefined);
144 }());
145
146 (function () {
147 var a, b, c, d, e;
148 [a, b, ...[c, d, ...e]] = "Cocoa";
149 shouldBe(a, 'C');
150 shouldBe(b, 'o');
151 shouldBe(c, 'c');
152 shouldBe(d, 'o');
153 shouldBe(JSON.stringify(e), String.raw`["a"]`);
154 }());
155
156 function iterator(array) {
157 var nextCount = 0;
158 var returnCount = 0;
159 var original = array.values();
160 return {
161 [Symbol.iterator]() {
162 return this;
163 },
164
165 next() {
166 ++nextCount;
167 return original.next();
168 },
169
170 return() {
171 ++returnCount;
172 return { done: true };
173 },
174
175 reportNext() {
176 return nextCount;
177 },
178
179 reportReturn() {
180 return returnCount;
181 }
182 };
183 };
184
185 (function () {
186 var iter = iterator([1, 2, 3]);
187 var [...a] = iter;
188 shouldBe(iter.reportNext(), 4);
189 shouldBe(iter.reportReturn(), 0);
190 shouldBe(JSON.stringify(a), String.raw`[1,2,3]`);
191 }());
192
193 (function () {
194 var iter = iterator([1, 2, 3]);
195 var [a, b, ...c] = iter;
196 shouldBe(iter.reportNext(), 4);
197 shouldBe(iter.reportReturn(), 0);
198 shouldBe(a, 1);
199 shouldBe(b, 2);
200 shouldBe(JSON.stringify(c), String.raw`[3]`);
201 }());
202
203 (function () {
204 var iter = iterator([1, 2, 3]);
205 var [a, b, c, d, ...e] = iter;
206 shouldBe(iter.reportNext(), 4);
207 shouldBe(iter.reportReturn(), 0);
208 shouldBe(a, 1);
209 shouldBe(b, 2);
210 shouldBe(c, 3);
211 shouldBe(d, undefined);
212 shouldBe(JSON.stringify(e), String.raw`[]`);
213 }());
214
215 (function () {
216 var iter = iterator([1, 2, 3]);
217 var a, b;
218 [...[a, b]] = iter;
219 shouldBe(iter.reportNext(), 4);
220 shouldBe(iter.reportReturn(), 0);
221 shouldBe(a, 1);
222 shouldBe(b, 2);
223 }());