toLocaleString2.js 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Object.prototype.toLocaleString tests for ES6 behaviors
  6. if (this.WScript && this.WScript.LoadScriptFile) { // Check for running in ch
  7. this.WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  8. }
  9. var toLocaleString = Object.prototype.toLocaleString;
  10. var tests = [
  11. {
  12. name: "Simple error cases for Object#toLocaleString",
  13. body: function () {
  14. assert.throws(function() { toLocaleString.call(); }, TypeError, "Object#toLocaleString throws when called with no parameters", "Object.prototype.toLocaleString: 'this' is null or undefined");
  15. assert.throws(function() { toLocaleString.call(undefined); }, TypeError, "Object#toLocaleString throws when called with undefined this parameter", "Object.prototype.toLocaleString: 'this' is null or undefined");
  16. assert.throws(function() { toLocaleString.call(null); }, TypeError, "Object#toLocaleString throws when called with null this parameter", "Object.prototype.toLocaleString: 'this' is null or undefined");
  17. }
  18. },
  19. {
  20. name: "Corner error cases for Object#toLocaleString",
  21. body: function () {
  22. var o = { toString : 'non-object' };
  23. assert.throws(function() { toLocaleString.call(o); }, TypeError, "Object#toLocaleString tries to get the 'toString' property from the this parameter of object with non-object toString property", "Object.prototype.toLocaleString: argument is not a Function object");
  24. o = { get toString() { throw TypeError('get toString'); } };
  25. assert.throws(function() { toLocaleString.call(o); }, TypeError, "Object#toLocaleString tries to get the 'toString' property from the this parameter of object with throwing accessor toString property", "get toString");
  26. o = { get toString() { return 'non-object'; } };
  27. assert.throws(function() { toLocaleString.call(o); }, TypeError, "Object#toLocaleString tries to get the 'toString' property from the this parameter of object with accessor toString property which returns non-function", "Object.prototype.toLocaleString: argument is not a Function object");
  28. o = Object.create(null); // o doesn't have Object.prototype.toString
  29. assert.throws(function() { toLocaleString.call(o); }, TypeError, "Object#toLocaleString tries to get the 'toString' property from the this parameter of object without toString property", "Object.prototype.toLocaleString: argument is not a Function object");
  30. o = {
  31. toString() {
  32. assert.areEqual(o, this, "This argument passed to toString function should be the same object passed to toLocaleString");
  33. throw TypeError('toString');
  34. }
  35. };
  36. assert.throws(function() { toLocaleString.call(o); }, TypeError, "Object#toLocaleString tries to call the 'toString' property from the this parameter of object with toString function that throws", "toString");
  37. }
  38. },
  39. {
  40. name: "Object#toLocaleString passes the this argument as-is to the toString function we load from ToObject(this)",
  41. body: function () {
  42. 'use strict';
  43. Boolean.prototype.toString = function() { return typeof this; }; // we will walk up to here from Object(true) before we find Object.prototype.toString
  44. assert.areEqual('boolean', true.toLocaleString(), "Calling Object#toLocaleString with a primitive this argument performs ToObject(this).toString.call(this) which will call the function we added to Boolean#toString");
  45. assert.areEqual('boolean', toLocaleString.call(false), "Calling Object#toLocaleString with a primitive this argument performs ToObject(this).toString.call(this) which will call the function we added to Boolean#toString - even if we apply/call it");
  46. assert.areEqual('5', toLocaleString.call(5), "Calling Object#toLocaleString with a primitive this argument performs ToObject(this).toString.call(this) which will call Object#toString(5)");
  47. Object.defineProperty(Boolean.prototype, "toString", { get: function() { return () => typeof this; }});
  48. assert.areEqual('boolean', true.toLocaleString(), "Calling Object#toLocaleString with a primitive this argument performs ToObject(this).toString.call(this) which will call the function we added to Boolean#toString");
  49. assert.areEqual('boolean', toLocaleString.call(false), "Calling Object#toLocaleString with a primitive this argument performs ToObject(this).toString.call(this) which will call the function we added to Boolean#toString - even if we apply/call it");
  50. }
  51. },
  52. ];
  53. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });