Add tests.
authorRicardo Wurmus <rekado@elephly.net>
Wed, 22 Apr 2020 15:37:33 +0000 (17:37 +0200)
committerRicardo Wurmus <rekado@elephly.net>
Wed, 22 Apr 2020 15:37:33 +0000 (17:37 +0200)
.gitignore
Makefile.am
tests/xapian.scm [new file with mode: 0644]

index 6099f1f..03b214c 100644 (file)
@@ -11,3 +11,5 @@ configure
 mumi/config.scm
 scripts/mumi
 pre-inst-env
+*.log
+*.trs
index 1f54042..61ab603 100644 (file)
@@ -52,3 +52,24 @@ SOURCES =                                                    \
   mumi/debbugs.scm                                     \
   mumi/xapian.scm
 
+TEST_EXTENSIONS = .scm
+
+SCM_TESTS = \
+  tests/xapian.scm
+
+TESTS = $(SCM_TESTS)
+
+EXTRA_DIST += $(TESTS)
+
+AM_TESTS_ENVIRONMENT = abs_top_srcdir="$(abs_top_srcdir)" GUILE_AUTO_COMPILE=0
+
+SCM_LOG_DRIVER =                                          \
+  $(top_builddir)/pre-inst-env                            \
+  $(GUILE) --no-auto-compile -e main                      \
+      $(top_srcdir)/build-aux/test-driver.scm
+
+AM_SCM_LOG_DRIVER_FLAGS = --brief=yes
+
+EXTRA_DIST +=                                  \
+  pre-inst-env.in                              \
+  build-aux/test-driver.scm
diff --git a/tests/xapian.scm b/tests/xapian.scm
new file mode 100644 (file)
index 0000000..607fdb8
--- /dev/null
@@ -0,0 +1,71 @@
+;;; mumi -- Mediocre, uh, mail interface
+;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
+;;;
+;;; This program is free software: you can redistribute it and/or
+;;; modify it under the terms of the GNU Affero General Public License
+;;; as published by the Free Software Foundation, either version 3 of
+;;; the License, or (at your option) any later version.
+;;;
+;;; This program is distributed in the hope that it will be useful,
+;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;;; Affero General Public License for more details.
+;;;
+;;; You should have received a copy of the GNU Affero General Public
+;;; License along with this program.  If not, see
+;;; <http://www.gnu.org/licenses/>.
+
+(define-module (test-xapian)
+  #:use-module (mumi xapian)
+  #:use-module (srfi srfi-19)
+  #:use-module (srfi srfi-64))
+
+(define-syntax-rule (mock (module proc replacement) body ...)
+  "Within BODY, replace the definition of PROC from MODULE with the definition
+given by REPLACEMENT."
+  (let* ((m (resolve-module 'module))
+         (original (module-ref m 'proc)))
+    (dynamic-wind
+      (lambda () (module-set! m 'proc replacement))
+      (lambda () body ...)
+      (lambda () (module-set! m 'proc original)))))
+
+(test-begin "xapian")
+
+(define (time->datestamp time)
+  (date->string (time-utc->date time) "~Y~m~d"))
+
+(define sanitize
+  (@@ (mumi xapian) sanitize-date-range))
+
+(test-equal "sanitize-date-range: turns \"today\" into datestamp"
+  (sanitize "date:today..today")
+  (let ((today (time->datestamp (current-time))))
+    (format #f "date:~a..~a" today today)))
+
+(test-equal "sanitize-date-range: turns \"now\" into datestamp"
+  (sanitize "date:today..now")
+  (let ((today (time->datestamp (current-time))))
+    (format #f "date:~a..~a" today today)))
+
+(test-equal "sanitize-date-range: turns \"yesterday\" into datestamp"
+  (sanitize "date:yesterday..today")
+  (let* ((today (current-time))
+         (yesterday (subtract-duration today (make-time time-duration 0 (* 60 60 24)))))
+    (format #f "date:~a..~a"
+            (time->datestamp yesterday)
+            (time->datestamp today))))
+
+(test-equal "sanitize-date-range: pads short dates"
+  (sanitize "date:20..19")
+  "date:20000000..19000000")
+
+(test-equal "sanitize-date-range: turns 1m into datestamp one month ago"
+  (sanitize "date:1m..now")
+  (let* ((today (current-time))
+         (1m (subtract-duration today (make-time time-duration 0 (* 60 60 24 30)))))
+    (format #f "date:~a..~a"
+            (time->datestamp 1m)
+            (time->datestamp today))))
+
+(test-end "xapian")