From b7de8e2c29d0ef1414f29c6b1cbf2b90fbc7e790 Mon Sep 17 00:00:00 2001 From: toastedbreadandomelette Date: Sat, 16 Sep 2023 20:14:38 +0530 Subject: [PATCH] Add additional unit tests for RegEx --- modules/regex/tests/test_regex.h | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/modules/regex/tests/test_regex.h b/modules/regex/tests/test_regex.h index 1c305f70f7e..af58e2487b2 100644 --- a/modules/regex/tests/test_regex.h +++ b/modules/regex/tests/test_regex.h @@ -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 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 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";