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

@ -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();
}

View File

@ -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();
}
}));

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();
}

View File

@ -52,8 +52,7 @@ struct Regex {
*/
static Result<Regex> 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<std::string> 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) {

View File

@ -319,8 +319,6 @@ test_summarize(void)
"fortification by the Bais raja Sathna. ");
}
int
main(int argc, char* argv[])
{

View File

@ -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,