mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:58:11 +00:00
LibJS: Add Proxy [[Call]] and [[Construct]] tests
This commit is contained in:
parent
98323e19e5
commit
19411e22d0
2 changed files with 82 additions and 0 deletions
39
Libraries/LibJS/Tests/Proxy.handler-apply.js
Normal file
39
Libraries/LibJS/Tests/Proxy.handler-apply.js
Normal 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);
|
||||||
|
}
|
43
Libraries/LibJS/Tests/Proxy.handler-construct.js
Normal file
43
Libraries/LibJS/Tests/Proxy.handler-construct.js
Normal 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);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue