summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/meson.build16
-rw-r--r--lib/message/mu-contact.cc5
-rw-r--r--lib/message/mu-document.cc2
-rw-r--r--lib/message/mu-fields.cc4
-rw-r--r--lib/message/mu-message-file.cc4
-rw-r--r--lib/message/test-mu-message.cc4
-rw-r--r--lib/mu-contacts-cache.cc10
-rw-r--r--lib/tests/bench-indexer.cc2
-rw-r--r--lib/tests/meson.build14
-rw-r--r--lib/tests/test-mu-common.cc89
-rw-r--r--lib/tests/test-mu-common.hh58
-rw-r--r--lib/tests/test-mu-container.cc2
-rw-r--r--lib/tests/test-mu-maildir.cc2
-rw-r--r--lib/tests/test-mu-msg-fields.cc2
-rw-r--r--lib/tests/test-mu-msg.cc26
-rw-r--r--lib/tests/test-mu-store-query.cc14
-rw-r--r--lib/tests/test-mu-store.cc13
-rw-r--r--lib/tests/test-parser.cc2
-rw-r--r--lib/tests/test-query.cc14
-rw-r--r--lib/utils/meson.build3
-rw-r--r--lib/utils/mu-test-utils.cc150
-rw-r--r--lib/utils/mu-test-utils.hh129
-rw-r--r--lib/utils/mu-utils.cc45
-rw-r--r--lib/utils/mu-utils.hh68
-rw-r--r--lib/utils/tests/test-command-parser.cc27
-rw-r--r--lib/utils/tests/test-sexp.cc3
-rw-r--r--lib/utils/tests/test-utils.cc3
27 files changed, 343 insertions, 368 deletions
diff --git a/lib/meson.build b/lib/meson.build
index ccbf091..eb04859 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -1,4 +1,4 @@
-## Copyright (C) 2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
+## Copyright (C) 2021-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
##
## 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
@@ -63,16 +63,6 @@ tokenize = executable(
dependencies: [ lib_mu_utils_dep, glib_dep ],
install: false)
-# test helpers
-lib_test_mu_common=static_library('mu-test-common', [
- 'tests/test-mu-common.cc',
- 'tests/test-mu-common.hh'],
- dependencies: [ glib_dep, thread_dep,
- config_h_dep ])
-lib_test_mu_common_dep=declare_dependency(
- link_with: lib_test_mu_common,
- include_directories: include_directories(['tests']))
-
# actual tests
test('test-threads',
@@ -80,12 +70,12 @@ test('test-threads',
'mu-query-threads.cc',
install: false,
cpp_args: ['-DBUILD_TESTS'],
- dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, lib_mu_dep]))
test('test-contacts-cache',
executable('test-contacts-cache',
'mu-contacts-cache.cc',
install: false,
cpp_args: ['-DBUILD_TESTS'],
- dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, lib_mu_dep]))
subdir('tests')
diff --git a/lib/message/mu-contact.cc b/lib/message/mu-contact.cc
index db77df4..711f4d2 100644
--- a/lib/message/mu-contact.cc
+++ b/lib/message/mu-contact.cc
@@ -80,7 +80,7 @@ Mu::lowercase_hash(const std::string& s)
*
*/
-#include "utils/mu-utils.hh"
+#include "utils/mu-test-utils.hh"
static void
test_ctor_foo()
@@ -189,14 +189,13 @@ static void
test_misc()
{
g_assert_false(!!contact_type_from_field_id(Field::Id::Subject));
-
}
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_mime_init();
g_test_add_func("/message/contact/ctor-foo", test_ctor_foo);
diff --git a/lib/message/mu-document.cc b/lib/message/mu-document.cc
index c0a207e..f0c230c 100644
--- a/lib/message/mu-document.cc
+++ b/lib/message/mu-document.cc
@@ -370,6 +370,8 @@ Document::remove(Field::Id field_id)
#ifdef BUILD_TESTS
+#include "utils/mu-test-utils.hh"
+
#define assert_same_contact(C1,C2) do { \
g_assert_cmpstr(C1.email.c_str(),==,C2.email.c_str()); \
g_assert_cmpstr(C2.name.c_str(),==,C2.name.c_str()); \
diff --git a/lib/message/mu-fields.cc b/lib/message/mu-fields.cc
index d114a1c..aec8c7a 100644
--- a/lib/message/mu-fields.cc
+++ b/lib/message/mu-fields.cc
@@ -20,6 +20,8 @@
#include "mu-fields.hh"
#include "mu-flags.hh"
+#include "utils/mu-test-utils.hh"
+
using namespace Mu;
std::string
@@ -184,7 +186,7 @@ test_xapian_term()
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_test_add_func("/message/fields/ids", test_ids);
g_test_add_func("/message/fields/shortcuts", test_shortcuts);
diff --git a/lib/message/mu-message-file.cc b/lib/message/mu-message-file.cc
index 6067f62..3c86c24 100644
--- a/lib/message/mu-message-file.cc
+++ b/lib/message/mu-message-file.cc
@@ -122,6 +122,8 @@ Mu::flags_from_path(const std::string& path)
#ifdef BUILD_TESTS
+#include "utils/mu-test-utils.hh"
+
static void
test_maildir_from_path()
{
@@ -192,7 +194,7 @@ test_flags_from_path()
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_test_add_func("/message/file/maildir-from-path",
test_maildir_from_path);
diff --git a/lib/message/test-mu-message.cc b/lib/message/test-mu-message.cc
index 5311e86..8d85fae 100644
--- a/lib/message/test-mu-message.cc
+++ b/lib/message/test-mu-message.cc
@@ -16,7 +16,7 @@
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
-
+#include "utils/mu-test-utils.hh"
#include "mu-message.hh"
#include "mu-mime-object.hh"
#include <glib.h>
@@ -834,7 +834,7 @@ test_message_sanitize_maildir()
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_test_add_func("/message/message/mailing-list",
test_message_mailing_list);
diff --git a/lib/mu-contacts-cache.cc b/lib/mu-contacts-cache.cc
index 24556ab..c4c8146 100644
--- a/lib/mu-contacts-cache.cc
+++ b/lib/mu-contacts-cache.cc
@@ -342,7 +342,7 @@ ContactsCache::is_personal(const std::string& addr) const
*
*/
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
static void
test_mu_contacts_cache_base()
@@ -499,19 +499,13 @@ test_mu_contacts_cache_sort()
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_test_add_func("/lib/contacts-cache/base", test_mu_contacts_cache_base);
g_test_add_func("/lib/contacts-cache/personal", test_mu_contacts_cache_personal);
g_test_add_func("/lib/contacts-cache/for-each", test_mu_contacts_cache_foreach);
g_test_add_func("/lib/contacts-cache/sort", test_mu_contacts_cache_sort);
- 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();
}
#endif /*BUILD_TESTS*/
diff --git a/lib/tests/bench-indexer.cc b/lib/tests/bench-indexer.cc
index c857847..b83bd90 100644
--- a/lib/tests/bench-indexer.cc
+++ b/lib/tests/bench-indexer.cc
@@ -28,7 +28,7 @@
#include <mu-store.hh>
#include "mu-maildir.hh"
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
using namespace Mu;
diff --git a/lib/tests/meson.build b/lib/tests/meson.build
index ab233f2..17a9b72 100644
--- a/lib/tests/meson.build
+++ b/lib/tests/meson.build
@@ -21,40 +21,40 @@ test('test-maildir',
executable('test-maildir',
'test-mu-maildir.cc',
install: false,
- dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, lib_mu_dep]))
test('test-msg',
executable('test-msg',
'test-mu-msg.cc',
install: false,
- dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, lib_mu_dep]))
test('test-store',
executable('test-store',
'test-mu-store.cc',
install: false,
- dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, lib_mu_dep]))
test('test-query',
executable('test-query',
'test-query.cc',
install: false,
- dependencies: [glib_dep, gmime_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, gmime_dep, lib_mu_dep]))
test('test-tokenizer',
executable('test-tokenizer',
'test-tokenizer.cc',
install: false,
- dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, lib_mu_dep]))
test('test-parser',
executable('test-parser',
'test-parser.cc',
install: false,
- dependencies: [glib_dep, gmime_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, gmime_dep, lib_mu_dep]))
test('test-store-query',
executable('test-store-query',
'test-mu-store-query.cc',
install: false,
- dependencies: [glib_dep, gmime_dep, lib_mu_dep, lib_test_mu_common_dep]))
+ dependencies: [glib_dep, gmime_dep, lib_mu_dep]))
#
# benchmarks
#
diff --git a/lib/tests/test-mu-common.cc b/lib/tests/test-mu-common.cc
deleted file mode 100644
index 92d706f..0000000
--- a/lib/tests/test-mu-common.cc
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
-** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
-**
-** 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 <glib.h>
-#include <glib/gstdio.h>
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-
-#include <langinfo.h>
-#include <locale.h>
-
-#include "test-mu-common.hh"
-
-char*
-test_mu_common_get_random_tmpdir(void)
-{
- char* dir;
- int res;
-
- dir = g_strdup_printf("%s%cmu-test-%d%ctest-%x",
- g_get_tmp_dir(),
- G_DIR_SEPARATOR,
- getuid(),
- G_DIR_SEPARATOR,
- (int)random() * getpid() * (int)time(NULL));
-
- res = g_mkdir_with_parents(dir, 0700);
- g_assert(res != -1);
-
- return dir;
-}
-
-const char*
-set_tz(const char* tz)
-{
- static const char* oldtz;
-
- oldtz = getenv("TZ");
- if (tz)
- setenv("TZ", tz, 1);
- else
- unsetenv("TZ");
-
- tzset();
- return oldtz;
-}
-
-gboolean
-set_en_us_utf8_locale(void)
-{
- setenv("LC_ALL", "en_US.UTF-8", 1);
- setlocale(LC_ALL, "en_US.UTF-8");
-
- if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {
- g_print("Note: Unit tests require the en_US.utf8 locale. "
- "Ignoring test cases.\n");
- return FALSE;
- }
-
- return TRUE;
-}
-
-void
-black_hole(void)
-{
- return; /* do nothing */
-}
diff --git a/lib/tests/test-mu-common.hh b/lib/tests/test-mu-common.hh
deleted file mode 100644
index cfb427f..0000000
--- a/lib/tests/test-mu-common.hh
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
-** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
-**
-** 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.
-**
-*/
-
-#ifndef __TEST_MU_COMMON_H__
-#define __TEST_MU_COMMON_H__
-
-#include <glib.h>
-
-G_BEGIN_DECLS
-
-/**
- * get a dir name for a random temporary directory to do tests
- *
- * @return a random dir name, g_free when it's no longer needed
- */
-char* test_mu_common_get_random_tmpdir(void);
-
-/**
- * set the output to /dev/null
- *
- */
-void black_hole(void);
-
-/**
- * set the timezone
- *
- * @param tz timezone
- *
- * @return the old timezone
- */
-const char* set_tz(const char* tz);
-
-/**
- * switch the locale to en_US.utf8, return TRUE if it succeeds
- *
- * @return TRUE if the switch succeeds, FALSE otherwise
- */
-gboolean set_en_us_utf8_locale(void);
-
-G_END_DECLS
-
-#endif /*__TEST_MU_COMMON_H__*/
diff --git a/lib/tests/test-mu-container.cc b/lib/tests/test-mu-container.cc
index 925de1a..4fb1939 100644
--- a/lib/tests/test-mu-container.cc
+++ b/lib/tests/test-mu-container.cc
@@ -20,7 +20,7 @@
#include "config.h"
#include <glib.h>
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
#include "mu-container.hh"
static gboolean
diff --git a/lib/tests/test-mu-maildir.cc b/lib/tests/test-mu-maildir.cc
index 23b36b3..d3713f6 100644
--- a/lib/tests/test-mu-maildir.cc
+++ b/lib/tests/test-mu-maildir.cc
@@ -26,7 +26,7 @@
#include <vector>
#include <fstream>
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
#include "mu-maildir.hh"
#include "utils/mu-result.hh"
#include "utils/mu-util.h"
diff --git a/lib/tests/test-mu-msg-fields.cc b/lib/tests/test-mu-msg-fields.cc
index 770689d..5f5df16 100644
--- a/lib/tests/test-mu-msg-fields.cc
+++ b/lib/tests/test-mu-msg-fields.cc
@@ -28,7 +28,7 @@
#include <locale.h>
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
#include "mu-message-fields.hh"
static void
diff --git a/lib/tests/test-mu-msg.cc b/lib/tests/test-mu-msg.cc
index cd3ec40..9056e07 100644
--- a/lib/tests/test-mu-msg.cc
+++ b/lib/tests/test-mu-msg.cc
@@ -28,7 +28,7 @@
#include <locale.h>
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
#include "utils/mu-result.hh"
#include "utils/mu-utils.hh"
@@ -207,12 +207,8 @@ test_mu_msg_references(void)
"non-exist-04@msg.id"
};
- g_assert_cmpuint(msg.references().size(), == , expected_refs.size());
- size_t n{};
- for (auto&& ref: msg.references()) {
- assert_equal(ref, expected_refs.at(n));
- ++n;
- };
+ assert_equal_seq_str(msg.references(), expected_refs);
+ assert_equal(msg.thread_id(), expected_refs[0]);
}
static void
@@ -230,12 +226,8 @@ test_mu_msg_references_dups(void)
"20051211184308.GB13513@gauss.org"
};
- g_assert_cmpuint(msg.references().size(), == , expected_refs.size());
- size_t n{};
- for (auto&& ref: msg.references()) {
- assert_equal(ref, expected_refs.at(n));
- ++n;
- };
+ assert_equal_seq_str(msg.references(), expected_refs);
+ assert_equal(msg.thread_id(), expected_refs[0]);
}
static void
@@ -258,12 +250,8 @@ test_mu_msg_references_many(void)
"8ioh48-8mu.ln1@leafnode-msgid.gclare.org.uk"
};
- g_assert_cmpuint(msg.references().size(), == , expected_refs.size());
- size_t n{};
- for (auto&& ref: msg.references()) {
- assert_equal(ref, expected_refs.at(n));
- ++n;
- };
+ assert_equal_seq_str(msg.references(), expected_refs);
+ assert_equal(msg.thread_id(), expected_refs[0]);
}
static void
diff --git a/lib/tests/test-mu-store-query.cc b/lib/tests/test-mu-store-query.cc
index 0331271..e24b7c2 100644
--- a/lib/tests/test-mu-store-query.cc
+++ b/lib/tests/test-mu-store-query.cc
@@ -17,7 +17,7 @@
**
*/
-#include "test-mu-common.hh"
+
#include <array>
#include <thread>
#include <string>
@@ -27,6 +27,7 @@
#include <mu-store.hh>
#include <utils/mu-utils.hh>
+#include <utils/mu-test-utils.hh>
#include <message/mu-message.hh>
using namespace Mu;
@@ -338,22 +339,15 @@ Child
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_test_bug_base("https://github.com/djcb/mu/issues/");
- g_test_add_func("/store/query/simple", test_simple);
+ g_test_add_func("/store/query/simple", test_simple);
g_test_add_func("/store/query/spam-address-components",
test_spam_address_components);
g_test_add_func("/store/query/dups-related",
test_dups_related);
- if (!g_test_verbose())
- 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/lib/tests/test-mu-store.cc b/lib/tests/test-mu-store.cc
index 3e281d9..1a140e3 100644
--- a/lib/tests/test-mu-store.cc
+++ b/lib/tests/test-mu-store.cc
@@ -28,7 +28,7 @@
#include <locale.h>
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
#include "mu-store.hh"
#include "utils/mu-result.hh"
#include <utils/mu-utils.hh>
@@ -343,7 +343,6 @@ test_store_fail()
"/../../root/non-existent-path/54321",
{}, {});
g_assert_false(!!store);
-
}
}
@@ -351,7 +350,7 @@ test_store_fail()
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_test_add_func("/store/ctor-dtor", test_store_ctor_dtor);
g_test_add_func("/store/add-count-remove", test_store_add_count_remove);
@@ -362,13 +361,5 @@ main(int argc, char* argv[])
g_test_add_func("/store/index/move", test_index_move);
g_test_add_func("/store/index/fail", test_store_fail);
- if (!g_test_verbose())
- 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/lib/tests/test-parser.cc b/lib/tests/test-parser.cc
index 4429040..74b5522 100644
--- a/lib/tests/test-parser.cc
+++ b/lib/tests/test-parser.cc
@@ -23,7 +23,7 @@
#include <iostream>
#include <sstream>
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
#include "mu-parser.hh"
#include "utils/mu-result.hh"
diff --git a/lib/tests/test-query.cc b/lib/tests/test-query.cc
index 2ebcdd1..d1ca0bb 100644
--- a/lib/tests/test-query.cc
+++ b/lib/tests/test-query.cc
@@ -30,7 +30,7 @@
#include "index/mu-indexer.hh"
#include "utils/mu-result.hh"
#include "utils/mu-utils.hh"
-#include "test-mu-common.hh"
+#include "utils/mu-test-utils.hh"
using namespace Mu;
@@ -87,20 +87,10 @@ test_query()
int
main(int argc, char* argv[])
try {
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
g_test_add_func("/query", test_query);
-
- if (!g_test_verbose())
- 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();
} catch (const std::runtime_error& re) {
diff --git a/lib/utils/meson.build b/lib/utils/meson.build
index ad466b2..6277396 100644
--- a/lib/utils/meson.build
+++ b/lib/utils/meson.build
@@ -1,4 +1,4 @@
-## Copyright (C) 2021 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
+## Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
##
## 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
@@ -21,6 +21,7 @@ lib_mu_utils=static_library('mu-utils', [
'mu-option.cc',
'mu-readline.cc',
'mu-sexp.cc',
+ 'mu-test-utils.cc',
'mu-util.c',
'mu-util.h',
'mu-utils.cc'],
diff --git a/lib/utils/mu-test-utils.cc b/lib/utils/mu-test-utils.cc
new file mode 100644
index 0000000..8e049b2
--- /dev/null
+++ b/lib/utils/mu-test-utils.cc
@@ -0,0 +1,150 @@
+/*
+** Copyright (C) 2008-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
+**
+** 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 <glib.h>
+#include <glib/gstdio.h>
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <langinfo.h>
+#include <locale.h>
+
+#include "utils/mu-test-utils.hh"
+#include "utils/mu-error.hh"
+
+
+using namespace Mu;
+
+char*
+Mu::test_mu_common_get_random_tmpdir()
+{
+ char* dir;
+ int res;
+
+ dir = g_strdup_printf("%s%cmu-test-%d%ctest-%x",
+ g_get_tmp_dir(),
+ G_DIR_SEPARATOR,
+ getuid(),
+ G_DIR_SEPARATOR,
+ (int)random() * getpid() * (int)time(NULL));
+
+ res = g_mkdir_with_parents(dir, 0700);
+ g_assert(res != -1);
+
+ return dir;
+}
+
+const char*
+Mu::set_tz(const char* tz)
+{
+ static const char* oldtz;
+
+ oldtz = getenv("TZ");
+ if (tz)
+ setenv("TZ", tz, 1);
+ else
+ unsetenv("TZ");
+
+ tzset();
+ return oldtz;
+}
+
+bool
+Mu::set_en_us_utf8_locale()
+{
+ setenv("LC_ALL", "en_US.UTF-8", 1);
+ setlocale(LC_ALL, "en_US.UTF-8");
+
+ if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {
+ g_print("Note: Unit tests require the en_US.utf8 locale. "
+ "Ignoring test cases.\n");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static void
+black_hole(void)
+{
+ return; /* do nothing */
+}
+
+void
+Mu::mu_test_init(int *argc, char ***argv)
+{
+ g_test_init(argc, argv, NULL);
+
+ if (!g_test_verbose())
+ g_log_set_handler(
+ NULL,
+ (GLogLevelFlags)(G_LOG_LEVEL_MASK |
+ G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
+ (GLogFunc)black_hole, NULL);
+}
+
+
+
+void
+Mu::allow_warnings()
+{
+ g_test_log_set_fatal_handler(
+ [](const char*, GLogLevelFlags, const char*, gpointer) { return FALSE; },
+ {});
+}
+
+
+
+Mu::TempDir::TempDir(bool autodelete): autodelete_{autodelete}
+{
+ GError *err{};
+ gchar *tmpdir = g_dir_make_tmp("mu-tmp-XXXXXX", &err);
+ if (!tmpdir)
+ throw Mu::Error(Error::Code::File, &err,
+ "failed to create temporary directory");
+
+ path_ = tmpdir;
+ g_free(tmpdir);
+
+ g_debug("created '%s'", path_.c_str());
+}
+
+Mu::TempDir::~TempDir()
+{
+ if (::access(path_.c_str(), F_OK) != 0)
+ return; /* nothing to do */
+
+ if (!autodelete_) {
+ g_debug("_not_ deleting %s", path_.c_str());
+ return;
+ }
+
+ /* ugly */
+ GError *err{};
+ const auto cmd{format("/bin/rm -rf '%s'", path_.c_str())};
+ if (!g_spawn_command_line_sync(cmd.c_str(), NULL, NULL, NULL, &err)) {
+ g_warning("error: %s\n", err ? err->message : "?");
+ g_clear_error(&err);
+ } else
+ g_debug("removed '%s'", path_.c_str());
+}
diff --git a/lib/utils/mu-test-utils.hh b/lib/utils/mu-test-utils.hh
new file mode 100644
index 0000000..c59b9d5
--- /dev/null
+++ b/lib/utils/mu-test-utils.hh
@@ -0,0 +1,129 @@
+/*
+** Copyright (C) 2008-2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
+**
+** 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.
+**
+*/
+
+#ifndef MU_TEST_UTILS_HH__
+#define MU_TEST_UTILS_HH__
+
+#include <string>
+
+namespace Mu {
+
+/**
+ * get a dir name for a random temporary directory to do tests
+ *
+ * @return a random dir name, g_free when it's no longer needed
+ */
+char* test_mu_common_get_random_tmpdir();
+
+/**
+ * mu wrapper for g_test_init
+ *
+ * @param argc
+ * @param argv
+ */
+void mu_test_init(int *argc, char ***argv);
+
+/**
+ * set the timezone
+ *
+ * @param tz timezone
+ *
+ * @return the old timezone
+ */
+const char* set_tz(const char* tz);
+
+/**
+ * switch the locale to en_US.utf8, return TRUE if it succeeds
+ *
+ * @return true if the switch succeeds, false otherwise
+ */
+bool set_en_us_utf8_locale();
+
+/**
+ * For unit tests, assert two std::string's are equal.
+ *
+ * @param s1 string1
+ * @param s2 string2
+ */
+#define assert_equal(s1__,s2__) do { \
+ std::string s1s__(s1__), s2s__(s2__); \
+ g_assert_cmpstr(s1s__.c_str(), ==, s2s__.c_str()); \
+ } while(0)
+
+
+#define assert_equal_seq(seq1__, seq2__) do { \
+ g_assert_cmpuint(seq1__.size(), ==, seq2__.size()); \
+ size_t n__{}; \
+ for (auto&& item__: seq1__) { \
+ g_assert_true(item__ == seq2__.at(n__)); \
+ ++n__; \
+ } \
+ } while(0)
+
+#define assert_equal_seq_str(seq1__, seq2__) do { \
+ g_assert_cmpuint(seq1__.size(), ==, seq2__.size()); \
+ size_t n__{}; \
+ for (auto&& item__: seq1__) { \
+ assert_equal(item__, seq2__.at(n__)); \
+ ++n__; \
+ } \
+ } while(0)
+
+/**
+ * For unit-tests, allow warnings in the current function.
+ *
+ */
+void allow_warnings();
+
+
+/**
+ * For unit-tests, a RAII tempdir.
+ *
+ */
+struct TempDir {
+ /**
+ * Construct a temporary directory
+ */
+ TempDir(bool autodelete=true);
+
+ /**
+ * DTOR; removes the temporary directory
+ *
+ *
+ * @return
+ */
+ ~TempDir();
+
+ /**
+ * Path to the temporary directory
+ *
+ * @return the path.
+ *
+ *
+ */
+ const std::string& path() {return path_; }
+private:
+ std::string path_;
+ const bool autodelete_;
+};
+
+} // namepace Mu
+
+
+#endif /* MU_TEST_UTILS_HH__ */
diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc
index 0f9e6e3..f32eed7 100644
--- a/lib/utils/mu-utils.cc
+++ b/lib/utils/mu-utils.cc
@@ -585,7 +585,6 @@ Mu::from_lexnum(const std::string& str)
}
-
std::string
Mu::canonicalize_filename(const std::string& path, const std::string& relative_to)
{
@@ -602,50 +601,6 @@ Mu::canonicalize_filename(const std::string& path, const std::string& relative_t
}
-void
-Mu::allow_warnings()
-{
- g_test_log_set_fatal_handler(
- [](const char*, GLogLevelFlags, const char*, gpointer) { return FALSE; },
- {});
-}
-
-
-
-Mu::TempDir::TempDir(bool autodelete): autodelete_{autodelete}
-{
- GError *err{};
- gchar *tmpdir = g_dir_make_tmp("mu-tmp-XXXXXX", &err);
- if (!tmpdir)
- throw Mu::Error(Error::Code::File, &err,
- "failed to create temporary directory");
-
- path_ = tmpdir;
- g_free(tmpdir);
-
- g_debug("created '%s'", path_.c_str());
-}
-
-Mu::TempDir::~TempDir()
-{
- if (::access(path_.c_str(), F_OK) != 0)
- return; /* nothing to do */
-
- if (!autodelete_) {
- g_debug("_not_ deleting %s", path_.c_str());
- return;
- }
-
- /* ugly */
- GError *err{};
- const auto cmd{format("/bin/rm -rf '%s'", path_.c_str())};
- if (!g_spawn_command_line_sync(cmd.c_str(), NULL, NULL, NULL, &err)) {
- g_warning("error: %s\n", err ? err->message : "?");
- g_clear_error(&err);
- } else
- g_debug("removed '%s'", path_.c_str());
-}
-
bool
Mu::locale_workaround() try
{
diff --git a/lib/utils/mu-utils.hh b/lib/utils/mu-utils.hh
index bfe1490..dd38416 100644
--- a/lib/utils/mu-utils.hh
+++ b/lib/utils/mu-utils.hh
@@ -454,74 +454,6 @@ private:
constexpr ET& operator|=(ET& e1, ET e2) { return e1 = e1 | e2; } \
static_assert(1==1) // require a semicolon
-/**
- * For unit tests, assert two std::string's are equal.
- *
- * @param s1 string1
- * @param s2 string2
- */
-#define assert_equal(s1__,s2__) do { \
- std::string s1s__(s1__), s2s__(s2__); \
- g_assert_cmpstr(s1s__.c_str(), ==, s2s__.c_str()); \
- } while(0)
-
-
-#define assert_equal_seq(seq1__, seq2__) do { \
- g_assert_cmpuint(seq1__.size(), ==, seq2__.size()); \
- size_t n__{}; \
- for (auto&& item__: seq1__) { \
- g_assert_true(item__ == seq2__.at(n__)); \
- ++n__; \
- } \
- } while(0)
-
-#define assert_equal_seq_str(seq1__, seq2__) do { \
- g_assert_cmpuint(seq1__.size(), ==, seq2__.size()); \
- size_t n__{}; \
- for (auto&& item__: seq1__) { \
- assert_equal(item__, seq2__.at(n__)); \
- ++n__; \
- } \
- } while(0)
-
-/**
- * For unit-tests, allow warnings in the current function.
- *
- */
-void allow_warnings();
-
-
-/**
- * For unit-tests, a RAII tempdir.
- *
- */
-struct TempDir {
- /**
- * Construct a temporary directory
- */
- TempDir(bool autodelete=true);
-
- /**
- * DTOR; removes the temporary directory
- *
- *
- * @return
- */
- ~TempDir();
-
- /**
- * Path to the temporary directory
- *
- * @return the path.
- *
- *
- */
- const std::string& path() {return path_; }
-private:
- std::string path_;
- const bool autodelete_;
-};
-
} // namespace Mu
#endif /* __MU_UTILS_HH__ */
diff --git a/lib/utils/tests/test-command-parser.cc b/lib/utils/tests/test-command-parser.cc
index 44c9a9c..4156b03 100644
--- a/lib/utils/tests/test-command-parser.cc
+++ b/lib/utils/tests/test-command-parser.cc
@@ -1,5 +1,5 @@
/*
-** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
+** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
@@ -25,6 +25,7 @@
#include "mu-command-parser.hh"
#include "mu-utils.hh"
+#include "mu-test-utils.hh"
using namespace Mu;
@@ -68,9 +69,9 @@ test_command()
cmap.emplace(
"my-command",
CommandInfo{ArgMap{{":param1", ArgInfo{Sexp::Type::String, true, "some string"}},
- {":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
- "My command,",
- {}});
+ {":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
+ "My command,",
+ {}});
g_assert_true(call(cmap, "(my-command :param1 \"hello\")"));
g_assert_true(call(cmap, "(my-command :param1 \"hello\" :param2 123)"));
@@ -86,12 +87,12 @@ test_command2()
CommandMap cmap;
cmap.emplace("bla",
- CommandInfo{ArgMap{
+ CommandInfo{ArgMap{
{":foo", ArgInfo{Sexp::Type::Number, false, "foo"}},
{":bar", ArgInfo{Sexp::Type::String, false, "bar"}},
},
- "yeah",
- [&](const auto& params) {}});
+ "yeah",
+ [&](const auto& params) {}});
g_assert_true(call(cmap, "(bla :foo nil)"));
g_assert_false(call(cmap, "(bla :foo nil :bla nil)"));
@@ -109,9 +110,9 @@ test_command_fail()
cmap.emplace(
"my-command",
CommandInfo{ArgMap{{":param1", ArgInfo{Sexp::Type::String, true, "some string"}},
- {":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
- "My command,",
- {}});
+ {":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
+ "My command,",
+ {}});
g_assert_false(call(cmap, "(my-command)"));
g_assert_false(call(cmap, "(my-command2)"));
@@ -125,9 +126,9 @@ black_hole()
}
int
-main(int argc, char* argv[])
-try {
- g_test_init(&argc, &argv, NULL);
+main(int argc, char* argv[]) try {
+
+ mu_test_init(&argc, &argv);
g_test_add_func("/utils/command-parser/param-getters", test_param_getters);
g_test_add_func("/utils/command-parser/command", test_command);
diff --git a/lib/utils/tests/test-sexp.cc b/lib/utils/tests/test-sexp.cc
index 770a157..3cb1c5a 100644
--- a/lib/utils/tests/test-sexp.cc
+++ b/lib/utils/tests/test-sexp.cc
@@ -25,6 +25,7 @@
#include "mu-command-parser.hh"
#include "mu-utils.hh"
+#include "mu-test-utils.hh"
using namespace Mu;
@@ -168,7 +169,7 @@ test_prop_list_remove()
int
main(int argc, char* argv[])
try {
- g_test_init(&argc, &argv, NULL);
+ mu_test_init(&argc, &argv);
if (argc == 2) {
std::cout << Sexp::make_parse(argv[1]) << '\n';
diff --git a/lib/utils/tests/test-utils.cc b/lib/utils/tests/test-utils.cc
index 6239945..b69c705 100644
--- a/lib/utils/tests/test-utils.cc
+++ b/lib/utils/tests/test-utils.cc
@@ -26,6 +26,7 @@
#include <array>
#include "mu-utils.hh"
+#include "mu-test-utils.hh"
#include "mu-error.hh"
using namespace Mu;
@@ -301,7 +302,7 @@ test_error()
int
main(int argc, char* argv[])
{
- g_test_init(&argc, &argv, nullptr);
+ mu_test_init(&argc, &argv);
g_test_add_func("/utils/date-basic", test_date_basic);
g_test_add_func("/utils/date-ymwdhMs", test_date_ymwdhMs);