lib: improve test coverage

Add a bunch of tests
This commit is contained in:
Dirk-Jan C. Binnema
2022-06-02 21:02:11 +03:00
parent 63521300a3
commit 13f0e24241
12 changed files with 275 additions and 54 deletions

View File

@ -169,6 +169,30 @@ test_encode()
}
static void
test_sender()
{
Contact c{"aa@example.com", "Anders Ångström",
Contact::Type::Sender, 54321};
assert_equal(c.email, "aa@example.com");
assert_equal(c.name, "Anders Ångström");
g_assert_false(c.personal);
g_assert_cmpuint(c.frequency,==,1);
g_assert_cmpuint(c.message_date,==,54321);
g_assert_false(!!c.field_id());
}
static void
test_misc()
{
g_assert_false(!!contact_type_from_field_id(Field::Id::Subject));
}
int
main(int argc, char* argv[])
{
@ -180,6 +204,9 @@ main(int argc, char* argv[])
g_test_add_func("/message/contact/ctor-cleanup", test_ctor_cleanup);
g_test_add_func("/message/contact/encode", test_encode);
g_test_add_func("/message/contact/sender", test_sender);
g_test_add_func("/message/contact/misc", test_misc);
return g_test_run();
}
#endif /*BUILD_TESTS*/

View File

@ -124,12 +124,17 @@ Message::Message(const std::string& text, const std::string& path,
Message::Options opts):
priv_{std::make_unique<Private>(opts)}
{
if (text.empty())
throw Error{Error::Code::InvalidArgument, "text must not be empty"};
if (!path.empty()) {
auto xpath{to_string_opt_gchar(g_canonicalize_filename(path.c_str(), {}))};
if (xpath)
priv_->doc.add(Field::Id::Path, std::move(xpath.value()));
}
priv_->ctime = ::time({});
priv_->doc.add(Field::Id::Size, static_cast<int64_t>(text.size()));
init_gmime();
@ -727,9 +732,10 @@ fill_document(Message::Private& priv)
case Field::Id::XBodyHtml:
doc.add(field.id, priv.body_html);
break;
/* ignore */
/* LCOV_EXCL_START */
case Field::Id::_count_:
break;
/* LCOV_EXCL_STOP */
}
#pragma GCC diagnostic pop
@ -739,27 +745,21 @@ fill_document(Message::Private& priv)
Option<std::string>
Message::header(const std::string& header_field) const
{
if (!load_mime_message())
return Nothing;
load_mime_message();
return priv_->mime_msg->header(header_field);
}
Option<std::string>
Message::body_text() const
{
if (!load_mime_message())
return {};
load_mime_message();
return priv_->body_txt;
}
Option<std::string>
Message::body_html() const
{
if (!load_mime_message())
return {};
load_mime_message();
return priv_->body_html;
}

View File

@ -43,8 +43,8 @@ public:
enum struct Options {
None = 0, /**< Defaults */
Decrypt = 1 << 0, /**< Attempt to decrypt */
RetrieveKeys = 1 << 1, /**< Auto-retrieve crypto keys (implies network
* access) */
RetrieveKeys = 1 << 1, /**< Auto-retrieve crypto keys (implies network
* access) */
};
/**
@ -77,9 +77,13 @@ public:
return Ok(Message{path,opts});
} catch (Error& err) {
return Err(err);
} catch (...) {
return Err(Mu::Error(Error::Code::Message, "failed to create message"));
}
/* LCOV_EXCL_START */
catch (...) {
return Err(Mu::Error(Error::Code::Message,
"failed to create message from path"));
}
/* LCOV_EXCL_STOP */
/**
* Construct a message based on a Xapian::Document
@ -92,13 +96,18 @@ public:
return Ok(Message{std::move(doc)});
} catch (Error& err) {
return Err(err);
} catch (...) {
return Err(Mu::Error(Error::Code::Message, "failed to create message"));
}
/* LCOV_EXCL_START */
catch (...) {
return Err(Mu::Error(Error::Code::Message,
"failed to create message from document"));
}
/* LCOV_EXCL_STOP */
/**
* Construct a message from a string. This is mostly useful for testing.
*
* @param text message text
* @param path path to message - optional; path does not have to exist.
* @param opts options
@ -111,10 +120,13 @@ public:
return Ok(Message{text, path, opts});
} catch (Error& err) {
return Err(err);
} catch (...) {
}
/* LCOV_EXCL_START */
catch (...) {
return Err(Mu::Error(Error::Code::Message,
"failed to create message from text"));
}
/* LCOV_EXCL_STOP */
/**
* DTOR
@ -233,9 +245,11 @@ public:
}
/**
* get the last change-time (ctime) for this message
* get the last change-time this message. For path/document-based
* messages this corresponds with the ctime of the underlying file; for
* the text-based ones (as used for testing) it is the creation time.
*
* @return chnage time or 0 if unknown
* @return last-change time or 0 if unknown
*/
::time_t changed() const {
return static_cast<::time_t>(document().integer_value(Field::Id::Changed));

View File

@ -65,7 +65,6 @@ goto * instructions[pOp->opcode];
test_message_1,
"/home/test/Maildir/inbox/cur/1649279256.107710_1.evergrey:2,S")};
g_assert_true(!!message);
assert_equal(message->path(),
"/home/test/Maildir/inbox/cur/1649279256.107710_1.evergrey:2,S");
g_assert_true(message->maildir().empty());
@ -99,6 +98,10 @@ goto * instructions[pOp->opcode];
g_assert_true(message->priority() == Priority::Low);
g_assert_cmpuint(message->size(),==,::strlen(test_message_1));
/* text-based message use time({}) as their changed-time */
g_assert_cmpuint(message->changed() - ::time({}), >=, 0);
g_assert_cmpuint(message->changed() - ::time({}), <=, 2);
g_assert_true(message->references().empty());
assert_equal(message->subject(),
@ -177,7 +180,7 @@ World!
auto message{Message::make_from_text(msg_text)};
g_assert_true(!!message);
g_assert_true(message->has_mime_message());
g_assert_true(message->path().empty());
g_assert_true(message->bcc().empty());
@ -202,6 +205,10 @@ World!
g_assert_true(message->priority() == Priority::Normal);
g_assert_cmpuint(message->size(),==,::strlen(msg_text));
/* text-based message use time({}) as their changed-time */
g_assert_cmpuint(message->changed() - ::time({}), >=, 0);
g_assert_cmpuint(message->changed() - ::time({}), <=, 2);
assert_equal(message->subject(), "ättächmeñts");
const auto cache_path{message->cache_path()};
@ -565,6 +572,20 @@ Moi,
}
static void
test_message_fail ()
{
{
const auto msg = Message::make_from_path("/root/non-existent-path-12345");
g_assert_false(!!msg);
}
{
const auto msg = Message::make_from_text("", "");
g_assert_false(!!msg);
}
}
int
main(int argc, char* argv[])
{
@ -582,6 +603,9 @@ main(int argc, char* argv[])
test_message_multipart_mixed_rfc822);
g_test_add_func("/message/message/detect-attachment",
test_message_detect_attachment);
g_test_add_func("/message/message/fail",
test_message_fail);
return g_test_run();
}