summaryrefslogtreecommitdiff
path: root/scm
diff options
context:
space:
mode:
authorDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2025-06-11 19:50:38 +0300
committerDirk-Jan C. Binnema <djcb@djcbsoftware.nl>2025-06-19 14:55:33 +0300
commitcaeb2ab3c91eb7d88119381b93cada85b39cb4be (patch)
treec74e7cf02594ddc56dd505cd3b7b0c55b1de95d8 /scm
parent527d9322e93c14685aaba61d49125483397cd948 (diff)
scm: add some unit-tests
Some basic unit tests. More will follow.
Diffstat (limited to 'scm')
-rw-r--r--scm/meson.build13
-rw-r--r--scm/mu-scm-test.scm76
-rw-r--r--scm/mu-scm.cc55
3 files changed, 144 insertions, 0 deletions
diff --git a/scm/meson.build b/scm/meson.build
index 6681d5a..a20785a 100644
--- a/scm/meson.build
+++ b/scm/meson.build
@@ -56,3 +56,16 @@ if makeinfo.found()
meson.add_install_script(install_info_script, infodir, 'mu-scm.info')
endif
endif
+
+if not get_option('tests').disabled()
+
+ srcdir=meson.current_source_dir()
+ def_srcdir='-DMU_SCM_SRCDIR="' + srcdir + '"'
+
+ test('test-scm',
+ executable('test-scm',
+ 'mu-scm.cc',
+ install: false,
+ dependencies: [mu_scm_dep],
+ cpp_args: [mu_scm_dir_arg, def_srcdir, '-DBUILD_TESTS']))
+endif
diff --git a/scm/mu-scm-test.scm b/scm/mu-scm-test.scm
new file mode 100644
index 0000000..d6c9325
--- /dev/null
+++ b/scm/mu-scm-test.scm
@@ -0,0 +1,76 @@
+;; unit tests
+
+(use-modules (mu) (srfi srfi-64))
+
+(define (test-basic)
+ (test-begin "test-basic")
+
+ (test-equal "mcount" 19 (mcount))
+ (test-equal "cfind" 29 (length (cfind "")))
+ (test-equal "mfind" 19 (length (mfind "")))
+
+ (test-end "test-basic"))
+
+(define (test-basic-mfind)
+
+ (test-begin "test-basic-mfind")
+
+ (let ((msg (car (mfind ""))))
+ ;; size
+ (test-equal 490 (size msg))
+ ;; message-id
+ (test-equal "abcd$efgh@example.com" (message-id msg))
+ ;; subject
+ (test-equal "Greetings from Lothlórien" (subject msg))
+ ;; from
+ (test-equal 1 (length (from msg)))
+ (let ((sender (car (from msg))))
+ (test-equal "Frodo Baggins" (assoc-ref sender 'name))
+ (test-equal "frodo@example.com" (assoc-ref sender 'email)))
+ ;; to
+ (test-equal 1 (length (to msg)))
+ (let ((recip (car (to msg))))
+ (test-equal "Bilbo Baggins" (assoc-ref recip 'name))
+ (test-equal "bilbo@anotherexample.com" (assoc-ref recip 'email)))
+
+ ;; no date
+ (test-assert (not (date msg)))
+
+ ;; flags
+ (test-equal '(unread) (flags msg))
+ (test-assert (unread? msg))
+ (test-assert (not (seen? msg)))
+ (test-assert (not (new? msg))))
+
+ (test-end "test-basic-mfind"))
+
+(define (test-mfind)
+ (test-begin "test-mfind")
+ (let ((msg (car (mfind "" #:sort-field 'date #:reverse? #t))))
+
+ (test-equal "test with multi to and cc" (subject msg) )
+ (test-equal "2016-05-15T16:57:25" (time-t->iso-date (date msg))))
+
+ (test-end "test-mfind"))
+
+(define (test-helpers)
+ (test-begin "test-helpers")
+ (test-equal 1750077792 (iso-date->time-t "2025-06-16T12:43:12"))
+ (test-equal 1750075200 (iso-date->time-t "2025-06-16T12"))
+
+ (test-equal "2025-06-16T12:43:12" (time-t->iso-date 1750077792))
+ (test-equal " " (time-t->iso-date #f))
+ (test-end "test-helpers"))
+
+(define* (main _ #:rest args)
+ (let ((runner (test-runner-simple)))
+ (test-with-runner runner
+ (test-begin "mu-scm-tests")
+
+ (test-basic)
+ (test-basic-mfind)
+ (test-mfind)
+ (test-helpers)
+
+ (test-end "mu-scm-tests")
+ (exit (test-runner-fail-count runner)))))
diff --git a/scm/mu-scm.cc b/scm/mu-scm.cc
index 10526e3..374716c 100644
--- a/scm/mu-scm.cc
+++ b/scm/mu-scm.cc
@@ -158,3 +158,58 @@ Mu::Scm::run(const Mu::Scm::Config& conf) {
return Ok();
}
+
+
+
+
+#ifdef BUILD_TESTS
+
+/*
+ * Tests.
+ *
+ */
+#include <config.h>
+#include <mu-store.hh>
+#include "utils/mu-test-utils.hh"
+
+
+static void
+test_scm_script()
+{
+ TempDir tempdir{};
+ const auto MuTestMaildir{ Mu::canonicalize_filename(MU_TESTMAILDIR, "/")};
+ auto store{Store::make_new(tempdir.path(), MuTestMaildir)};
+ assert_valid_result(store);
+
+ {
+ const auto res = store->indexer().start({}, true/*block*/);
+ g_assert_true(res);
+ }
+
+ Mu::Options opts{};
+ opts.scm.script_path = join_paths(MU_SCM_SRCDIR, "mu-scm-test.scm");
+
+ Mu::Scm::Config scm_conf {
+ .store = *store,
+ .options = opts
+ };
+
+ {
+ const auto res = Mu::Scm::run(scm_conf);
+ assert_valid_result(res);
+ }
+}
+
+int
+main(int argc, char* argv[])
+{
+ ::setenv("MU_SCM_DIR", MU_SCM_SRCDIR, 1);
+
+ mu_test_init(&argc, &argv);
+
+ g_test_add_func("/scm/script", test_scm_script);
+
+ return g_test_run();
+}
+
+#endif /*BUILD_TESTS*/