191 lines
7.1 KiB
C++
191 lines
7.1 KiB
C++
#pragma once
|
|
|
|
#include <Void/Testing/Config.hpp>
|
|
#include <Void/Testing/Detail/Registry.hpp>
|
|
|
|
#include <initializer_list>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Void::Testing
|
|
{
|
|
|
|
// Aliases to void so users can omit return statements safely
|
|
// and avoid compiler warnings/errors. Perfectly matches
|
|
// std::function<void()>.
|
|
using Test = void;
|
|
using DisabledTest = void;
|
|
|
|
namespace Detail
|
|
{
|
|
[[nodiscard]] inline std::string
|
|
normalize_suite_label(std::string_view raw)
|
|
{
|
|
if (raw.size() >= 2 && raw.front() == '"' && raw.back() == '"') {
|
|
raw = raw.substr(1, raw.size() - 2);
|
|
}
|
|
return std::string{raw};
|
|
}
|
|
|
|
class NativeRegistrar final
|
|
{
|
|
public:
|
|
|
|
NativeRegistrar(void (*fn)(), std::string_view suite,
|
|
const char* file, int line,
|
|
std::string_view name,
|
|
std::initializer_list<const char*> tags = {},
|
|
bool disabled = false,
|
|
std::string_view skip_reason = {})
|
|
{
|
|
std::vector<std::string> tag_vec;
|
|
tag_vec.reserve(tags.size());
|
|
for (const char* t : tags) {
|
|
if (t != nullptr) {
|
|
tag_vec.emplace_back(t);
|
|
}
|
|
}
|
|
Registry::instance().add(TestCase{
|
|
.run = fn,
|
|
.name = std::string{name},
|
|
.suite = normalize_suite_label(suite),
|
|
.file = file != nullptr ? file : "",
|
|
.line = line,
|
|
.disabled = disabled,
|
|
.skip_reason = std::string{skip_reason},
|
|
.tags = std::move(tag_vec),
|
|
});
|
|
}
|
|
};
|
|
|
|
template <typename FixtureT> class NativeFixtureRegistrar final
|
|
{
|
|
public:
|
|
|
|
NativeFixtureRegistrar(void (*fn)(FixtureT&),
|
|
std::string_view suite,
|
|
const char* file, int line,
|
|
std::string_view name,
|
|
std::initializer_list<const char*> tags = {},
|
|
bool disabled = false,
|
|
std::string_view skip_reason = {})
|
|
{
|
|
std::vector<std::string> tag_vec;
|
|
tag_vec.reserve(tags.size());
|
|
for (const char* t : tags) {
|
|
if (t != nullptr) {
|
|
tag_vec.emplace_back(t);
|
|
}
|
|
}
|
|
Registry::instance().add(TestCase{
|
|
.run =
|
|
[fn]() {
|
|
FixtureT fixture{};
|
|
if constexpr (requires { fixture.setup(); }) {
|
|
fixture.setup();
|
|
}
|
|
try {
|
|
fn(fixture);
|
|
} catch (...) {
|
|
if constexpr (requires {
|
|
fixture.teardown();
|
|
}) {
|
|
fixture.teardown();
|
|
}
|
|
throw;
|
|
}
|
|
if constexpr (requires { fixture.teardown(); }) {
|
|
fixture.teardown();
|
|
}
|
|
},
|
|
.name = std::string{name},
|
|
.suite = normalize_suite_label(suite),
|
|
.file = file != nullptr ? file : "",
|
|
.line = line,
|
|
.disabled = disabled,
|
|
.skip_reason = std::string{skip_reason},
|
|
.tags = std::move(tag_vec),
|
|
});
|
|
}
|
|
};
|
|
} // namespace Detail
|
|
|
|
} // namespace Void::Testing
|
|
|
|
#define VOID_TESTING_DETAIL_CAT_IMPL(a, b) a##b
|
|
#define VOID_TESTING_DETAIL_CAT(a, b) VOID_TESTING_DETAIL_CAT_IMPL(a, b)
|
|
|
|
#ifdef __COUNTER__
|
|
#define VOID_TESTING_DETAIL_NEXT_ID __COUNTER__
|
|
#else
|
|
#define VOID_TESTING_DETAIL_NEXT_ID __LINE__
|
|
#endif
|
|
|
|
#define VOID_TESTING_DETAIL_DEFINE_TEST(suite_expr, test_name, disabled_flag, \
|
|
skip_msg, uid, ...) \
|
|
static void VOID_TESTING_DETAIL_CAT(_void_native_test_fn_, uid)(); \
|
|
namespace \
|
|
{ \
|
|
[[maybe_unused]] const ::Void::Testing::Detail::NativeRegistrar \
|
|
VOID_TESTING_DETAIL_CAT(_void_native_test_reg_, uid){ \
|
|
&VOID_TESTING_DETAIL_CAT(_void_native_test_fn_, uid), \
|
|
(suite_expr), \
|
|
__FILE__, \
|
|
__LINE__, \
|
|
#test_name, \
|
|
{__VA_ARGS__}, \
|
|
(disabled_flag), \
|
|
(skip_msg)}; \
|
|
} \
|
|
static void VOID_TESTING_DETAIL_CAT(_void_native_test_fn_, uid)()
|
|
|
|
#define VOID_TESTING_DETAIL_DEFINE_FIXTURE_TEST(fixture_type, test_name, \
|
|
disabled_flag, skip_msg, uid) \
|
|
static void VOID_TESTING_DETAIL_CAT(_void_native_fx_fn_, uid)( \
|
|
fixture_type&); \
|
|
namespace \
|
|
{ \
|
|
[[maybe_unused]] const ::Void::Testing::Detail::NativeFixtureRegistrar< \
|
|
fixture_type> \
|
|
VOID_TESTING_DETAIL_CAT(_void_native_fx_reg_, uid){ \
|
|
&VOID_TESTING_DETAIL_CAT(_void_native_fx_fn_, uid), \
|
|
#fixture_type, \
|
|
__FILE__, \
|
|
__LINE__, \
|
|
#test_name, \
|
|
{}, \
|
|
(disabled_flag), \
|
|
(skip_msg)}; \
|
|
} \
|
|
static void VOID_TESTING_DETAIL_CAT(_void_native_fx_fn_, uid)( \
|
|
[[maybe_unused]] fixture_type& fixture)
|
|
|
|
/// Declare a self-registering test case with zero CMake scanner dependency.
|
|
/// Symbols are internal-linkage and counter-disambiguated so files sharing the
|
|
/// same basename in different subdirectories link cleanly together.
|
|
#define VOID_TEST(suite, name) \
|
|
VOID_TESTING_DETAIL_DEFINE_TEST(#suite, name, false, "", \
|
|
VOID_TESTING_DETAIL_NEXT_ID)
|
|
|
|
/// Same as VOID_TEST, taking tags (string literals) after `name`.
|
|
#define VOID_TEST_TAGGED(suite, name, ...) \
|
|
VOID_TESTING_DETAIL_DEFINE_TEST(#suite, name, false, "", \
|
|
VOID_TESTING_DETAIL_NEXT_ID, __VA_ARGS__)
|
|
|
|
/// Declare a skipped/disabled self-registering test case.
|
|
#define VOID_TEST_DISABLED(suite, name, reason) \
|
|
VOID_TESTING_DETAIL_DEFINE_TEST(#suite, name, true, (reason), \
|
|
VOID_TESTING_DETAIL_NEXT_ID)
|
|
|
|
/// Declare a self-registering fixture test. The fixture instance is bound as
|
|
/// `fixture` inside the test body, with optional setup()/teardown() hooks.
|
|
#define VOID_TEST_FIXTURE(fixture_type, name) \
|
|
VOID_TESTING_DETAIL_DEFINE_FIXTURE_TEST(fixture_type, name, false, "", \
|
|
VOID_TESTING_DETAIL_NEXT_ID)
|
|
|
|
#define VOID_TEST_FIXTURE_DISABLED(fixture_type, name, reason) \
|
|
VOID_TESTING_DETAIL_DEFINE_FIXTURE_TEST(fixture_type, name, true, (reason), \
|
|
VOID_TESTING_DETAIL_NEXT_ID)
|