toLocaleString.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. var echo = WScript.Echo;
  6. function guarded_call(func) {
  7. try {
  8. func();
  9. } catch (e) {
  10. echo(e.name + " : " + e.message);
  11. }
  12. }
  13. var testCount = 0;
  14. function scenario(title) {
  15. if (testCount > 0) {
  16. echo("\n");
  17. }
  18. echo((testCount++) + ".", title);
  19. }
  20. scenario("Non Object");
  21. var nonObjList = [
  22. 1, NaN, true, null, undefined
  23. ];
  24. for (var i = 0; i < nonObjList.length; i++) {
  25. var o = nonObjList[i];
  26. guarded_call(function () {
  27. echo(o, "-->", Array.prototype.toLocaleString.apply(o));
  28. });
  29. }
  30. scenario("Object, length not uint32");
  31. var badLength = [
  32. true, "abc", 1.234, {}
  33. ];
  34. for (var i = 0; i < badLength.length; i++) {
  35. var len = badLength[i];
  36. var o = { length: len };
  37. guarded_call(function () {
  38. echo("length:", len, "-->", Array.prototype.toLocaleString.apply(o));
  39. });
  40. }
  41. scenario("Array: normal");
  42. var o = [
  43. 0, 1.23, NaN, true, "abc", {}, [], [0, 1, 2]
  44. ];
  45. echo(Array.prototype.toLocaleString.apply(o));
  46. scenario("Array: element toLocaleString not callable");
  47. var o = [
  48. 0,
  49. {toLocaleString: 123}
  50. ];
  51. guarded_call(function () {
  52. echo(Array.prototype.toLocaleString.apply(o));
  53. });
  54. scenario("Array: element toLocaleString");
  55. var o = [
  56. 0,
  57. { toLocaleString: function () { return "anObject"; } },
  58. undefined,
  59. null,
  60. { toLocaleString: function () { return "another Object"; } },
  61. [
  62. 1,
  63. { toLocaleString: function () { return "a 3rd Object"; } },
  64. 2
  65. ]
  66. ];
  67. echo(Array.prototype.toLocaleString.apply(o));
  68. scenario("Object: normal");
  69. var o = {
  70. 0: 0,
  71. 1: 1.23,
  72. 2: NaN,
  73. 3: true,
  74. 4: "abc",
  75. 5: {},
  76. 6: [],
  77. 7: [0, 1, 2],
  78. length: 8,
  79. 8: "should not appear",
  80. "-1": "should not appear"
  81. };
  82. guarded_call(function () {
  83. echo(Array.prototype.toLocaleString.apply(o));
  84. });
  85. scenario("Object: element toLocaleString not callable");
  86. var o = {
  87. 0: 0,
  88. 1: { toLocaleString: 123 },
  89. length: 2
  90. };
  91. guarded_call(function () {
  92. echo(Array.prototype.toLocaleString.apply(o));
  93. });
  94. scenario("Object: element toLocaleString");
  95. var o = {
  96. 0: 0,
  97. 1: { toLocaleString: function () { return "anObject"; } },
  98. 2: undefined,
  99. 3: null,
  100. 4: { toLocaleString: function () { return "another Object"; } },
  101. 5: [
  102. 1,
  103. { toLocaleString: function () { return "a 3rd Object"; } },
  104. 2
  105. ],
  106. length: 6,
  107. 6: "should not appear",
  108. 7: "should not appear",
  109. "-1": "should not appear"
  110. };
  111. guarded_call(function () {
  112. echo(Array.prototype.toLocaleString.apply(o));
  113. });