Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
208 lines
5.7 KiB
C++
208 lines
5.7 KiB
C++
#pragma once
|
|
#include <Void/Functional/Detail/Repr.hpp>
|
|
#include <Void/Testing/Config.hpp>
|
|
#include <Void/Testing/Detail/Registry.hpp>
|
|
#include <concepts>
|
|
#include <format>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
namespace Void::Testing::Detail
|
|
{
|
|
struct TestContext {
|
|
TestResult* current_result{nullptr};
|
|
bool should_abort{false};
|
|
void* fixture{nullptr};
|
|
static TestContext& current()
|
|
{
|
|
thread_local TestContext ctx;
|
|
return ctx;
|
|
}
|
|
};
|
|
|
|
/// Render a value for assertion-failure messages. Special-cases `bool`,
|
|
/// strings, and `nullptr` for unambiguous output, then falls through to
|
|
/// Void::Result::Detail::repr — which uses std::format when available and
|
|
/// C++26 reflection otherwise to walk struct fields.
|
|
template <typename T> std::string repr(const T& val)
|
|
{
|
|
if constexpr (std::is_same_v<std::decay_t<T>, bool>) {
|
|
return val ? "true" : "false";
|
|
} else if constexpr (std::is_same_v<std::decay_t<T>, std::string> ||
|
|
std::is_same_v<std::decay_t<T>,
|
|
std::string_view>) {
|
|
return std::format("\"{}\"", val);
|
|
} else if constexpr (std::is_null_pointer_v<std::decay_t<T>>) {
|
|
return "nullptr";
|
|
} else {
|
|
return Void::Result::Detail::repr(val);
|
|
}
|
|
}
|
|
|
|
void handle_assertion(AssertionResult result);
|
|
|
|
struct RequireFailure : std::runtime_error {
|
|
using std::runtime_error::runtime_error;
|
|
};
|
|
} // namespace Void::Testing::Detail
|
|
|
|
namespace Void::Testing
|
|
{
|
|
|
|
template <typename T>
|
|
requires std::convertible_to<T, bool>
|
|
void expect(T&& condition,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
AssertionResult result;
|
|
result.passed = static_cast<bool>(condition);
|
|
result.kind = AssertKind::Expect;
|
|
result.location = loc;
|
|
result.expression = "expect(...)";
|
|
if (!result.passed) {
|
|
result.message = "expected condition to be true";
|
|
}
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
template <typename T>
|
|
requires std::convertible_to<T, bool>
|
|
void require(T&& condition,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
AssertionResult result;
|
|
result.passed = static_cast<bool>(condition);
|
|
result.kind = AssertKind::Require;
|
|
result.location = loc;
|
|
result.expression = "require(...)";
|
|
if (!result.passed) {
|
|
result.message = "required condition to be true";
|
|
}
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
requires std::equality_comparable_with<T, U>
|
|
void expect_eq(const T& lhs, const U& rhs,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
AssertionResult result;
|
|
result.passed = (lhs == rhs);
|
|
result.kind = AssertKind::Expect;
|
|
result.location = loc;
|
|
result.expected_repr = Detail::repr(rhs);
|
|
result.actual_repr = Detail::repr(lhs);
|
|
if (!result.passed) {
|
|
result.message = std::format("expected {}, got {}",
|
|
Detail::repr(rhs), Detail::repr(lhs));
|
|
}
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
requires std::equality_comparable_with<T, U>
|
|
void require_eq(const T& lhs, const U& rhs,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
AssertionResult result;
|
|
result.passed = (lhs == rhs);
|
|
result.kind = AssertKind::Require;
|
|
result.location = loc;
|
|
result.expected_repr = Detail::repr(rhs);
|
|
result.actual_repr = Detail::repr(lhs);
|
|
if (!result.passed) {
|
|
result.message = std::format("required {}, got {}",
|
|
Detail::repr(rhs), Detail::repr(lhs));
|
|
}
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
template <typename T, typename U>
|
|
requires std::equality_comparable_with<T, U>
|
|
void expect_ne(const T& lhs, const U& rhs,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
AssertionResult result;
|
|
result.passed = (lhs != rhs);
|
|
result.kind = AssertKind::Expect;
|
|
result.location = loc;
|
|
if (!result.passed) {
|
|
result.message = "expected values to differ";
|
|
}
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
template <typename Exception, typename Fn>
|
|
requires std::invocable<Fn>
|
|
void
|
|
expect_throw(Fn&& fn,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
bool threw = false;
|
|
try {
|
|
std::forward<Fn>(fn)();
|
|
} catch (const Exception&) {
|
|
threw = true;
|
|
} catch (...) {
|
|
}
|
|
AssertionResult result;
|
|
result.passed = threw;
|
|
result.kind = AssertKind::Expect;
|
|
result.location = loc;
|
|
if (!threw) {
|
|
result.message = "expected exception was not thrown";
|
|
}
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
template <typename Fn>
|
|
requires std::invocable<Fn>
|
|
void expect_nothrow(
|
|
Fn&& fn, std::source_location loc = std::source_location::current())
|
|
{
|
|
bool threw = false;
|
|
try {
|
|
std::forward<Fn>(fn)();
|
|
} catch (...) {
|
|
threw = true;
|
|
}
|
|
AssertionResult result;
|
|
result.passed = !threw;
|
|
result.kind = AssertKind::Expect;
|
|
result.location = loc;
|
|
if (threw) {
|
|
result.message = "expected no exception";
|
|
}
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
inline void fail(std::string_view message = "",
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
AssertionResult result;
|
|
result.passed = false;
|
|
result.kind = AssertKind::Expect;
|
|
result.location = loc;
|
|
result.message =
|
|
message.empty() ? "explicit failure" : std::string{message};
|
|
Detail::handle_assertion(std::move(result));
|
|
}
|
|
|
|
inline void skip(std::string_view reason,
|
|
[[maybe_unused]] std::source_location loc =
|
|
std::source_location::current())
|
|
{
|
|
auto& ctx = Detail::TestContext::current();
|
|
if (ctx.current_result) {
|
|
ctx.current_result->state = TestState::Skipped;
|
|
ctx.current_result->skip_reason = reason;
|
|
}
|
|
throw Detail::RequireFailure{std::string{reason}};
|
|
}
|
|
|
|
} // namespace Void::Testing
|