test_regex.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 != nullptr);
  69. CHECK(match->get_string(0) == "ea");
  70. match = re.search(s, 1, 2);
  71. REQUIRE(match != nullptr);
  72. CHECK(match->get_string(0) == "e");
  73. match = re.search(s, 2, 4);
  74. REQUIRE(match != nullptr);
  75. CHECK(match->get_string(0) == "a");
  76. match = re.search(s, 3, 5);
  77. CHECK(match == nullptr);
  78. match = re.search(s, 6, 2);
  79. CHECK(match == nullptr);
  80. const Array all_results = re.search_all(s);
  81. CHECK(all_results.size() == 2);
  82. match = all_results[0];
  83. REQUIRE(match != nullptr);
  84. CHECK(match->get_string(0) == "ea");
  85. match = all_results[1];
  86. REQUIRE(match != nullptr);
  87. CHECK(match->get_string(0) == "i");
  88. CHECK(re.compile(numerics) == OK);
  89. CHECK(re.is_valid());
  90. CHECK(re.search(s) == nullptr);
  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. }
  111. TEST_CASE("[RegEx] Substitution with empty input and/or replacement") {
  112. const String s1 = "";
  113. const String s2 = "gogogo";
  114. RegEx re1("");
  115. REQUIRE(re1.is_valid());
  116. CHECK(re1.sub(s1, "") == "");
  117. CHECK(re1.sub(s1, "a") == "a");
  118. CHECK(re1.sub(s2, "") == "gogogo");
  119. RegEx re2("go");
  120. REQUIRE(re2.is_valid());
  121. CHECK(re2.sub(s2, "") == "gogo");
  122. CHECK(re2.sub(s2, "", true) == "");
  123. }
  124. TEST_CASE("[RegEx] Uninitialized use") {
  125. const String s = "Godot";
  126. RegEx re;
  127. ERR_PRINT_OFF;
  128. CHECK(re.search(s) == nullptr);
  129. CHECK(re.search_all(s).size() == 0);
  130. CHECK(re.sub(s, "") == "");
  131. CHECK(re.get_group_count() == 0);
  132. CHECK(re.get_names().size() == 0);
  133. ERR_PRINT_ON
  134. }
  135. TEST_CASE("[RegEx] Empty Pattern") {
  136. const String s = "Godot";
  137. RegEx re;
  138. CHECK(re.compile("") == OK);
  139. CHECK(re.is_valid());
  140. }
  141. TEST_CASE("[RegEx] Invalid end position") {
  142. const String s = "Godot";
  143. RegEx re("o");
  144. REQUIRE(re.is_valid());
  145. Ref<RegExMatch> match = re.search(s, 0, 10);
  146. CHECK(match->get_string(0) == "o");
  147. const Array all_results = re.search_all(s, 0, 10);
  148. CHECK(all_results.size() == 2);
  149. match = all_results[0];
  150. REQUIRE(match != nullptr);
  151. CHECK(match->get_string(0) == String("o"));
  152. match = all_results[1];
  153. REQUIRE(match != nullptr);
  154. CHECK(match->get_string(0) == String("o"));
  155. CHECK(re.sub(s, "", true, 0, 10) == "Gdt");
  156. }
  157. TEST_CASE("[RegEx] Get match string list") {
  158. const String s = "Godot Engine";
  159. RegEx re("(Go)(dot)");
  160. Ref<RegExMatch> match = re.search(s);
  161. REQUIRE(match != nullptr);
  162. PackedStringArray result;
  163. result.append("Godot");
  164. result.append("Go");
  165. result.append("dot");
  166. CHECK(match->get_strings() == result);
  167. }
  168. TEST_CASE("[RegEx] Match start and end positions") {
  169. const String s = "Whole pattern";
  170. RegEx re1("pattern");
  171. REQUIRE(re1.is_valid());
  172. Ref<RegExMatch> match = re1.search(s);
  173. REQUIRE(match != nullptr);
  174. CHECK(match->get_start(0) == 6);
  175. CHECK(match->get_end(0) == 13);
  176. RegEx re2("(?<vowel>[aeiou])");
  177. REQUIRE(re2.is_valid());
  178. match = re2.search(s);
  179. REQUIRE(match != nullptr);
  180. CHECK(match->get_start("vowel") == 2);
  181. CHECK(match->get_end("vowel") == 3);
  182. }
  183. } // namespace TestRegEx
  184. #endif // TEST_REGEX_H