blob: c25bb1b572bf0bd09a451cb6ca4598a37b09ccca [file] [log] [blame]
commit-queue@webkit.org4d3e0c22015-12-01 20:11:20 +00001function testTypeError(script, message) {
2 var error = null;
3 try {
4 eval(script);
5 } catch (e) {
6 error = e;
7 }
8 if (!error)
9 throw new Error("Expected type error not thrown by `" + script + "`");
10
11 if (String(error) !== message)
12 throw new Error("Bad error: " + String(error));
13}
14
15function testOK(script) {
16 var error = null;
17 try {
18 eval(script);
19 } catch (e) {
20 error = e;
21 }
22 if (error)
23 throw new Error("Bad error: " + String(error));
24}
25
26testTypeError(`({ } = null)`, "TypeError: Right side of assignment cannot be destructured");
27testTypeError(`({ a } = null)`, "TypeError: Right side of assignment cannot be destructured");
28testTypeError(`({ a: { b } = null } = { })`, "TypeError: Right side of assignment cannot be destructured");
29testTypeError(`({ a: { b } } = { a: null })`, "TypeError: Right side of assignment cannot be destructured");
30testTypeError(`({ } = undefined)`, "TypeError: Right side of assignment cannot be destructured");
31testTypeError(`({ a } = undefined)`, "TypeError: Right side of assignment cannot be destructured");
32testTypeError(`({ a: { b } = undefined } = { })`, "TypeError: Right side of assignment cannot be destructured");
33testTypeError(`({ a: { b } } = { a: undefined })`, "TypeError: Right side of assignment cannot be destructured");
34
35testOK(`({ } = 123)`);
36testOK(`({ a } = 123)`);
37testOK(`({ a: { b } = 123 } = { })`);
38testOK(`({ a: { b } } = { a: 123 })`);
39
40testOK(`({ } = 0.5)`);
41testOK(`({ a } = 0.5)`);
42testOK(`({ a: { b } = 0.5 } = { })`);
43testOK(`({ a: { b } } = { a: 0.5 })`);
44
45testOK(`({ } = NaN)`);
46testOK(`({ a } = NaN)`);
47testOK(`({ a: { b } = NaN } = { })`);
48testOK(`({ a: { b } } = { a: NaN })`);
49
50testOK(`({ } = true)`);
51testOK(`({ a } = true)`);
52testOK(`({ a: { b } = true } = { })`);
53testOK(`({ a: { b } } = { a: true })`);
54
55testOK(`({ } = {})`);
56testOK(`({ a } = {})`);
57testOK(`({ a: { b } = {} } = { })`);
58testOK(`({ a: { b } } = { a: {} })`);
59
60testOK(`({ } = [])`);
61testOK(`({ a } = [])`);
62testOK(`({ a: { b } = [] } = { })`);
63testOK(`({ a: { b } } = { a: [] })`);
64
65testOK(`({ } = /1/)`);
66testOK(`({ a } = /1/)`);
67testOK(`({ a: { b } = /1/ } = { })`);
68testOK(`({ a: { b } } = { a: /1/ })`);