test_regex.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. #pragma once
  31. #include "../regex.h"
  32. #include "core/string/ustring.h"
  33. #include "tests/test_macros.h"
  34. namespace TestRegEx {
  35. TEST_CASE("[RegEx] Initialization") {
  36. const String pattern = "(?<vowel>[aeiou])";
  37. RegEx re1(pattern);
  38. CHECK(re1.is_valid());
  39. CHECK(re1.get_pattern() == pattern);
  40. CHECK(re1.get_group_count() == 1);
  41. PackedStringArray names = re1.get_names();
  42. CHECK(names.size() == 1);
  43. CHECK(names[0] == "vowel");
  44. RegEx re2;
  45. CHECK(re2.is_valid() == false);
  46. CHECK(re2.compile(pattern) == OK);
  47. CHECK(re2.is_valid());
  48. CHECK(re1.get_pattern() == re2.get_pattern());
  49. CHECK(re1.get_group_count() == re2.get_group_count());
  50. names = re2.get_names();
  51. CHECK(names.size() == 1);
  52. CHECK(names[0] == "vowel");
  53. }
  54. TEST_CASE("[RegEx] Clearing") {
  55. RegEx re("Godot");
  56. REQUIRE(re.is_valid());
  57. re.clear();
  58. CHECK(re.is_valid() == false);
  59. }
  60. TEST_CASE("[RegEx] Searching") {
  61. const String s = "Searching";
  62. const String vowels = "[aeiou]{1,2}";
  63. const String numerics = "\\d";
  64. RegEx re(vowels);
  65. REQUIRE(re.is_valid());
  66. Ref<RegExMatch> match = re.search(s);
  67. REQUIRE(match.is_valid());
  68. CHECK(match->get_string(0) == "ea");
  69. match = re.search(s, 1, 2);
  70. REQUIRE(match.is_valid());
  71. CHECK(match->get_string(0) == "e");
  72. match = re.search(s, 2, 4);
  73. REQUIRE(match.is_valid());
  74. CHECK(match->get_string(0) == "a");
  75. match = re.search(s, 3, 5);
  76. CHECK(match.is_null());
  77. match = re.search(s, 6, 2);
  78. CHECK(match.is_null());
  79. const Array all_results = re.search_all(s);
  80. CHECK(all_results.size() == 2);
  81. match = all_results[0];
  82. REQUIRE(match.is_valid());
  83. CHECK(match->get_string(0) == "ea");
  84. match = all_results[1];
  85. REQUIRE(match.is_valid());
  86. CHECK(match->get_string(0) == "i");
  87. CHECK(re.compile(numerics) == OK);
  88. CHECK(re.is_valid());
  89. CHECK(re.search(s).is_null());
  90. CHECK(re.search_all(s).size() == 0);
  91. }
  92. TEST_CASE("[RegEx] Substitution") {
  93. const String s1 = "Double all the vowels.";
  94. RegEx re1("(?<vowel>[aeiou])");
  95. REQUIRE(re1.is_valid());
  96. CHECK(re1.sub(s1, "$0$vowel", true) == "Doouublee aall thee vooweels.");
  97. const String s2 = "Substitution with group.";
  98. RegEx re2("Substitution (.+)");
  99. REQUIRE(re2.is_valid());
  100. CHECK(re2.sub(s2, "Test ${1}") == "Test with group.");
  101. const String s3 = "Useless substitution";
  102. RegEx re3("Anything");
  103. REQUIRE(re3.is_valid());
  104. CHECK(re3.sub(s3, "Something") == "Useless substitution");
  105. const String s4 = "acacac";
  106. RegEx re4("(a)(b){0}(c)");
  107. REQUIRE(re4.is_valid());
  108. CHECK(re4.sub(s4, "${1}.${3}.", true) == "a.c.a.c.a.c.");
  109. const String s5 = "aaaa";
  110. RegEx re5("a");
  111. REQUIRE(re5.is_valid());
  112. CHECK(re5.sub(s5, "b", true, 0, 2) == "bbaa");
  113. CHECK(re5.sub(s5, "b", true, 1, 3) == "abba");
  114. CHECK(re5.sub(s5, "b", true, 0, 0) == "aaaa");
  115. CHECK(re5.sub(s5, "b", true, 1, 1) == "aaaa");
  116. CHECK(re5.sub(s5, "cc", true, 0, 2) == "ccccaa");
  117. CHECK(re5.sub(s5, "cc", true, 1, 3) == "acccca");
  118. CHECK(re5.sub(s5, "", true, 0, 2) == "aa");
  119. const String s6 = "property get_property set_property";
  120. RegEx re6("(get_|set_)?property");
  121. REQUIRE(re6.is_valid());
  122. CHECK(re6.sub(s6, "$1new_property", true) == "new_property get_new_property set_new_property");
  123. ERR_PRINT_OFF;
  124. CHECK(re6.sub(s6, "$5new_property", true) == "new_property new_property new_property");
  125. ERR_PRINT_ON;
  126. }
  127. TEST_CASE("[RegEx] Substitution with empty input and/or replacement") {
  128. const String s1 = "";
  129. const String s2 = "gogogo";
  130. RegEx re1("");
  131. REQUIRE(re1.is_valid());
  132. CHECK(re1.sub(s1, "") == "");
  133. CHECK(re1.sub(s1, "a") == "a");
  134. CHECK(re1.sub(s2, "") == "gogogo");
  135. RegEx re2("go");
  136. REQUIRE(re2.is_valid());
  137. CHECK(re2.sub(s2, "") == "gogo");
  138. CHECK(re2.sub(s2, "", true) == "");
  139. }
  140. TEST_CASE("[RegEx] Uninitialized use") {
  141. const String s = "Godot";
  142. RegEx re;
  143. ERR_PRINT_OFF;
  144. CHECK(re.search(s).is_null());
  145. CHECK(re.search_all(s).size() == 0);
  146. CHECK(re.sub(s, "") == "");
  147. CHECK(re.get_group_count() == 0);
  148. CHECK(re.get_names().size() == 0);
  149. ERR_PRINT_ON
  150. }
  151. TEST_CASE("[RegEx] Empty pattern") {
  152. const String s = "Godot";
  153. RegEx re;
  154. CHECK(re.compile("") == OK);
  155. CHECK(re.is_valid());
  156. }
  157. TEST_CASE("[RegEx] Complex Grouping") {
  158. const String test = "https://docs.godotengine.org/en/latest/contributing/";
  159. // Ignored protocol in grouping.
  160. RegEx re("^(?:https?://)([a-zA-Z]{2,4})\\.([a-zA-Z][a-zA-Z0-9_\\-]{2,64})\\.([a-zA-Z]{2,4})");
  161. REQUIRE(re.is_valid());
  162. Ref<RegExMatch> expr = re.search(test);
  163. CHECK(expr->get_group_count() == 3);
  164. CHECK(expr->get_string(0) == "https://docs.godotengine.org");
  165. CHECK(expr->get_string(1) == "docs");
  166. CHECK(expr->get_string(2) == "godotengine");
  167. CHECK(expr->get_string(3) == "org");
  168. }
  169. TEST_CASE("[RegEx] Number Expression") {
  170. const String test = "(2.5e-3 + 35 + 46) / 2.8e0 = 28.9294642857";
  171. // Not an exact regex for number but a good test.
  172. RegEx re("([+-]?\\d+)(\\.\\d+([eE][+-]?\\d+)?)?");
  173. REQUIRE(re.is_valid());
  174. Array number_match = re.search_all(test);
  175. CHECK(number_match.size() == 5);
  176. Ref<RegExMatch> number = number_match[0];
  177. CHECK(number->get_string(0) == "2.5e-3");
  178. CHECK(number->get_string(1) == "2");
  179. number = number_match[1];
  180. CHECK(number->get_string(0) == "35");
  181. number = number_match[2];
  182. CHECK(number->get_string(0) == "46");
  183. number = number_match[3];
  184. CHECK(number->get_string(0) == "2.8e0");
  185. number = number_match[4];
  186. CHECK(number->get_string(0) == "28.9294642857");
  187. CHECK(number->get_string(1) == "28");
  188. CHECK(number->get_string(2) == ".9294642857");
  189. }
  190. TEST_CASE("[RegEx] Invalid end position") {
  191. const String s = "Godot";
  192. RegEx re("o");
  193. REQUIRE(re.is_valid());
  194. Ref<RegExMatch> match = re.search(s, 0, 10);
  195. CHECK(match->get_string(0) == "o");
  196. const Array all_results = re.search_all(s, 0, 10);
  197. CHECK(all_results.size() == 2);
  198. match = all_results[0];
  199. REQUIRE(match.is_valid());
  200. CHECK(match->get_string(0) == String("o"));
  201. match = all_results[1];
  202. REQUIRE(match.is_valid());
  203. CHECK(match->get_string(0) == String("o"));
  204. CHECK(re.sub(s, "", true, 0, 10) == "Gdt");
  205. }
  206. TEST_CASE("[RegEx] Get match string list") {
  207. const String s = "Godot Engine";
  208. RegEx re("(Go)(dot)");
  209. Ref<RegExMatch> match = re.search(s);
  210. REQUIRE(match.is_valid());
  211. PackedStringArray result;
  212. result.append("Godot");
  213. result.append("Go");
  214. result.append("dot");
  215. CHECK(match->get_strings() == result);
  216. }
  217. TEST_CASE("[RegEx] Match start and end positions") {
  218. const String s = "Whole pattern";
  219. RegEx re1("pattern");
  220. REQUIRE(re1.is_valid());
  221. Ref<RegExMatch> match = re1.search(s);
  222. REQUIRE(match.is_valid());
  223. CHECK(match->get_start(0) == 6);
  224. CHECK(match->get_end(0) == 13);
  225. RegEx re2("(?<vowel>[aeiou])");
  226. REQUIRE(re2.is_valid());
  227. match = re2.search(s);
  228. REQUIRE(match.is_valid());
  229. CHECK(match->get_start("vowel") == 2);
  230. CHECK(match->get_end("vowel") == 3);
  231. }
  232. TEST_CASE("[RegEx] Asterisk search all") {
  233. const String s = "Godot Engine";
  234. RegEx re("o*");
  235. REQUIRE(re.is_valid());
  236. Ref<RegExMatch> match;
  237. const Array all_results = re.search_all(s);
  238. CHECK(all_results.size() == 13);
  239. match = all_results[0];
  240. CHECK(match->get_string(0) == "");
  241. match = all_results[1];
  242. CHECK(match->get_string(0) == "o");
  243. match = all_results[2];
  244. CHECK(match->get_string(0) == "");
  245. match = all_results[3];
  246. CHECK(match->get_string(0) == "o");
  247. for (int i = 4; i < 13; i++) {
  248. match = all_results[i];
  249. CHECK(match->get_string(0) == "");
  250. }
  251. }
  252. TEST_CASE("[RegEx] Simple lookahead") {
  253. const String s = "Godot Engine";
  254. RegEx re("o(?=t)");
  255. REQUIRE(re.is_valid());
  256. Ref<RegExMatch> match = re.search(s);
  257. REQUIRE(match.is_valid());
  258. CHECK(match->get_start(0) == 3);
  259. CHECK(match->get_end(0) == 4);
  260. }
  261. TEST_CASE("[RegEx] Lookahead groups empty matches") {
  262. const String s = "12";
  263. RegEx re("(?=(\\d+))");
  264. REQUIRE(re.is_valid());
  265. Ref<RegExMatch> match = re.search(s);
  266. CHECK(match->get_string(0) == "");
  267. CHECK(match->get_string(1) == "12");
  268. const Array all_results = re.search_all(s);
  269. CHECK(all_results.size() == 2);
  270. match = all_results[0];
  271. REQUIRE(match.is_valid());
  272. CHECK(match->get_string(0) == String(""));
  273. CHECK(match->get_string(1) == String("12"));
  274. match = all_results[1];
  275. REQUIRE(match.is_valid());
  276. CHECK(match->get_string(0) == String(""));
  277. CHECK(match->get_string(1) == String("2"));
  278. }
  279. TEST_CASE("[RegEx] Simple lookbehind") {
  280. const String s = "Godot Engine";
  281. RegEx re("(?<=d)o");
  282. REQUIRE(re.is_valid());
  283. Ref<RegExMatch> match = re.search(s);
  284. REQUIRE(match.is_valid());
  285. CHECK(match->get_start(0) == 3);
  286. CHECK(match->get_end(0) == 4);
  287. }
  288. TEST_CASE("[RegEx] Simple lookbehind search all") {
  289. const String s = "ababbaabab";
  290. RegEx re("(?<=a)b");
  291. REQUIRE(re.is_valid());
  292. const Array all_results = re.search_all(s);
  293. CHECK(all_results.size() == 4);
  294. Ref<RegExMatch> match = all_results[0];
  295. REQUIRE(match.is_valid());
  296. CHECK(match->get_start(0) == 1);
  297. CHECK(match->get_end(0) == 2);
  298. match = all_results[1];
  299. REQUIRE(match.is_valid());
  300. CHECK(match->get_start(0) == 3);
  301. CHECK(match->get_end(0) == 4);
  302. match = all_results[2];
  303. REQUIRE(match.is_valid());
  304. CHECK(match->get_start(0) == 7);
  305. CHECK(match->get_end(0) == 8);
  306. match = all_results[3];
  307. REQUIRE(match.is_valid());
  308. CHECK(match->get_start(0) == 9);
  309. CHECK(match->get_end(0) == 10);
  310. }
  311. TEST_CASE("[RegEx] Lookbehind groups empty matches") {
  312. const String s = "abaaabab";
  313. RegEx re("(?<=(b))");
  314. REQUIRE(re.is_valid());
  315. Ref<RegExMatch> match;
  316. const Array all_results = re.search_all(s);
  317. CHECK(all_results.size() == 3);
  318. match = all_results[0];
  319. REQUIRE(match.is_valid());
  320. CHECK(match->get_start(0) == 2);
  321. CHECK(match->get_end(0) == 2);
  322. CHECK(match->get_start(1) == 1);
  323. CHECK(match->get_end(1) == 2);
  324. CHECK(match->get_string(0) == String(""));
  325. CHECK(match->get_string(1) == String("b"));
  326. match = all_results[1];
  327. REQUIRE(match.is_valid());
  328. CHECK(match->get_start(0) == 6);
  329. CHECK(match->get_end(0) == 6);
  330. CHECK(match->get_start(1) == 5);
  331. CHECK(match->get_end(1) == 6);
  332. CHECK(match->get_string(0) == String(""));
  333. CHECK(match->get_string(1) == String("b"));
  334. match = all_results[2];
  335. REQUIRE(match.is_valid());
  336. CHECK(match->get_start(0) == 8);
  337. CHECK(match->get_end(0) == 8);
  338. CHECK(match->get_start(1) == 7);
  339. CHECK(match->get_end(1) == 8);
  340. CHECK(match->get_string(0) == String(""));
  341. CHECK(match->get_string(1) == String("b"));
  342. }
  343. } // namespace TestRegEx