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:
@ -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
150
lib/utils/mu-test-utils.cc
Normal 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
129
lib/utils/mu-test-utils.hh
Normal 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__ */
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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__ */
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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';
|
||||
|
||||
@ -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);
|
||||
|
||||
Reference in New Issue
Block a user