1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 07:14:58 +00:00

LibJS: Convert Proxy tests

This commit is contained in:
Matthew Olsson 2020-07-03 22:43:08 -07:00 committed by Andreas Kling
parent b86faeaeef
commit 449a1cf3a2
14 changed files with 820 additions and 815 deletions

View file

@ -1,58 +1,62 @@
load("test-common.js");
try {
let p = new Proxy(function() { this.x = 5; }, { construct: null });
assert((new p).x === 5);
let p = new Proxy(function() { this.x = 5; }, { construct: undefined });
assert((new p).x === 5);
let p = new Proxy(function() { this.x = 5; }, {});
assert((new p).x === 5);
function f(value) {
this.x = value;
}
let p;
const handler = {
construct(target, arguments, newTarget) {
assert(target === f);
assert(newTarget === p);
if (arguments[1])
return Reflect.construct(target, [arguments[0] * 2], newTarget);
return Reflect.construct(target, arguments, newTarget);
},
};
p = new Proxy(f, handler);
assert(new p(15).x === 15);
assert(new p(15, true).x === 30);
let p;
function theNewTarget() {};
const handler = {
construct(target, arguments, newTarget) {
assert(target === f);
assert(newTarget === theNewTarget);
if (arguments[1])
return Reflect.construct(target, [arguments[0] * 2], newTarget);
return Reflect.construct(target, arguments, newTarget);
},
};
p = new Proxy(f, handler);
Reflect.construct(p, [15], theNewTarget);
// Invariants
[{}, [], new Proxy({}, {})].forEach(item => {
assertThrowsError(() => {
new (new Proxy(item, {}));
}, {
error: TypeError,
message: "[object ProxyObject] is not a constructor",
});
describe("[[Construct]] trap normal behavior", () => {
test("forwarding when not defined in handler", () => {
let p = new Proxy(function() { this.x = 5; }, { construct: null });
expect((new p).x).toBe(5);
p = new Proxy(function() { this.x = 5; }, { construct: undefined });
expect((new p).x).toBe(5);
p = new Proxy(function() { this.x = 5; }, {});
expect((new p).x).toBe(5);
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}
test("trapping 'new'", () => {
function f(value) {
this.x = value;
}
let p;
const handler = {
construct(target, arguments, newTarget) {
expect(target).toBe(f);
expect(newTarget).toBe(p);
if (arguments[1])
return Reflect.construct(target, [arguments[0] * 2], newTarget);
return Reflect.construct(target, arguments, newTarget);
},
};
p = new Proxy(f, handler);
expect(new p(15).x).toBe(15);
expect(new p(15, true).x).toBe(30);
});
test("trapping Reflect.construct", () => {
function f(value) {
this.x = value;
}
let p;
function theNewTarget() {};
const handler = {
construct(target, arguments, newTarget) {
expect(target).toBe(f);
expect(newTarget).toBe(theNewTarget);
if (arguments[1])
return Reflect.construct(target, [arguments[0] * 2], newTarget);
return Reflect.construct(target, arguments, newTarget);
},
};
p = new Proxy(f, handler);
Reflect.construct(p, [15], theNewTarget);
});
});
describe("[[Construct]] invariants", () => {
test("target must have a [[Construct]] slot", () => {
[{}, [], new Proxy({}, {})].forEach(item => {
expect(() => {
new (new Proxy(item, {}));
}).toThrowWithMessage(TypeError, "[object ProxyObject] is not a constructor");
});
});
});