Merge pull request #81742 from toastedbreadandomelette/regex_additional_tests

Add additional tests for RegEx
This commit is contained in:
Rémi Verschelde 2024-04-11 11:02:27 +02:00
commit 5a386287d3
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -184,6 +184,48 @@ TEST_CASE("[RegEx] Empty pattern") {
CHECK(re.is_valid());
}
TEST_CASE("[RegEx] Complex Grouping") {
const String test = "https://docs.godotengine.org/en/latest/contributing/";
// Ignored protocol in grouping.
RegEx re("^(?:https?://)([a-zA-Z]{2,4})\\.([a-zA-Z][a-zA-Z0-9_\\-]{2,64})\\.([a-zA-Z]{2,4})");
REQUIRE(re.is_valid());
Ref<RegExMatch> expr = re.search(test);
CHECK(expr->get_group_count() == 3);
CHECK(expr->get_string(0) == "https://docs.godotengine.org");
CHECK(expr->get_string(1) == "docs");
CHECK(expr->get_string(2) == "godotengine");
CHECK(expr->get_string(3) == "org");
}
TEST_CASE("[RegEx] Number Expression") {
const String test = "(2.5e-3 + 35 + 46) / 2.8e0 = 28.9294642857";
// Not an exact regex for number but a good test.
RegEx re("([+-]?\\d+)(\\.\\d+([eE][+-]?\\d+)?)?");
REQUIRE(re.is_valid());
Array number_match = re.search_all(test);
CHECK(number_match.size() == 5);
Ref<RegExMatch> number = number_match[0];
CHECK(number->get_string(0) == "2.5e-3");
CHECK(number->get_string(1) == "2");
number = number_match[1];
CHECK(number->get_string(0) == "35");
number = number_match[2];
CHECK(number->get_string(0) == "46");
number = number_match[3];
CHECK(number->get_string(0) == "2.8e0");
number = number_match[4];
CHECK(number->get_string(0) == "28.9294642857");
CHECK(number->get_string(1) == "28");
CHECK(number->get_string(2) == ".9294642857");
}
TEST_CASE("[RegEx] Invalid end position") {
const String s = "Godot";