toLocaleString.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // 0.toLocaleString() is supposed to be 0. However our baseline has an exception.
  46. // So, do not break it while supporting cross platform locale output
  47. var output = Array.prototype.toLocaleString.apply(o);
  48. if ( output == "0,1.23,NaN,true,abc,[object Object],,0,1,2" ) {
  49. echo("0.00,1.23,NaN,true,abc,[object Object],,0.00,1.00,2.00");
  50. } else {
  51. echo(output);
  52. }
  53. scenario("Array: element toLocaleString not callable");
  54. var o = [
  55. 0,
  56. {toLocaleString: 123}
  57. ];
  58. guarded_call(function () {
  59. echo(Array.prototype.toLocaleString.apply(o));
  60. });
  61. scenario("Array: element toLocaleString");
  62. var o = [
  63. 0,
  64. { toLocaleString: function () { return "anObject"; } },
  65. undefined,
  66. null,
  67. { toLocaleString: function () { return "another Object"; } },
  68. [
  69. 1,
  70. { toLocaleString: function () { return "a 3rd Object"; } },
  71. 2
  72. ]
  73. ];
  74. // 0.toLocaleString() is supposed to be 0. However our baseline has an exception.
  75. // So, do not break it while supporting cross platform locale output
  76. output = Array.prototype.toLocaleString.apply(o);
  77. if ( output == "0,anObject,,,another Object,1,a 3rd Object,2" ) {
  78. echo("0.00,anObject,,,another Object,1.00,a 3rd Object,2.00");
  79. } else {
  80. echo(output);
  81. }
  82. scenario("Object: normal");
  83. var o = {
  84. 0: 0,
  85. 1: 1.23,
  86. 2: NaN,
  87. 3: true,
  88. 4: "abc",
  89. 5: {},
  90. 6: [],
  91. 7: [0, 1, 2],
  92. length: 8,
  93. 8: "should not appear",
  94. "-1": "should not appear"
  95. };
  96. // 0.toLocaleString() is supposed to be 0. However our baseline has an exception.
  97. // So, do not break it while supporting cross platform locale output
  98. guarded_call(function () {
  99. output = Array.prototype.toLocaleString.apply(o);
  100. if ( output == "0,1.23,NaN,true,abc,[object Object],,0,1,2" ) {
  101. echo("0.00,1.23,NaN,true,abc,[object Object],,0.00,1.00,2.00");
  102. } else {
  103. echo(output);
  104. }
  105. });
  106. scenario("Object: element toLocaleString not callable");
  107. var o = {
  108. 0: 0,
  109. 1: { toLocaleString: 123 },
  110. length: 2
  111. };
  112. guarded_call(function () {
  113. echo(Array.prototype.toLocaleString.apply(o));
  114. });
  115. scenario("Object: element toLocaleString");
  116. var o = {
  117. 0: 0,
  118. 1: { toLocaleString: function () { return "anObject"; } },
  119. 2: undefined,
  120. 3: null,
  121. 4: { toLocaleString: function () { return "another Object"; } },
  122. 5: [
  123. 1,
  124. { toLocaleString: function () { return "a 3rd Object"; } },
  125. 2
  126. ],
  127. length: 6,
  128. 6: "should not appear",
  129. 7: "should not appear",
  130. "-1": "should not appear"
  131. };
  132. // 0.toLocaleString() is supposed to be 0. However our baseline has an exception.
  133. // So, do not break it while supporting cross platform locale output
  134. guarded_call(function () {
  135. output = Array.prototype.toLocaleString.apply(o);
  136. if ( output == "0,anObject,,,another Object,1,a 3rd Object,2" ) {
  137. echo("0.00,anObject,,,another Object,1.00,a 3rd Object,2.00");
  138. } else {
  139. echo(output);
  140. }
  141. });
  142. scenario("TypedArray: toLocaleString should use length from internal slot");
  143. var o = new Int8Array(2);
  144. o[1] = 31;
  145. Object.defineProperty(o, 'length', {value : 4});
  146. guarded_call(function () {
  147. output = Array.prototype.toLocaleString.apply(o);
  148. // On OSX and Linux the values are printed as 0 instead 0.00. This is a valid workaround as we have still validated the toLocaleString behavior is correct.
  149. if (output == "0,31") {
  150. echo("0.00,31.00");
  151. } else {
  152. echo(output);
  153. }
  154. });
  155. scenario("Array: toLocaleString should use length property");
  156. var o = [10, 20];
  157. o[2] = 30;
  158. Object.defineProperty(o, 'length', {value : 6});
  159. guarded_call(function () {
  160. output = Array.prototype.toLocaleString.apply(o);
  161. // On OSX and Linux the values are printed as 0 instead 0.00. This is a valid workaround as we have still validated the toLocaleString behavior is correct.
  162. if (output == "10,20,30,,,") {
  163. echo("10.00,20.00,30.00,,,");
  164. } else {
  165. echo(output);
  166. }
  167. });
  168. scenario("Array: toLocaleString should use toLocaleString accessor on element");
  169. var originalToLocaleString = String.prototype.toLocaleString;
  170. String.prototype.toLocaleString = function () {
  171. return "accessor-value";
  172. };
  173. var o = ["test"];
  174. guarded_call(function () {
  175. echo(Array.prototype.toLocaleString.apply(o));
  176. });
  177. String.prototype.toLocaleString = originalToLocaleString;
  178. scenario("Array: toLocaleString should throw TypeError on undefined");
  179. var originalToLocaleString = String.prototype.toLocaleString;
  180. String.prototype.toLocaleString = undefined;
  181. var o = ["test"];
  182. guarded_call(function () {
  183. echo(Array.prototype.toLocaleString.apply(o));
  184. });
  185. String.prototype.toLocaleString = originalToLocaleString;
  186. scenario("Array: toLocaleString should throw TypeError on invalid function");
  187. var originalToLocaleString = String.prototype.toLocaleString;
  188. String.prototype.toLocaleString = 0;
  189. var o = ["test"];
  190. guarded_call(function () {
  191. echo(Array.prototype.toLocaleString.apply(o));
  192. });
  193. String.prototype.toLocaleString = originalToLocaleString;