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