mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:37:35 +00:00
LibJS: Make (most) String.prototype functions generic
I.e. they don't require the |this| value to be a string object and "can be transferred to other kinds of objects for use as a method" as the spec describes it.
This commit is contained in:
parent
4bdb6daac5
commit
cfdb7b8806
2 changed files with 126 additions and 89 deletions
51
Libraries/LibJS/Tests/String.prototype-generic-functions.js
Normal file
51
Libraries/LibJS/Tests/String.prototype-generic-functions.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
const genericStringPrototypeFunctions = [
|
||||
"charAt",
|
||||
"repeat",
|
||||
"startsWith",
|
||||
"indexOf",
|
||||
"toLowerCase",
|
||||
"toUpperCase",
|
||||
"padStart",
|
||||
"padEnd",
|
||||
"trim",
|
||||
"trimStart",
|
||||
"trimEnd",
|
||||
"concat",
|
||||
"substring",
|
||||
"includes",
|
||||
];
|
||||
|
||||
genericStringPrototypeFunctions.forEach(name => {
|
||||
String.prototype[name].call({ toString: () => "hello friends" });
|
||||
String.prototype[name].call({ toString: () => 123 });
|
||||
String.prototype[name].call({ toString: () => undefined });
|
||||
|
||||
assertThrowsError(() => {
|
||||
String.prototype[name].call({ toString: () => new String() });
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot convert object to string"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
String.prototype[name].call({ toString: () => [] });
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot convert object to string"
|
||||
});
|
||||
|
||||
assertThrowsError(() => {
|
||||
String.prototype[name].call({ toString: () => ({}) });
|
||||
}, {
|
||||
error: TypeError,
|
||||
message: "Cannot convert object to string"
|
||||
});
|
||||
});
|
||||
|
||||
console.log("PASS");
|
||||
} catch (err) {
|
||||
console.log("FAIL: " + err);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue