mirror of https://github.com/djcb/mu.git
improve unit tests
This commit is contained in:
parent
e0417f0766
commit
4a0eba8ddf
|
@ -517,7 +517,7 @@ test_maildir_move_vanilla()
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_maildir_move_gio()
|
test_maildir_move_remote()
|
||||||
{
|
{
|
||||||
test_maildir_move(true/*assume_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/link", test_maildir_link);
|
||||||
g_test_add_func("/maildir/move-vanilla", test_maildir_move_vanilla);
|
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();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -211,7 +211,7 @@ test_logger_threads(void)
|
||||||
threads.emplace_back(
|
threads.emplace_back(
|
||||||
std::thread([n,&running]{
|
std::thread([n,&running]{
|
||||||
while (running) {
|
while (running) {
|
||||||
mu_debug("log message from thread <{}>", n);
|
//mu_debug("log message from thread <{}>", n);
|
||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
|
@ -33,8 +33,15 @@ test_regex_match()
|
||||||
auto rx = Regex::make("a.*b.c");
|
auto rx = Regex::make("a.*b.c");
|
||||||
assert_valid_result(rx);
|
assert_valid_result(rx);
|
||||||
|
|
||||||
|
assert_equal(mu_format("{}", *rx), "/a.*b.c/");
|
||||||
|
|
||||||
g_assert_true(rx->matches("axxxxxbqc"));
|
g_assert_true(rx->matches("axxxxxbqc"));
|
||||||
g_assert_false(rx->matches("axxxxxbqqc"));
|
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;
|
Regex rx;
|
||||||
{
|
{
|
||||||
|
|
||||||
std::string foo = "h.llo";
|
std::string foo = "h.llo";
|
||||||
rx = unwrap(Regex::make(foo.c_str()));
|
rx = unwrap(Regex::make(foo.c_str()));
|
||||||
}
|
}
|
||||||
|
@ -61,16 +67,37 @@ test_regex_replace()
|
||||||
{
|
{
|
||||||
auto rx = Regex::make("f.o");
|
auto rx = Regex::make("f.o");
|
||||||
assert_valid_result(rx);
|
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);
|
auto rx = Regex::make("f.o", G_REGEX_MULTILINE);
|
||||||
assert_valid_result(rx);
|
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
|
int
|
||||||
main(int argc, char* argv[])
|
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/match", test_regex_match);
|
||||||
g_test_add_func("/regex/match2", test_regex_match2);
|
g_test_add_func("/regex/match2", test_regex_match2);
|
||||||
g_test_add_func("/regex/replace", test_regex_replace);
|
g_test_add_func("/regex/replace", test_regex_replace);
|
||||||
|
g_test_add_func("/regex/fail", test_regex_fail);
|
||||||
|
|
||||||
return g_test_run();
|
return g_test_run();
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,8 +52,7 @@ struct Regex {
|
||||||
*/
|
*/
|
||||||
static Result<Regex> make(const std::string& ptrn,
|
static Result<Regex> make(const std::string& ptrn,
|
||||||
GRegexCompileFlags cflags = G_REGEX_DEFAULT,
|
GRegexCompileFlags cflags = G_REGEX_DEFAULT,
|
||||||
GRegexMatchFlags mflags = G_REGEX_MATCH_DEFAULT)
|
GRegexMatchFlags mflags = G_REGEX_MATCH_DEFAULT) noexcept try {
|
||||||
noexcept try {
|
|
||||||
return Regex(ptrn.c_str(), cflags, mflags);
|
return Regex(ptrn.c_str(), cflags, mflags);
|
||||||
} catch (const Error& err) {
|
} catch (const Error& err) {
|
||||||
return Err(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
|
* replacement string
|
||||||
*
|
*
|
||||||
* @param str some string
|
* @param str some string
|
||||||
* @param repl replacement string
|
* @param repl replacement string
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string or error
|
||||||
*/
|
*/
|
||||||
std::string replace(const std::string& str, const std::string& repl) {
|
Result<std::string> replace(const std::string& str, const std::string& repl) const {
|
||||||
char *s{g_regex_replace(rx_, str.c_str(), str.length(), 0,
|
GError *gerr{};
|
||||||
repl.c_str(), G_REGEX_MATCH_DEFAULT, {})};
|
|
||||||
if (!s)
|
if (!rx_)
|
||||||
throw Err(Error::Code::InvalidArgument, "error in Regex::replace");
|
return Err(Error::Code::InvalidArgument, "missing regexp");
|
||||||
return to_string_gchar(std::move(s));
|
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_; }
|
const GRegex* g_regex() const { return rx_; }
|
||||||
|
@ -168,11 +171,11 @@ private:
|
||||||
Regex(const char *ptrn, GRegexCompileFlags cflags, GRegexMatchFlags mflags) {
|
Regex(const char *ptrn, GRegexCompileFlags cflags, GRegexMatchFlags mflags) {
|
||||||
GError *err{};
|
GError *err{};
|
||||||
if (rx_ = g_regex_new(ptrn, cflags, mflags, &err); !rx_)
|
if (rx_ = g_regex_new(ptrn, cflags, mflags, &err); !rx_)
|
||||||
throw Err(Error::Code::InvalidArgument, &err,
|
throw Error{Error::Code::InvalidArgument, &err,
|
||||||
"invalid regexp: '{}'", ptrn);
|
"invalid regexp: '{}'", ptrn};
|
||||||
}
|
}
|
||||||
|
|
||||||
GRegex *rx_;
|
GRegex *rx_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline std::string format_as(const Regex& rx) {
|
static inline std::string format_as(const Regex& rx) {
|
||||||
|
|
|
@ -319,8 +319,6 @@ test_summarize(void)
|
||||||
"fortification by the Bais raja Sathna. ");
|
"fortification by the Bais raja Sathna. ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char* argv[])
|
main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
|
|
@ -251,7 +251,7 @@ test_view_plain()
|
||||||
|
|
||||||
// silly hack to avoid locale diffs
|
// silly hack to avoid locale diffs
|
||||||
auto rx = unwrap(Regex::make("^Date:.*", G_REGEX_MULTILINE));
|
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());
|
g_assert_true(output.standard_err.empty());
|
||||||
assert_equal(output.standard_out,
|
assert_equal(output.standard_out,
|
||||||
|
@ -273,7 +273,7 @@ test_view_html()
|
||||||
auto output{*res};
|
auto output{*res};
|
||||||
|
|
||||||
auto rx = unwrap(Regex::make("^Date:.*", G_REGEX_MULTILINE));
|
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());
|
g_assert_true(output.standard_err.empty());
|
||||||
assert_equal(output.standard_out,
|
assert_equal(output.standard_out,
|
||||||
|
|
Loading…
Reference in New Issue