Expectations
Expectations are checks whose outcomes are counted and reported. More...
Functions Index
template <class Expr_T> | |
| detail::check_result && | expect (const Expr_T &expr, const reflection::source_location &sl=reflection::source_location::current(), detail::check_result &&result=detail::check_result{}) |
|
Evaluates a condition and reports the result. More... | |
Description
Expectations are checks whose outcomes are counted and reported.
t.expect (condition) checks a condition, counts the result, and reports it; when the check fails, the test case continues, so that all its checks are evaluated and reported in one run.
The condition is one of:
- a function comparator, such as mt::eq (actual, 42), or a combination of them, which reports the values of its operands;
- an exception check, such as mt::throws (...);
- any other value that converts to bool in a condition, such as x == 1, a pointer, or a std::optional<int>, which reports only the location of the failed check.
The values that would make the check pass by mistake do not compile: the string literals and the other character arrays and pointers, the function names, the pointers to functions and to members, and the wrappers of a bool (std::optional<bool> and the smart pointers to bool), whose condition tells whether a value is present, not what it is. The scoped enumerations do not compile either, since only a cast converts them. Write the comparison instead, such as mt::eq (flag.value (), true). With the deprecated definitions enabled, all of these are accepted, with a deprecation warning, except the wrappers of a bool and the scoped enumerations.
A message can be appended with <<, with any values that can be written, for example t.expect (x == 2) << "x is " << x;. The check is reported at the end of the statement, with the message, or, without one, with the condition.
- Example
Both checks fail; the human-readable reporter displays:
Functions
expect()
|
Evaluates a condition and reports the result.
- Template Parameters
-
Expr_T The type of the custom expression.
- Constraints
Enabled only if Expr_T is a framework expression type or is contextually convertible to bool, including by an explicit operator bool (enforced via a C++20 requires clause, with extension::checkable).
- Parameters
-
expr Logical expression to evaluate.
sl Optional source location, defaulting to the current line.
result The check result, a temporary created by the caller as the default argument, which the applications do not pass; it is destroyed at the end of the statement.
- Returns
A reference to the check result, to which optional messages can be appended with operator<<; the check is reported when it is destroyed, at the end of the statement.
The result must not be named, stored, or kept in any variable. A reference such as auto&& r = t.expect (...) compiles, but dangles after the statement; the message operator and value() can be called only on a temporary, so the named result cannot be used, and forcing it with std::move() is undefined. The check must be reported at the end of the statement, and not later. Write the message in the same statement, with operator<<.
Initialises the detail::check_result received as the last argument, a temporary created by the caller, with the kind detail::check_kind::expectation, and returns a reference to it. The check result evaluates the condition when initialised, and records a pass or fail when it is destroyed, at the end of the expression statement.
Declaration at line 1628 of file test.h, definition at line 623 of file test-inlines.h.
References micro_os_plus::micro_test_plus::detail::expectation, micro_os_plus::micro_test_plus::detail::check_result::init, micro_os_plus::micro_test_plus::test_case::is_misdirected, micro_os_plus::micro_test_plus::test_case::report_misdirected and micro_os_plus::micro_test_plus::detail::runnable_base::test_reporter.
Referenced by micro_os_plus::micro_test_plus::test_case::operator=.
Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.