RegExpApisTestWithStickyFlag.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 createMessage(message, prefix) {
  7. prefix = (prefix != undefined) ? prefix + ": " : "";
  8. return prefix + message;
  9. }
  10. function assertSplit(re, messagePrefix) {
  11. var str = "a,ab,ba,b,";
  12. var result = str.split(re);
  13. assert.areEqual(3, result.length, createMessage("Sticky = true, RegExp.split() result's length", messagePrefix));
  14. assert.areEqual("", result[0], createMessage("Sticky = true, RegExp.split() result[0]", messagePrefix));
  15. assert.areEqual("ab,b", result[1], createMessage("Sticky = true, RegExp.split() result[1]", messagePrefix));
  16. assert.areEqual("b,", result[2], createMessage("Sticky = true, RegExp.split() result[2]", messagePrefix));
  17. assert.areEqual(0, re.lastIndex, createMessage("Sticky = true, lastIndex result on RegExp.split()", messagePrefix));
  18. }
  19. function assertSplitWithSingleCharacterPattern(re, messagePrefix) {
  20. var str = "abaca";
  21. var result = str.split(re);
  22. assert.areEqual(4, result.length, createMessage("Sticky = true, RegExp.split() result's length", messagePrefix));
  23. assert.areEqual("", result[0], createMessage("Sticky = true, RegExp.split() result", messagePrefix));
  24. assert.areEqual("b", result[1], createMessage("Sticky = true, RegExp.split() result", messagePrefix));
  25. assert.areEqual("c", result[2], createMessage("Sticky = true, RegExp.split() result", messagePrefix));
  26. assert.areEqual("", result[3], createMessage("Sticky = true, RegExp.split() result", messagePrefix));
  27. assert.areEqual(0, re.lastIndex, createMessage("Sticky = true, lastIndex result on RegExp.split()", messagePrefix));
  28. }
  29. function createReplaceValue(replaceValueType) {
  30. return replaceValueType === "string" ? "1" : function () { return "1"; };
  31. }
  32. var tests = [
  33. {
  34. name: "RegExp.test() - matches for the beginning of string, otherwise terminates if sticky = true",
  35. body: function () {
  36. var str = "abcababc";
  37. var re = /abc/y;
  38. assert.isTrue(re.test(str), "Sticky = true, RegExp.test() result");
  39. assert.isTrue(re.lastIndex == 3, "Sticky = true, lastIndex result on RegExp.test()");
  40. assert.isFalse(re.test(str), "Sticky = true, RegExp.test() result");
  41. assert.isTrue(re.lastIndex == 0, "Sticky = true, lastIndex result on RegExp.test()");
  42. assert.isTrue(re.test(str), "Sticky = true, RegExp.test() result");
  43. assert.isTrue(re.lastIndex == 3, "Sticky = true, lastIndex result on RegExp.test()");
  44. }
  45. },
  46. {
  47. name: "RegExp.exec() - matches for the beginning of string, otherwise terminates if sticky = true",
  48. body: function () {
  49. var str = "abcababc";
  50. var re = /abc/y;
  51. assert.isTrue(re.exec(str) == "abc", "Sticky = true, RegExp.exec() result");
  52. assert.isTrue(re.lastIndex == 3, "Sticky = true, lastIndex result on RegExp.exec()");
  53. assert.isTrue(re.exec(str) == null, "Sticky = true, RegExp.exec() result");
  54. assert.isTrue(re.lastIndex == 0, "Sticky = true, lastIndex result on RegExp.exec()");
  55. }
  56. },
  57. {
  58. name: "RegExp.match() - matches for the beginning of string, otherwise terminates if sticky = true",
  59. body: function () {
  60. var str = "abcababc";
  61. var re = /abc/y;
  62. assert.isTrue(str.match(re) == "abc", "Sticky = true, RegExp.match() result");
  63. assert.isTrue(re.lastIndex == 3, "Sticky = true, lastIndex result on RegExp.match()");
  64. assert.isTrue(str.match(re) == null, "Sticky = true, RegExp.match() result");
  65. assert.isTrue(re.lastIndex == 0, "Sticky = true, lastIndex result on RegExp.match()");
  66. }
  67. },
  68. {
  69. name: "RegExp.match() - matches for the beginning of string, otherwise terminates if sticky = true with lastindex set",
  70. body: function () {
  71. var str = "abcabcababc";
  72. var re = /abc/y;
  73. re.lastIndex = 3;
  74. assert.isTrue(str.match(re) == "abc", "Sticky = true, RegExp.match() result");
  75. assert.isTrue(re.lastIndex == 6, "Sticky = true, lastIndex result on RegExp.match()");
  76. assert.isTrue(str.match(re) == null, "Sticky = true, RegExp.match() result");
  77. assert.isTrue(re.lastIndex == 0, "Sticky = true, lastIndex result on RegExp.match()");
  78. }
  79. },
  80. {
  81. name: "RegExp.search() - matches for the beginning of string, otherwise terminates if sticky = true",
  82. body: function () {
  83. var str = "ababcabc";
  84. var re = /abc/y;
  85. assert.isTrue(str.search(re) == -1, "Sticky = true, RegExp.search() result");
  86. assert.isTrue(re.lastIndex == 0, "Sticky = true, lastIndex result on RegExp.search()");
  87. assert.isTrue(str.search(re) == -1, "Sticky = true, RegExp.search() result");
  88. assert.isTrue(re.lastIndex == 0, "Sticky = true, lastIndex result on RegExp.search()");
  89. }
  90. },
  91. {
  92. name: "RegExp.replace() - matches for the beginning of string, otherwise terminates if sticky = true",
  93. body: function () {
  94. function assertReplace(replaceValueType) {
  95. var str = "abcabcababc";
  96. var re = /abc/y;
  97. var replaceValue = createReplaceValue(replaceValueType);
  98. assert.isTrue(str.replace(re, replaceValue) == "1abcababc", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  99. assert.isTrue(re.lastIndex == 3, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  100. assert.isTrue(str.replace(re, replaceValue) == "abc1ababc", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  101. assert.isTrue(re.lastIndex == 6, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  102. assert.isTrue(str.replace(re, replaceValue) == "abcabcababc", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  103. assert.isTrue(re.lastIndex == 0, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  104. }
  105. ["string", "function"].forEach(assertReplace);
  106. }
  107. },
  108. {
  109. name: "RegExp.replace() - matches for the beginning of string, otherwise terminates if sticky = true, lastIndex set",
  110. body: function () {
  111. function assertReplace(replaceValueType) {
  112. var str = "abcabcababc";
  113. var re = /abc/y;
  114. re.lastIndex = 4;
  115. var replaceValue = createReplaceValue(replaceValueType);
  116. assert.isTrue(str.replace(re, replaceValue) == "abcabcababc", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  117. assert.isTrue(re.lastIndex == 0, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  118. }
  119. ["string", "function"].forEach(assertReplace);
  120. }
  121. },
  122. {
  123. name: "RegExp.replace() - matches for the beginning of string, otherwise terminates if sticky = true, global = true",
  124. body: function () {
  125. function assertReplace(replaceValueType) {
  126. var str = "abcabcababc";
  127. var re = /abc/gy;
  128. var replaceValue = createReplaceValue(replaceValueType);
  129. assert.isTrue(str.replace(re, replaceValue) == "11ababc", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  130. assert.isTrue(re.lastIndex == 0, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  131. assert.isTrue(str.replace(re, replaceValue) == "11ababc", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  132. assert.isTrue(re.lastIndex == 0, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  133. }
  134. ["string", "function"].forEach(assertReplace);
  135. }
  136. },
  137. {
  138. name: "RegExp.replace() - matches for the beginning of string, otherwise terminates if global = true",
  139. body: function () {
  140. function assertReplace(replaceValueType) {
  141. var str = "abcabcababc";
  142. var re = /abc/g;
  143. var replaceValue = createReplaceValue(replaceValueType);
  144. assert.isTrue(str.replace(re, replaceValue) == "11ab1", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  145. assert.isTrue(re.lastIndex == 0, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  146. assert.isTrue(str.replace(re, replaceValue) == "11ab1", "Sticky = true, replaceValue type = " + replaceValueType + ", RegExp.replace() result");
  147. assert.isTrue(re.lastIndex == 0, "Sticky = true, replaceValue type = " + replaceValueType + ", lastIndex result on RegExp.replace()");
  148. }
  149. ["string", "function"].forEach(assertReplace);
  150. }
  151. },
  152. {
  153. name: "RegExp.replace() - returns the input string as it is when lastIndex >= input length",
  154. body: function () {
  155. function assertReplace(replaceValueType) {
  156. var str = "abc";
  157. var re = /a/y;
  158. var lastIndex = str.length;
  159. re.lastIndex = lastIndex;
  160. var replaceValue = createReplaceValue(replaceValueType);
  161. var result = str.replace(re, replaceValue);
  162. var messageBase = "Input length: " + str.length + ", lastIndex = " + lastIndex + ", replaceValue type = " + replaceValueType;
  163. var message = messageBase + ", result";
  164. assert.areEqual(str, result, message);
  165. var message = messageBase + ", lastIndex after replace()";
  166. assert.areEqual(0, re.lastIndex, message);
  167. }
  168. ["string", "function"].forEach(assertReplace);
  169. }
  170. },
  171. {
  172. name: "RegExp.split() - ignores sticky flag if created via literal",
  173. body: function () {
  174. var re = /a,/y;
  175. assertSplit(re);
  176. }
  177. },
  178. {
  179. name: "RegExp.split() - ignores sticky flag if created via constructor",
  180. body: function () {
  181. var re = new RegExp("a,", "y");
  182. assertSplit(re, "Non-RegExp pattern");
  183. var re2 = new RegExp(re);
  184. assertSplit(re2, "RegExp pattern, 'flags' aren't present");
  185. var re3 = new RegExp(re, "y", "RegExp pattern, 'flags' are present");
  186. assertSplit(re3);
  187. var re4 = new RegExp("A,", "y");
  188. "asd".split(re);
  189. var re5 = new RegExp(re4, "i");
  190. assertSplit(re2, "Changed flags");
  191. }
  192. },
  193. {
  194. name: "RegExp.split() - ignores sticky flag if created via RegExp.prototype.compile",
  195. body: function () {
  196. var re = /./.compile("a,", "y");
  197. assertSplit(re, "Non-RegExp pattern");
  198. var re2 = /./.compile(re);
  199. assertSplit(re2, "RegExp pattern");
  200. }
  201. },
  202. {
  203. name: "RegExp.split() - ignores sticky flag if single-character pattern",
  204. body: function () {
  205. var reCaseSensitive = /a/y;
  206. assertSplitWithSingleCharacterPattern(reCaseSensitive, "Case-sensitive");
  207. var reCaseInsensitive = /A/iy;
  208. assertSplitWithSingleCharacterPattern(reCaseInsensitive, "Case-insensitive");
  209. }
  210. },
  211. {
  212. name: "RegExp.split() - result is propagated to the constructor",
  213. body: function () {
  214. var str = "abac";
  215. var re = /(a[c]?)/y;
  216. var result = str.split(re);
  217. // If the actual result is "a", that means we propagated "re"s original pattern
  218. // as opposed to the separate one for split().
  219. assert.areEqual("ac", RegExp.$1);
  220. }
  221. },
  222. {
  223. name: "RegExp.split() - ignores lastIndex",
  224. body: function () {
  225. var re = /a,/y;
  226. re.lastIndex = 3;
  227. assertSplit(re);
  228. }
  229. }
  230. ];
  231. testRunner.runTests(tests, { verbose: WScript.Arguments[0] != "summary" });