regex-set.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. var normalTests = [
  7. {
  8. regExp: /[-]/u,
  9. acceptedCharacters: ['-'],
  10. rejectedCharacters: [',', '.']
  11. },
  12. {
  13. regExp: /[b-]/u,
  14. acceptedCharacters: ['b', '-'],
  15. rejectedCharacters: ['a', 'c']
  16. },
  17. {
  18. regExp: /[\u{10001}-]/u,
  19. acceptedCharacters: ['\u{10001}', '-'],
  20. rejectedCharacters: ['\u{10000}', '\u{10002}']
  21. },
  22. {
  23. regExp: /[-b]/u,
  24. acceptedCharacters: ['-', 'b'],
  25. rejectedCharacters: ['a', 'c']
  26. },
  27. {
  28. regExp: /[-\u{10001}]/u,
  29. acceptedCharacters: ['-', '\u{10001}'],
  30. rejectedCharacters: ['\u{10000}', '\u{10002}']
  31. },
  32. {
  33. regExp: /[b-d]/u,
  34. acceptedCharacters: ['b', 'c', 'd'],
  35. rejectedCharacters: ['-', 'a', 'e']
  36. },
  37. {
  38. regExp: /[\u{10001}-\u{10003}]/u,
  39. acceptedCharacters: ['\u{10001}', '\u{10002}', '\u{10003}'],
  40. rejectedCharacters: ['-', '\u{10000}', '\u{10004}']
  41. },
  42. {
  43. regExp: /[+--]/u,
  44. acceptedCharacters: ['+', ',', '-'],
  45. rejectedCharacters: ['*', '.']
  46. },
  47. {
  48. regExp: /[--/]/u,
  49. acceptedCharacters: ['-', '.', '/'],
  50. rejectedCharacters: [',', '0']
  51. },
  52. {
  53. regExp: /[b-d-]/u,
  54. acceptedCharacters: ['b', 'c', 'd', '-'],
  55. rejectedCharacters: ['a', 'e']
  56. },
  57. {
  58. regExp: /[\u{10001}-\u{10003}-]/u,
  59. acceptedCharacters: ['\u{10001}', '\u{10002}', '\u{10003}', '-'],
  60. rejectedCharacters: ['\u{10000}', '\u{10004}']
  61. },
  62. {
  63. regExp: /[b-df]/u,
  64. acceptedCharacters: ['b', 'c', 'd', 'f'],
  65. rejectedCharacters: ['-', 'a', 'e']
  66. },
  67. {
  68. regExp: /[\u{10001}-\u{10003}\u{10005}]/u,
  69. acceptedCharacters: ['\u{10001}', '\u{10002}', '\u{10003}', '\u{10005}'],
  70. rejectedCharacters: ['-', '\u{10000}', '\u{10004}']
  71. },
  72. {
  73. regExp: /[b-df-]/u,
  74. acceptedCharacters: ['b', 'c', 'd', 'f', '-'],
  75. rejectedCharacters: ['a', 'e', 'g']
  76. },
  77. {
  78. regExp: /[\u{10001}-\u{10003}\u{10005}-]/u,
  79. acceptedCharacters: ['\u{10001}', '\u{10002}', '\u{10003}', '\u{10005}', '-'],
  80. rejectedCharacters: ['\u{10000}', '\u{10004}', '\u{10006}']
  81. },
  82. {
  83. // Third non-dash character's mathematical value is higher than the second's
  84. regExp: /[b-d-f]/u,
  85. acceptedCharacters: ['b', 'c', 'd', '-', 'f'],
  86. rejectedCharacters: ['a', 'e']
  87. },
  88. {
  89. // Third non-dash character's mathematical value is higher than the second's
  90. regExp: /[\u{10001}-\u{10003}-\u{10005}]/u,
  91. acceptedCharacters: ['\u{10001}', '\u{10002}', '\u{10003}', '-', '\u{10005}'],
  92. rejectedCharacters: ['\u{10000}', '\u{10004}']
  93. },
  94. {
  95. // Third non-dash character's mathematical value is lower than the second's
  96. regExp: /[c-e-a]/u,
  97. acceptedCharacters: ['c', 'd', 'e', '-', 'a'],
  98. rejectedCharacters: ['b', 'f']
  99. },
  100. {
  101. // Third non-dash character's mathematical value is lower than the second's
  102. regExp: /[\u{10002}-\u{10004}-\u{10000}]/u,
  103. acceptedCharacters: ['\u{10002}', '\u{10003}', '\u{10004}', '-', '\u{10000}'],
  104. rejectedCharacters: ['\u{10001}', '\u{10005}']
  105. },
  106. {
  107. regExp: /[b-df-h]/u,
  108. acceptedCharacters: ['b', 'c', 'd', 'f', 'g', 'h'],
  109. rejectedCharacters: ['-', 'a', 'e', 'i']
  110. },
  111. {
  112. regExp: /[\u{10001}-\u{10003}\u{10005}-\u{10007}]/u,
  113. acceptedCharacters: ['\u{10001}', '\u{10002}', '\u{10003}', '\u{10005}', '\u{10006}', '\u{10007}'],
  114. rejectedCharacters: ['-', '\u{10000}', '\u{10004}', '\u{10008}']
  115. },
  116. {
  117. regExp: /[b-d\u{10001}-\u{10003}]/u,
  118. acceptedCharacters: ['b', 'c', 'd', '\u{10001}', '\u{10002}', '\u{10003}'],
  119. rejectedCharacters: ['-', 'a', 'e', '\u{10000}', '\u{10004}']
  120. },
  121. {
  122. // Make sure we don't omit the \u{10400}-\u{107FF} range
  123. regExp: /[\u{10001}-\u{10BFE}]/u,
  124. acceptedCharacters: ['\u{10001}', '\u{103FF}', '\u{10400}', '\u{107FF}', '\u{10800}', '\u{10BFE}']
  125. },
  126. {
  127. regExp: /[\u{10000}-\u{107FF}]/u,
  128. acceptedCharacters: ['\u{10000}', '\u{103FF}', '\u{10400}', '\u{107FF}']
  129. },
  130. {
  131. // Make sure we don't omit the \u{10400}-\u{107FF} range
  132. regExp: /[\u{10000}-\u{10802}]/u,
  133. acceptedCharacters: ['\u{10000}', '\u{103FF}', '\u{10400}', '\u{107FF}', '\u{10800}', '\u{10802}']
  134. }
  135. ];
  136. var testsForRunner = normalTests.map(function (test) {
  137. return {
  138. name: '' + test.regExp + ' should work correctly',
  139. body: function () {
  140. function makePrintable(ch) {
  141. var charCode = ch.charCodeAt(0);
  142. if ('a'.charCodeAt(0) <= charCode && charCode <= 'z'.charCodeAt(0)
  143. || 'A'.charCodeAt(0) <= charCode && charCode <= 'Z'.charCodeAt(0)
  144. || '0'.charCodeAt(0) <= charCode && charCode <= '9'.charCodeAt(0)
  145. || ch === '-') {
  146. return ch;
  147. }
  148. else {
  149. var hexString = ch.codePointAt(0).toString(16);
  150. return '\\u{' + hexString + '}';
  151. }
  152. }
  153. function createMessage(ch, result) {
  154. return "'" + makePrintable(ch) + "' should be " + result;
  155. }
  156. test.acceptedCharacters.forEach(function (ch) {
  157. assert.isTrue(test.regExp.test(ch), createMessage(ch, 'acceptedCharacters'));
  158. });
  159. rejectedCharacters = test.rejectedCharacters || [];
  160. rejectedCharacters.forEach(function (ch) {
  161. assert.isFalse(test.regExp.test(ch), createMessage(ch, 'rejectedCharacters'));
  162. });
  163. }
  164. };
  165. });
  166. var disallowedPatterns = [
  167. '/[b-a]/',
  168. '/[\\u{10001}-\\u{10000}]/u',
  169. ];
  170. testsForRunner = testsForRunner.concat(disallowedPatterns.map(function (pattern) {
  171. return {
  172. name: '' + pattern + ' should throw SyntaxError',
  173. body: function () {
  174. assert.throws(function () { eval(pattern); }, SyntaxError);
  175. }
  176. };
  177. }));
  178. testRunner.runTests(testsForRunner, { verbose: WScript.Arguments[0] != 'summary' });