toLocaleString.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. });