test_regex.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**************************************************************************/
  2. /* test_regex.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_REGEX_H
  31. #define TEST_REGEX_H
  32. #include "../regex.h"
  33. #include "core/string/ustring.h"
  34. #include "tests/test_macros.h"
  35. namespace TestRegEx {
  36. TEST_CASE("[RegEx] Initialization") {
  37. const String pattern = "(?<vowel>[aeiou])";
  38. RegEx re1(pattern);
  39. CHECK(re1.is_valid());
  40. CHECK(re1.get_pattern() == pattern);
  41. CHECK(re1.get_group_count() == 1);
  42. PackedStringArray names = re1.get_names();
  43. CHECK(names.size() == 1);
  44. CHECK(names[0] == "vowel");
  45. RegEx re2;
  46. CHECK(re2.is_valid() == false);
  47. CHECK(re2.compile(pattern) == OK);
  48. CHECK(re2.is_valid());
  49. CHECK(re1.get_pattern() == re2.get_pattern());
  50. CHECK(re1.get_group_count() == re2.get_group_count());
  51. names = re2.get_names();
  52. CHECK(names.size() == 1);
  53. CHECK(names[0] == "vowel");
  54. }
  55. TEST_CASE("[RegEx] Clearing") {
  56. RegEx re("Godot");
  57. REQUIRE(re.is_valid());
  58. re.clear();
  59. CHECK(re.is_valid() == false);
  60. }
  61. TEST_CASE("[RegEx] Searching") {
  62. const String s = "Searching";
  63. const String vowels = "[aeiou]{1,2}";
  64. const String numerics = "\\d";
  65. RegEx re(vowels);
  66. REQUIRE(re.is_valid());
  67. Ref<RegExMatch> match = re.search(s);
  68. REQUIRE(match.is_valid());
  69. CHECK(match->get_string(0) == "ea");
  70. match = re.search(s, 1, 2);
  71. REQUIRE(match.is_valid());
  72. CHECK(match->get_string(0) == "e");
  73. match = re.search(s, 2, 4);
  74. REQUIRE(match.is_valid());
  75. CHECK(match->get_string(0) == "a");
  76. match = re.search(s, 3, 5);
  77. CHECK(match.is_null());
  78. match = re.search(s, 6, 2);
  79. CHECK(match.is_null());
  80. const Array all_results = re.search_all(s);
  81. CHECK(all_results.size() == 2);
  82. match = all_results[0];
  83. REQUIRE(match.is_valid());
  84. CHECK(match->get_string(0) == "ea");
  85. match = all_results[1];
  86. REQUIRE(match.is_valid());
  87. CHECK(match->get_string(0) == "i");
  88. CHECK(re.compile(numerics) == OK);
  89. CHECK(re.is_valid());
  90. CHECK(re.search(s).is_null());
  91. CHECK(re.search_all(s).size() == 0);
  92. }
  93. TEST_CASE("[RegEx] Substitution") {
  94. const String s1 = "Double all the vowels.";
  95. RegEx re1("(?<vowel>[aeiou])");
  96. REQUIRE(re1.is_valid());
  97. CHECK(re1.sub(s1, "$0$vowel", true) == "Doouublee aall thee vooweels.");
  98. const String s2 = "Substitution with group.";
  99. RegEx re2("Substitution (.+)");
  100. REQUIRE(re2.is_valid());
  101. CHECK(re2.sub(s2, "Test ${1}") == "Test with group.");
  102. const String s3 = "Useless substitution";
  103. RegEx re3("Anything");
  104. REQUIRE(re3.is_valid());
  105. CHECK(re3.sub(s3, "Something") == "Useless substitution");
  106. const String s4 = "acacac";
  107. RegEx re4("(a)(b){0}(c)");
  108. REQUIRE(re4.is_valid());
  109. CHECK(re4.sub(s4, "${1}.${3}.", true) == "a.c.a.c.a.c.");
  110. const String s5 = "aaaa";
  111. RegEx re5("a");
  112. REQUIRE(re5.is_valid());
  113. CHECK(re5.sub(s5, "b", true, 0, 2) == "bbaa");
  114. CHECK(re5.sub(s5, "b", true, 1, 3) == "abba");
  115. CHECK(re5.sub(s5, "b", true, 0, 0) == "aaaa");
  116. CHECK(re5.sub(s5, "b", true, 1, 1) == "aaaa");
  117. CHECK(re5.sub(s5, "cc", true, 0, 2) == "ccccaa");
  118. CHECK(re5.sub(s5, "cc", true, 1, 3) == "acccca");
  119. CHECK(re5.sub(s5, "", true, 0, 2) == "aa");
  120. }
  121. TEST_CASE("[RegEx] Substitution with empty input and/or replacement") {
  122. const String s1 = "";
  123. const String s2 = "gogogo";
  124. RegEx re1("");
  125. REQUIRE(re1.is_valid());
  126. CHECK(re1.sub(s1, "") == "");
  127. CHECK(re1.sub(s1, "a") == "a");
  128. CHECK(re1.sub(s2, "") == "gogogo");
  129. RegEx re2("go");
  130. REQUIRE(re2.is_valid());
  131. CHECK(re2.sub(s2, "") == "gogo");
  132. CHECK(re2.sub(s2, "", true) == "");
  133. }
  134. TEST_CASE("[RegEx] Uninitialized use") {
  135. const String s = "Godot";
  136. RegEx re;
  137. ERR_PRINT_OFF;
  138. CHECK(re.search(s).is_null());
  139. CHECK(re.search_all(s).size() == 0);
  140. CHECK(re.sub(s, "") == "");
  141. CHECK(re.get_group_count() == 0);
  142. CHECK(re.get_names().size() == 0);
  143. ERR_PRINT_ON
  144. }
  145. TEST_CASE("[RegEx] Empty pattern") {
  146. const String s = "Godot";
  147. RegEx re;
  148. CHECK(re.compile("") == OK);
  149. CHECK(re.is_valid());
  150. }
  151. TEST_CASE("[RegEx] Complex Grouping") {
  152. const String test = "https://docs.godotengine.org/en/latest/contributing/";
  153. // Ignored protocol in grouping.
  154. RegEx re("^(?:https?://)([a-zA-Z]{2,4})\\.([a-zA-Z][a-zA-Z0-9_\\-]{2,64})\\.([a-zA-Z]{2,4})");
  155. REQUIRE(re.is_valid());
  156. Ref<RegExMatch> expr = re.search(test);
  157. CHECK(expr->get_group_count() == 3);
  158. CHECK(expr->get_string(0) == "https://docs.godotengine.org");
  159. CHECK(expr->get_string(1) == "docs");
  160. CHECK(expr->get_string(2) == "godotengine");
  161. CHECK(expr->get_string(3) == "org");
  162. }
  163. TEST_CASE("[RegEx] Number Expression") {
  164. const String test = "(2.5e-3 + 35 + 46) / 2.8e0 = 28.9294642857";
  165. // Not an exact regex for number but a good test.
  166. RegEx re("([+-]?\\d+)(\\.\\d+([eE][+-]?\\d+)?)?");
  167. REQUIRE(re.is_valid());
  168. Array number_match = re.search_all(test);
  169. CHECK(number_match.size() == 5);
  170. Ref<RegExMatch> number = number_match[0];
  171. CHECK(number->get_string(0) == "2.5e-3");
  172. CHECK(number->get_string(1) == "2");
  173. number = number_match[1];
  174. CHECK(number->get_string(0) == "35");
  175. number = number_match[2];
  176. CHECK(number->get_string(0) == "46");
  177. number = number_match[3];
  178. CHECK(number->get_string(0) == "2.8e0");
  179. number = number_match[4];
  180. CHECK(number->get_string(0) == "28.9294642857");
  181. CHECK(number->get_string(1) == "28");
  182. CHECK(number->get_string(2) == ".9294642857");
  183. }
  184. TEST_CASE("[RegEx] Invalid end position") {
  185. const String s = "Godot";
  186. RegEx re("o");
  187. REQUIRE(re.is_valid());
  188. Ref<RegExMatch> match = re.search(s, 0, 10);
  189. CHECK(match->get_string(0) == "o");
  190. const Array all_results = re.search_all(s, 0, 10);
  191. CHECK(all_results.size() == 2);
  192. match = all_results[0];
  193. REQUIRE(match.is_valid());
  194. CHECK(match->get_string(0) == String("o"));
  195. match = all_results[1];
  196. REQUIRE(match.is_valid());
  197. CHECK(match->get_string(0) == String("o"));
  198. CHECK(re.sub(s, "", true, 0, 10) == "Gdt");
  199. }
  200. TEST_CASE("[RegEx] Get match string list") {
  201. const String s = "Godot Engine";
  202. RegEx re("(Go)(dot)");
  203. Ref<RegExMatch> match = re.search(s);
  204. REQUIRE(match.is_valid());
  205. PackedStringArray result;
  206. result.append("Godot");
  207. result.append("Go");
  208. result.append("dot");
  209. CHECK(match->get_strings() == result);
  210. }
  211. TEST_CASE("[RegEx] Match start and end positions") {
  212. const String s = "Whole pattern";
  213. RegEx re1("pattern");
  214. REQUIRE(re1.is_valid());
  215. Ref<RegExMatch> match = re1.search(s);
  216. REQUIRE(match.is_valid());
  217. CHECK(match->get_start(0) == 6);
  218. CHECK(match->get_end(0) == 13);
  219. RegEx re2("(?<vowel>[aeiou])");
  220. REQUIRE(re2.is_valid());
  221. match = re2.search(s);
  222. REQUIRE(match.is_valid());
  223. CHECK(match->get_start("vowel") == 2);
  224. CHECK(match->get_end("vowel") == 3);
  225. }
  226. TEST_CASE("[RegEx] Asterisk search all") {
  227. const String s = "Godot Engine";
  228. RegEx re("o*");
  229. REQUIRE(re.is_valid());
  230. Ref<RegExMatch> match;
  231. const Array all_results = re.search_all(s);
  232. CHECK(all_results.size() == 13);
  233. match = all_results[0];
  234. CHECK(match->get_string(0) == "");
  235. match = all_results[1];
  236. CHECK(match->get_string(0) == "o");
  237. match = all_results[2];
  238. CHECK(match->get_string(0) == "");
  239. match = all_results[3];
  240. CHECK(match->get_string(0) == "o");
  241. for (int i = 4; i < 13; i++) {
  242. match = all_results[i];
  243. CHECK(match->get_string(0) == "");
  244. }
  245. }
  246. TEST_CASE("[RegEx] Simple lookahead") {
  247. const String s = "Godot Engine";
  248. RegEx re("o(?=t)");
  249. REQUIRE(re.is_valid());
  250. Ref<RegExMatch> match = re.search(s);
  251. REQUIRE(match.is_valid());
  252. CHECK(match->get_start(0) == 3);
  253. CHECK(match->get_end(0) == 4);
  254. }
  255. TEST_CASE("[RegEx] Lookahead groups empty matches") {
  256. const String s = "12";
  257. RegEx re("(?=(\\d+))");
  258. REQUIRE(re.is_valid());
  259. Ref<RegExMatch> match = re.search(s);
  260. CHECK(match->get_string(0) == "");
  261. CHECK(match->get_string(1) == "12");
  262. const Array all_results = re.search_all(s);
  263. CHECK(all_results.size() == 2);
  264. match = all_results[0];
  265. REQUIRE(match.is_valid());
  266. CHECK(match->get_string(0) == String(""));
  267. CHECK(match->get_string(1) == String("12"));
  268. match = all_results[1];
  269. REQUIRE(match.is_valid());
  270. CHECK(match->get_string(0) == String(""));
  271. CHECK(match->get_string(1) == String("2"));
  272. }
  273. TEST_CASE("[RegEx] Simple lookbehind") {
  274. const String s = "Godot Engine";
  275. RegEx re("(?<=d)o");
  276. REQUIRE(re.is_valid());
  277. Ref<RegExMatch> match = re.search(s);
  278. REQUIRE(match.is_valid());
  279. CHECK(match->get_start(0) == 3);
  280. CHECK(match->get_end(0) == 4);
  281. }
  282. TEST_CASE("[RegEx] Simple lookbehind search all") {
  283. const String s = "ababbaabab";
  284. RegEx re("(?<=a)b");
  285. REQUIRE(re.is_valid());
  286. const Array all_results = re.search_all(s);
  287. CHECK(all_results.size() == 4);
  288. Ref<RegExMatch> match = all_results[0];
  289. REQUIRE(match.is_valid());
  290. CHECK(match->get_start(0) == 1);
  291. CHECK(match->get_end(0) == 2);
  292. match = all_results[1];
  293. REQUIRE(match.is_valid());
  294. CHECK(match->get_start(0) == 3);
  295. CHECK(match->get_end(0) == 4);
  296. match = all_results[2];
  297. REQUIRE(match.is_valid());
  298. CHECK(match->get_start(0) == 7);
  299. CHECK(match->get_end(0) == 8);
  300. match = all_results[3];
  301. REQUIRE(match.is_valid());
  302. CHECK(match->get_start(0) == 9);
  303. CHECK(match->get_end(0) == 10);
  304. }
  305. TEST_CASE("[RegEx] Lookbehind groups empty matches") {
  306. const String s = "abaaabab";
  307. RegEx re("(?<=(b))");
  308. REQUIRE(re.is_valid());
  309. Ref<RegExMatch> match;
  310. const Array all_results = re.search_all(s);
  311. CHECK(all_results.size() == 3);
  312. match = all_results[0];
  313. REQUIRE(match.is_valid());
  314. CHECK(match->get_start(0) == 2);
  315. CHECK(match->get_end(0) == 2);
  316. CHECK(match->get_start(1) == 1);
  317. CHECK(match->get_end(1) == 2);
  318. CHECK(match->get_string(0) == String(""));
  319. CHECK(match->get_string(1) == String("b"));
  320. match = all_results[1];
  321. REQUIRE(match.is_valid());
  322. CHECK(match->get_start(0) == 6);
  323. CHECK(match->get_end(0) == 6);
  324. CHECK(match->get_start(1) == 5);
  325. CHECK(match->get_end(1) == 6);
  326. CHECK(match->get_string(0) == String(""));
  327. CHECK(match->get_string(1) == String("b"));
  328. match = all_results[2];
  329. REQUIRE(match.is_valid());
  330. CHECK(match->get_start(0) == 8);
  331. CHECK(match->get_end(0) == 8);
  332. CHECK(match->get_start(1) == 7);
  333. CHECK(match->get_end(1) == 8);
  334. CHECK(match->get_string(0) == String(""));
  335. CHECK(match->get_string(1) == String("b"));
  336. }
  337. } // namespace TestRegEx
  338. #endif // TEST_REGEX_H