micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
Expectations

Expectations are checks whose outcomes are counted and reported. More...

Functions

template<class Expr_T>
requires extension::checkable<Expr_T>
detail::check_result && micro_os_plus::micro_test_plus::test_case::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.

Detailed Description

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
ts.test ("Check the values", [] (auto& t) {
int x = 1;
t.expect (mt::eq (x, 2));
t.expect (x == 2) << "x is " << x;
});

Both checks fail; the human-readable reporter displays:

• Check the values
✗ FAILED (main.cpp:13, 1 == 2)
✗ x is 1 FAILED (main.cpp:14)

Function Documentation

◆ expect()

template<class Expr_T>
requires extension::checkable<Expr_T>
detail::check_result && micro_os_plus::micro_test_plus::test_case::expect ( const Expr_T & expr,
const reflection::source_location & sl = reflection::source_location::current (),
detail::check_result && result = detail::check_result{} )
Template Parameters
Expr_TThe 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
exprLogical expression to evaluate.
slOptional source location, defaulting to the current line.
resultThe 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.
Warning
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.

Definition at line 623 of file test-inlines.h.

625 {
626 if (is_misdirected ()) [[unlikely]]
627 {
629 std::move (result));
630 }
631
632 result.init (expr, detail::check_kind::expectation, sl, *this,
633 test_reporter ().expression_,
634 test_reporter ().writes_passed_checks ());
635 return std::move (result);
636 }
class reporter & test_reporter(void) const noexcept
Returns the test reporter associated with this test runnable.
Definition test.cpp:155
bool is_misdirected(void) const noexcept
Tells whether another test case is running.
detail::check_result && report_misdirected(detail::check_kind kind, const reflection::source_location &sl, detail::check_result &&result)
Reports a check on this test case as a failure of the running one.
Definition test.cpp:444

References micro_os_plus::micro_test_plus::detail::expectation, micro_os_plus::micro_test_plus::detail::check_result::init(), is_misdirected(), report_misdirected(), and micro_os_plus::micro_test_plus::detail::runnable_base::test_reporter().

Referenced by operator=().