tests: update test helpers and users

Move test-mu-common to mu-test-utils. Use mu_test_init as a wrapper for
g_test_init. Update users.
This commit is contained in:
Dirk-Jan C. Binnema 2022-08-10 08:20:58 +03:00
parent 3ba2c4ea08
commit 11389247c5
33 changed files with 355 additions and 406 deletions

View File

@ -28,4 +28,4 @@ test('test-mu-guile',
'-DGUILE_LOAD_PATH="' + guile_load_path + '"',
'-DGUILE_EXTENSIONS_PATH="' + guile_load_path + '"'
],
dependencies: [glib_dep, lib_mu_dep, lib_test_mu_common_dep]))
dependencies: [glib_dep, lib_mu_dep]))

View File

@ -27,7 +27,7 @@
#include <unistd.h>
#include <string.h>
#include "test-mu-common.hh"
#include "utils/mu-test-utils.hh"
#include <lib/mu-store.hh>
#include <utils/mu-utils.hh>
@ -116,7 +116,7 @@ main(int argc, char* argv[])
TempDir tempdir;
test_dir = tempdir.path();
g_test_init(&argc, &argv, NULL);
mu_test_init(&argc, &argv);
if (!set_en_us_utf8_locale())
return 0; /* don't error out... */
@ -125,12 +125,6 @@ main(int argc, char* argv[])
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,
(GLogLevelFlags)(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;

View File

@ -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')

View File

@ -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);

View File

@ -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()); \

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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*/

View File

@ -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;

View File

@ -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
#

View File

@ -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 */
}

View File

@ -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__*/

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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"

View File

@ -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) {

View File

@ -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'],

150
lib/utils/mu-test-utils.cc Normal file
View File

@ -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());
}

129
lib/utils/mu-test-utils.hh Normal file
View File

@ -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__ */

View File

@ -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
{

View File

@ -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__ */

View File

@ -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);

View File

@ -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';

View File

@ -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);

View File

@ -23,18 +23,18 @@ test('test-cmd',
executable('test-cmd',
'test-mu-cmd.cc',
install: false,
dependencies: [glib_dep, lib_test_mu_common_dep, config_h_dep, lib_mu_dep]))
dependencies: [glib_dep, config_h_dep, lib_mu_dep]))
test('test-cmd-cfind',
executable('test-cmd-cfind',
'test-mu-cmd-cfind.cc',
install: false,
dependencies: [glib_dep, lib_test_mu_common_dep, config_h_dep, lib_mu_dep]))
dependencies: [glib_dep, config_h_dep, lib_mu_dep]))
test('test-cmd-query',
executable('test-cmd-query',
'test-mu-query.cc',
install: false,
dependencies: [glib_dep, lib_test_mu_common_dep, config_h_dep, lib_mu_dep]))
dependencies: [glib_dep, config_h_dep, lib_mu_dep]))
gmime_test = executable(
'gmime-test', [

View File

@ -27,7 +27,7 @@
#include <unistd.h>
#include <string.h>
#include "test-mu-common.hh"
#include "utils/mu-test-utils.hh"
#include "mu-store.hh"
#include "mu-query.hh"
#include "utils/mu-utils.hh"
@ -325,7 +325,7 @@ test_mu_cfind_csv(void)
int
main(int argc, char* argv[])
{
g_test_init(&argc, &argv, NULL);
mu_test_init(&argc, &argv);
if (!set_en_us_utf8_locale())
return 0; /* don't error out... */
@ -341,11 +341,5 @@ main(int argc, char* argv[])
g_test_add_func("/mu-cmd-cfind/test-mu-cfind-org-contact", test_mu_cfind_org_contact);
g_test_add_func("/mu-cmd-cfind/test-mu-cfind-csv", test_mu_cfind_csv);
g_log_set_handler(NULL,
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_LEVEL_WARNING |
G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
(GLogFunc)black_hole,
NULL);
return g_test_run();
}

View File

@ -28,7 +28,7 @@
#include <unistd.h>
#include <string.h>
#include "test-mu-common.hh"
#include "utils/mu-test-utils.hh"
#include "mu-store.hh"
#include "mu-query.hh"
#include "utils/mu-result.hh"
@ -829,8 +829,7 @@ main(int argc, char* argv[])
if (!g_getenv("RUN_TEST_MU_CMD"))
return 0;
g_test_init(&argc, &argv, NULL);
mu_test_init(&argc, &argv);
if (!set_en_us_utf8_locale())
return 0; /* don't error out... */
@ -870,12 +869,6 @@ main(int argc, char* argv[])
g_test_add_func("/mu-cmd/test-mu-verify-good", test_mu_verify_good);
g_test_add_func("/mu-cmd/test-mu-verify-bad", test_mu_verify_bad);
g_log_set_handler(NULL,
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_LEVEL_WARNING |
G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
(GLogFunc)black_hole,
NULL);
TempDir tempdir;
DBPATH = tempdir.path();
fill_database();

View File

@ -30,7 +30,7 @@
#include <string.h>
#include <locale.h>
#include "test-mu-common.hh"
#include "utils/mu-test-utils.hh"
#include "mu-query.hh"
#include "utils/mu-result.hh"
#include "utils/mu-utils.hh"
@ -625,7 +625,7 @@ main(int argc, char* argv[])
setlocale(LC_ALL, "");
g_test_init(&argc, &argv, nullptr);
mu_test_init(&argc, &argv);
DB_PATH1 = make_database(MU_TESTMAILDIR);
g_assert_false(DB_PATH1.empty());
@ -668,13 +668,6 @@ main(int argc, char* argv[])
g_test_add_func("/mu-query/test-mu-query-cjk",
test_mu_query_cjk);
if (!g_test_verbose())
g_log_set_handler(NULL,
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL |
G_LOG_LEVEL_WARNING | G_LOG_FLAG_RECURSION),
(GLogFunc)black_hole,
NULL);
rv = g_test_run();
return rv;