1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:28:11 +00:00

LibJS: Convert most builtin tests to new system

This commit is contained in:
Matthew Olsson 2020-07-04 10:09:48 -07:00 committed by Andreas Kling
parent 46581773c1
commit 3f97d75778
107 changed files with 2031 additions and 2243 deletions

View file

@ -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");
});
});