mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:57:35 +00:00
LibJS: Convert most builtin tests to new system
This commit is contained in:
parent
46581773c1
commit
3f97d75778
107 changed files with 2031 additions and 2243 deletions
|
@ -1,123 +1,127 @@
|
|||
load("test-common.js");
|
||||
describe("normal functionality", () => {
|
||||
test("non-configurable property", () => {
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });
|
||||
|
||||
try {
|
||||
var o = {};
|
||||
Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });
|
||||
expect(o.foo).toBe(1);
|
||||
o.foo = 2;
|
||||
expect(o.foo).toBe(1);
|
||||
|
||||
assert(o.foo === 1);
|
||||
o.foo = 2;
|
||||
assert(o.foo === 1);
|
||||
Object.defineProperty(o, 2, { get() { return 10; } });
|
||||
assert(o[2] === 10);
|
||||
|
||||
var d = Object.getOwnPropertyDescriptor(o, "foo");
|
||||
assert(d.configurable === false);
|
||||
assert(d.enumerable === false);
|
||||
assert(d.writable === false);
|
||||
assert(d.value === 1);
|
||||
|
||||
Object.defineProperty(o, "bar", { value: "hi", writable: true, enumerable: true });
|
||||
|
||||
assert(o.bar === "hi");
|
||||
o.bar = "ho";
|
||||
assert(o.bar === "ho");
|
||||
|
||||
d = Object.getOwnPropertyDescriptor(o, "bar");
|
||||
assert(d.configurable === false);
|
||||
assert(d.enumerable === true);
|
||||
assert(d.writable === true);
|
||||
assert(d.value === "ho");
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.defineProperty(o, "bar", { value: "xx", enumerable: false });
|
||||
}, {
|
||||
error: TypeError
|
||||
expect(o).not.toHaveConfigurableProperty("foo");
|
||||
expect(o).not.toHaveEnumerableProperty("foo");
|
||||
expect(o).not.toHaveWritableProperty("foo");
|
||||
expect(o).toHaveValueProperty("foo", 1);
|
||||
});
|
||||
|
||||
Object.defineProperty(o, "baz", { value: 9, configurable: true, writable: false });
|
||||
Object.defineProperty(o, "baz", { configurable: true, writable: true });
|
||||
|
||||
d = Object.getOwnPropertyDescriptor(o, "baz");
|
||||
assert(d.configurable === true);
|
||||
assert(d.writable === true);
|
||||
assert(d.value === 9);
|
||||
|
||||
Object.defineProperty(o, "qux", {
|
||||
configurable: true,
|
||||
get() {
|
||||
return o.secret_qux + 1;
|
||||
},
|
||||
set(value) {
|
||||
this.secret_qux = value + 1;
|
||||
},
|
||||
test("array index getter", () => {
|
||||
let o = {};
|
||||
Object.defineProperty(o, 2, { get() { return 10; } });
|
||||
expect(o[2]).toBe(10);
|
||||
});
|
||||
|
||||
o.qux = 10;
|
||||
assert(o.qux === 12);
|
||||
o.qux = 20;
|
||||
assert(o.qux = 22);
|
||||
test("configurable property", () => {
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { value: "hi", writable: true, enumerable: true });
|
||||
|
||||
Object.defineProperty(o, "qux", { configurable: true, value: 4 });
|
||||
expect(o.foo).toBe("hi");
|
||||
o.foo = "ho";
|
||||
expect(o.foo).toBe("ho");
|
||||
|
||||
assert(o.qux === 4);
|
||||
o.qux = 5;
|
||||
assert(o.qux = 4);
|
||||
|
||||
Object.defineProperty(o, "qux", {
|
||||
configurable: false,
|
||||
get() {
|
||||
return this.secret_qux + 2;
|
||||
},
|
||||
set(value) {
|
||||
o.secret_qux = value + 2;
|
||||
},
|
||||
expect(o).not.toHaveConfigurableProperty("foo");
|
||||
expect(o).toHaveEnumerableProperty("foo");
|
||||
expect(o).toHaveWritableProperty("foo");
|
||||
expect(o).toHaveValueProperty("foo", "ho");
|
||||
});
|
||||
|
||||
o.qux = 10;
|
||||
assert(o.qux === 14);
|
||||
o.qux = 20;
|
||||
assert(o.qux = 24);
|
||||
test("reconfigure configurable property", () => {
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { value: 9, configurable: true, writable: false });
|
||||
Object.defineProperty(o, "foo", { configurable: true, writable: true });
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.defineProperty(o, "qux", {
|
||||
configurable: false,
|
||||
expect(o).toHaveConfigurableProperty("foo");
|
||||
expect(o).toHaveWritableProperty("foo");
|
||||
expect(o).not.toHaveEnumerableProperty("foo");
|
||||
expect(o).toHaveValueProperty("foo", 9);
|
||||
});
|
||||
|
||||
test("define accessor", () => {
|
||||
let o = {};
|
||||
|
||||
Object.defineProperty(o, "foo", {
|
||||
configurable: true,
|
||||
get() {
|
||||
return this.secret_qux + 2;
|
||||
return o.secret_foo + 1;
|
||||
},
|
||||
set(value) {
|
||||
this.secret_foo = value + 1;
|
||||
},
|
||||
});
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot change attributes of non-configurable property 'qux'",
|
||||
|
||||
o.foo = 10;
|
||||
expect(o.foo).toBe(12);
|
||||
o.foo = 20;
|
||||
expect(o.foo).toBe(22);
|
||||
|
||||
Object.defineProperty(o, "foo", { configurable: true, value: 4 });
|
||||
|
||||
expect(o.foo).toBe(4);
|
||||
expect(o.foo = 5).toBe(5);
|
||||
expect(o.foo = 4).toBe(4);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("redefine non-configurable property", () => {
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { value: 1, writable: true, enumerable: true });
|
||||
|
||||
expect(() => {
|
||||
Object.defineProperty(o, "foo", { value: 2, writable: false, enumerable: true });
|
||||
}).toThrowWithMessage(TypeError, "Cannot change attributes of non-configurable property 'foo'");
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.defineProperty(o, "qux", { value: 2 });
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot change attributes of non-configurable property 'qux'",
|
||||
test("cannot define 'value' and 'get' in the same descriptor", () => {
|
||||
let o = {};
|
||||
|
||||
expect(() => {
|
||||
Object.defineProperty(o, "a", {
|
||||
get() {},
|
||||
value: 9,
|
||||
});
|
||||
}).toThrowWithMessage(TypeError, "Accessor property descriptor cannot specify a value or writable key");
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.defineProperty(o, "a", {
|
||||
get() {},
|
||||
value: 9,
|
||||
test("cannot define 'value' and 'set' in the same descriptor", () => {
|
||||
let o = {};
|
||||
|
||||
expect(() => {
|
||||
Object.defineProperty(o, "a", {
|
||||
set() {},
|
||||
writable: true,
|
||||
});
|
||||
}).toThrowWithMessage(TypeError, "Accessor property descriptor cannot specify a value or writable key");
|
||||
});
|
||||
|
||||
test("redefine non-configurable accessor", () => {
|
||||
let o = {};
|
||||
|
||||
Object.defineProperty(o, "foo", {
|
||||
configurable: false,
|
||||
get() {
|
||||
return this.secret_foo + 2;
|
||||
},
|
||||
set(value) {
|
||||
o.secret_foo = value + 2;
|
||||
},
|
||||
});
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Accessor property descriptor cannot specify a value or writable key",
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.defineProperty(o, "a", {
|
||||
set() {},
|
||||
writable: true,
|
||||
});
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Accessor property descriptor cannot specify a value or writable key",
|
||||
expect(() => {
|
||||
Object.defineProperty(o, "foo", {
|
||||
configurable: false,
|
||||
get() {
|
||||
return this.secret_foo + 2;
|
||||
},
|
||||
});
|
||||
}).toThrowWithMessage(TypeError, "Cannot change attributes of non-configurable property 'foo'");
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,54 +1,45 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Object.entries.length === 1);
|
||||
assert(Object.entries(true).length === 0);
|
||||
assert(Object.entries(45).length === 0);
|
||||
assert(Object.entries(-998).length === 0);
|
||||
assert(Object.entries("abcd").length === 4);
|
||||
assert(Object.entries([1, 2, 3]).length === 3);
|
||||
assert(Object.entries({ a: 1, b: 2, c: 3 }).length === 3);
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.entries(null);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "ToObject on null or undefined",
|
||||
describe("basic functionality", () => {
|
||||
test("length", () => {
|
||||
expect(Object.entries).toHaveLength(1);
|
||||
expect(Object.entries(true)).toHaveLength(0);
|
||||
expect(Object.entries(45)).toHaveLength(0);
|
||||
expect(Object.entries(-998)).toHaveLength(0);
|
||||
expect(Object.entries("abcd")).toHaveLength(4);
|
||||
expect(Object.entries([1, 2, 3])).toHaveLength(3);
|
||||
expect(Object.entries({ a: 1, b: 2, c: 3 })).toHaveLength(3);
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.entries(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "ToObject on null or undefined",
|
||||
test("entries with object", () => {
|
||||
let entries = Object.entries({ foo: 1, bar: 2, baz: 3 });
|
||||
|
||||
expect(entries).toEqual([["foo", 1], ["bar", 2], ["baz", 3]]);
|
||||
});
|
||||
|
||||
let entries = Object.entries({ foo: 1, bar: 2, baz: 3 });
|
||||
assert(
|
||||
entries.length === 3 && entries[0].length === 2 &&
|
||||
entries[1].length === 2 && entries[2].length === 2 &&
|
||||
entries[0][0] === "foo" && entries[0][1] === 1 &&
|
||||
entries[1][0] === "bar" && entries[1][1] === 2 &&
|
||||
entries[2][0] === "baz" && entries[2][1] === 3
|
||||
);
|
||||
|
||||
entries = Object.entries(["a", "b", "c"]);
|
||||
assert(
|
||||
entries.length === 3 && entries[0].length === 2 &&
|
||||
entries[1].length === 2 && entries[2].length === 2 &&
|
||||
entries[0][0] === "0" && entries[0][1] === "a" &&
|
||||
entries[1][0] === "1" && entries[1][1] === "b" &&
|
||||
entries[2][0] === "2" && entries[2][1] === "c"
|
||||
);
|
||||
|
||||
let obj = { foo: 1 };
|
||||
Object.defineProperty(obj, "getFoo", {
|
||||
value: function() { return this.foo; },
|
||||
test("entries with array", () => {
|
||||
entries = Object.entries(["a", "b", "c"]);
|
||||
expect(entries).toEqual([["0", "a"], ["1", "b"], ["2", "c"]]);
|
||||
});
|
||||
let entries = Object.entries(obj);
|
||||
assert(entries.length === 1 && entries[0][0] === "foo" && entries[0][1] === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("ignores non-enumerable properties", () => {
|
||||
let obj = { foo: 1 };
|
||||
Object.defineProperty(obj, "getFoo", {
|
||||
value: function() { return this.foo; },
|
||||
});
|
||||
let entries = Object.entries(obj);
|
||||
expect(entries).toEqual([["foo", 1]]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("null argument", () => {
|
||||
expect(() => {
|
||||
Object.entries(null);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
|
||||
test("undefined argument", () => {
|
||||
expect(() => {
|
||||
Object.entries(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
})
|
||||
});
|
||||
|
|
|
@ -1,51 +1,49 @@
|
|||
load("test-common.js");
|
||||
test("plain property", () => {
|
||||
let o = { foo: "bar" };
|
||||
|
||||
try {
|
||||
let o = {
|
||||
foo: "bar",
|
||||
get x() { },
|
||||
set ["hi" + 1](_) { },
|
||||
};
|
||||
expect(o).toHaveConfigurableProperty("foo");
|
||||
expect(o).toHaveEnumerableProperty("foo");
|
||||
expect(o).toHaveWritableProperty("foo");
|
||||
expect(o).toHaveValueProperty("foo", "bar");
|
||||
expect(o).not.toHaveGetterProperty("foo");
|
||||
expect(o).not.toHaveSetterProperty("foo");
|
||||
});
|
||||
|
||||
Object.defineProperty(o, "baz", {
|
||||
test("getter property", () => {
|
||||
let o = { get foo() {} };
|
||||
|
||||
expect(o).toHaveConfigurableProperty("foo");
|
||||
expect(o).toHaveEnumerableProperty("foo");
|
||||
expect(o).not.toHaveWritableProperty("foo");
|
||||
expect(o).not.toHaveValueProperty("foo");
|
||||
expect(o).toHaveGetterProperty("foo");
|
||||
expect(o).not.toHaveSetterProperty("foo");
|
||||
});
|
||||
|
||||
test("setter property", () => {
|
||||
let o = { set foo(_) {} };
|
||||
|
||||
expect(o).toHaveConfigurableProperty("foo");
|
||||
expect(o).toHaveEnumerableProperty("foo");
|
||||
expect(o).not.toHaveWritableProperty("foo");
|
||||
expect(o).not.toHaveValueProperty("foo");
|
||||
expect(o).not.toHaveGetterProperty("foo");
|
||||
expect(o).toHaveSetterProperty("foo");
|
||||
});
|
||||
|
||||
test("defined property", () => {
|
||||
let o = {};
|
||||
|
||||
Object.defineProperty(o, "foo", {
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
value: 10,
|
||||
});
|
||||
|
||||
let d = Object.getOwnPropertyDescriptor(o, "foo");
|
||||
assert(d.enumerable === true);
|
||||
assert(d.configurable === true);
|
||||
assert(d.writable === true);
|
||||
assert(d.value === "bar");
|
||||
assert(d.get === undefined);
|
||||
assert(d.set === undefined);
|
||||
|
||||
let d = Object.getOwnPropertyDescriptor(o, "x");
|
||||
assert(d.enumerable === true);
|
||||
assert(d.configurable === true);
|
||||
assert(d.writable === undefined);
|
||||
assert(d.value === undefined);
|
||||
assert(typeof d.get === "function");
|
||||
assert(d.set === undefined);
|
||||
|
||||
let d = Object.getOwnPropertyDescriptor(o, "hi1");
|
||||
assert(d.enumerable === true);
|
||||
assert(d.configurable === true);
|
||||
assert(d.writable === undefined);
|
||||
assert(d.value === undefined);
|
||||
assert(d.get === undefined);
|
||||
assert(typeof d.set === "function");
|
||||
|
||||
let d = Object.getOwnPropertyDescriptor(o, "baz");
|
||||
assert(d.enumerable === false);
|
||||
assert(d.configurable === false);
|
||||
assert(d.writable === true);
|
||||
assert(d.value === 10);
|
||||
assert(d.get === undefined);
|
||||
assert(d.set === undefined);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(o).not.toHaveConfigurableProperty("foo");
|
||||
expect(o).not.toHaveEnumerableProperty("foo");
|
||||
expect(o).toHaveWritableProperty("foo");
|
||||
expect(o).toHaveValueProperty("foo", 10);
|
||||
expect(o).not.toHaveGetterProperty("foo");
|
||||
expect(o).not.toHaveSetterProperty("foo");
|
||||
});
|
||||
|
|
|
@ -1,21 +1,9 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("use with array", () => {
|
||||
let names = Object.getOwnPropertyNames([1, 2, 3]);
|
||||
expect(names).toEqual(["0", "1", "2", "length"]);
|
||||
});
|
||||
|
||||
assert(names.length === 4);
|
||||
assert(names[0] === "0");
|
||||
assert(names[1] === "1");
|
||||
assert(names[2] === "2");
|
||||
assert(names[3] === "length");
|
||||
|
||||
names = Object.getOwnPropertyNames({ foo: 1, bar: 2, baz: 3 });
|
||||
assert(names.length === 3);
|
||||
assert(names[0] === "foo");
|
||||
assert(names[1] === "bar");
|
||||
assert(names[2] === "baz");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("use with object", () => {
|
||||
let names = Object.getOwnPropertyNames({ foo: 1, bar: 2, baz: 3 });
|
||||
expect(names).toEqual(["foo", "bar", "baz"]);
|
||||
});
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
load("test-common.js");
|
||||
test("basic functionality", () => {
|
||||
let o1 = new Object();
|
||||
let o2 = {};
|
||||
|
||||
try {
|
||||
var o1 = new Object();
|
||||
var o2 = {};
|
||||
assert(Object.getPrototypeOf(o1) === Object.getPrototypeOf(o2));
|
||||
assert(Object.getPrototypeOf(Object.getPrototypeOf(o1)) === null);
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(Object.getPrototypeOf(o1)).toBe(Object.getPrototypeOf(o2));
|
||||
expect(Object.getPrototypeOf(Object.getPrototypeOf(o1))).toBe(null);
|
||||
|
||||
Object.setPrototypeOf(o1, o2);
|
||||
expect(Object.getPrototypeOf(o1)).toBe(o2);
|
||||
});
|
||||
|
|
|
@ -1,53 +1,54 @@
|
|||
load("test-common.js");
|
||||
test("length", () => {
|
||||
expect(Object.is).toHaveLength(2);
|
||||
});
|
||||
|
||||
try {
|
||||
assert(Object.is.length === 2);
|
||||
test("arguments that evaluate to true", () => {
|
||||
let a = [1, 2, 3];
|
||||
let o = { foo: "bar" };
|
||||
|
||||
var a = [1, 2, 3];
|
||||
var o = {foo: "bar"};
|
||||
expect(Object.is("", "")).toBeTrue();
|
||||
expect(Object.is("foo", "foo")).toBeTrue();
|
||||
expect(Object.is(0, 0)).toBeTrue();
|
||||
expect(Object.is(+0, +0)).toBeTrue();
|
||||
expect(Object.is(-0, -0)).toBeTrue();
|
||||
expect(Object.is(1.23, 1.23)).toBeTrue();
|
||||
expect(Object.is(42, 42)).toBeTrue();
|
||||
expect(Object.is(NaN, NaN)).toBeTrue();
|
||||
expect(Object.is(Infinity, Infinity)).toBeTrue();
|
||||
expect(Object.is(+Infinity, +Infinity)).toBeTrue();
|
||||
expect(Object.is(-Infinity, -Infinity)).toBeTrue();
|
||||
expect(Object.is(true, true)).toBeTrue();
|
||||
expect(Object.is(false, false)).toBeTrue();
|
||||
expect(Object.is(null, null)).toBeTrue();
|
||||
expect(Object.is(undefined, undefined)).toBeTrue();
|
||||
expect(Object.is(undefined)).toBeTrue();
|
||||
expect(Object.is()).toBeTrue();
|
||||
expect(Object.is(a, a)).toBeTrue();
|
||||
expect(Object.is(o, o)).toBeTrue();
|
||||
});
|
||||
|
||||
assert(Object.is("", "") === true);
|
||||
assert(Object.is("foo", "foo") === true);
|
||||
assert(Object.is(0, 0) === true);
|
||||
assert(Object.is(+0, +0) === true);
|
||||
assert(Object.is(-0, -0) === true);
|
||||
assert(Object.is(1.23, 1.23) === true);
|
||||
assert(Object.is(42, 42) === true);
|
||||
assert(Object.is(NaN, NaN) === true);
|
||||
assert(Object.is(Infinity, Infinity) === true);
|
||||
assert(Object.is(+Infinity, +Infinity) === true);
|
||||
assert(Object.is(-Infinity, -Infinity) === true);
|
||||
assert(Object.is(true, true) === true);
|
||||
assert(Object.is(false, false) === true);
|
||||
assert(Object.is(null, null) === true);
|
||||
assert(Object.is(undefined, undefined) === true);
|
||||
assert(Object.is(undefined) === true);
|
||||
assert(Object.is() === true);
|
||||
assert(Object.is(a, a) === true);
|
||||
assert(Object.is(o, o) === true);
|
||||
test("arguments that evaluate to false", () => {
|
||||
let a = [1, 2, 3];
|
||||
let o = { foo: "bar" };
|
||||
|
||||
assert(Object.is("test") === false);
|
||||
assert(Object.is("foo", "bar") === false);
|
||||
assert(Object.is(1, "1") === false);
|
||||
assert(Object.is(+0, -0) === false);
|
||||
assert(Object.is(-0, +0) === false);
|
||||
assert(Object.is(42, 24) === false);
|
||||
assert(Object.is(Infinity, -Infinity) === false);
|
||||
assert(Object.is(-Infinity, +Infinity) === false);
|
||||
assert(Object.is(true, false) === false);
|
||||
assert(Object.is(false, true) === false);
|
||||
assert(Object.is(undefined, null) === false);
|
||||
assert(Object.is(null, undefined) === false);
|
||||
assert(Object.is([], []) === false);
|
||||
assert(Object.is(a, [1, 2, 3]) === false);
|
||||
assert(Object.is([1, 2, 3], a) === false);
|
||||
assert(Object.is({}, {}) === false);
|
||||
assert(Object.is(o, {foo: "bar"}) === false);
|
||||
assert(Object.is({foo: "bar"}, o) === false);
|
||||
assert(Object.is(a, o) === false);
|
||||
assert(Object.is(o, a) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(Object.is("test")).toBeFalse();
|
||||
expect(Object.is("foo", "bar")).toBeFalse();
|
||||
expect(Object.is(1, "1")).toBeFalse();
|
||||
expect(Object.is(+0, -0)).toBeFalse();
|
||||
expect(Object.is(-0, +0)).toBeFalse();
|
||||
expect(Object.is(42, 24)).toBeFalse();
|
||||
expect(Object.is(Infinity, -Infinity)).toBeFalse();
|
||||
expect(Object.is(-Infinity, +Infinity)).toBeFalse();
|
||||
expect(Object.is(true, false)).toBeFalse();
|
||||
expect(Object.is(false, true)).toBeFalse();
|
||||
expect(Object.is(undefined, null)).toBeFalse();
|
||||
expect(Object.is(null, undefined)).toBeFalse();
|
||||
expect(Object.is([], [])).toBeFalse();
|
||||
expect(Object.is(a, [1, 2, 3])).toBeFalse();
|
||||
expect(Object.is([1, 2, 3], a)).toBeFalse();
|
||||
expect(Object.is({}, {})).toBeFalse();
|
||||
expect(Object.is(o, {foo: "bar"})).toBeFalse();
|
||||
expect(Object.is({foo: "bar"}, o)).toBeFalse();
|
||||
expect(Object.is(a, o)).toBeFalse();
|
||||
expect(Object.is(o, a)).toBeFalse();
|
||||
});
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
load("test-common.js");
|
||||
test("basic functionality", () => {
|
||||
expect(Object.isExtensible).toHaveLength(1);
|
||||
|
||||
try {
|
||||
assert(Object.isExtensible() === false);
|
||||
assert(Object.isExtensible(undefined) === false);
|
||||
assert(Object.isExtensible(null) === false);
|
||||
assert(Object.isExtensible(true) === false);
|
||||
assert(Object.isExtensible(6) === false);
|
||||
assert(Object.isExtensible("test") === false);
|
||||
expect(Object.isExtensible()).toBeFalse();
|
||||
expect(Object.isExtensible(undefined)).toBeFalse();
|
||||
expect(Object.isExtensible(null)).toBeFalse();
|
||||
expect(Object.isExtensible(true)).toBeFalse();
|
||||
expect(Object.isExtensible(6)).toBeFalse();
|
||||
expect(Object.isExtensible("test")).toBeFalse();
|
||||
|
||||
let s = Symbol();
|
||||
assert(Object.isExtensible(s) === false);
|
||||
expect(Object.isExtensible(s)).toBeFalse();
|
||||
|
||||
let o = { foo: "foo" };
|
||||
assert(Object.isExtensible(o) === true);
|
||||
expect(Object.isExtensible(o)).toBeTrue();
|
||||
Object.preventExtensions(o);
|
||||
assert(Object.isExtensible(o) === false);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(Object.isExtensible(o)).toBeFalse();
|
||||
});
|
||||
|
|
|
@ -1,14 +1,8 @@
|
|||
load("test-common.js");
|
||||
test("basic functionality", () => {
|
||||
expect(Object).toHaveLength(1);
|
||||
expect(Object.name).toBe("Object");
|
||||
expect(Object.prototype.length).toBe(undefined);
|
||||
|
||||
try {
|
||||
assert(Object.length === 1);
|
||||
assert(Object.name === "Object");
|
||||
assert(Object.prototype.length === undefined);
|
||||
|
||||
assert(typeof Object() === "object");
|
||||
assert(typeof new Object() === "object");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(typeof Object()).toBe("object");
|
||||
expect(typeof new Object()).toBe("object");
|
||||
});
|
||||
|
|
|
@ -1,42 +1,44 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Object.keys.length === 1);
|
||||
assert(Object.keys(true).length === 0);
|
||||
assert(Object.keys(45).length === 0);
|
||||
assert(Object.keys(-998).length === 0);
|
||||
assert(Object.keys("abcd").length === 4);
|
||||
assert(Object.keys([1, 2, 3]).length === 3);
|
||||
assert(Object.keys({ a: 1, b: 2, c: 3 }).length === 3);
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.keys(null);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "ToObject on null or undefined",
|
||||
describe("correct behavior", () => {
|
||||
test("length", () => {
|
||||
expect(Object.keys).toHaveLength(1);
|
||||
expect(Object.keys(true)).toHaveLength(0);
|
||||
expect(Object.keys(45)).toHaveLength(0);
|
||||
expect(Object.keys(-998)).toHaveLength(0);
|
||||
expect(Object.keys("abcd")).toHaveLength(4);
|
||||
expect(Object.keys([1, 2, 3])).toHaveLength(3);
|
||||
expect(Object.keys({ a: 1, b: 2, c: 3 })).toHaveLength(3);
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.keys(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "ToObject on null or undefined",
|
||||
test("object argument", () => {
|
||||
let keys = Object.keys({ foo: 1, bar: 2, baz: 3 });
|
||||
expect(keys).toEqual(["foo", "bar", "baz"]);
|
||||
});
|
||||
|
||||
let keys = Object.keys({ foo: 1, bar: 2, baz: 3 });
|
||||
assert(keys[0] === "foo" && keys[1] === "bar" && keys[2] === "baz");
|
||||
|
||||
keys = Object.keys(["a", "b", "c"]);
|
||||
assert(keys[0] === "0" && keys[1] === "1" && keys[2] === "2");
|
||||
|
||||
let obj = { foo: 1 };
|
||||
Object.defineProperty(obj, 'getFoo', {
|
||||
value: function() { return this.foo; },
|
||||
test("array argument", () => {
|
||||
let keys = Object.keys(["a", "b", "c"]);
|
||||
expect(keys).toEqual(["0", "1", "2"]);
|
||||
});
|
||||
keys = Object.keys(obj);
|
||||
assert(keys.length === 1 && keys[0] === 'foo');
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("ignores non-enumerable properties", () => {
|
||||
let obj = { foo: 1 };
|
||||
Object.defineProperty(obj, "getFoo", {
|
||||
value: function() { return this.foo; },
|
||||
});
|
||||
keys = Object.keys(obj);
|
||||
expect(keys).toEqual(["foo"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("null argument value", () => {
|
||||
expect(() => {
|
||||
Object.keys(null);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
|
||||
test("undefined argument value", () => {
|
||||
expect(() => {
|
||||
Object.keys(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,54 +1,53 @@
|
|||
load("test-common.js");
|
||||
describe("correct behavior", () => {
|
||||
test("non-object arguments", () => {
|
||||
expect(Object.preventExtensions()).toBeUndefined();
|
||||
expect(Object.preventExtensions(undefined)).toBeUndefined();
|
||||
expect(Object.preventExtensions(null)).toBeNull();
|
||||
expect(Object.preventExtensions(true)).toBeTrue();
|
||||
expect(Object.preventExtensions(6)).toBe(6);
|
||||
expect(Object.preventExtensions("test")).toBe("test");
|
||||
|
||||
try {
|
||||
assert(Object.preventExtensions() === undefined);
|
||||
assert(Object.preventExtensions(undefined) === undefined);
|
||||
assert(Object.preventExtensions(null) === null);
|
||||
assert(Object.preventExtensions(true) === true);
|
||||
assert(Object.preventExtensions(6) === 6);
|
||||
assert(Object.preventExtensions("test") === "test");
|
||||
|
||||
let s = Symbol();
|
||||
assert(Object.preventExtensions(s) === s);
|
||||
|
||||
let o = { foo: "foo" };
|
||||
assert(o.foo === "foo");
|
||||
o.bar = "bar";
|
||||
assert(o.bar === "bar");
|
||||
|
||||
assert(Object.preventExtensions(o) === o);
|
||||
assert(o.foo === "foo");
|
||||
assert(o.bar === "bar");
|
||||
|
||||
o.baz = "baz";
|
||||
assert(o.baz === undefined);
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.defineProperty(o, "baz", { value: "baz" });
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot define property baz on non-extensible object",
|
||||
let s = Symbol();
|
||||
expect(Object.preventExtensions(s)).toBe(s);
|
||||
});
|
||||
|
||||
assert(o.baz === undefined);
|
||||
test("basic functionality", () => {
|
||||
let o = { foo: "foo" };
|
||||
expect(o.foo).toBe("foo");
|
||||
o.bar = "bar";
|
||||
expect(o.bar).toBe("bar");
|
||||
|
||||
expect(Object.preventExtensions(o)).toBe(o);
|
||||
expect(o.foo).toBe("foo");
|
||||
expect(o.bar).toBe("bar");
|
||||
|
||||
assertThrowsError(() => {
|
||||
"use strict";
|
||||
o.baz = "baz";
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot define property baz on non-extensible object",
|
||||
expect(o.baz).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("defining a property on a non-extensible object", () => {
|
||||
let o = {};
|
||||
Object.preventExtensions(o);
|
||||
|
||||
expect(() => {
|
||||
Object.defineProperty(o, "baz", { value: "baz" });
|
||||
}).toThrowWithMessage(TypeError, "Cannot define property baz on non-extensible object");
|
||||
|
||||
expect(o.baz).toBeUndefined();
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
"use strict";
|
||||
Object.defineProperty(o, "baz", { value: "baz" });
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot define property baz on non-extensible object",
|
||||
});
|
||||
test("putting property on a non-extensible object", () => {
|
||||
let o = {};
|
||||
Object.preventExtensions(o);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(() => {
|
||||
"use strict";
|
||||
o.foo = "foo";
|
||||
}).toThrowWithMessage(TypeError, "Cannot define property foo on non-extensible object");
|
||||
|
||||
expect(o.foo = "foo").toBe("foo");
|
||||
expect(o.foo).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,36 +1,18 @@
|
|||
load("test-common.js");
|
||||
test("basic functionality", () => {
|
||||
[Array, BigInt, Boolean, Date, Error, Function, Number, Object, String].forEach(constructor => {
|
||||
expect(constructor.prototype.constructor).toBe(constructor);
|
||||
if (constructor !== BigInt)
|
||||
expect(Reflect.construct(constructor, []).constructor).toBe(constructor);
|
||||
});
|
||||
|
||||
try {
|
||||
assert(Array.prototype.constructor === Array);
|
||||
assert(Boolean.prototype.constructor === Boolean);
|
||||
assert(Date.prototype.constructor === Date);
|
||||
assert(Error.prototype.constructor === Error);
|
||||
assert(Function.prototype.constructor === Function);
|
||||
assert(Number.prototype.constructor === Number);
|
||||
assert(Object.prototype.constructor === Object);
|
||||
|
||||
o = {};
|
||||
assert(o.constructor === Object);
|
||||
|
||||
o = new Object();
|
||||
assert(o.constructor === Object);
|
||||
let o = {};
|
||||
expect(o.constructor).toBe(Object);
|
||||
|
||||
a = [];
|
||||
assert(a.constructor === Array);
|
||||
expect(a.constructor).toBe(Array);
|
||||
|
||||
a = new Array();
|
||||
assert(a.constructor === Array);
|
||||
|
||||
n = new Number(3);
|
||||
assert(n.constructor === Number);
|
||||
|
||||
d = Object.getOwnPropertyDescriptor(Object.prototype, "constructor");
|
||||
assert(d.configurable === true);
|
||||
assert(d.enumerable === false);
|
||||
assert(d.writable === true);
|
||||
assert(d.value === Object);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(Object.prototype).toHaveConfigurableProperty("constructor");
|
||||
expect(Object.prototype).not.toHaveEnumerableProperty("constructor");
|
||||
expect(Object.prototype).toHaveWritableProperty("constructor");
|
||||
expect(Object.prototype).toHaveValueProperty("constructor", Object);
|
||||
});
|
||||
|
|
|
@ -1,17 +1,13 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("basic functionality", () => {
|
||||
var o = {};
|
||||
o.foo = 1;
|
||||
assert(o.hasOwnProperty("foo") === true);
|
||||
assert(o.hasOwnProperty("bar") === false);
|
||||
assert(o.hasOwnProperty() === false);
|
||||
assert(o.hasOwnProperty(undefined) === false);
|
||||
o.undefined = 2;
|
||||
assert(o.hasOwnProperty() === true);
|
||||
assert(o.hasOwnProperty(undefined) === true);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
o.foo = 1;
|
||||
expect(o.hasOwnProperty("foo")).toBeTrue();
|
||||
expect(o.hasOwnProperty("bar")).toBeFalse();
|
||||
expect(o.hasOwnProperty()).toBeFalse();
|
||||
expect(o.hasOwnProperty(undefined)).toBeFalse();
|
||||
|
||||
o.undefined = 2;
|
||||
expect(o.hasOwnProperty()).toBeTrue();
|
||||
expect(o.hasOwnProperty(undefined)).toBeTrue();
|
||||
});
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
test("basic functionality", () => {
|
||||
var o = new Object();
|
||||
Object.prototype.foo = 123;
|
||||
assert(o.foo === 123);
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
expect(o.foo).toBe(123);
|
||||
});
|
||||
|
|
|
@ -1,32 +1,31 @@
|
|||
load("test-common.js");
|
||||
describe("correct behavior", () => {
|
||||
test("length", () => {
|
||||
expect(Object.prototype.toLocaleString).toHaveLength(0);
|
||||
})
|
||||
|
||||
try {
|
||||
assert(Object.prototype.toLocaleString.length === 0);
|
||||
test("basic functionality", () => {
|
||||
let o;
|
||||
|
||||
var o;
|
||||
o = {};
|
||||
expect(o.toString()).toBe(o.toLocaleString());
|
||||
|
||||
o = {};
|
||||
assert(o.toString() === o.toLocaleString());
|
||||
o = { toString: () => 42 };
|
||||
expect(o.toString()).toBe(42);
|
||||
});
|
||||
});
|
||||
|
||||
o = { toString: () => 42 };
|
||||
assert(o.toString() === 42);
|
||||
|
||||
o = { toString: () => { throw Error(); } };
|
||||
assertThrowsError(() => {
|
||||
o.toLocaleString();
|
||||
}, {
|
||||
error: Error
|
||||
describe("errors", () => {
|
||||
test("toString that throws error", () => {
|
||||
let o = { toString: () => { throw new Error(); } };
|
||||
expect(() => {
|
||||
o.toLocaleString();
|
||||
}).toThrow(Error);
|
||||
});
|
||||
|
||||
o = { toString: "foo" };
|
||||
assertThrowsError(() => {
|
||||
o.toLocaleString();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "foo is not a function"
|
||||
test("toString that is not a function", () => {
|
||||
let o = { toString: "foo" };
|
||||
expect(() => {
|
||||
o.toLocaleString();
|
||||
}).toThrowWithMessage(TypeError, "foo is not a function");
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(typeof Object.prototype.toString() === "string");
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("basic functionality", () => {
|
||||
expect(Object.prototype.toString).toHaveLength(0);
|
||||
// FIXME: The tag is ObjectPrototype, but should be Object
|
||||
// expect(Object.prototype.toString()).toBe("[object Object]");
|
||||
expect({ foo: 1 }.toString()).toBe("[object Object]");
|
||||
expect([].toString()).toBe("");
|
||||
expect(Object.prototype.toString.call([])).toBe("[object Array]");
|
||||
});
|
||||
|
|
|
@ -1,36 +1,43 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Object.setPrototypeOf.length === 2);
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.setPrototypeOf();
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Object.setPrototypeOf requires at least two arguments",
|
||||
describe("correct behavior", () => {
|
||||
test("length", () => {
|
||||
expect(Object.setPrototypeOf).toHaveLength(2);
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.setPrototypeOf({}, "foo");
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Prototype must be an object or null"
|
||||
test("basic functionality", () => {
|
||||
let o = {};
|
||||
let p = {};
|
||||
expect(Object.setPrototypeOf(o, p)).toBe(o);
|
||||
expect(Object.getPrototypeOf(o)).toBe(p);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("requires two arguments", () => {
|
||||
expect(() => {
|
||||
Object.setPrototypeOf();
|
||||
}).toThrowWithMessage(TypeError, "Object.setPrototypeOf requires at least two arguments");
|
||||
|
||||
expect(() => {
|
||||
Object.setPrototypeOf({});
|
||||
}).toThrowWithMessage(TypeError, "Object.setPrototypeOf requires at least two arguments");
|
||||
});
|
||||
|
||||
o = {};
|
||||
p = {};
|
||||
assert(Object.setPrototypeOf(o, p) === o);
|
||||
|
||||
Object.preventExtensions(o);
|
||||
assertThrowsError(() => {
|
||||
Object.setPrototypeOf(o, {});
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Object's [[SetPrototypeOf]] method returned false"
|
||||
test("prototype must be an object", () => {
|
||||
expect(() => {
|
||||
Object.setPrototypeOf({}, "foo");
|
||||
}).toThrowWithMessage(TypeError, "Prototype must be an object or null");
|
||||
});
|
||||
assert(Object.setPrototypeOf(o, p) === o);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("non-extensible target", () => {
|
||||
let o = {};
|
||||
let p = {};
|
||||
Object.setPrototypeOf(o, p);
|
||||
Object.preventExtensions(o);
|
||||
|
||||
expect(() => {
|
||||
Object.setPrototypeOf(o, {});
|
||||
}).toThrowWithMessage(TypeError, "Object's [[SetPrototypeOf]] method returned false");
|
||||
|
||||
expect(Object.setPrototypeOf(o, p)).toBe(o);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,42 +1,44 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(Object.values.length === 1);
|
||||
assert(Object.values(true).length === 0);
|
||||
assert(Object.values(45).length === 0);
|
||||
assert(Object.values(-998).length === 0);
|
||||
assert(Object.values("abcd").length === 4);
|
||||
assert(Object.values([1, 2, 3]).length === 3);
|
||||
assert(Object.values({ a: 1, b: 2, c: 3 }).length === 3);
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.values(null);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "ToObject on null or undefined",
|
||||
describe("correct behavior", () => {
|
||||
test("lengths", () => {
|
||||
expect(Object.values).toHaveLength(1);
|
||||
expect(Object.values(true)).toHaveLength(0);
|
||||
expect(Object.values(45)).toHaveLength(0);
|
||||
expect(Object.values(-998)).toHaveLength(0);
|
||||
expect(Object.values("abcd")).toHaveLength(4);
|
||||
expect(Object.values([1, 2, 3])).toHaveLength(3);
|
||||
expect(Object.values({ a: 1, b: 2, c: 3 })).toHaveLength(3);
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
Object.values(undefined);
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "ToObject on null or undefined",
|
||||
test("object argument", () => {
|
||||
let values = Object.values({ foo: 1, bar: 2, baz: 3 });
|
||||
expect(values).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
let values = Object.values({ foo: 1, bar: 2, baz: 3 });
|
||||
assert(values[0] === 1 && values[1] === 2 && values[2] === 3);
|
||||
|
||||
values = Object.values(["a", "b", "c"]);
|
||||
assert(values[0] === "a" && values[1] === "b" && values[2] === "c");
|
||||
|
||||
let obj = { foo: 1 };
|
||||
Object.defineProperty(obj, 'getFoo', {
|
||||
value: function() { return this.foo; },
|
||||
test("array argument", () => {
|
||||
let values = Object.values(["a", "b", "c"]);
|
||||
expect(values).toEqual(["a", "b", "c"]);
|
||||
});
|
||||
let values = Object.values(obj);
|
||||
assert(values.length === 1 && values[0] === 1);
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
||||
test("ignores non-enumerable properties", () => {
|
||||
let obj = { foo: 1 };
|
||||
Object.defineProperty(obj, 'getFoo', {
|
||||
value: function() { return this.foo; },
|
||||
});
|
||||
let values = Object.values(obj);
|
||||
expect(values).toEqual([1]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
test("null argument", () => {
|
||||
expect(() => {
|
||||
Object.values(null);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
|
||||
test("undefined argument", () => {
|
||||
expect(() => {
|
||||
Object.values(undefined);
|
||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue