micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::detail::nothrow_op Struct Reference

Operator struct to check if an expression does not throw any exception. More...

#include "micro-os-plus/micro-test-plus.h"

Inheritance diagram for micro_os_plus::micro_test_plus::detail::nothrow_op:

Public Types

using op_type = op
 Marker identifying the framework expression types.

Public Member Functions

template<class Callable_T>
requires std::invocable<Callable_T&>
 nothrow_op (Callable_T &&func)
 Constructs a nothrow checking operator for the given callable.
constexpr operator bool () const
 Conversion operator to boolean.
throw_outcome outcome (void) const noexcept
 Retrieves what the invocation threw.
const std::string & what (void) const noexcept
 Retrieves the what() of the std::exception thrown.

Private Attributes

throw_outcome outcome_ { throw_outcome::none }
 Stores what the invocation threw, when the check failed.
const bool value_ {}
 Stores the result of the callable invocation.
std::string what_ {}
 Stores the what() of the std::exception thrown, when the check failed.

Detailed Description

This struct checks whether invoking the given callable completes without throwing an exception. The check is performed at construction time, with the result cached for reporting and composition. Use this when a test must verify that an operation does not throw.

The callable is a parameter of the constructor only, so all the checks share one struct, and one formatter overload.

Definition at line 1562 of file detail.h.

Member Typedef Documentation

◆ op_type

Detected by the extension::is_op concept, and inherited by all types derived from op.

Definition at line 128 of file extension.h.

Constructor & Destructor Documentation

◆ nothrow_op()

template<class Callable_T>
requires std::invocable<Callable_T&>
micro_os_plus::micro_test_plus::detail::nothrow_op::nothrow_op ( Callable_T && func)
explicit
Template Parameters
Callable_TThe type of the callable object to be invoked; it must be invocable without arguments, as an lvalue, so a mutable lambda is accepted.
Parameters
funcThe callable object to be invoked.

Invokes func inside a try/catch block. If func completes without throwing, the result is true; if any exception is thrown the result is false. The result is passed to the callable_op base class constructor, with what was thrown, if the result is false, to be written in the report: a std::exception, with its what(), or another exception.

The failed_requirement thrown to stop the test case after a failed require() in func is not an exception of func, and is rethrown.

Definition at line 1273 of file detail-inlines.h.

1275 : callable_op{ [&func] () -> callable_result
1276 {
1277 try
1278 {
1279 func ();
1280 }
1281 catch (const failed_requirement&)
1282 {
1283 throw;
1284 }
1285 catch (const std::exception& e)
1286 {
1287 return { false, throw_outcome::std_exception,
1288 e.what () != nullptr ? e.what () : "" };
1289 }
1290 catch (...)
1291 {
1292 return { false, throw_outcome::unknown_exception, {} };
1293 }
1294 return { true, throw_outcome::none, {} };
1295 }() }
1296 {
@ std_exception
A std::exception that was not the expected one was thrown; its what() is kept.
Definition detail.h:1346
@ none
Nothing was thrown, or the expected exception was thrown.
Definition detail.h:1340
@ unknown_exception
Another exception was thrown.
Definition detail.h:1351
callable_op(callable_result &&result)
Constructs a callable operator with the pre-computed result.

References micro_os_plus::micro_test_plus::detail::callable_op::callable_op(), nothrow_op(), micro_os_plus::micro_test_plus::detail::none, micro_os_plus::micro_test_plus::detail::std_exception, and micro_os_plus::micro_test_plus::detail::unknown_exception.

Referenced by nothrow_op().

Member Function Documentation

◆ operator bool()

micro_os_plus::micro_test_plus::detail::callable_op::operator bool ( ) const
nodiscardconstexprinherited
Parameters
None.
Returns
The result of the callable invocation.

Returns the result computed by the constructor of the derived operator; the callable is not invoked again.

Definition at line 1136 of file detail-inlines.h.

1139 {
1140 return value_;
const bool value_
Stores the result of the callable invocation.
Definition detail.h:1453

References value_.

◆ outcome()

throw_outcome micro_os_plus::micro_test_plus::detail::callable_op::outcome ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The kind of the exception thrown, throw_outcome::none if the callable completed, or if the expected exception was thrown.

Returns what was recorded when the check failed.

Definition at line 1147 of file detail-inlines.h.

1149 {
1150 return outcome_;
throw_outcome outcome_
Stores what the invocation threw, when the check failed.
Definition detail.h:1461

References outcome_.

Referenced by callable_op().

◆ what()

const std::string & micro_os_plus::micro_test_plus::detail::callable_op::what ( void ) const
inlinenodiscardnoexceptinherited
Parameters
None.
Returns
The text, empty unless the outcome is throw_outcome::std_exception.

Returns the text recorded when a std::exception was thrown.

Definition at line 1157 of file detail-inlines.h.

1159 {
1160 return what_;
std::string what_
Stores the what() of the std::exception thrown, when the check failed.
Definition detail.h:1471

References what_.

Referenced by callable_op().

Member Data Documentation

◆ outcome_

throw_outcome micro_os_plus::micro_test_plus::detail::callable_op::outcome_ { throw_outcome::none }
privateinherited

Copied from the callable_result by the constructor.

Definition at line 1461 of file detail.h.

Referenced by callable_op(), and outcome().

◆ value_

const bool micro_os_plus::micro_test_plus::detail::callable_op::value_ {}
privateinherited

Computed once, by the derived operator constructor, which invokes the callable.

Definition at line 1453 of file detail.h.

Referenced by callable_op(), and operator bool().

◆ what_

std::string micro_os_plus::micro_test_plus::detail::callable_op::what_ {}
privateinherited

Moved from the callable_result by the constructor; empty in the usual case, so nothing is allocated.

Definition at line 1471 of file detail.h.

Referenced by callable_op(), and what().


The documentation for this struct was generated from the following files: