sots-engine/tests/mars_parse/test_main.h

49 lines
1.9 KiB
C++

// Tiny self-contained test harness (no third-party deps).
#pragma once
#include <cstdio>
#include <functional>
#include <string>
#include <vector>
namespace testing {
struct Case {
const char* name;
std::function<void()> fn;
};
std::vector<Case>& registry();
int& failures();
void report_failure(const char* file, int line, const std::string& expr);
struct Register {
Register(const char* name, std::function<void()> fn) { registry().push_back({name, std::move(fn)}); }
};
} // namespace testing
#define TEST(name) \
static void test_##name(); \
static testing::Register reg_##name(#name, test_##name); \
static void test_##name()
#define CHECK(expr) \
do { \
if (!(expr)) testing::report_failure(__FILE__, __LINE__, #expr); \
} while (0)
#define CHECK_EQ(a, b) \
do { \
if (!((a) == (b))) \
testing::report_failure(__FILE__, __LINE__, \
std::string(#a " == " #b " [got: ") + \
testing_to_string(a) + " vs " + \
testing_to_string(b) + "]"); \
} while (0)
inline std::string testing_to_string(const std::string& s) { return "\"" + s + "\""; }
inline std::string testing_to_string(const char* s) { return std::string("\"") + s + "\""; }
inline std::string testing_to_string(bool b) { return b ? "true" : "false"; }
template <class T>
std::string testing_to_string(const T& v) { return std::to_string(v); }