mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-24 02:03:06 -05:00
LibJS: Add Proxy [[Call]] and [[Construct]] tests
This commit is contained in:
parent
98323e19e5
commit
19411e22d0
Notes:
sideshowbarker
2024-07-19 05:18:42 +09:00
Author: https://github.com/mattco98 Commit: https://github.com/SerenityOS/serenity/commit/19411e22d0d Pull-request: https://github.com/SerenityOS/serenity/pull/2630 Issue: https://github.com/SerenityOS/serenity/issues/2624 Reviewed-by: https://github.com/awesomekling
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
Reference in a new issue