Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
Test

A minimal unit testing framework. More...

Classes

struct  sourcemeta::core::TestAbortError

Functions

SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_register (std::string_view suite, std::string_view name, std::string_view file, int line, std::function< void()> body) -> int
SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_register (std::string_view name, std::function< void()> body, std::source_location location=std::source_location::current()) -> int
SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_report_failure (std::string_view file, int line, std::string_view message) -> void
SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_suite_from_path (std::string_view path) -> std::string
SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_run (int argc, char **argv) -> int
SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_stringify_difference (std::string_view actual, std::string_view expected) -> std::string

Detailed Description

A minimal unit testing framework.

This functionality is included as follows:

#include <sourcemeta/core/test.h>

Class Documentation

◆ sourcemeta::core::TestAbortError

struct sourcemeta::core::TestAbortError

Thrown to abort the current test on the first failed expectation. It does not derive from the standard exception hierarchy so that a broad catch block in a test does not accidentally swallow it.

Public Attributes

std::string message
 The message describing the failed expectation.

Function Documentation

◆ test_register() [1/2]

SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_register ( std::string_view name,
std::function< void()> body,
std::source_location location = std::source_location::current() ) -> int

Register a test case whose suite and source location are taken from the call site. Intended for suites that discover their cases at runtime.

◆ test_register() [2/2]

SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_register ( std::string_view suite,
std::string_view name,
std::string_view file,
int line,
std::function< void()> body ) -> int

Register a test by name with a callable body. Returns a dummy value so it can be used to initialise a namespace-scope variable.

◆ test_report_failure()

SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_report_failure ( std::string_view file,
int line,
std::string_view message ) -> void

Print a failure at the given location and abort the current test.

◆ test_run()

SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_run ( int argc,
char ** argv ) -> int

Run every registered test, optionally filtered, and return a process exit code.

◆ test_stringify_difference()

SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_stringify_difference ( std::string_view actual,
std::string_view expected ) -> std::string

Render the differences between two strings in the unified format, or nothing if showing both values as they are is just as informative.

◆ test_suite_from_path()

SOURCEMETA_CORE_TEST_EXPORT auto sourcemeta::core::test_suite_from_path ( std::string_view path) -> std::string

Derive a suite name from a source file path by taking its stem and stripping a trailing _test suffix.