]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
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 () { | |
22 | var [a, b, c] = [1, 2, 3]; | |
23 | shouldBe(a, 1); | |
24 | shouldBe(b, 2); | |
25 | shouldBe(c, 3); | |
26 | }()); | |
27 | ||
28 | (function () { | |
29 | var [a, b, c] = [1, 2, 3].keys(); | |
30 | shouldBe(a, 0); | |
31 | shouldBe(b, 1); | |
32 | shouldBe(c, 2); | |
33 | }()); | |
34 | ||
35 | (function () { | |
36 | var [a, b, c] = [1, 2, 3].values(); | |
37 | shouldBe(a, 1); | |
38 | shouldBe(b, 2); | |
39 | shouldBe(c, 3); | |
40 | }()); | |
41 | ||
42 | (function () { | |
43 | var [a, , c] = [1, 2, 3].values(); | |
44 | shouldBe(a, 1); | |
45 | shouldBe(c, 3); | |
46 | }()); | |
47 | ||
48 | (function () { | |
49 | var [a, b, c] = [1, , 3].values(); | |
50 | shouldBe(a, 1); | |
51 | shouldBe(b, undefined); | |
52 | shouldBe(c, 3); | |
53 | }()); | |
54 | ||
55 | (function () { | |
56 | var [, b, c] = [1, 2, 3, 4, 5, 6].values(); | |
57 | shouldBe(b, 2); | |
58 | shouldBe(c, 3); | |
59 | }()); | |
60 | ||
61 | (function () { | |
62 | var [a, b, c] = [1].values(); | |
63 | shouldBe(a, 1); | |
64 | shouldBe(b, undefined); | |
65 | shouldBe(c, undefined); | |
66 | }()); | |
67 | ||
68 | (function ([a, b, c]) { | |
69 | shouldBe(a, 1); | |
70 | shouldBe(b, undefined); | |
71 | shouldBe(c, undefined); | |
72 | }([1].values())); | |
73 | ||
74 | (function () { | |
75 | var [a = 0, b = 2, c = 3] = [1].values(); | |
76 | shouldBe(a, 1); | |
77 | shouldBe(b, 2); | |
78 | shouldBe(c, 3); | |
79 | }()); | |
80 | ||
81 | (function () { | |
82 | var [a = 1, b = 2, c = 3] = [undefined, undefined, undefined]; | |
83 | shouldBe(a, 1); | |
84 | shouldBe(b, 2); | |
85 | shouldBe(c, 3); | |
86 | }()); | |
87 | ||
88 | // String with a surrogate pair. | |
89 | (function () { | |
90 | var string = "𠮷野家"; | |
91 | var [a, b, c] = string; | |
92 | shouldBe(string.length, 4); | |
93 | shouldBe(a, '𠮷'); | |
94 | shouldBe(b, '野'); | |
95 | shouldBe(c, '家'); | |
96 | }()); | |
97 | ||
98 | (function () { | |
99 | var set = new Set([1, 2, 3]); | |
100 | var [a, b, c] = set; | |
101 | shouldBe(set.has(a), true); | |
102 | shouldBe(set.has(b), true); | |
103 | shouldBe(set.has(c), true); | |
104 | }()); | |
105 | ||
106 | (function () { | |
107 | var map = new Map([[1, 1], [2, 2], [3, 3]]); | |
108 | var [a, b, c] = map; | |
109 | shouldBe(Array.isArray(a), true); | |
110 | shouldBe(Array.isArray(b), true); | |
111 | shouldBe(Array.isArray(c), true); | |
112 | shouldBe(map.has(a[0]), true); | |
113 | shouldBe(map.has(b[0]), true); | |
114 | shouldBe(map.has(c[0]), true); | |
115 | }()); | |
116 | ||
117 | // Errors | |
118 | ||
119 | shouldThrow(function () { | |
120 | var [a, b, c] = { | |
121 | [Symbol.iterator]() { | |
122 | return 42; | |
123 | } | |
124 | }; | |
125 | }, "TypeError: [a, b, c] is not a function. (In '[a, b, c]', '[a, b, c]' is undefined)"); | |
126 | ||
127 | shouldThrow(function () { | |
128 | var [a, b, c] = { | |
129 | [Symbol.iterator]() { | |
130 | return {}; | |
131 | } | |
132 | }; | |
133 | }, "TypeError: [a, b, c] is not a function. (In '[a, b, c]', '[a, b, c]' is undefined)"); | |
134 | ||
135 | shouldThrow(function () { | |
136 | var [a, b, c] = { | |
137 | [Symbol.iterator]() { | |
138 | return this; | |
139 | }, | |
140 | ||
141 | next() { | |
142 | throw new Error('out'); | |
143 | } | |
144 | }; | |
145 | }, 'Error: out'); | |
146 | ||
147 | shouldThrow(function () { | |
148 | var [a, b, c] = { | |
149 | [Symbol.iterator]() { | |
150 | return this; | |
151 | }, | |
152 | ||
153 | next() { | |
154 | return 42; | |
155 | } | |
156 | }; | |
157 | }, 'TypeError: Iterator result interface is not an object.'); | |
158 | ||
159 | (function () { | |
160 | var ok = 0; | |
161 | shouldThrow(function () { | |
162 | var [a, b, c] = { | |
163 | [Symbol.iterator]() { | |
164 | return this; | |
165 | }, | |
166 | ||
167 | return() { | |
168 | ok++; | |
169 | }, | |
170 | ||
171 | next() { | |
172 | return 42; | |
173 | } | |
174 | }; | |
175 | }, 'TypeError: Iterator result interface is not an object.'); | |
176 | ||
177 | shouldBe(ok, 0); | |
178 | }()); | |
179 | ||
180 | (function () { | |
181 | var ok = 0; | |
182 | shouldThrow(function () { | |
183 | var [a, b, c] = { | |
184 | [Symbol.iterator]() { | |
185 | return this; | |
186 | }, | |
187 | ||
188 | return() { | |
189 | ok++; | |
190 | }, | |
191 | ||
192 | next() { | |
193 | return { value: 20, done: false }; | |
194 | } | |
195 | }; | |
196 | }, 'TypeError: Iterator result interface is not an object.'); | |
197 | ||
198 | shouldBe(ok, 1); | |
199 | }()); | |
200 | ||
201 | (function () { | |
202 | var ok = 0; | |
203 | ||
204 | var [a, b, c] = { | |
205 | [Symbol.iterator]() { | |
206 | return this; | |
207 | }, | |
208 | ||
209 | return() { | |
210 | ok++; | |
211 | }, | |
212 | ||
213 | next() { | |
214 | return { value: 20, done: true }; | |
215 | } | |
216 | }; | |
217 | ||
218 | shouldBe(a, undefined); | |
219 | shouldBe(b, undefined); | |
220 | shouldBe(c, undefined); | |
221 | shouldBe(ok, 0); | |
222 | }()); | |
223 | ||
224 | (function () { | |
225 | var ok = 0; | |
226 | var n = 0; | |
227 | ||
228 | var done = false; | |
229 | var [a, b, c] = { | |
230 | [Symbol.iterator]() { | |
231 | return this; | |
232 | }, | |
233 | ||
234 | return() { | |
235 | ok++; | |
236 | }, | |
237 | ||
238 | next() { | |
239 | var prev = done; | |
240 | done = true; | |
241 | ++n; | |
242 | return { value: 20, done: prev }; | |
243 | } | |
244 | }; | |
245 | ||
246 | shouldBe(a, 20); | |
247 | shouldBe(b, undefined); | |
248 | shouldBe(c, undefined); | |
249 | shouldBe(n, 2); | |
250 | shouldBe(ok, 0); | |
251 | }()); | |
252 | ||
253 | (function () { | |
254 | var ok = 0; | |
255 | var n = 0; | |
256 | ||
257 | var done = false; | |
258 | var [a, b, c] = { | |
259 | [Symbol.iterator]() { | |
260 | return this; | |
261 | }, | |
262 | ||
263 | return() { | |
264 | ++ok; | |
265 | return { done: true }; | |
266 | }, | |
267 | ||
268 | next() { | |
269 | ++n; | |
270 | return { value: 20, done: false }; | |
271 | } | |
272 | }; | |
273 | ||
274 | shouldBe(a, 20); | |
275 | shouldBe(b, 20); | |
276 | shouldBe(c, 20); | |
277 | shouldBe(n, 3); | |
278 | shouldBe(ok, 1); | |
279 | }()); | |
280 | ||
281 | (function () { | |
282 | var ok = 0; | |
283 | var n = 0; | |
284 | ||
285 | var done = false; | |
286 | var [a, b, c] = { | |
287 | [Symbol.iterator]() { | |
288 | return this; | |
289 | }, | |
290 | ||
291 | return() { | |
292 | ++ok; | |
293 | return { done: true }; | |
294 | }, | |
295 | ||
296 | count: 0, | |
297 | ||
298 | next() { | |
299 | ++n; | |
300 | var done = ++this.count === 3; | |
301 | return { value: 20, done }; | |
302 | } | |
303 | }; | |
304 | ||
305 | shouldBe(a, 20); | |
306 | shouldBe(b, 20); | |
307 | shouldBe(c, undefined); | |
308 | shouldBe(n, 3); | |
309 | shouldBe(ok, 0); | |
310 | }()); | |
311 | ||
312 | (function () { | |
313 | var ok = 0; | |
314 | var n = 0; | |
315 | ||
316 | var done = false; | |
317 | var [a, b, c] = { | |
318 | [Symbol.iterator]() { | |
319 | return this; | |
320 | }, | |
321 | ||
322 | return() { | |
323 | ++ok; | |
324 | return { done: true }; | |
325 | }, | |
326 | ||
327 | count: 0, | |
328 | ||
329 | next() { | |
330 | ++n; | |
331 | var done = ++this.count === 4; | |
332 | return { value: 20, done }; | |
333 | } | |
334 | }; | |
335 | ||
336 | shouldBe(a, 20); | |
337 | shouldBe(b, 20); | |
338 | shouldBe(c, 20); | |
339 | shouldBe(n, 3); | |
340 | shouldBe(ok, 1); | |
341 | }()); | |
342 | ||
343 | (function () { | |
344 | var ok = 0; | |
345 | var n = 0; | |
346 | shouldThrow(function () { | |
347 | var [a, b, c] = { | |
348 | [Symbol.iterator]() { | |
349 | return this; | |
350 | }, | |
351 | ||
352 | return() { | |
353 | ok++; | |
354 | throw new Error('out'); | |
355 | }, | |
356 | ||
357 | next() { | |
358 | n++; | |
359 | return { value: 20, done: false }; | |
360 | } | |
361 | }; | |
362 | }, 'Error: out'); | |
363 | ||
364 | shouldBe(n, 3); | |
365 | shouldBe(ok, 1); | |
366 | }()); | |
367 | ||
368 | (function () { | |
369 | var ok = 0; | |
370 | var n = 0; | |
371 | shouldThrow(function () { | |
372 | var [a, b, c] = { | |
373 | [Symbol.iterator]() { | |
374 | return this; | |
375 | }, | |
376 | ||
377 | get return() { | |
378 | ok++; | |
379 | throw new Error('out'); | |
380 | }, | |
381 | ||
382 | next() { | |
383 | n++; | |
384 | return { value: 20, done: false }; | |
385 | } | |
386 | }; | |
387 | }, 'Error: out'); | |
388 | ||
389 | shouldBe(n, 3); | |
390 | shouldBe(ok, 1); | |
391 | }()); | |
392 | ||
393 | (function () { | |
394 | var ok = 0; | |
395 | var n = 0; | |
396 | shouldThrow(function () { | |
397 | var [a, b, c] = { | |
398 | [Symbol.iterator]() { | |
399 | return this; | |
400 | }, | |
401 | ||
402 | get return() { | |
403 | ok++; | |
404 | throw new Error('ng'); | |
405 | }, | |
406 | ||
407 | next() { | |
408 | n++; | |
409 | throw new Error('out'); | |
410 | } | |
411 | }; | |
412 | }, 'Error: out'); | |
413 | ||
414 | shouldBe(n, 1); | |
415 | shouldBe(ok, 0); | |
416 | }()); | |
417 | ||
418 | (function () { | |
419 | var ok = 0; | |
420 | var n = 0; | |
421 | shouldThrow(function () { | |
422 | var [a, b, c] = { | |
423 | [Symbol.iterator]() { | |
424 | return this; | |
425 | }, | |
426 | ||
427 | get return() { | |
428 | ok++; | |
429 | throw new Error('ng'); | |
430 | }, | |
431 | ||
432 | get next() { | |
433 | ++n; | |
434 | throw new Error('out'); | |
435 | } | |
436 | }; | |
437 | }, 'Error: out'); | |
438 | ||
439 | shouldBe(n, 1); | |
440 | shouldBe(ok, 0); | |
441 | }()); |