summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Reflect/ownKeys.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/Reflect/ownKeys.js
parentInitial commit. (diff)
downloadfirefox-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/Reflect/ownKeys.js')
-rw-r--r--js/src/tests/non262/Reflect/ownKeys.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/js/src/tests/non262/Reflect/ownKeys.js b/js/src/tests/non262/Reflect/ownKeys.js
new file mode 100644
index 0000000000..5433562eb5
--- /dev/null
+++ b/js/src/tests/non262/Reflect/ownKeys.js
@@ -0,0 +1,66 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/licenses/publicdomain/ */
+
+// Reflect.ownKeys(obj) returns an array of an object's own property keys.
+
+// Test that Reflect.ownKeys gets the expected result when applied to various
+// objects. (These tests also check the basics: that the result is an array,
+// that its prototype is correct, etc.)
+var sym = Symbol.for("comet");
+var sym2 = Symbol.for("meteor");
+var cases = [
+ {object: {z: 3, y: 2, x: 1},
+ keys: ["z", "y", "x"]},
+ {object: [],
+ keys: ["length"]},
+ {object: new Int8Array(4),
+ keys: ["0", "1", "2", "3"]},
+ {object: new Proxy({a: 7}, {}),
+ keys: ["a"]},
+ {object: {[sym]: "ok"},
+ keys: [sym]},
+ {object: {[sym]: 0, // test 9.1.12 ordering
+ "str": 0,
+ "773": 0,
+ "0": 0,
+ [sym2]: 0,
+ "-1": 0,
+ "8": 0,
+ "second str": 0},
+ keys: ["0", "8", "773", // indexes in numeric order
+ "str", "-1", "second str", // strings in insertion order
+ sym, sym2]}, // symbols in insertion order
+ {object: newGlobal().Math, // cross-compartment wrapper
+ keys: Reflect.ownKeys(Math)}
+];
+for (var {object, keys} of cases)
+ assertDeepEq(Reflect.ownKeys(object), keys);
+
+// Reflect.ownKeys() creates a new array each time it is called.
+var object = {}, keys = [];
+for (var i = 0; i < 3; i++) {
+ var newKeys = Reflect.ownKeys(object);
+ assertEq(newKeys !== keys, true);
+ keys = newKeys;
+}
+
+// Proxy behavior with successful ownKeys() handler
+keys = ["str", "0"];
+obj = {};
+proxy = new Proxy(obj, {
+ ownKeys() { return keys; }
+});
+var actual = Reflect.ownKeys(proxy);
+assertDeepEq(actual, keys); // we get correct answers
+assertEq(actual !== keys, true); // but not the same object
+
+// If a proxy breaks invariants, a TypeError is thrown.
+var obj = Object.preventExtensions({});
+var proxy = new Proxy(obj, {
+ ownKeys() { return ["something"]; }
+});
+assertThrowsInstanceOf(() => Reflect.ownKeys(proxy), TypeError);
+
+// For more Reflect.ownKeys tests, see target.js.
+
+reportCompare(0, 0);