test_regex.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. const String s6 = "property get_property set_property";
  121. RegEx re6("(get_|set_)?property");
  122. REQUIRE(re6.is_valid());
  123. CHECK(re6.sub(s6, "$1new_property", true) == "new_property get_new_property set_new_property");
  124. ERR_PRINT_OFF;
  125. CHECK(re6.sub(s6, "$5new_property", true) == "new_property new_property new_property");
  126. ERR_PRINT_ON;
  127. }
  128. TEST_CASE("[RegEx] Substitution with empty input and/or replacement") {
  129. const String s1 = "";
  130. const String s2 = "gogogo";
  131. RegEx re1("");
  132. REQUIRE(re1.is_valid());
  133. CHECK(re1.sub(s1, "") == "");
  134. CHECK(re1.sub(s1, "a") == "a");
  135. CHECK(re1.sub(s2, "") == "gogogo");
  136. RegEx re2("go");
  137. REQUIRE(re2.is_valid());
  138. CHECK(re2.sub(s2, "") == "gogo");
  139. CHECK(re2.sub(s2, "", true) == "");
  140. }
  141. TEST_CASE("[RegEx] Uninitialized use") {
  142. const String s = "Godot";
  143. RegEx re;
  144. ERR_PRINT_OFF;
  145. CHECK(re.search(s).is_null());
  146. CHECK(re.search_all(s).size() == 0);
  147. CHECK(re.sub(s, "") == "");
  148. CHECK(re.get_group_count() == 0);
  149. CHECK(re.get_names().size() == 0);
  150. ERR_PRINT_ON
  151. }
  152. TEST_CASE("[RegEx] Empty pattern") {
  153. const String s = "Godot";
  154. RegEx re;
  155. CHECK(re.compile("") == OK);
  156. CHECK(re.is_valid());
  157. }
  158. TEST_CASE("[RegEx] Complex Grouping") {
  159. const String test = "https://docs.godotengine.org/en/latest/contributing/";
  160. // Ignored protocol in grouping.
  161. RegEx re("^(?:https?://)([a-zA-Z]{2,4})\\.([a-zA-Z][a-zA-Z0-9_\\-]{2,64})\\.([a-zA-Z]{2,4})");
  162. REQUIRE(re.is_valid());
  163. Ref<RegExMatch> expr = re.search(test);
  164. CHECK(expr->get_group_count() == 3);
  165. CHECK(expr->get_string(0) == "https://docs.godotengine.org");
  166. CHECK(expr->get_string(1) == "docs");
  167. CHECK(expr->get_string(2) == "godotengine");
  168. CHECK(expr->get_string(3) == "org");
  169. }
  170. TEST_CASE("[RegEx] Number Expression") {
  171. const String test = "(2.5e-3 + 35 + 46) / 2.8e0 = 28.9294642857";
  172. // Not an exact regex for number but a good test.
  173. RegEx re("([+-]?\\d+)(\\.\\d+([eE][+-]?\\d+)?)?");
  174. REQUIRE(re.is_valid());
  175. Array number_match = re.search_all(test);
  176. CHECK(number_match.size() == 5);
  177. Ref<RegExMatch> number = number_match[0];
  178. CHECK(number->get_string(0) == "2.5e-3");
  179. CHECK(number->get_string(1) == "2");
  180. number = number_match[1];
  181. CHECK(number->get_string(0) == "35");
  182. number = number_match[2];
  183. CHECK(number->get_string(0) == "46");
  184. number = number_match[3];
  185. CHECK(number->get_string(0) == "2.8e0");
  186. number = number_match[4];
  187. CHECK(number->get_string(0) == "28.9294642857");
  188. CHECK(number->get_string(1) == "28");
  189. CHECK(number->get_string(2) == ".9294642857");
  190. }
  191. TEST_CASE("[RegEx] Invalid end position") {
  192. const String s = "Godot";
  193. RegEx re("o");
  194. REQUIRE(re.is_valid());
  195. Ref<RegExMatch> match = re.search(s, 0, 10);
  196. CHECK(match->get_string(0) == "o");
  197. const Array all_results = re.search_all(s, 0, 10);
  198. CHECK(all_results.size() == 2);
  199. match = all_results[0];
  200. REQUIRE(match.is_valid());
  201. CHECK(match->get_string(0) == String("o"));
  202. match = all_results[1];
  203. REQUIRE(match.is_valid());
  204. CHECK(match->get_string(0) == String("o"));
  205. CHECK(re.sub(s, "", true, 0, 10) == "Gdt");
  206. }
  207. TEST_CASE("[RegEx] Get match string list") {
  208. const String s = "Godot Engine";
  209. RegEx re("(Go)(dot)");
  210. Ref<RegExMatch> match = re.search(s);
  211. REQUIRE(match.is_valid());
  212. PackedStringArray result;
  213. result.append("Godot");
  214. result.append("Go");
  215. result.append("dot");
  216. CHECK(match->get_strings() == result);
  217. }
  218. TEST_CASE("[RegEx] Match start and end positions") {
  219. const String s = "Whole pattern";
  220. RegEx re1("pattern");
  221. REQUIRE(re1.is_valid());
  222. Ref<RegExMatch> match = re1.search(s);
  223. REQUIRE(match.is_valid());
  224. CHECK(match->get_start(0) == 6);
  225. CHECK(match->get_end(0) == 13);
  226. RegEx re2("(?<vowel>[aeiou])");
  227. REQUIRE(re2.is_valid());
  228. match = re2.search(s);
  229. REQUIRE(match.is_valid());
  230. CHECK(match->get_start("vowel") == 2);
  231. CHECK(match->get_end("vowel") == 3);
  232. }
  233. TEST_CASE("[RegEx] Asterisk search all") {
  234. const String s = "Godot Engine";
  235. RegEx re("o*");
  236. REQUIRE(re.is_valid());
  237. Ref<RegExMatch> match;
  238. const Array all_results = re.search_all(s);
  239. CHECK(all_results.size() == 13);
  240. match = all_results[0];
  241. CHECK(match->get_string(0) == "");
  242. match = all_results[1];
  243. CHECK(match->get_string(0) == "o");
  244. match = all_results[2];
  245. CHECK(match->get_string(0) == "");
  246. match = all_results[3];
  247. CHECK(match->get_string(0) == "o");
  248. for (int i = 4; i < 13; i++) {
  249. match = all_results[i];
  250. CHECK(match->get_string(0) == "");
  251. }
  252. }
  253. TEST_CASE("[RegEx] Simple lookahead") {
  254. const String s = "Godot Engine";
  255. RegEx re("o(?=t)");
  256. REQUIRE(re.is_valid());
  257. Ref<RegExMatch> match = re.search(s);
  258. REQUIRE(match.is_valid());
  259. CHECK(match->get_start(0) == 3);
  260. CHECK(match->get_end(0) == 4);
  261. }
  262. TEST_CASE("[RegEx] Lookahead groups empty matches") {
  263. const String s = "12";
  264. RegEx re("(?=(\\d+))");
  265. REQUIRE(re.is_valid());
  266. Ref<RegExMatch> match = re.search(s);
  267. CHECK(match->get_string(0) == "");
  268. CHECK(match->get_string(1) == "12");
  269. const Array all_results = re.search_all(s);
  270. CHECK(all_results.size() == 2);
  271. match = all_results[0];
  272. REQUIRE(match.is_valid());
  273. CHECK(match->get_string(0) == String(""));
  274. CHECK(match->get_string(1) == String("12"));
  275. match = all_results[1];
  276. REQUIRE(match.is_valid());
  277. CHECK(match->get_string(0) == String(""));
  278. CHECK(match->get_string(1) == String("2"));
  279. }
  280. TEST_CASE("[RegEx] Simple lookbehind") {
  281. const String s = "Godot Engine";
  282. RegEx re("(?<=d)o");
  283. REQUIRE(re.is_valid());
  284. Ref<RegExMatch> match = re.search(s);
  285. REQUIRE(match.is_valid());
  286. CHECK(match->get_start(0) == 3);
  287. CHECK(match->get_end(0) == 4);
  288. }
  289. TEST_CASE("[RegEx] Simple lookbehind search all") {
  290. const String s = "ababbaabab";
  291. RegEx re("(?<=a)b");
  292. REQUIRE(re.is_valid());
  293. const Array all_results = re.search_all(s);
  294. CHECK(all_results.size() == 4);
  295. Ref<RegExMatch> match = all_results[0];
  296. REQUIRE(match.is_valid());
  297. CHECK(match->get_start(0) == 1);
  298. CHECK(match->get_end(0) == 2);
  299. match = all_results[1];
  300. REQUIRE(match.is_valid());
  301. CHECK(match->get_start(0) == 3);
  302. CHECK(match->get_end(0) == 4);
  303. match = all_results[2];
  304. REQUIRE(match.is_valid());
  305. CHECK(match->get_start(0) == 7);
  306. CHECK(match->get_end(0) == 8);
  307. match = all_results[3];
  308. REQUIRE(match.is_valid());
  309. CHECK(match->get_start(0) == 9);
  310. CHECK(match->get_end(0) == 10);
  311. }
  312. TEST_CASE("[RegEx] Lookbehind groups empty matches") {
  313. const String s = "abaaabab";
  314. RegEx re("(?<=(b))");
  315. REQUIRE(re.is_valid());
  316. Ref<RegExMatch> match;
  317. const Array all_results = re.search_all(s);
  318. CHECK(all_results.size() == 3);
  319. match = all_results[0];
  320. REQUIRE(match.is_valid());
  321. CHECK(match->get_start(0) == 2);
  322. CHECK(match->get_end(0) == 2);
  323. CHECK(match->get_start(1) == 1);
  324. CHECK(match->get_end(1) == 2);
  325. CHECK(match->get_string(0) == String(""));
  326. CHECK(match->get_string(1) == String("b"));
  327. match = all_results[1];
  328. REQUIRE(match.is_valid());
  329. CHECK(match->get_start(0) == 6);
  330. CHECK(match->get_end(0) == 6);
  331. CHECK(match->get_start(1) == 5);
  332. CHECK(match->get_end(1) == 6);
  333. CHECK(match->get_string(0) == String(""));
  334. CHECK(match->get_string(1) == String("b"));
  335. match = all_results[2];
  336. REQUIRE(match.is_valid());
  337. CHECK(match->get_start(0) == 8);
  338. CHECK(match->get_end(0) == 8);
  339. CHECK(match->get_start(1) == 7);
  340. CHECK(match->get_end(1) == 8);
  341. CHECK(match->get_string(0) == String(""));
  342. CHECK(match->get_string(1) == String("b"));
  343. }
  344. } // namespace TestRegEx
  345. #endif // TEST_REGEX_H