From 1f0342a91f4073e8106e5ad17bafe1d640211f10 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Fri, 28 Jul 2023 19:43:46 +0300 Subject: [PATCH] mu-view: add unit-test --- lib/utils/mu-test-utils.hh | 2 - mu/meson.build | 7 +- mu/mu-cmd-view.cc | 131 +++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 3 deletions(-) diff --git a/lib/utils/mu-test-utils.hh b/lib/utils/mu-test-utils.hh index 6537046f..b61a4af1 100644 --- a/lib/utils/mu-test-utils.hh +++ b/lib/utils/mu-test-utils.hh @@ -129,8 +129,6 @@ struct TempDir { * Path to the temporary directory * * @return the path. - * - * */ const std::string& path() {return path_; } private: diff --git a/mu/meson.build b/mu/meson.build index e8602622..7bc3956a 100644 --- a/mu/meson.build +++ b/mu/meson.build @@ -62,6 +62,11 @@ test('test-cmd-remove', cpp_args: ['-DBUILD_TESTS'], dependencies: [glib_dep, lib_mu_dep])) - +test('test-cmd-view', + executable('test-cmd-view', + 'mu-cmd-view.cc', + install: false, + cpp_args: ['-DBUILD_TESTS'], + dependencies: [glib_dep, lib_mu_dep])) subdir('tests') diff --git a/mu/mu-cmd-view.cc b/mu/mu-cmd-view.cc index 8d646dfb..73bb4957 100644 --- a/mu/mu-cmd-view.cc +++ b/mu/mu-cmd-view.cc @@ -16,6 +16,7 @@ ** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ** */ +#include #include "mu-cmd.hh" @@ -199,3 +200,133 @@ Mu::mu_cmd_view(const Options& opts) } return Ok(); } + + +#ifdef BUILD_TESTS +/* + * Tests. + * + */ + +#include /* Definition of AT_* constants */ +#include +#include +#include "utils/mu-test-utils.hh" + +static constexpr std::string_view test_msg = +R"(From: Test +To: abc@example.com +Date: Mon, 23 May 2011 10:53:45 +0200 +Subject: vla +MIME-Version: 1.0 +Content-Type: multipart/alternative; + boundary="_=aspNetEmail=_5ed4592191214c7a99bd7f6a3a0f077d" +Message-ID: <10374608.109906.11909.20115aabbccdd.MSGID@mailinglijst.nl> + +--_=aspNetEmail=_5ed4592191214c7a99bd7f6a3a0f077d +Content-Type: text/plain; charset="iso-8859-15" +Content-Transfer-Encoding: quoted-printable + +text + +--_=aspNetEmail=_5ed4592191214c7a99bd7f6a3a0f077d +Content-Type: text/html; charset="iso-8859-15" +Content-Transfer-Encoding: quoted-printable + +html + +--_=aspNetEmail=_5ed4592191214c7a99bd7f6a3a0f077d-- +)"; + + +static std::string msgpath; + +static void +test_view_plain() +{ + auto res = run_command({MU_PROGRAM, "view", msgpath}); + assert_valid_command(res); + auto output{*res}; + + g_assert_true(output.standard_err.empty()); + assert_equal(output.standard_out, +R"(From: Test +To: abc@example.com +Subject: vla +Date: 2011-05-23T10:53:45 CEST + +text +)"); + +} + +static void +test_view_html() +{ + auto res = run_command({MU_PROGRAM, "view", "--format=html", msgpath}); + assert_valid_command(res); + auto output{*res}; + + g_assert_true(output.standard_err.empty()); + assert_equal(output.standard_out, +R"(From: Test +To: abc@example.com +Subject: vla +Date: 2011-05-23T10:53:45 CEST + +html +)"); + +} + +static void +test_view_sexp() +{ + auto res = run_command({MU_PROGRAM, "view", "--format=sexp", msgpath}); + assert_valid_command(res); + auto output{*res}; + + g_assert_true(output.standard_err.empty()); + + // Note: :path, :changed (file ctime) change per run. + struct stat statbuf{}; + g_assert_true(::stat(msgpath.c_str(), &statbuf) == 0); + + const auto expected = mu_format( + R"((:path "{}" :size 638 :changed ({} {} 0) :date (19930 8345 0) :flags (unread) :from ((:email "test@example.com" :name "Test")) :message-id "10374608.109906.11909.20115aabbccdd.MSGID@mailinglijst.nl" :priority normal :subject "vla" :to ((:email "abc@example.com"))) +)", + msgpath, + statbuf.st_ctime >> 16, + statbuf.st_ctime & 0xffff); + + assert_equal(output.standard_out, expected); +} + +int +main(int argc, char* argv[]) try { + + TempDir tmpdir{false}; + msgpath = join_paths(tmpdir.path(), "test-message.txt"); + std::ofstream strm{msgpath}; + strm.write(test_msg.data(), test_msg.size()); + strm.close(); + g_assert_true(strm.good()); + + set_tz("Europe/Amsterdam"); + + mu_test_init(&argc, &argv); + + g_test_add_func("/cmd/view/plain", test_view_plain); + g_test_add_func("/cmd/view/html", test_view_html); + g_test_add_func("/cmd/view/sexp", test_view_sexp); + + return g_test_run(); + +} catch (const Error& e) { + mu_printerrln("{}", e.what()); + return 1; +} catch (...) { + mu_printerrln("caught exception"); + return 1; +} +#endif /*BUILD_TESTS*/