diff --git a/lib/tests/test-mu-maildir.cc b/lib/tests/test-mu-maildir.cc index 62cd3215..ded4e191 100644 --- a/lib/tests/test-mu-maildir.cc +++ b/lib/tests/test-mu-maildir.cc @@ -517,7 +517,7 @@ test_maildir_move_vanilla() } static void -test_maildir_move_gio() +test_maildir_move_remote() { test_maildir_move(true/*assume_remote*/); } @@ -549,7 +549,7 @@ main(int argc, char* argv[]) g_test_add_func("/maildir/link", test_maildir_link); g_test_add_func("/maildir/move-vanilla", test_maildir_move_vanilla); - g_test_add_func("/maildir/aildir-move-gio", test_maildir_move_gio); + g_test_add_func("/maildir/move-remote", test_maildir_move_remote); return g_test_run(); } diff --git a/lib/utils/mu-logger.cc b/lib/utils/mu-logger.cc index 258643c9..04c6611a 100644 --- a/lib/utils/mu-logger.cc +++ b/lib/utils/mu-logger.cc @@ -211,7 +211,7 @@ test_logger_threads(void) threads.emplace_back( std::thread([n,&running]{ while (running) { - mu_debug("log message from thread <{}>", n); + //mu_debug("log message from thread <{}>", n); std::this_thread::yield(); } })); diff --git a/lib/utils/mu-regex.cc b/lib/utils/mu-regex.cc index cfdeccf0..81276952 100644 --- a/lib/utils/mu-regex.cc +++ b/lib/utils/mu-regex.cc @@ -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(); } diff --git a/lib/utils/mu-regex.hh b/lib/utils/mu-regex.hh index 409c7d3a..c5fd4a00 100644 --- a/lib/utils/mu-regex.hh +++ b/lib/utils/mu-regex.hh @@ -52,8 +52,7 @@ struct Regex { */ static Result make(const std::string& ptrn, GRegexCompileFlags cflags = G_REGEX_DEFAULT, - GRegexMatchFlags mflags = G_REGEX_MATCH_DEFAULT) - noexcept try { + GRegexMatchFlags mflags = G_REGEX_MATCH_DEFAULT) noexcept try { return Regex(ptrn.c_str(), cflags, mflags); } catch (const Error& err) { return Err(err); @@ -146,20 +145,24 @@ struct Regex { } /** - * Replace all occurences of @this regexp in some string with a + * Replace all occurrences of @this regexp in some string with a * replacement string * * @param str some string * @param repl replacement string * - * @return string + * @return string or error */ - std::string replace(const std::string& str, const std::string& repl) { - char *s{g_regex_replace(rx_, str.c_str(), str.length(), 0, - repl.c_str(), G_REGEX_MATCH_DEFAULT, {})}; - if (!s) - throw Err(Error::Code::InvalidArgument, "error in Regex::replace"); - return to_string_gchar(std::move(s)); + Result replace(const std::string& str, const std::string& repl) const { + GError *gerr{}; + + if (!rx_) + return Err(Error::Code::InvalidArgument, "missing regexp"); + else if (auto&& s{g_regex_replace(rx_, str.c_str(), str.length(), 0, + repl.c_str(), G_REGEX_MATCH_DEFAULT, &gerr)}; !s) + return Err(Error::Code::InvalidArgument, &gerr, "error in Regex::replace"); + else + return Ok(to_string_gchar(std::move(s))); } const GRegex* g_regex() const { return rx_; } @@ -168,11 +171,11 @@ private: Regex(const char *ptrn, GRegexCompileFlags cflags, GRegexMatchFlags mflags) { GError *err{}; if (rx_ = g_regex_new(ptrn, cflags, mflags, &err); !rx_) - throw Err(Error::Code::InvalidArgument, &err, - "invalid regexp: '{}'", ptrn); + throw Error{Error::Code::InvalidArgument, &err, + "invalid regexp: '{}'", ptrn}; } - GRegex *rx_; + GRegex *rx_{}; }; static inline std::string format_as(const Regex& rx) { diff --git a/lib/utils/tests/test-utils.cc b/lib/utils/tests/test-utils.cc index 7b34aad1..993c875c 100644 --- a/lib/utils/tests/test-utils.cc +++ b/lib/utils/tests/test-utils.cc @@ -319,8 +319,6 @@ test_summarize(void) "fortification by the Bais raja Sathna. "); } - - int main(int argc, char* argv[]) { diff --git a/mu/mu-cmd-view.cc b/mu/mu-cmd-view.cc index c80e46f0..3ddd78e1 100644 --- a/mu/mu-cmd-view.cc +++ b/mu/mu-cmd-view.cc @@ -251,7 +251,7 @@ test_view_plain() // silly hack to avoid locale diffs auto rx = unwrap(Regex::make("^Date:.*", G_REGEX_MULTILINE)); - output.standard_out = rx.replace(output.standard_out, "Date: xxx"); + output.standard_out = unwrap(rx.replace(output.standard_out, "Date: xxx")); g_assert_true(output.standard_err.empty()); assert_equal(output.standard_out, @@ -273,7 +273,7 @@ test_view_html() auto output{*res}; auto rx = unwrap(Regex::make("^Date:.*", G_REGEX_MULTILINE)); - output.standard_out = rx.replace(output.standard_out, "Date: xxx"); + output.standard_out = unwrap(rx.replace(output.standard_out, "Date: xxx")); g_assert_true(output.standard_err.empty()); assert_equal(output.standard_out,