diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/expressions/nullish-coalescing.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/tests/non262/expressions/nullish-coalescing.js')
-rw-r--r-- | js/src/tests/non262/expressions/nullish-coalescing.js | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/js/src/tests/non262/expressions/nullish-coalescing.js b/js/src/tests/non262/expressions/nullish-coalescing.js new file mode 100644 index 0000000000..4d3ea8f644 --- /dev/null +++ b/js/src/tests/non262/expressions/nullish-coalescing.js @@ -0,0 +1,112 @@ +var BUGNUMBER = 1566141; +var summary = "Implement the Nullish Coalescing operator (??) proposal"; + +print(BUGNUMBER + ": " + summary); + +// These tests are originally from webkit. +// webkit specifics have been removed and a test for `document.all` has +// been added. +function shouldBe(actual, expected) { + if (actual !== expected) + throw new Error(`expected ${expected} but got ${actual}`); +} + +function shouldNotThrow(script) { + eval(script); +} + +function shouldThrowSyntaxError(script) { + let error; + try { + eval(script); + } catch (e) { + error = e; + } + + if (!(error instanceof SyntaxError)) + throw new Error('Expected SyntaxError!'); +} + +function testBasicCases() { + shouldBe(undefined ?? 3, 3); + shouldBe(null ?? 3, 3); + shouldBe(true ?? 3, true); + shouldBe(false ?? 3, false); + shouldBe(0 ?? 3, 0); + shouldBe(1 ?? 3, 1); + shouldBe('' ?? 3, ''); + shouldBe('hi' ?? 3, 'hi'); + shouldBe(({} ?? 3) instanceof Object, true); + shouldBe(({ x: 'hi' } ?? 3).x, 'hi'); + shouldBe(([] ?? 3) instanceof Array, true); + shouldBe((['hi'] ?? 3)[0], 'hi'); + // test document.all, which has odd behavior + shouldBe(typeof(createIsHTMLDDA() ?? 3), "undefined"); +} + +for (let i = 0; i < 1e5; i++) + testBasicCases(); + +shouldBe(1 | null ?? 3, 1); +shouldBe(1 ^ null ?? 3, 1); +shouldBe(1 & null ?? 3, 0); +shouldBe(3 == null ?? 3, false); +shouldBe(3 != null ?? 3, true); +shouldBe(3 === null ?? 3, false); +shouldBe(3 !== null ?? 3, true); +shouldBe(1 < null ?? 3, false); +shouldBe(1 > null ?? 3, true); +shouldBe(1 <= null ?? 3, false); +shouldBe(1 >= null ?? 3, true); +shouldBe(1 << null ?? 3, 1); +shouldBe(1 >> null ?? 3, 1); +shouldBe(1 >>> null ?? 3, 1); +shouldBe(1 + null ?? 3, 1); +shouldBe(1 - null ?? 3, 1); +shouldBe(1 * null ?? 3, 0); +shouldBe(1 / null ?? 3, Infinity); +shouldBe(isNaN(1 % null ?? 3), true); +shouldBe(1 ** null ?? 3, 1); +shouldBe((void 0) ?? 3, 3); + +const obj = { + count: 0, + get x() { this.count++; return 'x'; } +}; +false ?? obj.x; +shouldBe(obj.count, 0); +null ?? obj.x; +shouldBe(obj.count, 1); +obj.x ?? obj.x; +shouldBe(obj.count, 2); + +shouldThrowSyntaxError('0 || 1 ?? 2'); +shouldThrowSyntaxError('0 && 1 ?? 2'); +shouldThrowSyntaxError('0 ?? 1 || 2'); +shouldThrowSyntaxError('0 ?? 1 && 2'); +shouldNotThrow('(0 || 1) ?? 2'); +shouldNotThrow('0 || (1 ?? 2)'); +shouldNotThrow('(0 && 1) ?? 2'); +shouldNotThrow('0 && (1 ?? 2)'); +shouldNotThrow('(0 ?? 1) || 2'); +shouldNotThrow('0 ?? (1 || 2)'); +shouldNotThrow('(0 ?? 1) && 2'); +shouldNotThrow('0 ?? (1 && 2)'); + +shouldNotThrow('0 || 1 && 2 | 3 ^ 4 & 5 == 6 != 7 === 8 !== 9 < 0 > 1 <= 2 >= 3 << 4 >> 5 >>> 6 + 7 - 8 * 9 / 0 % 1 ** 2'); +shouldThrowSyntaxError('0 || 1 && 2 | 3 ^ 4 & 5 == 6 != 7 === 8 !== 9 < 0 > 1 <= 2 >= 3 << 4 >> 5 >>> 6 + 7 - 8 * 9 / 0 % 1 ** 2 ?? 3'); +shouldThrowSyntaxError('3 ?? 2 ** 1 % 0 / 9 * 8 - 7 + 6 >>> 5 >> 4 << 3 >= 2 <= 1 > 0 < 9 !== 8 === 7 != 6 == 5 & 4 ^ 3 | 2 && 1 || 0'); + +shouldBe(null?.x ?? 3, 3); +shouldBe(({})?.x ?? 3, 3); +shouldBe(({ x: 0 })?.x ?? 3, 0); +shouldBe(null?.() ?? 3, 3); +shouldBe((() => 0)?.() ?? 3, 0); +shouldBe(({ x: 0 })?.[null?.a ?? 'x'] ?? 3, 0); +shouldBe((() => 0)?.(null?.a ?? 'x') ?? 3, 0); + +if (typeof reportCompare === "function") + reportCompare(true, true); + +print("Tests complete"); + |