12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- load("test-common.js");
- try {
- const genericStringPrototypeFunctions = [
- "charAt",
- "repeat",
- "startsWith",
- "indexOf",
- "toLowerCase",
- "toUpperCase",
- "padStart",
- "padEnd",
- "trim",
- "trimStart",
- "trimEnd",
- "concat",
- "substring",
- "includes",
- "slice",
- ];
- 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);
- }
|