diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am index 98aaabc9..b0dc6cbb 100644 --- a/lib/tests/Makefile.am +++ b/lib/tests/Makefile.am @@ -78,6 +78,10 @@ TEST_PROGS += test-mu-container test_mu_container_SOURCES= test-mu-container.c dummy.cc test_mu_container_LDADD= libtestmucommon.la +TEST_PROGS += test-mu-contacts +test_mu_contacts_SOURCES= test-mu-contacts.cc +test_mu_contacts_LDADD= libtestmucommon.la + # we need to use dummy.cc to enforce c++ linking... BUILT_SOURCES= \ dummy.cc diff --git a/lib/tests/test-mu-contacts.cc b/lib/tests/test-mu-contacts.cc new file mode 100644 index 00000000..141f8b94 --- /dev/null +++ b/lib/tests/test-mu-contacts.cc @@ -0,0 +1,91 @@ +/* +** Copyright (C) 2019 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 "config.h" + +#include +#include "test-mu-common.h" +#include "mu-contacts.hh" + +static void +test_mu_contacts_01() +{ + Mu::Contacts contacts (""); + + g_assert_true (contacts.empty()); + g_assert_cmpuint (contacts.size(), ==, 0); + + contacts.add(std::move(Mu::ContactInfo ("Foo ", + "foo.bar@example.com", "Foo", + false, 12345))); + g_assert_false (contacts.empty()); + g_assert_cmpuint (contacts.size(), ==, 1); + + contacts.add(std::move(Mu::ContactInfo ("Cuux ", + "cuux@example.com", "Cuux", true, + 54321))); + + g_assert_cmpuint (contacts.size(), ==, 2); + + contacts.add(std::move(Mu::ContactInfo ("foo.bar@example.com", + "foo.bar@example.com", "Foo", + false, 77777))); + g_assert_cmpuint (contacts.size(), ==, 2); + + contacts.add(std::move(Mu::ContactInfo ("Foo.Bar@Example.Com", + "Foo.Bar@Example.Com", "Foo", + false, 88888))); + g_assert_cmpuint (contacts.size(), ==, 2); + // note: replaces first. + + { + const auto info = contacts._find("bla@example.com"); + g_assert_false (info); + } + + + { + const auto info = contacts._find("foo.BAR@example.com"); + g_assert_true (info); + + g_assert_cmpstr(info->email.c_str(), ==, "Foo.Bar@Example.Com"); + } + + contacts.clear(); + g_assert_true (contacts.empty()); + g_assert_cmpuint (contacts.size(), ==, 0); +} + + + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/mu-contacts/01", test_mu_contacts_01); + + g_log_set_handler (NULL, + (GLogLevelFlags) + (G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION), + (GLogFunc)black_hole, NULL); + + return g_test_run (); +} diff --git a/mu/tests/Makefile.am b/mu/tests/Makefile.am index 645cf517..dc1d10f6 100644 --- a/mu/tests/Makefile.am +++ b/mu/tests/Makefile.am @@ -50,10 +50,6 @@ TEST_PROGS += test-mu-query test_mu_query_SOURCES= test-mu-query.c dummy.cc test_mu_query_LDADD=${top_builddir}/lib/tests/libtestmucommon.la -TEST_PROGS += test-mu-contacts -test_mu_contacts_SOURCES= test-mu-contacts.c dummy.cc -test_mu_contacts_LDADD=${top_builddir}/lib/tests/libtestmucommon.la - TEST_PROGS += test-mu-cmd test_mu_cmd_SOURCES= test-mu-cmd.c dummy.cc test_mu_cmd_LDADD=${top_builddir}/lib/tests/libtestmucommon.la diff --git a/mu/tests/test-mu-contacts.c b/mu/tests/test-mu-contacts.c deleted file mode 100644 index 5fbec2d9..00000000 --- a/mu/tests/test-mu-contacts.c +++ /dev/null @@ -1,195 +0,0 @@ -/* -** Copyright (C) 2008-2013 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. -** -*/ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif /*HAVE_CONFIG_H*/ - -#include -#include - -#include -#include -#include - -#include "test-mu-common.h" -#include "mu-contacts.h" - -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_TESTMAILDIR); - - err = NULL; - if (!g_spawn_command_line_sync (cmdline, NULL, NULL, - NULL, &err)) { - g_printerr ("Error: %s\n", err ? err->message : "?"); - g_clear_error (&err); - g_assert (0); - } - - g_clear_error (&err); - g_free (cmdline); - - return tmpdir; -} - - -struct _Contact { - char *name, *email; - gboolean personal; - time_t tstamp; -}; -typedef struct _Contact Contact; - -static Contact* -contact_new (const char *email, const char *name, - gboolean personal, size_t tstamp) -{ - Contact *contact; - - contact = g_slice_new (Contact); - contact->name = name ? g_strdup (name) :NULL; - contact->email = email ? g_strdup (email) : NULL; - contact->personal = personal; - contact->tstamp = tstamp; - - return contact; - -} - -static void -contact_destroy (Contact *contact) -{ - if (contact) { - g_free (contact->name); - g_free (contact->email); - g_slice_free (Contact, contact); - } -} - - -static void -each_contact (const char *email, const char *name, gboolean personal, - time_t tstamp, unsigned freq, GSList **lst) -{ - Contact *contact; - - /* g_print ("[n:%s, e:%s]\n", name, email); */ - - contact = contact_new (email, name, personal, tstamp); - *lst = g_slist_prepend (*lst, contact); -} - -static gboolean -has_contact (GSList *lst, const char* name_or_email, gboolean use_name) -{ - while (lst) { - Contact *c; - c = (Contact*)lst->data; - - /* g_print ("{n:%s,e:%s}\n", c->name, c->email); */ - - if (use_name && g_strcmp0(name_or_email, c->name) == 0) - return TRUE; - if (g_strcmp0 (name_or_email, c->email) == 0) - return TRUE; - - lst = g_slist_next (lst); - } - - return FALSE; -} - - - -static GSList* -accumulate_contacts (MuContacts *contacts, const gchar *pattern) -{ - GSList *lst; - - lst = NULL; - g_assert (mu_contacts_foreach (contacts, - (MuContactsForeachFunc)each_contact, - &lst, pattern, NULL)); - return lst; -} - - -static void -test_mu_contacts_01 (void) -{ - MuContacts *contacts; - gchar *muhome, *contactsfile; - GSList *clist; - - muhome = fill_database (); - g_assert (muhome != NULL); - contactsfile = g_strdup_printf ("%s%ccache%ccontacts", - muhome, G_DIR_SEPARATOR, G_DIR_SEPARATOR); - /* g_print ("[%s]\n", contactsfile); */ - - contacts = mu_contacts_new (contactsfile); - g_assert (contacts); - - clist = accumulate_contacts (contacts, "Mü"); - g_assert_cmpint (g_slist_length (clist), ==, 1); - g_assert (has_contact (clist, "Mü", TRUE)); - g_assert (has_contact (clist, "testmu@testmu.xx", FALSE)); - g_slist_foreach (clist, (GFunc)contact_destroy, NULL); - g_slist_free (clist); - - clist = accumulate_contacts (contacts, "testmu\\.xxx?"); - g_assert_cmpint (g_slist_length (clist), ==, 2); - g_assert (has_contact (clist, "Mü", TRUE)); - g_assert (has_contact (clist, "testmu@testmu.xx", FALSE)); - g_assert (has_contact (clist, "Helmut Kröger", TRUE)); - g_assert (has_contact (clist, "hk@testmu.xxx", FALSE)); - - g_slist_foreach (clist, (GFunc)contact_destroy, NULL); - g_slist_free (clist); - - mu_contacts_destroy (contacts); - - g_free (contactsfile); - g_free (muhome); -} - - -int -main (int argc, char *argv[]) -{ - int rv; - - g_test_init (&argc, &argv, NULL); - g_test_add_func ("/mu-contacts/test-mu-contacts-01", test_mu_contacts_01); - - g_log_set_handler (NULL, - G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION, - (GLogFunc)black_hole, NULL); - rv = g_test_run (); - - return rv; -}