unicode_whitespace.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // ES6 unicode whitespace tests
  6. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  7. var whitespace_characters = [
  8. '\u0009',
  9. '\u000a',
  10. '\u000b',
  11. '\u000c',
  12. '\u000d',
  13. '\u0020',
  14. '\u00a0',
  15. '\u1680',
  16. '\u180e',
  17. '\u2000',
  18. '\u2001',
  19. '\u2002',
  20. '\u2003',
  21. '\u2004',
  22. '\u2005',
  23. '\u2006',
  24. '\u2007',
  25. '\u2008',
  26. '\u2009',
  27. '\u200a',
  28. '\u2028',
  29. '\u2029',
  30. '\u202f',
  31. '\u205f',
  32. '\u3000',
  33. '\ufeff',
  34. ];
  35. // These characters may not be printable but are not considered whitespace.
  36. // See ES6 Section 11.2 White Space
  37. var special_non_whitespace_characters = [
  38. '\u0085',
  39. '\u200b',
  40. '\u200c',
  41. '\u200d',
  42. '\u2060',
  43. '\u00b7',
  44. '\u237d',
  45. '\u2420',
  46. '\u2422',
  47. '\u2423',
  48. ];
  49. var tests = [
  50. {
  51. name: "String#trim correctly removes whitespace characters",
  52. body: function () {
  53. for (idx in whitespace_characters) {
  54. var ch = whitespace_characters[idx];
  55. var result = ch.trim();
  56. assert.areEqual(0, result.length, "String#trim removes whitespace characters, result should have 0 length");
  57. assert.areEqual("", result, "String#trim removes whitespace characters, result should be empty string");
  58. }
  59. }
  60. },
  61. {
  62. name: "String#trim correctly removes all whitespace characters",
  63. body: function () {
  64. var str = whitespace_characters.join('');
  65. var result = str.trim();
  66. assert.areEqual(0, result.length, "String#trim removes whitespace characters, result should have 0 length");
  67. assert.areEqual("", result, "String#trim removes whitespace characters, result should be empty string");
  68. }
  69. },
  70. {
  71. name: "String#trim correctly ignores special non-whitespace characters",
  72. body: function () {
  73. for (idx in special_non_whitespace_characters) {
  74. var ch = special_non_whitespace_characters[idx];
  75. var result = ch.trim();
  76. assert.areEqual(1, result.length, "String#trim leaves non-whitespace characters, result should 1 character long");
  77. assert.areEqual(ch, result, "String#trim leaves non-whitespace characters, result should be equal to the input");
  78. }
  79. }
  80. },
  81. {
  82. name: "String#trim correctly ignores all non-whitespace characters",
  83. body: function () {
  84. for (var hex = 0x0000; hex <= 0xffff; hex++) {
  85. var ch = String.fromCodePoint(hex);
  86. var result = ch.trim();
  87. if (result.length === 0) {
  88. var found = false;
  89. for (idx in whitespace_characters) {
  90. if (ch === whitespace_characters[idx]) {
  91. found = true;
  92. }
  93. }
  94. assert.isTrue(found, "If we found a whitespace character, it had to be one of the known whitespace characters");
  95. } else {
  96. assert.areEqual(ch, result, "If the character we found is not a whitespace character, the trimmed string has to be equal to the character itself");
  97. }
  98. }
  99. }
  100. },
  101. ];
  102. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });