String.prototype-generic-functions.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. load("test-common.js");
  2. try {
  3. const genericStringPrototypeFunctions = [
  4. "charAt",
  5. "repeat",
  6. "startsWith",
  7. "indexOf",
  8. "toLowerCase",
  9. "toUpperCase",
  10. "padStart",
  11. "padEnd",
  12. "trim",
  13. "trimStart",
  14. "trimEnd",
  15. "concat",
  16. "substring",
  17. "includes",
  18. "slice",
  19. ];
  20. genericStringPrototypeFunctions.forEach(name => {
  21. String.prototype[name].call({ toString: () => "hello friends" });
  22. String.prototype[name].call({ toString: () => 123 });
  23. String.prototype[name].call({ toString: () => undefined });
  24. assertThrowsError(() => {
  25. String.prototype[name].call({ toString: () => new String() });
  26. }, {
  27. error: TypeError,
  28. message: "Cannot convert object to string"
  29. });
  30. assertThrowsError(() => {
  31. String.prototype[name].call({ toString: () => [] });
  32. }, {
  33. error: TypeError,
  34. message: "Cannot convert object to string"
  35. });
  36. assertThrowsError(() => {
  37. String.prototype[name].call({ toString: () => ({}) });
  38. }, {
  39. error: TypeError,
  40. message: "Cannot convert object to string"
  41. });
  42. });
  43. console.log("PASS");
  44. } catch (err) {
  45. console.log("FAIL: " + err);
  46. }