improve unit tests

This commit is contained in:
Dirk-Jan C. Binnema
2023-09-24 20:21:57 +03:00
parent e0417f0766
commit 4a0eba8ddf
6 changed files with 52 additions and 23 deletions

View File

@ -33,8 +33,15 @@ test_regex_match()
auto rx = Regex::make("a.*b.c");
assert_valid_result(rx);
assert_equal(mu_format("{}", *rx), "/a.*b.c/");
g_assert_true(rx->matches("axxxxxbqc"));
g_assert_false(rx->matches("axxxxxbqqc"));
{ // unset matches nothing.
Regex rx2;
g_assert_false(rx2.matches(""));
}
}
@ -43,7 +50,6 @@ test_regex_match2()
{
Regex rx;
{
std::string foo = "h.llo";
rx = unwrap(Regex::make(foo.c_str()));
}
@ -61,16 +67,37 @@ test_regex_replace()
{
auto rx = Regex::make("f.o");
assert_valid_result(rx);
assert_equal(rx->replace("foobar", "cuux"), "cuuxbar");
assert_equal(rx->replace("foobar", "cuux").value_or("error"), "cuuxbar");
}
{
auto rx = Regex::make("f.o", G_REGEX_MULTILINE);
assert_valid_result(rx);
assert_equal(rx->replace("foobar\nfoobar", "cuux"), "cuuxbar\ncuuxbar");
assert_equal(rx->replace("foobar\nfoobar", "cuux").value_or("error"),
"cuuxbar\ncuuxbar");
}
}
static void
test_regex_fail()
{
allow_warnings();
{ // unset rx can't replace / error.
Regex rx;
assert_equal(mu_format("{}", rx), "//");
g_assert_false(!!rx.replace("foo", "bar"));
}
{
auto rx = Regex::make("(");
g_assert_false(!!rx);
}
}
int
main(int argc, char* argv[])
{
@ -79,6 +106,7 @@ main(int argc, char* argv[])
g_test_add_func("/regex/match", test_regex_match);
g_test_add_func("/regex/match2", test_regex_match2);
g_test_add_func("/regex/replace", test_regex_replace);
g_test_add_func("/regex/fail", test_regex_fail);
return g_test_run();
}