Skip to content

kaycxx/assert

Repository files navigation

kaycxx-assert

Exception-based assertions for unit tests and defensive runtime checks.

GitHub | API Documentation

Requirements

  • C++23 compiler and standard library
  • Exception support
  • Run-time type information (RTTI)

Usage

#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)

Source Locations

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.cpp

Assertions

The assertion functions throw kaycxx::assert::assertion_error when the checked condition is not met.

Boolean

Equality

Ordering

Nullness

Matching

  • assert_match checks that a string matches a regular expression.
  • assert_not_match checks that a string does not match a regular expression.

Numeric Distance

  • 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.

Containment

Exceptions

Build From Source

cmake -B build
cmake --build build

A shared library is built by default. For a static build:

cmake -B build -D BUILD_SHARED_LIBS=OFF
cmake --build build

Install

cmake --install build --prefix /tmp/root

If no prefix is specified, CMake installs to /usr/local by default on Unix systems.

Development

Run all tests:

cmake --build build --target test

Generate API documentation with Doxygen:

cmake --build build --target apidoc

The generated HTML documentation is written to build/apidoc/html/index.html.

Sponsor this project

Contributors