Skip to main content

throws_op Struct Template

Operator struct template to check if an expression throws a specific exception. More...

Declaration​

template <class Exception_T = void>
struct micro_os_plus::micro_test_plus::detail::throws_op<Exception_T> { ... }

Included Headers​

Base struct​

structcallable_op

Common base struct for callable-wrapping operators. More...

Public Member Typedefs Index​

template <class Exception_T = void>
usingop_type = op

Marker identifying the framework expression types. More...

Public Constructors Index​

template <class Callable_T>
throws_op (Callable_T &&func)

Constructs an exception checking operator for the given callable. More...

Public Operators Index​

template <class Exception_T = void>
constexproperator bool () const

Conversion operator to boolean. More...

Public Member Functions Index​

template <class Exception_T = void>
throw_outcomeoutcome (void) const noexcept

Retrieves what the invocation threw. More...

template <class Exception_T = void>
const std::string &what (void) const noexcept

Retrieves the what() of the std::exception thrown. More...

Private Member Attributes Index​

template <class Exception_T = void>
throw_outcomeoutcome_ { throw_outcome::none }

Stores what the invocation threw, when the check failed. More...

template <class Exception_T = void>
const boolvalue_ {}

Stores the result of the callable invocation. More...

template <class Exception_T = void>
std::stringwhat_ {}

Stores the what() of the std::exception thrown, when the check failed. More...

Description​

Operator struct template to check if an expression throws a specific exception.

This primary template checks whether invoking the given callable throws an exception of the specified type. 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 raises an expected exception of a particular type.

The callable is used only by the constructor, so it is a parameter of the constructor, not of the struct; all checks of the same exception type share one struct, and one formatter instantiation.

Template Parameters
Exception_T

The type of the exception to check for (defaults to void for any exception).

Definition at line 1497 of file detail.h.

Public Member Typedefs​

op_type​

template <class Exception_T = void>
using micro_os_plus::micro_test_plus::detail::op::op_type = op

Marker identifying the framework expression types.

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

Definition at line 128 of file extension.h.

128 using op_type = op;

Public Constructors​

throws_op()​

template <class Callable_T>
micro_os_plus::micro_test_plus::detail::throws_op< Exception_T >::throws_op (Callable_T && func)
explicit

Constructs an exception checking operator for the given callable.

Template Parameters
Callable_T

The type of the callable object to be invoked; it must be invocable without arguments, as an lvalue, so a mutable lambda is accepted.

Parameters
func

The callable object to be invoked.

Invokes func inside a try/catch block. If func throws an exception of type Exception_T, the result is true; if it throws any other exception the result is false; if no 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: nothing, 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.

Declaration at line 1510 of file detail.h, definition at line 1188 of file detail-inlines.h.

1188 requires std::invocable<Callable_T&>
1189 throws_op<Exception_T>::throws_op (Callable_T&& func)
1190 : callable_op{ [&func] () -> callable_result
1191 {
1192 try
1193 {
1194 func ();
1195 }
1196 catch (const failed_requirement&)
1197 {
1198 throw;
1199 }
1200 catch (const Exception_T&)
1201 {
1202 return { true, throw_outcome::none, {} };
1203 }
1204 catch (const std::exception& e)
1205 {
1206 return { false, throw_outcome::std_exception,
1207 e.what () != nullptr ? e.what () : "" };
1208 }
1209 catch (...)
1210 {
1211 return { false, throw_outcome::unknown_exception, {} };
1212 }
1213 return { false, throw_outcome::none, {} };
1214 }() }
1215 {

References micro_os_plus::micro_test_plus::detail::callable_op::callable_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 micro_os_plus::micro_test_plus::detail::throws_op< void >::throws_op.

Public Operators​

operator bool()​

template <class Exception_T = void>
micro_os_plus::micro_test_plus::detail::callable_op::operator bool ()
nodiscard constexpr

Conversion operator to boolean.

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.

Declaration at line 1420 of file detail.h, definition at line 1137 of file detail-inlines.h.

1137 constexpr callable_op::
1138 operator bool () const
1139 {
1140 return value_;

Reference micro_os_plus::micro_test_plus::detail::callable_op::value_.

Public Member Functions​

outcome()​

template <class Exception_T = void>
throw_outcome micro_os_plus::micro_test_plus::detail::callable_op::outcome (void)
inline nodiscard noexcept

Retrieves what the invocation threw.

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.

Declaration at line 1432 of file detail.h, definition at line 1147 of file detail-inlines.h.

1148 callable_op::outcome (void) const noexcept
1149 {
1150 return outcome_;

Reference micro_os_plus::micro_test_plus::detail::callable_op::outcome_.

Referenced by micro_os_plus::micro_test_plus::detail::callable_op::callable_op.

what()​

template <class Exception_T = void>
const std::string & micro_os_plus::micro_test_plus::detail::callable_op::what (void)
inline nodiscard noexcept

Retrieves the what() of the std::exception thrown.

Parameters

None.

Returns

The text, empty unless the outcome is throw_outcome::std_exception.

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

Declaration at line 1443 of file detail.h, definition at line 1157 of file detail-inlines.h.

1157 inline const std::string&
1158 callable_op::what (void) const noexcept
1159 {
1160 return what_;

Reference micro_os_plus::micro_test_plus::detail::callable_op::what_.

Referenced by micro_os_plus::micro_test_plus::detail::callable_op::callable_op.

Private Member Attributes​

outcome_​

template <class Exception_T = void>
throw_outcome micro_os_plus::micro_test_plus::detail::callable_op::outcome_ { throw_outcome::none }

Stores what the invocation threw, when the check failed.

Copied from the callable_result by the constructor.

Definition at line 1461 of file detail.h.

Referenced by micro_os_plus::micro_test_plus::detail::callable_op::callable_op and micro_os_plus::micro_test_plus::detail::callable_op::outcome.

value_​

template <class Exception_T = void>
const bool micro_os_plus::micro_test_plus::detail::callable_op::value_ {}

Stores the result of the callable invocation.

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

Definition at line 1453 of file detail.h.

1453 const bool value_{};

Referenced by micro_os_plus::micro_test_plus::detail::callable_op::callable_op and micro_os_plus::micro_test_plus::detail::callable_op::operator bool.

what_​

template <class Exception_T = void>
std::string micro_os_plus::micro_test_plus::detail::callable_op::what_ {}

Stores the what() of the std::exception thrown, when the check failed.

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.

1471 std::string what_{};

Referenced by micro_os_plus::micro_test_plus::detail::callable_op::callable_op and micro_os_plus::micro_test_plus::detail::callable_op::what.


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


Generated via doxygen2docusaurus 2.2.8 by Doxygen 1.18.0.