characterclass_with_range.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
  6. function testRegExp(str, regexp, expectedResult)
  7. {
  8. actualResult = regexp.test(str);
  9. errorMsg = "Expected result of test for match between string: '" + str + "' and regular expression: " + regexp + " to be " +
  10. expectedResult + " but was " + actualResult;
  11. assert.areEqual(expectedResult, actualResult, errorMsg);
  12. }
  13. var tests = [
  14. {
  15. name : "RegExp tests with no flags",
  16. body : function ()
  17. {
  18. let re = /[\s-a-z]/;
  19. testRegExp("b", re, false);
  20. testRegExp("a", re, true);
  21. testRegExp(" ", re, true);
  22. testRegExp("z", re, true);
  23. testRegExp("\t", re, true);
  24. testRegExp("q", re, false);
  25. testRegExp("\\", re, false);
  26. testRegExp("\u2028", re, true);
  27. testRegExp("\u2009", re, true);
  28. }
  29. },
  30. {
  31. name : "RegExp tests with IgnoreCase flag set",
  32. body : function ()
  33. {
  34. let reIgnoreCase = /^[\s-a-z]$/i;
  35. testRegExp("O", reIgnoreCase, false);
  36. testRegExp("A", reIgnoreCase, true);
  37. testRegExp(" ", reIgnoreCase, true);
  38. testRegExp("z", reIgnoreCase, true);
  39. testRegExp("\t", reIgnoreCase, true);
  40. testRegExp("\u2028", reIgnoreCase, true);
  41. testRegExp("\u2009", reIgnoreCase, true);
  42. }
  43. },
  44. {
  45. name : "RegExp tests with Unicode flag set",
  46. body : function ()
  47. {
  48. let reUnicode = /^[a-d]$/u;
  49. testRegExp("a", reUnicode, true);
  50. testRegExp("c", reUnicode, true);
  51. testRegExp("d", reUnicode, true);
  52. testRegExp("C", reUnicode, false);
  53. testRegExp("g", reUnicode, false);
  54. testRegExp("\u2028", reUnicode, false);
  55. testRegExp("\u2009", reUnicode, false);
  56. assert.throws(() => eval("/^[\\s-z]$/u.exec(\"-\")"), SyntaxError, "Expected an error due to character sets not being allowed in ranges when unicode flag is set.", "Character classes not allowed in class ranges");
  57. }
  58. },
  59. {
  60. name : "Non-character class tests",
  61. body : function ()
  62. {
  63. let reNoCharClass = /^[a-c-z]$/;
  64. testRegExp("b", reNoCharClass, true);
  65. testRegExp("-", reNoCharClass, true);
  66. testRegExp("z", reNoCharClass, true);
  67. testRegExp("y", reNoCharClass, false);
  68. }
  69. },
  70. {
  71. name : "Regression tests from bugFixRegression",
  72. body : function ()
  73. {
  74. assert.areEqual(" -a", /[\s-a-c]*/.exec(" -abc")[0]);
  75. assert.areEqual(" -abc", /[\s\-a-c]*/.exec(" -abc")[0]);
  76. assert.areEqual(" -ab", /[a-\s-b]*/.exec(" -ab")[0]);
  77. assert.areEqual(" -ab", /[a\-\s\-b]*/.exec(" -ab")[0]);
  78. }
  79. },
  80. {
  81. name : "Special character tests",
  82. body : function ()
  83. {
  84. let re = /^[\s][a\sb][\s--c-f]$/;
  85. testRegExp(' \\', re, false);
  86. testRegExp(' \\ ', re, false);
  87. testRegExp('\\ ', re, false);
  88. re = /[-][\d\-]/;
  89. testRegExp('--', re, true);
  90. testRegExp('-9', re, true);
  91. testRegExp(' ', re, false);
  92. }
  93. },
  94. {
  95. name : "Negation character set tests",
  96. body : function ()
  97. {
  98. let reNegationCharSet = /[\D-\s]+/;
  99. testRegExp('555686', reNegationCharSet, false);
  100. testRegExp('555-686', reNegationCharSet, true);
  101. testRegExp('alphabet-123', reNegationCharSet, true);
  102. }
  103. },
  104. {
  105. name : "Non-range tests",
  106. body : function ()
  107. {
  108. let reNonRange = /[-\w]/
  109. testRegExp('-', reNonRange, true);
  110. testRegExp('g', reNonRange, true);
  111. testRegExp('5', reNonRange, true);
  112. testRegExp(' ', reNonRange, false);
  113. testRegExp('\t', reNonRange, false);
  114. testRegExp('\u2028', reNonRange, false);
  115. reNonRange = /[\w-]/
  116. testRegExp('-', reNonRange, true);
  117. testRegExp('g', reNonRange, true);
  118. testRegExp('5', reNonRange, true);
  119. testRegExp(' ', reNonRange, false);
  120. testRegExp('\t', reNonRange, false);
  121. testRegExp('\u2028', reNonRange, false);
  122. }
  123. }
  124. ];
  125. testRunner.runTests(tests, {
  126. verbose : WScript.Arguments[0] != "summary"
  127. });