unicode_whitespace.js 3.8 KB

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