X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/2d39b0e377c0896910ee49ae70082ba665faf986..ed1e77d3adeb83d26fd1dfb16dd84cabdcefd250:/tests/stress/trailing-comma-in-patterns.js diff --git a/tests/stress/trailing-comma-in-patterns.js b/tests/stress/trailing-comma-in-patterns.js new file mode 100644 index 0000000..7353e90 --- /dev/null +++ b/tests/stress/trailing-comma-in-patterns.js @@ -0,0 +1,157 @@ +function shouldBe(actual, expected) { + if (actual !== expected) + throw new Error('bad value: ' + actual); +} + +function iterator(array) { + var nextCount = 0; + var returnCount = 0; + var original = array.values(); + return { + [Symbol.iterator]() { + return this; + }, + + next() { + ++nextCount; + return original.next(); + }, + + return() { + ++returnCount; + return { done: true }; + }, + + reportNext() { + return nextCount; + }, + + reportReturn() { + return returnCount; + } + }; +}; + +(function () { + var iter = iterator([1, 2, 3]); + var [] = iter; + shouldBe(iter.reportNext(), 0); + shouldBe(iter.reportReturn(), 1); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [,] = iter; + shouldBe(iter.reportNext(), 1); + shouldBe(iter.reportReturn(), 1); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [,,] = iter; + shouldBe(iter.reportNext(), 2); + shouldBe(iter.reportReturn(), 1); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [,,,] = iter; + shouldBe(iter.reportNext(), 3); + shouldBe(iter.reportReturn(), 1); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [,,,,] = iter; + shouldBe(iter.reportNext(), 4); + shouldBe(iter.reportReturn(), 0); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [,,,,,] = iter; + shouldBe(iter.reportNext(), 4); + shouldBe(iter.reportReturn(), 0); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [,a,] = iter; + shouldBe(iter.reportNext(), 2); + shouldBe(iter.reportReturn(), 1); + shouldBe(a, 2); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [a,] = iter; + shouldBe(iter.reportNext(), 1); + shouldBe(iter.reportReturn(), 1); + shouldBe(a, 1); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [a,,] = iter; + shouldBe(iter.reportNext(), 2); + shouldBe(iter.reportReturn(), 1); + shouldBe(a, 1); +}()); + +(function () { + var iter = iterator([1, 2, 3]); + var [a,b = 42,] = iter; + shouldBe(iter.reportNext(), 2); + shouldBe(iter.reportReturn(), 1); + shouldBe(a, 1); + shouldBe(b, 2); +}()); + +(function () { + var {} = { Cocoa: 15, Cappuccino: 13 }; +}()); + +(function () { + var {Cocoa,} = { Cocoa: 15, Cappuccino: 13 }; + shouldBe(Cocoa, 15); +}()); + +(function () { + var {Cocoa = 'Cocoa',} = { Cocoa: 15, Cappuccino: 13 }; + shouldBe(Cocoa, 15); +}()); + +(function () { + var {Cocoa, Kilimanjaro = 'Coffee'} = { Cocoa: 15, Cappuccino: 13 }; + shouldBe(Cocoa, 15); + shouldBe(Kilimanjaro, 'Coffee'); +}()); + +(function () { + var {Cocoa, Kilimanjaro = 'Coffee'} = {}; + shouldBe(Cocoa, undefined); + shouldBe(Kilimanjaro, 'Coffee'); +}()); + +(function () { + var {Cocoa, Kilimanjaro = 'Coffee',} = { Cocoa: 15, Cappuccino: 13 }; + shouldBe(Cocoa, 15); + shouldBe(Kilimanjaro, 'Coffee'); +}()); + +function testSyntaxError(script, message) { + var error = null; + try { + eval(script); + } catch (e) { + error = e; + } + if (!error) + throw new Error("Expected syntax error not thrown"); + + if (String(error) !== message) + throw new Error("Bad error: " + String(error)); +} + +testSyntaxError(String.raw`var {,} = {Cocoa: 15}`, String.raw`SyntaxError: Unexpected token ','. Expected a property name.`); +testSyntaxError(String.raw`var {,} = {}`, String.raw`SyntaxError: Unexpected token ','. Expected a property name.`);