Exception-based assertions for unit tests and defensive runtime checks.
- C++23 compiler and standard library
- Exception support
- Run-time type information (RTTI)
#include <kaycxx/assert.hpp>
using namespace kaycxx::assert;
int main() {
assert_equal(40 + 2, 42);
assert_match("hello", "h.*");
}CMake users consume the installed package with:
find_package(kaycxx-assert 1.0.0 CONFIG REQUIRED)
target_link_libraries(my-target PRIVATE kaycxx::assert)Non-CMake users can use pkg-config:
c++ $(pkg-config --cflags kaycxx-assert) -c main.cpp
c++ main.o $(pkg-config --libs kaycxx-assert)Assertion errors do not record source locations by default, because std::source_location can embed local absolute source paths into consumer binaries. Enable source locations only for test or debug targets:
target_compile_definitions(my-tests PRIVATE KAYCXX_ASSERT_SOURCE_LOCATION)Use PRIVATE unless you intentionally want this behavior to propagate to dependent targets.
Without CMake, pass the define directly to the compiler:
c++ -DKAYCXX_ASSERT_SOURCE_LOCATION $(pkg-config --cflags kaycxx-assert) -c main.cppThe assertion functions throw kaycxx::assert::assertion_error when the checked condition is not met.
- assert_true checks that a boolean value is
true. - assert_false checks that a boolean value is
false.
- assert_equal checks that two values are equal.
- assert_not_equal checks that two values are not equal.
- assert_greater checks that a value is greater than another value.
- assert_greater_or_equal checks that a value is greater than or equal to another value.
- assert_less checks that a value is less than another value.
- assert_less_or_equal checks that a value is less than or equal to another value.
- assert_null checks that a nullable value is null.
- assert_not_null checks that a nullable value is not null.
- assert_match checks that a string matches a regular expression.
- assert_not_match checks that a string does not match a regular expression.
- assert_close checks that two numeric values are within a maximum distance.
- assert_not_close checks that two numeric values are outside a maximum distance.
- assert_contain checks that a string or range contains a value.
- assert_not_contain checks that a string or range does not contain a value.
- assert_throw checks that a callable throws the expected exception.
- assert_not_throw checks that a callable does not throw.
cmake -B build
cmake --build buildA shared library is built by default. For a static build:
cmake -B build -D BUILD_SHARED_LIBS=OFF
cmake --build buildcmake --install build --prefix /tmp/rootIf no prefix is specified, CMake installs to /usr/local by default on Unix systems.
Run all tests:
cmake --build build --target testGenerate API documentation with Doxygen:
cmake --build build --target apidocThe generated HTML documentation is written to build/apidoc/html/index.html.