Clean up testing library
parent
d49409718c
commit
31c0950b1f
|
@ -1,23 +1,42 @@
|
|||
#include <utility>
|
||||
|
||||
#include <map>
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "tests.hpp"
|
||||
|
||||
bool do_test(const std::string &name, const std::function<bool()> &test) {
|
||||
bool eval = test();
|
||||
bool do_test(const std::string &name, const std::function<void()> &test) {
|
||||
try {
|
||||
test();
|
||||
|
||||
printf("[Tests] %s%-30s :: %s%s",
|
||||
(eval?COLOR_GREEN:""),
|
||||
name.c_str(),
|
||||
(eval?"PASSED":"FAILED"),
|
||||
COLOR_CLEAR);
|
||||
printf("[Tests] %s%-30s :: PASSED%s\n",
|
||||
COLOR_GREEN,
|
||||
name.c_str(),
|
||||
COLOR_CLEAR);
|
||||
|
||||
return eval;
|
||||
return true;
|
||||
} catch (TestException &e) {
|
||||
printf("%s\n", e.what());
|
||||
printf("[Tests] %s%-30s :: FAILED%s\n",
|
||||
COLOR_RED,
|
||||
name.c_str(),
|
||||
COLOR_CLEAR);
|
||||
return false;
|
||||
} catch (std::exception &e) {
|
||||
printf("%s\n", e.what());
|
||||
printf("[Tests] %s%-30s :: ERRORED%s\n",
|
||||
COLOR_RED,
|
||||
name.c_str(),
|
||||
COLOR_CLEAR);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, std::function<bool()>> TestRunner::Tests;
|
||||
std::map<std::string, std::function<void()>> TestRunner::Tests;
|
||||
|
||||
bool TestRunner::Register(const std::string &name, std::function<bool()> func) {
|
||||
bool TestRunner::Register(const std::string &name, std::function<void()> func) {
|
||||
auto it = Tests.find(name);
|
||||
if (it == Tests.end()) {
|
||||
Tests[name] = std::move(func);
|
||||
|
@ -34,3 +53,9 @@ bool TestRunner::RunTests() {
|
|||
|
||||
return eval;
|
||||
}
|
||||
|
||||
TestException::TestException(const char *msg, const char *file, int line, const char *func) {
|
||||
std::ostringstream os;
|
||||
os << file << ":" << line << ": Failed assertion in " << func << ": " << msg;
|
||||
message = os.str();
|
||||
}
|
||||
|
|
|
@ -3,12 +3,24 @@
|
|||
#include <string>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <exception>
|
||||
|
||||
#define Test(name) \
|
||||
void test_##name(); \
|
||||
static void test_##name##_registered = TestRunner::Register(#name, &test_##name); \
|
||||
static bool test_##name##_registered = TestRunner::Register(#name, &test_##name); \
|
||||
void test_##name()
|
||||
|
||||
class TestException : public std::exception {
|
||||
std::string message;
|
||||
|
||||
public:
|
||||
TestException(const char* msg, const char* file, int line, const char *func);
|
||||
|
||||
const char *what() const noexcept override { return message.c_str(); }
|
||||
};
|
||||
|
||||
#define TAssert(cond) if (!(cond)) { throw TestException(#cond, __FILE__, __LINE__, __func__); }
|
||||
|
||||
#define COLOR_CLEAR "\033[0m"
|
||||
#define COLOR_RED "\033[;31m"
|
||||
#define COLOR_GREEN "\033[;32m"
|
||||
|
@ -19,9 +31,9 @@
|
|||
|
||||
class TestRunner {
|
||||
public:
|
||||
static bool Register(const std::string &name, std::function<bool()> func);
|
||||
static bool Register(const std::string &name, std::function<void()> func);
|
||||
static bool RunTests();
|
||||
|
||||
private:
|
||||
static std::map<std::string, std::function<bool()>> Tests;
|
||||
static std::map<std::string, std::function<void()>> Tests;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue