mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 20:45:14 +00:00
LibJS: Add more test matchers
This commit is contained in:
parent
5e971c91e3
commit
fc08222f46
Notes:
sideshowbarker
2024-07-19 05:04:52 +09:00
Author: https://github.com/mattco98 Commit: https://github.com/SerenityOS/serenity/commit/fc08222f469 Pull-request: https://github.com/SerenityOS/serenity/pull/2689 Reviewed-by: https://github.com/linusg
8 changed files with 200 additions and 46 deletions
|
@ -14,9 +14,9 @@ describe("[[DefineProperty]] trap normal behavior", () => {
|
|||
defineProperty(target, name, descriptor) {
|
||||
expect(target).toBe(o);
|
||||
expect(name).toBe("foo");
|
||||
expect(descriptor.configurable).toBe(true);
|
||||
expect(descriptor.configurable).toBeTrue();
|
||||
expect(descriptor.enumerable).toBeUndefined();
|
||||
expect(descriptor.writable).toBe(true);
|
||||
expect(descriptor.writable).toBeTrue();
|
||||
expect(descriptor.value).toBe(10);
|
||||
expect(descriptor.get).toBeUndefined();
|
||||
expect(descriptor.set).toBeUndefined();
|
||||
|
@ -38,22 +38,20 @@ describe("[[DefineProperty]] trap normal behavior", () => {
|
|||
});
|
||||
|
||||
Object.defineProperty(p, "foo", { value: 10, enumerable: true, configurable: false, writable: true });
|
||||
let d = Object.getOwnPropertyDescriptor(p, "foo");
|
||||
expect(d.enumerable).toBe(true);
|
||||
expect(d.configurable).toBe(false);
|
||||
expect(d.writable).toBe(true);
|
||||
expect(d.value).toBe(10);
|
||||
expect(d.get).toBeUndefined();
|
||||
expect(d.set).toBeUndefined();
|
||||
expect(p).toHaveEnumerableProperty("foo");
|
||||
expect(p).not.toHaveConfigurableProperty("foo");
|
||||
expect(p).toHaveWritableProperty("foo");
|
||||
expect(p).toHaveValueProperty("foo", 10);
|
||||
expect(p).not.toHaveGetterProperty("foo");
|
||||
expect(p).not.toHaveSetterProperty("foo");
|
||||
|
||||
Object.defineProperty(p, "foo", { value: 20, enumerable: true, configurable: false, writable: true });
|
||||
d = Object.getOwnPropertyDescriptor(p, "foo");
|
||||
expect(d.enumerable).toBe(true);
|
||||
expect(d.configurable).toBe(false);
|
||||
expect(d.writable).toBe(true);
|
||||
expect(d.value).toBe(10);
|
||||
expect(d.get).toBeUndefined();
|
||||
expect(d.set).toBeUndefined();
|
||||
expect(p).toHaveEnumerableProperty("foo");
|
||||
expect(p).not.toHaveConfigurableProperty("foo");
|
||||
expect(p).toHaveWritableProperty("foo");
|
||||
expect(p).toHaveValueProperty("foo", 10);
|
||||
expect(p).not.toHaveGetterProperty("foo");
|
||||
expect(p).not.toHaveSetterProperty("foo");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
describe("[[Delete]] trap normal behavior", () => {
|
||||
test("forwarding when not defined in handler", () => {
|
||||
expect(delete (new Proxy({}, { deleteProperty: undefined })).foo).toBe(true);
|
||||
expect(delete (new Proxy({}, { deleteProperty: null })).foo).toBe(true);
|
||||
expect(delete (new Proxy({}, {})).foo).toBe(true);
|
||||
expect(delete (new Proxy({}, { deleteProperty: undefined })).foo).toBeTrue();
|
||||
expect(delete (new Proxy({}, { deleteProperty: null })).foo).toBeTrue();
|
||||
expect(delete (new Proxy({}, {})).foo).toBeTrue();
|
||||
});
|
||||
|
||||
test("correct arguments supplied to trap", () => {
|
||||
|
@ -30,8 +30,8 @@ describe("[[Delete]] trap normal behavior", () => {
|
|||
}
|
||||
});
|
||||
|
||||
expect(delete p.foo).toBe(true);
|
||||
expect(delete p.bar).toBe(false);
|
||||
expect(delete p.foo).toBeTrue();
|
||||
expect(delete p.bar).toBeFalse();
|
||||
|
||||
expect(o.foo).toBe(undefined);
|
||||
expect(o.bar).toBe(2);
|
||||
|
|
|
@ -29,21 +29,21 @@ describe("[Call][GetOwnProperty]] trap normal behavior", () => {
|
|||
}
|
||||
});
|
||||
|
||||
let d = Object.getOwnPropertyDescriptor(p, "baz");
|
||||
expect(d.configurable).toBe(true);
|
||||
expect(d.enumerable).toBe(false);
|
||||
expect(d.writable).toBe(false);
|
||||
expect(d.value).toBe("qux");
|
||||
expect(d.get).toBeUndefined();
|
||||
expect(d.set).toBeUndefined();
|
||||
expect(p).toHaveConfigurableProperty("baz");
|
||||
expect(p).not.toHaveEnumerableProperty("baz");
|
||||
expect(p).not.toHaveWritableProperty("baz");
|
||||
expect(p).toHaveValueProperty("baz", "qux");
|
||||
expect(p).not.toHaveGetterProperty("baz");
|
||||
expect(p).not.toHaveSetterProperty("baz");
|
||||
|
||||
d = Object.getOwnPropertyDescriptor(p, "foo");
|
||||
expect(d.configurable).toBe(true);
|
||||
expect(d.enumerable).toBe(false);
|
||||
expect(d.writable).toBe(true);
|
||||
expect(d.value).toBe("bar");
|
||||
expect(d.get).toBeUndefined();
|
||||
expect(d.set).toBeUndefined();
|
||||
|
||||
expect(p).toHaveConfigurableProperty("foo");
|
||||
expect(p).not.toHaveEnumerableProperty("foo");
|
||||
expect(p).toHaveWritableProperty("foo");
|
||||
expect(p).toHaveValueProperty("foo", "bar");
|
||||
expect(p).not.toHaveGetterProperty("foo");
|
||||
expect(p).not.toHaveSetterProperty("foo");
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
describe("[[Has]] trap normal behavior", () => {
|
||||
test("forwarding when not defined in handler", () => {
|
||||
expect("foo" in new Proxy({}, { has: null })).toBe(false);
|
||||
expect("foo" in new Proxy({}, { has: undefined})).toBe(false);
|
||||
expect("foo" in new Proxy({}, {})).toBe(false);
|
||||
expect("foo" in new Proxy({}, { has: null })).toBeFalse();
|
||||
expect("foo" in new Proxy({}, { has: undefined})).toBeFalse();
|
||||
expect("foo" in new Proxy({}, {})).toBeFalse();
|
||||
});
|
||||
|
||||
test("correct arguments supplied to trap", () => {
|
||||
|
@ -30,8 +30,8 @@ describe("[[Has]] trap normal behavior", () => {
|
|||
}
|
||||
});
|
||||
|
||||
expect("foo" in p).toBe(false);
|
||||
expect("foo" in p).toBe(true);
|
||||
expect("foo" in p).toBeFalse();
|
||||
expect("foo" in p).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
describe("[[IsExtensible]] trap normal behavior", () => {
|
||||
test("forwarding when not defined in handler", () => {
|
||||
expect(Object.isExtensible(new Proxy({}, { isExtensible: null }))).toBe(true);
|
||||
expect(Object.isExtensible(new Proxy({}, { isExtensible: undefined }))).toBe(true);
|
||||
expect(Object.isExtensible(new Proxy({}, {}))).toBe(true);
|
||||
expect(Object.isExtensible(new Proxy({}, { isExtensible: null }))).toBeTrue();
|
||||
expect(Object.isExtensible(new Proxy({}, { isExtensible: undefined }))).toBeTrue();
|
||||
expect(Object.isExtensible(new Proxy({}, {}))).toBeTrue();
|
||||
});
|
||||
|
||||
test("correct arguments supplied to trap", () => {
|
||||
|
@ -14,7 +14,7 @@ describe("[[IsExtensible]] trap normal behavior", () => {
|
|||
}
|
||||
});
|
||||
|
||||
expect(Object.isExtensible(p)).toBe(true);
|
||||
expect(Object.isExtensible(p)).toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ describe("[[SetPrototypeOf]] trap normal behavior", () => {
|
|||
Object.setPrototypeOf(p, { foo: 1 });
|
||||
expect(Object.getPrototypeOf(p).foo).toBeUndefined();
|
||||
p.shouldSet = true;
|
||||
expect(o.shouldSet).toBe(true);
|
||||
expect(o.shouldSet).toBeTrue();
|
||||
Object.setPrototypeOf(p, { foo: 1 });
|
||||
expect(Object.getPrototypeOf(p).foo).toBe(1);
|
||||
});
|
||||
|
|
|
@ -9,8 +9,8 @@ test("toBe", () => {
|
|||
expect("1").toBe("1");
|
||||
expect("1").not.toBe("2");
|
||||
|
||||
expect(true).toBe(true);
|
||||
expect(true).not.toBe(false);
|
||||
expect(true).toBeTrue();
|
||||
expect(true).not.toBeFalse();
|
||||
|
||||
expect({}).not.toBe({});
|
||||
expect([]).not.toBe([]);
|
||||
|
@ -100,6 +100,22 @@ test("toBeNaN", () => {
|
|||
expect(5).not.toBeNaN();
|
||||
});
|
||||
|
||||
test("toBeTrue", () => {
|
||||
expect(true).toBeTrue();
|
||||
expect(false).not.toBeTrue();
|
||||
expect(null).not.toBeTrue();
|
||||
expect(undefined).not.toBeTrue();
|
||||
expect(0).not.toBeTrue();
|
||||
});
|
||||
|
||||
test("toBeFalse", () => {
|
||||
expect(true).not.toBeFalse();
|
||||
expect(false).toBeFalse();
|
||||
expect(null).not.toBeFalse();
|
||||
expect(undefined).not.toBeFalse();
|
||||
expect(0).not.toBeFalse();
|
||||
});
|
||||
|
||||
test("toContain", () => {
|
||||
expect([1, 2, 3]).toContain(1);
|
||||
expect([1, 2, 3]).toContain(2);
|
||||
|
@ -222,3 +238,69 @@ test("toEvalTo", () => {
|
|||
expect("*^&%%").not.toEvalTo();
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test("toHaveConfigurableProperty", () => {
|
||||
expect({ foo: 1 }).toHaveConfigurableProperty("foo");
|
||||
|
||||
expect(() => {
|
||||
expect({ foo: 1 }).not.toHaveConfigurableProperty("bar");
|
||||
}).toThrow();
|
||||
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { configurable: true, value: 1 });
|
||||
Object.defineProperty(o, "bar", { configurable: false, value: 1 });
|
||||
expect(o).toHaveConfigurableProperty("foo");
|
||||
expect(o).not.toHaveConfigurableProperty("bar");
|
||||
});
|
||||
|
||||
test("toHaveEnumerableProperty", () => {
|
||||
expect({ foo: 1 }).toHaveEnumerableProperty("foo");
|
||||
|
||||
expect(() => {
|
||||
expect({ foo: 1 }).not.toHaveEnumerableProperty("bar");
|
||||
}).toThrow();
|
||||
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { enumerable: true, value: 1 });
|
||||
Object.defineProperty(o, "bar", { enumerable: false, value: 1 });
|
||||
expect(o).toHaveEnumerableProperty("foo");
|
||||
expect(o).not.toHaveEnumerableProperty("bar");
|
||||
});
|
||||
|
||||
test("toHaveWritableProperty", () => {
|
||||
expect({ foo: 1 }).toHaveWritableProperty("foo");
|
||||
|
||||
expect(() => {
|
||||
expect({ foo: 1 }).not.toHaveWritableProperty("bar");
|
||||
}).toThrow();
|
||||
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { writable: true, value: 1 });
|
||||
Object.defineProperty(o, "bar", { writable: false, value: 1 });
|
||||
expect(o).toHaveWritableProperty("foo");
|
||||
expect(o).not.toHaveWritableProperty("bar");
|
||||
});
|
||||
|
||||
test("toHaveGetterProperty", () => {
|
||||
expect(() => {
|
||||
expect({ foo: 1 }).not.toHaveGetterProperty("bar");
|
||||
}).toThrow();
|
||||
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { get() { return 1; }});
|
||||
Object.defineProperty(o, "bar", { value: 1 });
|
||||
expect(o).toHaveGetterProperty("foo");
|
||||
expect(o).not.toHaveGetterProperty("bar");
|
||||
});
|
||||
|
||||
test("toHaveSetterProperty", () => {
|
||||
expect(() => {
|
||||
expect({ foo: 1 }).not.toHaveSetterProperty("bar");
|
||||
}).toThrow();
|
||||
|
||||
let o = {};
|
||||
Object.defineProperty(o, "foo", { set(_) { }});
|
||||
Object.defineProperty(o, "bar", { value: 1 });
|
||||
expect(o).toHaveSetterProperty("foo");
|
||||
expect(o).not.toHaveSetterProperty("bar");
|
||||
});
|
||||
|
|
|
@ -144,6 +144,18 @@ class Expector {
|
|||
});
|
||||
}
|
||||
|
||||
toBeTrue() {
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(this.target === true);
|
||||
});
|
||||
}
|
||||
|
||||
toBeFalse() {
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(this.target === false);
|
||||
})
|
||||
}
|
||||
|
||||
toContain(item) {
|
||||
this.__doMatcher(() => {
|
||||
// FIXME: Iterator check
|
||||
|
@ -268,6 +280,68 @@ class Expector {
|
|||
});
|
||||
}
|
||||
|
||||
toHaveConfigurableProperty(property) {
|
||||
this.__expect(this.target !== undefined && this.target !== null);
|
||||
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
||||
this.__expect(d !== undefined);
|
||||
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(d.configurable);
|
||||
});
|
||||
}
|
||||
|
||||
toHaveEnumerableProperty(property) {
|
||||
this.__expect(this.target !== undefined && this.target !== null);
|
||||
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
||||
this.__expect(d !== undefined);
|
||||
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(d.enumerable);
|
||||
});
|
||||
}
|
||||
|
||||
toHaveWritableProperty(property) {
|
||||
this.__expect(this.target !== undefined && this.target !== null);
|
||||
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
||||
this.__expect(d !== undefined);
|
||||
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(d.writable);
|
||||
});
|
||||
}
|
||||
|
||||
toHaveValueProperty(property, value) {
|
||||
this.__expect(this.target !== undefined && this.target !== null);
|
||||
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
||||
this.__expect(d !== undefined);
|
||||
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(d.value !== undefined);
|
||||
if (value !== undefined)
|
||||
this.__expect(deepEquals(value, d.value));
|
||||
});
|
||||
}
|
||||
|
||||
toHaveGetterProperty(property) {
|
||||
this.__expect(this.target !== undefined && this.target !== null);
|
||||
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
||||
this.__expect(d !== undefined);
|
||||
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(d.get !== undefined);
|
||||
});
|
||||
}
|
||||
|
||||
toHaveSetterProperty(property) {
|
||||
this.__expect(this.target !== undefined && this.target !== null);
|
||||
let d = Object.getOwnPropertyDescriptor(this.target, property);
|
||||
this.__expect(d !== undefined);
|
||||
|
||||
this.__doMatcher(() => {
|
||||
this.__expect(d.set !== undefined);
|
||||
});
|
||||
}
|
||||
|
||||
__doMatcher(matcher) {
|
||||
if (!this.inverted) {
|
||||
matcher();
|
||||
|
|
Loading…
Add table
Reference in a new issue