LibJS: Convert Reflect object tests to new testing framework

This commit is contained in:
Linus Groh 2020-07-04 15:42:19 +01:00 committed by Andreas Kling
parent fc08222f46
commit 46581773c1
Notes: sideshowbarker 2024-07-19 05:04:48 +09:00
14 changed files with 599 additions and 462 deletions

View file

@ -1,54 +1,69 @@
load("test-common.js");
test("length is 2", () => {
expect(Reflect.construct).toHaveLength(2);
});
try {
assert(Reflect.construct.length === 2);
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
assertThrowsError(() => {
Reflect.construct(value);
}, {
error: TypeError,
message: "First argument of Reflect.construct() must be a function"
});
assertThrowsError(() => {
Reflect.construct(() => {}, value);
}, {
error: TypeError,
message: "Arguments list must be an object"
});
assertThrowsError(() => {
Reflect.construct(() => {}, [], value);
}, {
error: TypeError,
message: "Optional third argument of Reflect.construct() must be a constructor"
describe("errors", () => {
test("target must be a function", () => {
[null, undefined, "foo", 123, NaN, Infinity, {}].forEach(value => {
expect(() => {
Reflect.construct(value);
}).toThrowWithMessage(TypeError, "First argument of Reflect.construct() must be a function");
});
});
var a = Reflect.construct(Array, [5]);
assert(a instanceof Array);
assert(a.length === 5);
test("arguments list must be an object", () => {
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
expect(() => {
Reflect.construct(() => {}, value);
}).toThrowWithMessage(TypeError, "Arguments list must be an object");
});
});
var s = Reflect.construct(String, [123]);
assert(s instanceof String);
assert(s.length === 3);
assert(s.toString() === "123");
test("new target must be a function", () => {
[null, undefined, "foo", 123, NaN, Infinity, {}].forEach(value => {
expect(() => {
Reflect.construct(() => {}, [], value);
}).toThrowWithMessage(TypeError, "Optional third argument of Reflect.construct() must be a constructor");
});
});
});
function Foo() {
this.name = "foo";
}
function Bar() {
this.name = "bar";
}
describe("normal behavior", () => {
test("built-in Array function", () => {
var a = Reflect.construct(Array, [5]);
expect(a instanceof Array).toBeTrue();
expect(a).toHaveLength(5);
});
var o = Reflect.construct(Foo, [], Bar);
assert(o.name === "foo");
assert(o instanceof Foo === false);
assert(o instanceof Bar === true);
test("built-in String function", () => {
var s = Reflect.construct(String, [123]);
expect(s instanceof String).toBeTrue();
expect(s).toHaveLength(3);
expect(s.toString()).toBe("123");
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("user-defined function", () => {
function Foo() {
this.name = "foo";
}
var o = Reflect.construct(Foo, []);
expect(o.name).toBe("foo");
expect(o instanceof Foo).toBeTrue();
});
test("user-defined function with different new target", () => {
function Foo() {
this.name = "foo";
}
function Bar() {
this.name = "bar";
}
var o = Reflect.construct(Foo, [], Bar);
expect(o.name).toBe("foo");
expect(o instanceof Foo).toBeFalse();
expect(o instanceof Bar).toBeTrue();
});
});