summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/expressions/nullish-coalescing.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /js/src/tests/non262/expressions/nullish-coalescing.js
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
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.js112
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");
+