test_regex.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, 2, 4);
  71. REQUIRE(match != nullptr);
  72. CHECK(match->get_string(0) == "a");
  73. const Array all_results = re.search_all(s);
  74. CHECK(all_results.size() == 2);
  75. match = all_results[0];
  76. REQUIRE(match != nullptr);
  77. CHECK(match->get_string(0) == "ea");
  78. match = all_results[1];
  79. REQUIRE(match != nullptr);
  80. CHECK(match->get_string(0) == "i");
  81. CHECK(re.compile(numerics) == OK);
  82. CHECK(re.is_valid());
  83. CHECK(re.search(s) == nullptr);
  84. CHECK(re.search_all(s).size() == 0);
  85. }
  86. TEST_CASE("[RegEx] Substitution") {
  87. String s = "Double all the vowels.";
  88. RegEx re("(?<vowel>[aeiou])");
  89. REQUIRE(re.is_valid());
  90. CHECK(re.sub(s, "$0$vowel", true) == "Doouublee aall thee vooweels.");
  91. }
  92. TEST_CASE("[RegEx] Uninitialized use") {
  93. const String s = "Godot";
  94. RegEx re;
  95. ERR_PRINT_OFF;
  96. CHECK(re.search(s) == nullptr);
  97. CHECK(re.search_all(s).size() == 0);
  98. CHECK(re.sub(s, "") == "");
  99. CHECK(re.get_group_count() == 0);
  100. CHECK(re.get_names().size() == 0);
  101. ERR_PRINT_ON
  102. }
  103. TEST_CASE("[RegEx] Empty Pattern") {
  104. const String s = "Godot";
  105. RegEx re;
  106. CHECK(re.compile("") == OK);
  107. CHECK(re.is_valid());
  108. }
  109. TEST_CASE("[RegEx] Invalid end position") {
  110. const String s = "Godot";
  111. RegEx re("o");
  112. REQUIRE(re.is_valid());
  113. Ref<RegExMatch> match = re.search(s, 0, 10);
  114. CHECK(match->get_string(0) == "o");
  115. const Array all_results = re.search_all(s, 0, 10);
  116. CHECK(all_results.size() == 2);
  117. match = all_results[0];
  118. REQUIRE(match != nullptr);
  119. CHECK(match->get_string(0) == String("o"));
  120. match = all_results[1];
  121. REQUIRE(match != nullptr);
  122. CHECK(match->get_string(0) == String("o"));
  123. CHECK(re.sub(s, "", true, 0, 10) == "Gdt");
  124. }
  125. } // namespace TestRegEx
  126. #endif // TEST_REGEX_H