1 parent
e7186f17f0
commit
f0825fbd1a
13 files changed
+105
-36
No files matched your search
@@ -4,6 +4,8 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
|
|||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|
||||||
|
Requires CMake 4.0+ and a C++23 compiler (clang++ is the default in the configure command below).
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Configure (clang++, Ninja, Debug)
|
# Configure (clang++, Ninja, Debug)
|
||||||
cmake -B build -S . -G Ninja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Debug
|
cmake -B build -S . -G Ninja -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Debug
|
||||||
@@ -29,7 +31,7 @@ VoidLibrary is a C++23 modular shared library. Each module is an independently i
|
|||||||
|
|
||||||
### Module system
|
### Module system
|
||||||
|
|
||||||
`cmake/Module.cmake` defines `void_add_module(NAME ...)`. The root `CMakeLists.txt` auto-discovers every subdirectory under `src/` and calls it. A module with only headers becomes an `INTERFACE` library; one with `.cpp` sources becomes a `SHARED` library.
|
`cmake/Module.cmake` defines `void_add_module(NAME ...)`. The root `CMakeLists.txt` auto-discovers every subdirectory under `src/` and calls it. A module with only headers becomes an `INTERFACE` library; one with `.cpp` sources becomes `SHARED` — except `Testing`, which is special-cased to `STATIC`.
|
||||||
|
|
||||||
Module layout:
|
Module layout:
|
||||||
- Headers: `include/Void/<ModuleName>/*.hpp`
|
- Headers: `include/Void/<ModuleName>/*.hpp`
|
||||||
@@ -39,7 +41,10 @@ Module layout:
|
|||||||
|
|
||||||
To add a new module, create `src/<ModuleName>/` and populate it — the build system picks it up automatically. If a module needs special dependency wiring, drop a `module.cmake` file inside the source directory (see `cmake/module.cmake.template` for the pattern).
|
To add a new module, create `src/<ModuleName>/` and populate it — the build system picks it up automatically. If a module needs special dependency wiring, drop a `module.cmake` file inside the source directory (see `cmake/module.cmake.template` for the pattern).
|
||||||
|
|
||||||
External dependencies are fetched via `cmake/Dependencies.cmake` using `FetchContent`; add new ones there and reference them via `EXTERNAL_DEPS` in `void_add_module`.
|
`void_add_module` accepts three dependency lists:
|
||||||
|
- `INTERNAL_DEPS` — other `Void::*` modules (linked unconditionally).
|
||||||
|
- `EXTERNAL_DEPS` — third-party targets fetched via `cmake/Dependencies.cmake` (`FetchContent`); add new ones there before referencing.
|
||||||
|
- `OPTIONAL_DEPS` — `Void::*` modules linked only if they exist; each present dep additionally defines `VOID_HAS_<UPPERCASE>` for conditional code.
|
||||||
|
|
||||||
### Tests
|
### Tests
|
||||||
|
|
||||||
@@ -52,6 +57,32 @@ External dependencies are fetched via `cmake/Dependencies.cmake` using `FetchCon
|
|||||||
|
|
||||||
Standalone tests are preferred for testing a single module in isolation.
|
Standalone tests are preferred for testing a single module in isolation.
|
||||||
|
|
||||||
|
#### Writing a test
|
||||||
|
|
||||||
|
Tests are free functions (or methods on a `Void::Testing::Fixture` subclass) returning `Void::Testing::Test` or `Void::Testing::DisabledTest` (both aliases for `void`). **Never write the registration code yourself** — `cmake/VoidTestScanner.cmake` parses each `.cpp` at configure time, generates `*.void_tests.cpp` shims with `Void::Testing::Detail::test_registrar` calls, and rewrites the original source to `HEADER_FILE_ONLY`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include <Void/Testing/Assert.hpp>
|
||||||
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
Void::Testing::Test addition() { Void::Testing::expect_eq(1 + 1, 2); }
|
||||||
|
|
||||||
|
// @tag integration, database
|
||||||
|
Void::Testing::Test db_query() { Void::Testing::expect(true); }
|
||||||
|
|
||||||
|
// @skip "Feature #42 not implemented"
|
||||||
|
Void::Testing::DisabledTest broken() { Void::Testing::fail("not ready"); }
|
||||||
|
```
|
||||||
|
|
||||||
|
Fixture-based tests work the same way — declare a class derived from `Void::Testing::Fixture`, override `setup()`/`teardown()`, and write methods returning `Test`/`DisabledTest`.
|
||||||
|
|
||||||
|
Comment metadata (immediately preceding the function):
|
||||||
|
- `// @tag a, b, c` — attaches tags.
|
||||||
|
- `// @skip <reason>` — marks the test skipped with a reason; quoted or unquoted strings both work. `DisabledTest` implies `@skip "disabled"` if no explicit reason is given.
|
||||||
|
- Any non-comment, non-empty line resets the accumulated tags/skip — keep the comment block adjacent to the function.
|
||||||
|
|
||||||
|
The sweep in `cmake/VoidTesting.cmake` auto-applies the scan to every executable that transitively links `Void::Testing`. To opt a target out, set the `VOID_TESTING_NO_AUTO_SCAN` property; to opt in explicitly (e.g. for a target that doesn't link `Void::Testing` directly), call `void_add_tests(<target>)`.
|
||||||
|
|
||||||
### Logging module
|
### Logging module
|
||||||
|
|
||||||
`Void::Logging::Logger` is an abstract base class. Derive from it to create a sink:
|
`Void::Logging::Logger` is an abstract base class. Derive from it to create a sink:
|
||||||
|
|||||||
@@ -2,7 +2,28 @@ include(FetchContent)
|
|||||||
|
|
||||||
function(void_fetch_dependency DEP_NAME)
|
function(void_fetch_dependency DEP_NAME)
|
||||||
if(DEP_NAME STREQUAL "Boost")
|
if(DEP_NAME STREQUAL "Boost")
|
||||||
find_package(Boost REQUIRED COMPONENTS stacktrace_backtrace)
|
if(TARGET Boost::stacktrace_backtrace)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
find_package(Boost QUIET COMPONENTS stacktrace_backtrace)
|
||||||
|
if(TARGET Boost::stacktrace_backtrace)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
message(STATUS "[Void] Boost::stacktrace_backtrace not found — falling back to FetchContent")
|
||||||
|
set(BOOST_INCLUDE_LIBRARIES stacktrace CACHE STRING "" FORCE)
|
||||||
|
set(BOOST_ENABLE_CMAKE ON CACHE BOOL "" FORCE)
|
||||||
|
FetchContent_Declare(Boost
|
||||||
|
URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz
|
||||||
|
URL_HASH SHA256=2e64e5d79a738d0fa6fb546c6e5c2bd28f88d268a2a080546f74e5ff98f29d0e
|
||||||
|
EXCLUDE_FROM_ALL
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(Boost)
|
||||||
|
|
||||||
|
if(NOT TARGET Boost::stacktrace_backtrace AND TARGET boost_stacktrace_backtrace)
|
||||||
|
add_library(Boost::stacktrace_backtrace ALIAS boost_stacktrace_backtrace)
|
||||||
|
endif()
|
||||||
return()
|
return()
|
||||||
endif()
|
endif()
|
||||||
# FIXME: Add if needed external
|
# FIXME: Add if needed external
|
||||||
|
|||||||
+20
-1
@@ -2,7 +2,9 @@ set_property(GLOBAL PROPERTY VOID_MODULES "")
|
|||||||
set_property(GLOBAL PROPERTY VOID_EXT_DEPS "")
|
set_property(GLOBAL PROPERTY VOID_EXT_DEPS "")
|
||||||
|
|
||||||
function(void_add_module MODULE_NAME)
|
function(void_add_module MODULE_NAME)
|
||||||
cmake_parse_arguments(ARG "" "" "INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS" ${ARGN})
|
cmake_parse_arguments(ARG "" ""
|
||||||
|
"INTERNAL_DEPS;EXTERNAL_DEPS;OPTIONAL_DEPS;DEPS_WIN32;DEPS_UNIX;OPTIONS_WIN32;OPTIONS_UNIX"
|
||||||
|
${ARGN})
|
||||||
|
|
||||||
set(TGT ${PROJECT_NAME}_${MODULE_NAME})
|
set(TGT ${PROJECT_NAME}_${MODULE_NAME})
|
||||||
set(ALIAS ${PROJECT_NAME}::${MODULE_NAME})
|
set(ALIAS ${PROJECT_NAME}::${MODULE_NAME})
|
||||||
@@ -56,6 +58,23 @@ function(void_add_module MODULE_NAME)
|
|||||||
if(ARG_EXTERNAL_DEPS)
|
if(ARG_EXTERNAL_DEPS)
|
||||||
target_link_libraries(${TGT} ${SCOPE} ${ARG_EXTERNAL_DEPS})
|
target_link_libraries(${TGT} ${SCOPE} ${ARG_EXTERNAL_DEPS})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
foreach(_lib IN LISTS ARG_DEPS_WIN32)
|
||||||
|
target_link_libraries(${TGT} ${SCOPE} "$<$<PLATFORM_ID:Windows>:${_lib}>")
|
||||||
|
endforeach()
|
||||||
|
foreach(_lib IN LISTS ARG_DEPS_UNIX)
|
||||||
|
target_link_libraries(${TGT} ${SCOPE} "$<$<NOT:$<PLATFORM_ID:Windows>>:${_lib}>")
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
if(SRC)
|
||||||
|
foreach(_opt IN LISTS ARG_OPTIONS_WIN32)
|
||||||
|
target_compile_options(${TGT} PRIVATE "$<$<PLATFORM_ID:Windows>:${_opt}>")
|
||||||
|
endforeach()
|
||||||
|
foreach(_opt IN LISTS ARG_OPTIONS_UNIX)
|
||||||
|
target_compile_options(${TGT} PRIVATE "$<$<NOT:$<PLATFORM_ID:Windows>>:${_opt}>")
|
||||||
|
endforeach()
|
||||||
|
endif()
|
||||||
|
|
||||||
set_target_properties(${TGT} PROPERTIES
|
set_target_properties(${TGT} PROPERTIES
|
||||||
CXX_STANDARD 23
|
CXX_STANDARD 23
|
||||||
CXX_STANDARD_REQUIRED ON
|
CXX_STANDARD_REQUIRED ON
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace Void::Testing::Detail::Color {
|
namespace Void::Stacktrace::Color {
|
||||||
inline bool g_color_enabled{true};
|
inline bool g_color_enabled{true};
|
||||||
|
|
||||||
constexpr std::string_view reset = "\033[0m";
|
constexpr std::string_view reset = "\033[0m";
|
||||||
@@ -31,4 +31,4 @@ inline std::string info(std::string_view text) { return apply(bold_cyan, tex
|
|||||||
inline std::string hint(std::string_view text) { return apply(dim, text); }
|
inline std::string hint(std::string_view text) { return apply(dim, text); }
|
||||||
inline std::string heading(std::string_view text) { return apply(bold_blue, text); }
|
inline std::string heading(std::string_view text) { return apply(bold_blue, text); }
|
||||||
inline std::string emphasis(std::string_view text) { return apply(bold, text); }
|
inline std::string emphasis(std::string_view text) { return apply(bold, text); }
|
||||||
} // namespace Void::Testing::Detail::Color
|
} // namespace Void::Stacktrace::Color
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
namespace Void::Testing::Detail {
|
namespace Void::Stacktrace {
|
||||||
struct StackFrame {
|
struct StackFrame {
|
||||||
std::string function_name;
|
std::string function_name;
|
||||||
std::string source_file;
|
std::string source_file;
|
||||||
@@ -15,4 +15,4 @@ std::vector<StackFrame> capture_stacktrace(unsigned skip_frames = 0, unsigned ma
|
|||||||
std::string read_source_snippet(const std::string& file, int line, int context_lines = 3);
|
std::string read_source_snippet(const std::string& file, int line, int context_lines = 3);
|
||||||
std::string format_stacktrace(const std::vector<StackFrame>& frames);
|
std::string format_stacktrace(const std::vector<StackFrame>& frames);
|
||||||
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames);
|
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames);
|
||||||
} // namespace Void::Testing::Detail
|
} // namespace Void::Stacktrace
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Void/Testing/Reporter.hpp>
|
#include <Void/Testing/Reporter.hpp>
|
||||||
#include <Void/Testing/Detail/Color.hpp>
|
#include <Void/Stacktrace/Color.hpp>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
namespace Void::Testing {
|
namespace Void::Testing {
|
||||||
class ConsoleReporter final : public Reporter {
|
class ConsoleReporter final : public Reporter {
|
||||||
public:
|
public:
|
||||||
explicit ConsoleReporter(std::ostream& os, bool color_enabled = true) : os_(os) {
|
explicit ConsoleReporter(std::ostream& os, bool color_enabled = true) : os_(os) {
|
||||||
Detail::Color::g_color_enabled = color_enabled;
|
Void::Stacktrace::Color::g_color_enabled = color_enabled;
|
||||||
}
|
}
|
||||||
void on_run_start(const RunConfig& config, int total_tests) override;
|
void on_run_start(const RunConfig& config, int total_tests) override;
|
||||||
void on_suite_start(const std::string& suite_name, int test_count) override;
|
void on_suite_start(const std::string& suite_name, int test_count) override;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <Void/Testing/Detail/Stacktrace.hpp>
|
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||||
#include <Void/Testing/Detail/Color.hpp>
|
#include <Void/Stacktrace/Color.hpp>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <format>
|
#include <format>
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
#include <boost/stacktrace.hpp>
|
#include <boost/stacktrace.hpp>
|
||||||
#include <boost/core/demangle.hpp>
|
#include <boost/core/demangle.hpp>
|
||||||
|
|
||||||
namespace Void::Testing::Detail {
|
namespace Void::Stacktrace {
|
||||||
|
|
||||||
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames, unsigned max_frames) {
|
std::vector<StackFrame> capture_stacktrace(unsigned skip_frames, unsigned max_frames) {
|
||||||
std::vector<StackFrame> result;
|
std::vector<StackFrame> result;
|
||||||
@@ -61,4 +61,4 @@ std::string format_stacktrace(const std::vector<StackFrame>& frames) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames) { os << format_stacktrace(frames); }
|
void print_stacktrace(std::ostream& os, const std::vector<StackFrame>& frames) { os << format_stacktrace(frames); }
|
||||||
} // namespace Void::Testing::Detail
|
} // namespace Void::Stacktrace
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
void_add_module(Stacktrace
|
||||||
|
EXTERNAL_DEPS Boost::stacktrace_backtrace
|
||||||
|
DEPS_WIN32 dbghelp
|
||||||
|
DEPS_UNIX dl
|
||||||
|
OPTIONS_UNIX -g -fno-omit-frame-pointer
|
||||||
|
)
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#include <Void/Testing/Assert.hpp>
|
#include <Void/Testing/Assert.hpp>
|
||||||
#include <Void/Testing/Detail/Stacktrace.hpp>
|
|
||||||
namespace Void::Testing::Detail {
|
namespace Void::Testing::Detail {
|
||||||
void handle_assertion(AssertionResult result) {
|
void handle_assertion(AssertionResult result) {
|
||||||
auto& ctx = TestContext::current();
|
auto& ctx = TestContext::current();
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
#include <Void/Testing/ConsoleReporter.hpp>
|
#include <Void/Testing/ConsoleReporter.hpp>
|
||||||
#include <Void/Testing/Detail/Color.hpp>
|
#include <Void/Stacktrace/Color.hpp>
|
||||||
#include <Void/Testing/Detail/SignalHandler.hpp>
|
#include <Void/Testing/Detail/SignalHandler.hpp>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
namespace Void::Testing {
|
namespace Void::Testing {
|
||||||
using namespace Detail::Color;
|
using namespace Void::Stacktrace::Color;
|
||||||
|
|
||||||
void ConsoleReporter::on_run_start(const RunConfig&, int total_tests) {
|
void ConsoleReporter::on_run_start(const RunConfig&, int total_tests) {
|
||||||
os_ << "\n" << apply(bold_blue, "╭──────────────────────────────────────────────╮") << "\n"
|
os_ << "\n" << apply(bold_blue, "╭──────────────────────────────────────────────╮") << "\n"
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
#include "Void/Testing/Config.hpp"
|
#include "Void/Testing/Config.hpp"
|
||||||
#include <Void/Testing/Assert.hpp>
|
#include <Void/Testing/Assert.hpp>
|
||||||
#include <Void/Testing/ConsoleReporter.hpp>
|
#include <Void/Testing/ConsoleReporter.hpp>
|
||||||
#include <Void/Testing/Detail/Color.hpp>
|
#include <Void/Stacktrace/Color.hpp>
|
||||||
|
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||||
#include <Void/Testing/Detail/Registry.hpp>
|
#include <Void/Testing/Detail/Registry.hpp>
|
||||||
#include <Void/Testing/Detail/SignalHandler.hpp>
|
#include <Void/Testing/Detail/SignalHandler.hpp>
|
||||||
#include <Void/Testing/Detail/Stacktrace.hpp>
|
|
||||||
#include <Void/Testing/JUnitReporter.hpp>
|
#include <Void/Testing/JUnitReporter.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
@@ -161,13 +161,13 @@ namespace Void::Testing
|
|||||||
result.state = TestState::Failed;
|
result.state = TestState::Failed;
|
||||||
result.crash_info =
|
result.crash_info =
|
||||||
std::format("Unhandled exception: {}", e.what());
|
std::format("Unhandled exception: {}", e.what());
|
||||||
result.stacktrace =
|
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
||||||
Detail::format_stacktrace(Detail::capture_stacktrace(1, 8));
|
Void::Stacktrace::capture_stacktrace(1, 8));
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
result.state = TestState::Failed;
|
result.state = TestState::Failed;
|
||||||
result.crash_info = "Unhandled unknown exception";
|
result.crash_info = "Unhandled unknown exception";
|
||||||
result.stacktrace =
|
result.stacktrace = Void::Stacktrace::format_stacktrace(
|
||||||
Detail::format_stacktrace(Detail::capture_stacktrace(1, 8));
|
Void::Stacktrace::capture_stacktrace(1, 8));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
@@ -209,9 +209,9 @@ namespace Void::Testing
|
|||||||
|
|
||||||
RunResult Runner::run(const RunConfig& config)
|
RunResult Runner::run(const RunConfig& config)
|
||||||
{
|
{
|
||||||
Detail::Color::g_color_enabled = !config.no_color;
|
Void::Stacktrace::Color::g_color_enabled = !config.no_color;
|
||||||
if (config.show_help) {
|
if (config.show_help) {
|
||||||
using namespace Detail::Color;
|
using namespace Void::Stacktrace::Color;
|
||||||
|
|
||||||
std::cout
|
std::cout
|
||||||
<< "\n"
|
<< "\n"
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <Void/Testing/Detail/SignalHandler.hpp>
|
#include <Void/Testing/Detail/SignalHandler.hpp>
|
||||||
#include <Void/Testing/Detail/Stacktrace.hpp>
|
#include <Void/Stacktrace/Stacktrace.hpp>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
@@ -30,8 +30,8 @@ const char* signal_name(int sig) {
|
|||||||
|
|
||||||
static void crash_handler(int sig) {
|
static void crash_handler(int sig) {
|
||||||
g_last_signal = sig;
|
g_last_signal = sig;
|
||||||
auto frames = capture_stacktrace(1, 16);
|
auto frames = Void::Stacktrace::capture_stacktrace(1, 16);
|
||||||
auto trace = format_stacktrace(frames);
|
auto trace = Void::Stacktrace::format_stacktrace(frames);
|
||||||
if (g_crash_callback) g_crash_callback(sig, trace);
|
if (g_crash_callback) g_crash_callback(sig, trace);
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
if (g_jump_ready) { g_jump_ready = false; siglongjmp(g_jump_buffer, sig); }
|
if (g_jump_ready) { g_jump_ready = false; siglongjmp(g_jump_buffer, sig); }
|
||||||
|
|||||||
@@ -1,8 +1 @@
|
|||||||
void_add_module(Testing EXTERNAL_DEPS Boost::stacktrace_backtrace)
|
void_add_module(Testing INTERNAL_DEPS Stacktrace)
|
||||||
|
|
||||||
if(WIN32)
|
|
||||||
target_link_libraries(Void_Testing PUBLIC dbghelp)
|
|
||||||
else()
|
|
||||||
target_link_libraries(Void_Testing PUBLIC dl)
|
|
||||||
target_compile_options(Void_Testing PRIVATE -g -fno-omit-frame-pointer)
|
|
||||||
endif()
|
|
||||||
Reference in new issue
Block a user