From 9bddf2f0b915154ea7e4167a71a9915d6fc64c81 Mon Sep 17 00:00:00 2001 From: djcb Date: Thu, 12 Jul 2012 21:19:59 +0300 Subject: [PATCH] * guile: beginnings of some unit tests --- configure.ac | 17 ++-- guile/tests/Makefile.am | 49 ++++++++++++ guile/tests/test-mu-guile.c | 144 ++++++++++++++++++++++++++++++++++ guile/tests/test-mu-guile.scm | 101 ++++++++++++++++++++++++ 4 files changed, 305 insertions(+), 6 deletions(-) create mode 100644 guile/tests/Makefile.am create mode 100644 guile/tests/test-mu-guile.c create mode 100755 guile/tests/test-mu-guile.scm diff --git a/configure.ac b/configure.ac index d0270b0a..60e058e8 100644 --- a/configure.ac +++ b/configure.ac @@ -252,8 +252,6 @@ AM_CONDITIONAL(BUILD_WIDGETS, [test "x$build_webkit" = "xyes" -a "x$have_gio" = - - ############################################################################### # check for guile 2.0 & guile-snarf build_guile=no @@ -265,13 +263,20 @@ AS_IF([test "x$enable_guile" != "xno"], [ AC_SUBST(GUILE_CFLAGS) AC_SUBST(GUILE_LIBS) + GUILE_EXECDIR="`${PKG_CONFIG} guile-2.0 --variable=exec_prefix`/bin" + AS_IF([test "x$build_guile" = "xyes"],[ - AC_PATH_PROG(GUILE_SNARF, [guile-snarf], [], [$PATH]) - AS_IF([test "x$GUILE_SNARF" != "x"], - [AC_DEFINE_UNQUOTED([GUILE_SNARF], ["$GUILE_SNARF"],[Path to guile-snarf]) - GUILE_SITEDIR="`${PKG_CONFIG} guile-2.0 --variable=sitedir`"], + AC_PATH_PROG(GUILE_BINARY, [guile], [], [$GUILE_EXECDIR]) + AS_IF([test "x$GUILE_BINARY" != "x"], + [AC_DEFINE_UNQUOTED([GUILE_BINARY],["$GUILE_BINARY"],[Path to the guile binary])], [AC_MSG_WARN([cannot find guile-snarf])])]) + AS_IF([test "x$build_guile" = "xyes"],[ + AC_PATH_PROG(GUILE_SNARF, [guile-snarf], [], [$GUILE_EXECDIR]) + AS_IF([test "x$GUILE_SNARF" != "x"], + [AC_DEFINE_UNQUOTED([GUILE_SNARF],["$GUILE_SNARF"],[Path to guile-snarf]) + GUILE_SITEDIR="`${PKG_CONFIG} guile-2.0 --variable=sitedir`"], + [AC_MSG_WARN([cannot find guile-snarf])])]) AC_SUBST(GUILE_SITEDIR)]) AS_IF([test "x$build_guile" = "xyes" -a "x$GUILE_SNARF" != "x"], diff --git a/guile/tests/Makefile.am b/guile/tests/Makefile.am new file mode 100644 index 00000000..e374d85d --- /dev/null +++ b/guile/tests/Makefile.am @@ -0,0 +1,49 @@ +# Copyright (C) 2008-2011 Dirk-Jan C. Binnema +## +## This program is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by the +## Free Software Foundation; either version 3, 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 General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software Foundation, +## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +include $(top_srcdir)/gtest.mk + +INCLUDES=$(XAPIAN_CXXFLAGS) \ + $(GMIME_CFLAGS) \ + $(GLIB_CFLAGS) \ + -I ${top_srcdir} \ + -I ${top_srcdir}/lib \ + -I ${top_srcdir}/lib/tests \ + -DMU_TESTMAILDIR=\"${top_srcdir}/lib/tests/testdir\" \ + -DMU_TESTMAILDIR2=\"${top_srcdir}/lib/tests/testdir2\" \ + -DMU_TESTMAILDIR3=\"${top_srcdir}/lib/tests/testdir3\" \ + -DMU_PROGRAM=\"${abs_top_builddir}/mu/mu\" \ + -DMU_GUILE_MODULE_PATH=\"${top_srcdir}/guile/mu\" \ + -DABS_CURDIR=\"${abs_builddir}\" \ + -DABS_SRCDIR=\"${abs_srcdir}\" + +# don't use -Werror, as it might break on other compilers +# use -Wno-unused-parameters, because some callbacks may not +# really need all the params they get +AM_CFLAGS=-Wall -Wextra -Wno-unused-parameter -Wdeclaration-after-statement +AM_CXXFLAGS=-Wall -Wextra -Wno-unused-parameter + +noinst_PROGRAMS= $(TEST_PROGS) + +TEST_PROGS += test-mu-guile +test_mu_guile_SOURCES= test-mu-guile.c dummy.cc +test_mu_guile_LDADD=${top_builddir}/lib/tests/libtestmucommon.la + +# we need to use dummy.cc to enforce c++ linking... +BUILT_SOURCES= \ + dummy.cc +dummy.cc: + touch dummy.cc diff --git a/guile/tests/test-mu-guile.c b/guile/tests/test-mu-guile.c new file mode 100644 index 00000000..17036bf8 --- /dev/null +++ b/guile/tests/test-mu-guile.c @@ -0,0 +1,144 @@ +/* -*- mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- +** +** Copyright (C) 2012 Dirk-Jan C. Binnema +** +** This program is free software; you can redistribute it and/or modify it +** under the terms of the GNU General Public License as published by the +** Free Software Foundation; either version 3, 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 General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software Foundation, +** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +** +*/ + +#if HAVE_CONFIG_H +#include "config.h" +#endif /*HAVE_CONFIG_H*/ + +#include +#include + +#include "../mu-query.h" + +#include +#include +#include + +#include "test-mu-common.h" +#include "mu-store.h" + + +/* tests for the command line interface, uses testdir2 */ + +static gchar* +fill_database (void) +{ + gchar *cmdline, *tmpdir; + GError *err; + + tmpdir = test_mu_common_get_random_tmpdir(); + cmdline = g_strdup_printf ("%s index --muhome=%s --maildir=%s" + " --quiet", + MU_PROGRAM, + tmpdir, MU_TESTMAILDIR2); + if (g_test_verbose()) + g_print ("%s\n", cmdline); + + err = NULL; + if (!g_spawn_command_line_sync (cmdline, NULL, NULL, + NULL, &err)) { + g_printerr ("Error: %s\n", err ? err->message : "?"); + g_assert (0); + } + + g_free (cmdline); + return tmpdir; +} + + +static void +test_something (const char *what) +{ + char *dir, *cmdline, *output, *erroutput; + gint result; + + dir = fill_database (); + cmdline = g_strdup_printf ( + "%s -q -L %s -e main %s/test-mu-guile.scm --muhome=%s --test=%s", + GUILE_BINARY, + MU_GUILE_MODULE_PATH, + ABS_SRCDIR, + dir, + what); + + if (g_test_verbose ()) + g_print ("cmdline: %s\n", cmdline); + + output = erroutput = NULL; + g_assert (g_spawn_command_line_sync (cmdline, + &output, &erroutput, + &result, NULL)); + g_assert (result == 0); + + if (g_test_verbose ()) { + if (output) + g_print ("stdout: %s\n", output); + if (erroutput) + g_print ("stderr: %s\n", erroutput); + } + + g_free (output); + g_free (erroutput); + g_free (dir); + g_free (cmdline); +} + +static void +test_mu_guile_queries (void) +{ + test_something ("queries"); +} + +static void +test_mu_guile_messages (void) +{ + test_something ("message"); +} + +static void +test_mu_guile_stats (void) +{ + test_something ("stats"); +} + + +int +main (int argc, char *argv[]) +{ + int rv; + g_test_init (&argc, &argv, NULL); + + if (!set_en_us_utf8_locale()) + return 0; /* don't error out... */ + + g_test_add_func ("/guile/queries", test_mu_guile_queries); + g_test_add_func ("/guile/message", test_mu_guile_messages); + g_test_add_func ("/guile/stats", test_mu_guile_stats); + + + g_log_set_handler (NULL, + G_LOG_LEVEL_MASK | G_LOG_LEVEL_WARNING| + G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION, + (GLogFunc)black_hole, NULL); + + rv = g_test_run (); + + return rv; +} diff --git a/guile/tests/test-mu-guile.scm b/guile/tests/test-mu-guile.scm new file mode 100755 index 00000000..65b20770 --- /dev/null +++ b/guile/tests/test-mu-guile.scm @@ -0,0 +1,101 @@ +#!/bin/sh +exec guile -e main -s $0 $@ +!# + +;; Copyright (C) 2012 Dirk-Jan C. Binnema +;; +;; This program is free software; you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by the +;; Free Software Foundation; either version 3, 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 General Public License for more details. +;; + +;; You should have received a copy of the GNU General Public License +;; along with this program; if not, write to the Free Software Foundation, +;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +(setlocale LC_ALL "") + +(use-modules (ice-9 getopt-long) (ice-9 optargs) (ice-9 popen) (ice-9 format)) +(use-modules (mu) (mu message) (mu stats) (mu plot)) + +(define (n-results-or-exit query n) + "Run QUERY, and exit 1 if the number of results != N." + (let ((lst (mu:message-list query))) + (if (not (= (length lst) n)) + (begin + (simple-format (current-error-port) "Query: \"~A\"; expected ~A, got ~A\n" + query n (length lst)) + (exit 1))))) + +(define (test-queries) + "Test a bunch of queries (or die trying)." + + (n-results-or-exit "hello" 1) + (n-results-or-exit "f:john fruit" 1) + (n-results-or-exit "f:soc@example.com" 1) + (n-results-or-exit "t:alki@example.com" 1) + (n-results-or-exit "t:alcibiades" 1) + (n-results-or-exit "f:soc@example.com OR f:john" 2) + (n-results-or-exit "f:soc@example.com OR f:john OR t:edmond" 3) + (n-results-or-exit "t:julius" 1) + (n-results-or-exit "s:dude" 1) + (n-results-or-exit "t:dantès" 1) + (n-results-or-exit "file:sittingbull.jpg" 1) + (n-results-or-exit "file:custer.jpg" 1) + (n-results-or-exit "file:custer.*" 1) + (n-results-or-exit "j:sit*" 1) + (n-results-or-exit "mime:image/jpeg" 1) + (n-results-or-exit "mime:text/plain" 12) + (n-results-or-exit "y:text*" 12) + (n-results-or-exit "y:image*" 1) + (n-results-or-exit "mime:message/rfc822" 2)) + + +(define (str-equal-or-exit s1 s2) + "S1 == S2 or exit 1." + ;; (format #t "'~A' <=> '~A'\n" s1 s2) + (if (not (string= s1 s2)) + (begin + (simple-format (current-error-port) "Message: expected \"~A\", got \"~A\"\n" + s1 s2) + (exit 1)))) + +(define (test-message) + "Test functions for a particular message." + (let ((msg (car (mu:message-list "hello")))) + (str-equal-or-exit (mu:subject msg) "Fwd: rfc822") + (str-equal-or-exit (mu:to msg) "martin") + (str-equal-or-exit (mu:from msg) "foobar "))) + + +(define (test-stats) + "Test statistical functions." + ) + + +(define (main args) + + (let* ((optionspec '((muhome (value #t)) + (test (value #t)))) + (options (getopt-long args optionspec)) + (muhome (option-ref options 'muhome #f)) + (test (option-ref options 'test #f))) + + (mu:initialize muhome) + + (if test + (cond + ((string= test "queries") (test-queries)) + ((string= test "message") (test-message)) + ((string= test "stats") (test-stats)) + (#t (exit 1)))))) + + +;; Local Variables: +;; mode: scheme +;; End: