зеркало из https://github.com/mozilla/gecko-dev.git
Bug 645417, part 15 - Tests for ToObject on symbols. r=sfink.
The test as-base-value.js is not testing symbols as property keys (which is implemented in a later patch), but on the other side of the . operator: as things that can have property accesses done to them, just like you can do "name".length or (3.14).toString(). --HG-- extra : rebase_source : 3dac7660999bd021ec32c13985471e1608a29f64
This commit is contained in:
Родитель
83ab6a2b1a
Коммит
d05b2005e2
|
@ -0,0 +1,86 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/licenses/publicdomain/ */
|
||||
|
||||
// Like other primitives, symbols can be treated as objects, using object-like
|
||||
// syntax: `symbol.prop` or `symbol[key]`.
|
||||
//
|
||||
// In ECMAScript spec jargon, this creates a Reference whose base value is a
|
||||
// primitive Symbol value.
|
||||
|
||||
var symbols = [
|
||||
Symbol(),
|
||||
Symbol("ponies"),
|
||||
Symbol.for("sym"),
|
||||
Symbol.iterator
|
||||
];
|
||||
|
||||
// Test accessor property, used below.
|
||||
var gets, sets;
|
||||
Object.defineProperty(Symbol.prototype, "prop", {
|
||||
get: function () {
|
||||
"use strict";
|
||||
gets++;
|
||||
assertEq(typeof this, "object");
|
||||
assertEq(this instanceof Symbol, true);
|
||||
return "got";
|
||||
},
|
||||
set: function (v) {
|
||||
"use strict";
|
||||
sets++;
|
||||
assertEq(typeof this, "object");
|
||||
assertEq(this instanceof Symbol, true);
|
||||
assertEq(v, "newvalue");
|
||||
}
|
||||
});
|
||||
|
||||
for (var sym of symbols) {
|
||||
assertEq(sym.constructor, Symbol);
|
||||
|
||||
// method on Object.prototype
|
||||
assertEq(sym.hasOwnProperty("constructor"), false);
|
||||
assertEq(sym.toLocaleString(), sym.toString()); // once .toString() exists
|
||||
|
||||
// custom method monkeypatched onto Symbol.prototype
|
||||
Symbol.prototype.nonStrictMethod = function (arg) {
|
||||
assertEq(arg, "ok");
|
||||
assertEq(this instanceof Symbol, true);
|
||||
return 13;
|
||||
};
|
||||
assertEq(sym.nonStrictMethod("ok"), 13);
|
||||
|
||||
// the same, but strict mode
|
||||
Symbol.prototype.strictMethod = function (arg) {
|
||||
"use strict";
|
||||
assertEq(arg, "ok2");
|
||||
assertEq(this, sym);
|
||||
return 14;
|
||||
};
|
||||
assertEq(sym.strictMethod("ok2"), 14);
|
||||
|
||||
// getter/setter on Symbol.prototype
|
||||
gets = 0;
|
||||
sets = 0;
|
||||
var propname = "prop";
|
||||
|
||||
assertEq(sym.prop, "got");
|
||||
assertEq(gets, 1);
|
||||
assertEq(sym[propname], "got");
|
||||
assertEq(gets, 2);
|
||||
|
||||
assertEq(sym.prop = "newvalue", "newvalue");
|
||||
assertEq(sets, 1);
|
||||
assertEq(sym[propname] = "newvalue", "newvalue");
|
||||
assertEq(sets, 2);
|
||||
|
||||
// non-existent property
|
||||
assertEq(sym.noSuchProp, undefined);
|
||||
var noSuchPropName = "nonesuch";
|
||||
assertEq(sym[noSuchPropName], undefined);
|
||||
|
||||
// non-existent method
|
||||
assertThrowsInstanceOf(() => sym.noSuchProp(), TypeError);
|
||||
assertThrowsInstanceOf(() => sym[noSuchPropName](), TypeError);
|
||||
}
|
||||
|
||||
if (typeof reportCompare === "function")
|
||||
reportCompare(0, 0);
|
|
@ -24,6 +24,16 @@ for (var sym of symbols) {
|
|||
assertThrowsInstanceOf(() => sym + "", TypeError);
|
||||
assertThrowsInstanceOf(() => "" + [1, 2, Symbol()], TypeError);
|
||||
assertThrowsInstanceOf(() => ["simple", "thimble", Symbol()].join(), TypeError);
|
||||
|
||||
// 7.1.13 ToObject
|
||||
var obj = Object(sym);
|
||||
assertEq(typeof obj, "object");
|
||||
assertEq(Object.prototype.toString.call(obj), "[object Symbol]");
|
||||
assertEq(Object.getPrototypeOf(obj), Symbol.prototype);
|
||||
assertEq(Object.getOwnPropertyNames(obj).length, 0);
|
||||
assertEq(Object(sym) === Object(sym), false); // new object each time
|
||||
var f = function () { return this; };
|
||||
assertEq(f.call(sym) === f.call(sym), false); // new object each time
|
||||
}
|
||||
|
||||
if (typeof reportCompare === "function")
|
||||
|
|
|
@ -5,5 +5,7 @@ assertEq(typeof Symbol(), "symbol");
|
|||
assertEq(typeof Symbol("ponies"), "symbol");
|
||||
assertEq(typeof Symbol.for("ponies"), "symbol");
|
||||
|
||||
assertEq(typeof Object(Symbol()), "object");
|
||||
|
||||
if (typeof reportCompare === "function")
|
||||
reportCompare(0, 0);
|
||||
|
|
Загрузка…
Ссылка в новой задаче