LibJS: Add Proxy [[Call]] and [[Construct]] tests

This commit is contained in:
Matthew Olsson 2020-06-25 14:50:57 -07:00 committed by Andreas Kling
parent 98323e19e5
commit 19411e22d0
2 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,39 @@
load("test-common.js");
try {
let p = new Proxy(() => 5, { apply: null });
assert(p() === 5);
let p = new Proxy(() => 5, { apply: undefined });
assert(p() === 5);
let p = new Proxy(() => 5, {});
assert(p() === 5);
const f = (a, b) => a + b;
const handler = {
apply(target, this_, arguments) {
assert(target === f);
assert(this_ === handler);
if (arguments[2])
return arguments[0] * arguments[1];
return f(...arguments);
},
};
p = new Proxy(f, handler);
assert(p(2, 4) === 6);
assert(p(2, 4, true) === 8);
// Invariants
[{}, [], new Proxy({}, {})].forEach(item => {
assertThrowsError(() => {
new Proxy(item, {})();
}, {
error: TypeError,
message: "[object ProxyObject] is not a function",
});
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -0,0 +1,43 @@
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);
// Invariants
[{}, [], new Proxy({}, {})].forEach(item => {
assertThrowsError(() => {
new (new Proxy(item, {}));
}, {
error: TypeError,
message: "[object ProxyObject] is not a constructor",
});
});
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}