|
kaycxx-assert
C++ assertion library
|
Assertion functions and related types. More...
Classes | |
| class | assertion_error |
| Thrown when an assertion fails. More... | |
Functions | |
| template<typename Actual , typename Expected > requires detail::close_supported<Actual, Expected> | |
| void | assert_close (Actual const &actual, Expected const &expected, int precision=2, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that two numeric values are close enough. | |
| template<typename Actual , typename Expected > requires detail::contains_supported<Actual, Expected> | |
| void | assert_contain (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that a string contains a substring or character, or that a range contains an element. | |
| template<typename Actual , typename Expected > requires detail::equals_supported<Actual, Expected> | |
| void | assert_equal (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that two values are equal. | |
| void | assert_false (bool actual, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument is false. | |
| template<detail::ordered_value T> | |
| void | assert_greater (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the actual value is greater than the expected value. | |
| template<detail::ordered_value T> | |
| void | assert_greater_or_equal (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the actual value is greater than or equal to the expected value. | |
| template<detail::ordered_value T> | |
| void | assert_less (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the actual value is less than the expected value. | |
| template<detail::ordered_value T> | |
| void | assert_less_or_equal (T const &actual, T const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the actual value is less than or equal to the expected value. | |
| template<detail::string_like T> | |
| void | assert_match (T const &actual, std::string_view pattern, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument matches the given pattern. | |
| template<detail::string_like T> | |
| void | assert_match (T const &actual, std::regex const ®ex, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument matches the given regular expression. | |
| template<typename Actual , typename Expected > requires detail::close_supported<Actual, Expected> | |
| void | assert_not_close (Actual const &actual, Expected const &expected, int precision=2, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that two numeric values are not close enough. | |
| template<typename Actual , typename Expected > requires detail::contains_supported<Actual, Expected> | |
| void | assert_not_contain (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that a string does not contain a substring or character, or that a range does not contain an element. | |
| template<typename Actual , typename Expected > requires detail::equals_supported<Actual, Expected> | |
| void | assert_not_equal (Actual const &actual, Expected const &expected, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that two values are not equal. | |
| template<detail::string_like T> | |
| void | assert_not_match (T const &actual, std::string_view pattern, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument does not match the given pattern. | |
| template<detail::string_like T> | |
| void | assert_not_match (T const &actual, std::regex const ®ex, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument does not match the given regular expression. | |
| template<detail::nullable T> | |
| void | assert_not_null (T const &actual, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument is not null. | |
| template<std::invocable Function> | |
| void | assert_not_throw (Function &&function, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the function does not throw any exception. | |
| template<detail::exception_type Exception, std::invocable Function> | |
| void | assert_not_throw (Function &&function, std::string_view unexpected_message, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the function does not throw an exception of the given type with the exact unexpected message. | |
| template<detail::exception_type Exception, std::invocable Function> | |
| void | assert_not_throw (Function &&function, std::regex const &unexpected_message, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the function does not throw an exception of the given type with a message matching the regex. | |
| template<detail::nullable T> | |
| void | assert_null (T const &actual, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument is null. | |
| template<detail::exception_type Exception, std::invocable Function> | |
| void | assert_throw (Function &&function, std::string_view expected_message, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the function throws an exception of the expected type with the exact expected message. | |
| template<detail::exception_type Exception, std::invocable Function> | |
| void | assert_throw (Function &&function, std::regex const &expected_message, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the function throws an exception of the expected type with a message matching the regex. | |
| void | assert_true (bool actual, std::string_view reason={}, std::optional< std::source_location > location=KAYCXX_ASSERT_DEFAULT_SOURCE) |
| Asserts that the argument is true. | |
Assertion functions and related types.
| void kaycxx::assert::assert_close | ( | Actual const & | actual, |
| Expected const & | expected, | ||
| int | precision = 2, |
||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that two numeric values are close enough.
Values are close when they are equal after rounding to the given number of decimal digits. The default precision is 2 decimal digits.
| Actual | The actual value type. |
| Expected | The expected value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| precision | The number of decimal digits to round to before comparing. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the values are not close enough. |
| void kaycxx::assert::assert_contain | ( | Actual const & | actual, |
| Expected const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that a string contains a substring or character, or that a range contains an element.
String-like values use substring semantics. Other ranges use std::ranges::find and therefore check whether one element compares equal to the expected value.
| Actual | The actual value type. |
| Expected | The expected contained value type. |
| actual | The actual value to check. |
| expected | The expected contained value. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the actual value does not contain the expected value. |
| void kaycxx::assert::assert_equal | ( | Actual const & | actual, |
| Expected const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that two values are equal.
Non-string values must have the same unqualified type and are compared with operator==. C string forms may be compared with each other and with std::string or std::string_view values, and are compared by text content instead of pointer identity.
| Actual | The actual value type. |
| Expected | The expected value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the values are not equal. |
| void kaycxx::assert::assert_false | ( | bool | actual, |
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument is false.
| actual | The actual value to check. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value is not false. |
| void kaycxx::assert::assert_greater | ( | T const & | actual, |
| T const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the actual value is greater than the expected value.
The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.
| T | The ordered value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the actual value is not greater than the expected value. |
| void kaycxx::assert::assert_greater_or_equal | ( | T const & | actual, |
| T const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the actual value is greater than or equal to the expected value.
The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.
| T | The ordered value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the actual value is less than the expected value. |
| void kaycxx::assert::assert_less | ( | T const & | actual, |
| T const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the actual value is less than the expected value.
The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.
| T | The ordered value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the actual value is not less than the expected value. |
| void kaycxx::assert::assert_less_or_equal | ( | T const & | actual, |
| T const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the actual value is less than or equal to the expected value.
The value type must provide a complete ordering. Raw pointers are rejected because their relational operators compare addresses instead of pointed-to values.
| T | The ordered value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the actual value is greater than the expected value. |
| void kaycxx::assert::assert_match | ( | T const & | actual, |
| std::regex const & | regex, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument matches the given regular expression.
| T | The string-like value type. |
| actual | The actual value to check. |
| regex | The regular expression to match. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value does not match the regular expression. |
| void kaycxx::assert::assert_match | ( | T const & | actual, |
| std::string_view | pattern, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument matches the given pattern.
| T | The string-like value type. |
| actual | The actual value to check. |
| pattern | The pattern to match. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value does not match the pattern. |
| void kaycxx::assert::assert_not_close | ( | Actual const & | actual, |
| Expected const & | expected, | ||
| int | precision = 2, |
||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that two numeric values are not close enough.
Values are close when they are equal after rounding to the given number of decimal digits. The default precision is 2 decimal digits.
| Actual | The actual value type. |
| Expected | The expected value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| precision | The number of decimal digits to round to before comparing. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the values are close enough. |
| void kaycxx::assert::assert_not_contain | ( | Actual const & | actual, |
| Expected const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that a string does not contain a substring or character, or that a range does not contain an element.
String-like values use substring semantics. Other ranges use std::ranges::find and therefore check whether one element compares equal to the expected value.
| Actual | The actual value type. |
| Expected | The unexpected contained value type. |
| actual | The actual value to check. |
| expected | The unexpected contained value. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the actual value contains the expected value. |
| void kaycxx::assert::assert_not_equal | ( | Actual const & | actual, |
| Expected const & | expected, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that two values are not equal.
Non-string values must have the same unqualified type and are compared with operator==. C string forms may be compared with each other and with std::string or std::string_view values, and are compared by text content instead of pointer identity.
| Actual | The actual value type. |
| Expected | The expected value type. |
| actual | The actual value to check. |
| expected | The expected value to compare to. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the values are equal. |
| void kaycxx::assert::assert_not_match | ( | T const & | actual, |
| std::regex const & | regex, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument does not match the given regular expression.
| T | The string-like value type. |
| actual | The actual value to check. |
| regex | The regular expression not to match. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value matches the regular expression. |
| void kaycxx::assert::assert_not_match | ( | T const & | actual, |
| std::string_view | pattern, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument does not match the given pattern.
| T | The string-like value type. |
| actual | The actual value to check. |
| pattern | The pattern not to match. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value matches the pattern. |
| void kaycxx::assert::assert_not_null | ( | T const & | actual, |
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument is not null.
| T | The nullable value type. |
| actual | The actual value to check. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value is null. |
| void kaycxx::assert::assert_not_throw | ( | Function && | function, |
| std::regex const & | unexpected_message, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the function does not throw an exception of the given type with a message matching the regex.
Derived exception types match the unexpected type, like instanceof in JavaScript. Other exception types or non-matching messages are allowed.
| Exception | The unexpected exception type. |
| Function | The function type. |
| function | The function to call. |
| unexpected_message | The regular expression the exception message must not match. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the function throws the matching exception. |
| void kaycxx::assert::assert_not_throw | ( | Function && | function, |
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the function does not throw any exception.
| Function | The function type. |
| function | The function to call. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the function throws. |
| void kaycxx::assert::assert_not_throw | ( | Function && | function, |
| std::string_view | unexpected_message, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the function does not throw an exception of the given type with the exact unexpected message.
Derived exception types match the unexpected type, like instanceof in JavaScript. Other exception types or different messages are allowed.
| Exception | The unexpected exception type. |
| Function | The function type. |
| function | The function to call. |
| unexpected_message | The exact unexpected exception message. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the function throws the matching exception. |
| void kaycxx::assert::assert_null | ( | T const & | actual, |
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument is null.
| T | The nullable value type. |
| actual | The actual value to check. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value is not null. |
| void kaycxx::assert::assert_throw | ( | Function && | function, |
| std::regex const & | expected_message, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the function throws an exception of the expected type with a message matching the regex.
Derived exception types match the expected type, like instanceof in JavaScript. The expected exception type must derive from std::exception because assert_throw checks the thrown message through what().
| Exception | The expected exception type. |
| Function | The function type. |
| function | The function to call. |
| expected_message | The regular expression the exception message must match. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the function does not throw, throws the wrong type, or throws a wrong message. |
| void kaycxx::assert::assert_throw | ( | Function && | function, |
| std::string_view | expected_message, | ||
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the function throws an exception of the expected type with the exact expected message.
Derived exception types match the expected type, like instanceof in JavaScript. The expected exception type must derive from std::exception because assert_throw checks the thrown message through what().
| Exception | The expected exception type. |
| Function | The function type. |
| function | The function to call. |
| expected_message | The exact expected exception message. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the function does not throw, throws the wrong type, or throws a wrong message. |
| void kaycxx::assert::assert_true | ( | bool | actual, |
| std::string_view | reason = {}, |
||
| std::optional< std::source_location > | location = KAYCXX_ASSERT_DEFAULT_SOURCE |
||
| ) |
Asserts that the argument is true.
| actual | The actual value to check. |
| reason | Optional reason added to the assertion error. |
| location | Optional source location of the assertion call. |
| assertion_error | When the value is not true. |