commit-queue@webkit.org | 4d3e0c2 | 2015-12-01 20:11:20 +0000 | [diff] [blame] | 1 | function 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 | |
| 15 | function 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 | |
| 26 | testTypeError(`({ } = null)`, "TypeError: Right side of assignment cannot be destructured"); |
| 27 | testTypeError(`({ a } = null)`, "TypeError: Right side of assignment cannot be destructured"); |
| 28 | testTypeError(`({ a: { b } = null } = { })`, "TypeError: Right side of assignment cannot be destructured"); |
| 29 | testTypeError(`({ a: { b } } = { a: null })`, "TypeError: Right side of assignment cannot be destructured"); |
| 30 | testTypeError(`({ } = undefined)`, "TypeError: Right side of assignment cannot be destructured"); |
| 31 | testTypeError(`({ a } = undefined)`, "TypeError: Right side of assignment cannot be destructured"); |
| 32 | testTypeError(`({ a: { b } = undefined } = { })`, "TypeError: Right side of assignment cannot be destructured"); |
| 33 | testTypeError(`({ a: { b } } = { a: undefined })`, "TypeError: Right side of assignment cannot be destructured"); |
| 34 | |
| 35 | testOK(`({ } = 123)`); |
| 36 | testOK(`({ a } = 123)`); |
| 37 | testOK(`({ a: { b } = 123 } = { })`); |
| 38 | testOK(`({ a: { b } } = { a: 123 })`); |
| 39 | |
| 40 | testOK(`({ } = 0.5)`); |
| 41 | testOK(`({ a } = 0.5)`); |
| 42 | testOK(`({ a: { b } = 0.5 } = { })`); |
| 43 | testOK(`({ a: { b } } = { a: 0.5 })`); |
| 44 | |
| 45 | testOK(`({ } = NaN)`); |
| 46 | testOK(`({ a } = NaN)`); |
| 47 | testOK(`({ a: { b } = NaN } = { })`); |
| 48 | testOK(`({ a: { b } } = { a: NaN })`); |
| 49 | |
| 50 | testOK(`({ } = true)`); |
| 51 | testOK(`({ a } = true)`); |
| 52 | testOK(`({ a: { b } = true } = { })`); |
| 53 | testOK(`({ a: { b } } = { a: true })`); |
| 54 | |
| 55 | testOK(`({ } = {})`); |
| 56 | testOK(`({ a } = {})`); |
| 57 | testOK(`({ a: { b } = {} } = { })`); |
| 58 | testOK(`({ a: { b } } = { a: {} })`); |
| 59 | |
| 60 | testOK(`({ } = [])`); |
| 61 | testOK(`({ a } = [])`); |
| 62 | testOK(`({ a: { b } = [] } = { })`); |
| 63 | testOK(`({ a: { b } } = { a: [] })`); |
| 64 | |
| 65 | testOK(`({ } = /1/)`); |
| 66 | testOK(`({ a } = /1/)`); |
| 67 | testOK(`({ a: { b } = /1/ } = { })`); |
| 68 | testOK(`({ a: { b } } = { a: /1/ })`); |