regex-annex-b.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 withCharacterSet(assertions) {
  7. var newAssertions = [];
  8. assertions.forEach(function (assertion) {
  9. newAssertions.push(assertion);
  10. var pattern = assertion[0];
  11. var rest = assertion.slice(1);
  12. var characterSetAssertion = ['[' + pattern + ']'].concat(rest);
  13. newAssertions.push(characterSetAssertion);
  14. });
  15. return newAssertions;
  16. }
  17. var tests = [
  18. {
  19. name: 'Lookahead assertions should be quantifiable',
  20. assertions: [
  21. ['(?=a)+ab', 'ab'],
  22. ['(?!a)+b', 'b'],
  23. ]
  24. },
  25. {
  26. name: 'Curly braces should be allowed on their own',
  27. assertions: withCharacterSet([
  28. ['{', '{'],
  29. ['}', '}']
  30. ])
  31. },
  32. {
  33. name: 'Legacy octal escape sequences should be allowed',
  34. assertions: withCharacterSet([
  35. ['\\0', '\x00'],
  36. ['\\7', '\x07'],
  37. ['\\00', '\x00'],
  38. ['\\37', '\x1F'],
  39. ['\\41', '\x21'],
  40. ['\\77', '\x3F'],
  41. ['\\000', '\x00'],
  42. ['\\377', '\xFF'],
  43. ])
  44. },
  45. {
  46. name: '"\\8" and "\\9" should be identity escapes',
  47. assertions: withCharacterSet([
  48. ['\\8', '8'],
  49. ['\\9', '9'],
  50. ])
  51. },
  52. {
  53. name: 'Legacy octal escape sequence should be disambiguated based on the number of groups',
  54. assertions: [
  55. ['(1)(2)\\1\\2\\3', '1212\x03'],
  56. ]
  57. },
  58. {
  59. name: '"\\x" should be treated as an identity escape if ill-formed',
  60. assertions: withCharacterSet([
  61. ['\\x', 'x'],
  62. ['\\xa', 'xa'],
  63. ])
  64. },
  65. {
  66. name: '"\\u" should be treated as an identity escape if ill-formed',
  67. assertions: withCharacterSet([
  68. ['\\u', 'u'],
  69. ['\\ua', 'ua'],
  70. ])
  71. },
  72. {
  73. name: 'Any character except "c" should be escapable',
  74. assertions: withCharacterSet([
  75. ['\\q', 'q'],
  76. ])
  77. },
  78. ];
  79. var testsForRunner = tests.map(function (test) {
  80. return {
  81. name: test.name,
  82. body: function () {
  83. test.assertions.forEach(function (assertion) {
  84. var pattern = assertion[0];
  85. var inputString = assertion[1];
  86. var re = eval('/' + pattern + '/');
  87. assert.isTrue(re.test(inputString), re.source);
  88. });
  89. }
  90. }
  91. });
  92. testRunner.runTests(testsForRunner, { verbose: WScript.Arguments[0] != 'summary' });