dot-xx
A personal collection of small C++ modules
Loading...
Searching...
No Matches
dxx::selftest Namespace Reference

Main namespace of dxx::selftest module. More...

Classes

struct  test_failure
 The exception type used by test assertions. More...
class  UnitTest
 Create an object of that class to declare a unit test. More...

Concepts

concept  named_test_body

Typedefs

using TestBody = std::function<void()>
using TestsMap = std::map<std::string, TestBody>

Functions

template<typename T>
consteval std::string_view function_name ()
 Forwards its own std::source_location::function_name()
template<typename T>
consteval auto function_name_sanitize ()
 Removes anonymous namespace mentions from the function name.
template<typename T>
consteval std::tuple< std::optional< std::string_view >, uz > get_test_name_cpp ()
 Gets the C++ name of the type's scope (if it starts with test::) and gets rid of the test:: part, e.g. for test::a::b::c it will return a::b::c
template<typename T>
consteval auto get_test_name_data ()
 Convertes get_test_name_cpp return value into a fixed length character array (or an empty optional)
template<typename T>
consteval std::optional< std::string_view > get_test_name ()
 Get an optional string_view with the test name, or nullopt if not a test for some reason.
void dxx::selftest::test (bool condition, std::source_location st=std::source_location::current())
 Test assertion. Calls dxx::assert::always with test_failure.
int dxx::selftest::run_all (const std::unordered_set< std::string > &filters={})
 Run all tests that match a filter.

Variables

constexpr auto test_prefix = "T = test::"sv
constexpr std::array anonymous_indicators
template<typename T>
constexpr auto test_name = get_test_name_data<T>()
 A constexpr string_view returned by get_test_name should point to somewhere in the program memmory. This is that somewhere.
constinit std::unique_ptr< TestsMap > all_tests = nullptr
 Storage for all unit tests.

Detailed Description

Main namespace of dxx::selftest module.

Function Documentation

◆ function_name()

template<typename T>
std::string_view dxx::selftest::function_name ( )
consteval

Forwards its own std::source_location::function_name()

Required because we want to use it as a constexpr variable and some compilers may skip specifying the template parameters for such. So we wrap it in a consteval function within which this value is not constexpr, but outside of which it can be used as such.

◆ get_test_name_cpp()

template<typename T>
std::tuple< std::optional< std::string_view >, uz > dxx::selftest::get_test_name_cpp ( )
consteval

Gets the C++ name of the type's scope (if it starts with test::) and gets rid of the test:: part, e.g. for test::a::b::c it will return a::b::c

Returns
A tuple of C++ name of the type's scope (or nullopt if the scope is not test scope) and an expected length of the test name string

◆ run_all()

int dxx::selftest::run_all ( const std::unordered_set< std::string > & filters = {})
export

Run all tests that match a filter.

filters
test filters. Only tests that match at least one filter will be performed. If no filters are specified, all tests will be run

The tests are declared by creating objects of unit_test, you would usually want to declare them as static const ... objects in a translation unit:

// in file test_example.cc
static const dxx::selftest::unit_test test{
"/path/to/test", [] {
dxx::selftest::test(important_condition);
}
};
void test(bool condition, std::source_location st=std::source_location::current())
Test assertion. Calls dxx::assert::always with test_failure.
Definition selftest.xx:127

The test is successful if it does not produce exceptions. The test is failed if it produced an exception of type test_failure (or derived). If the test produces any other exception type, it is not caught by this function and will lead to program termination if not caught by code surrounding run_all.

Test names could be any strings, but the filtering mechanism expects them to follow the UNIX-like filename format: starting with "/" and using it to further create the test "tree". Each filter in filters is tested as follows:

  • if the filter starts with "/", the "abolute path" to the test is matched: this means that the test name must start with the filter value
  • if the filter does not start with "/" any test that contains the filter as a substring in its name matches that filter

Variable Documentation

◆ all_tests

std::unique_ptr<TestsMap> dxx::selftest::all_tests = nullptr
inlineconstinit

Storage for all unit tests.

constinit to ensure the smart pointer is initialized before the tests are

◆ anonymous_indicators

std::array dxx::selftest::anonymous_indicators
inlineconstexpr
Initial value:
{
"{anonymous}::"sv,
"(anonymous namespace)::"sv,
}