summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/expressions/ToPropertyKey-symbols.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/src/tests/non262/expressions/ToPropertyKey-symbols.js')
-rw-r--r--js/src/tests/non262/expressions/ToPropertyKey-symbols.js90
1 files changed, 90 insertions, 0 deletions
diff --git a/js/src/tests/non262/expressions/ToPropertyKey-symbols.js b/js/src/tests/non262/expressions/ToPropertyKey-symbols.js
new file mode 100644
index 0000000000..32b3994b3c
--- /dev/null
+++ b/js/src/tests/non262/expressions/ToPropertyKey-symbols.js
@@ -0,0 +1,90 @@
+var symbols = [
+ Symbol(), Symbol("iterator"), Symbol.for("iterator"), Symbol.iterator
+];
+
+for (var sym of symbols) {
+ var key = {
+ toString() { return sym; }
+ };
+
+ // Test that ToPropertyKey can return a symbol in each of the following
+ // contexts.
+
+ // Computed property names.
+ var obj = {[key]: 13};
+ var found = Reflect.ownKeys(obj);
+ assertEq(found.length, 1);
+ assertEq(found[0], sym);
+
+ // Computed accessor property names.
+ var obj2 = {
+ get [key]() { return "got"; },
+ set [key](v) { this.v = v; }
+ };
+ assertEq(obj2[sym], "got");
+ obj2[sym] = 33;
+ assertEq(obj2.v, 33);
+
+ // Getting and setting properties.
+ assertEq(obj[key], 13);
+ obj[key] = 19;
+ assertEq(obj[sym], 19);
+ (function () { "use strict"; obj[key] = 20; })();
+ assertEq(obj[sym], 20);
+ obj[key]++;
+ assertEq(obj[sym], 21);
+
+ // Getting properties of primitive values.
+ Number.prototype[sym] = "success";
+ assertEq(Math.PI[key], "success");
+ delete Number.prototype[sym];
+
+ // Getting a super property.
+ class X {
+ [sym]() { return "X"; }
+ }
+ class Y extends X {
+ [sym]() { return super[key]() + "Y"; }
+ }
+ var y = new Y();
+ assertEq(y[sym](), "XY");
+
+ // Setting a super property.
+ class Z {
+ set [sym](v) {
+ this.self = this;
+ this.value = v;
+ }
+ }
+ class W extends Z {
+ set [sym](v) {
+ this.isW = true;
+ super[key] = v;
+ }
+ }
+ var w = new W();
+ w[key] = "ok";
+ assertEq(w.self, w);
+ assertEq(w.value, "ok");
+ assertEq(w.isW, true);
+
+ // Deleting properties.
+ obj = {[sym]: 1};
+ assertEq(delete obj[key], true);
+ assertEq(sym in obj, false);
+
+ // LHS of `in` expressions.
+ assertEq(key in {iterator: 0}, false);
+ assertEq(key in {[sym]: 0}, true);
+
+ // Methods of Object and Object.prototype
+ obj = {};
+ Object.defineProperty(obj, key, {value: "ok", enumerable: true});
+ assertEq(obj[sym], "ok");
+ assertEq(obj.hasOwnProperty(key), true);
+ assertEq(obj.propertyIsEnumerable(key), true);
+ var desc = Object.getOwnPropertyDescriptor(obj, key);
+ assertEq(desc.value, "ok");
+}
+
+reportCompare(0, 0);