micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
Checking exceptions

Functions to check exceptions. More...

Functions

template<class Callable_T >
constexpr auto micro_os_plus::micro_test_plus::nothrow (const Callable_T &func)
 Check if a callable doesn't throw an exception.
 
template<class Exception_T , class Callable_T >
constexpr auto micro_os_plus::micro_test_plus::throws (const Callable_T &func)
 Check if a callable throws a specific exception.
 
template<class Callable_T >
constexpr auto micro_os_plus::micro_test_plus::throws (const Callable_T &func)
 Check if a callable throws an exception (any exception).
 

Detailed Description

Functions to check exceptions.

A C++ testing framework must be able to check if an expression (usually a function call), throws or not an exception.

Examples
mt::expect (mt::throws ([] { exercise_throw (true); })) << "exception thrown";
mt::expect (mt::throws<std::runtime_error> ([] { throw std::runtime_error{ "" }; }))
<< "std::runtime_error thrown";
mt::expect (mt::nothrow ([] { exercise_throw (false); })) << "exception not thrown";

If a more elaborate logic is required, for example for expecting multiple exceptions, use an explicit try with multiple catch statements and report the results with expect(true) or expect(false).

try
{
compute_answer ();
}
catch (const std::overflow_error& e)
{
mt::expect (true) << "std::overflow_error thrown";
}
catch (const std::runtime_error& e)
{
mt::expect (true) << "std::runtime_error thrown";
}
catch (...)
{
mt::expect (false) << "known exception thrown";
}

Function Documentation

◆ nothrow()

template<class Callable_T >
constexpr auto micro_os_plus::micro_test_plus::nothrow ( const Callable_T & func)
constexpr

Check if a callable doesn't throw an exception.

Template Parameters
Callable_TThe type of an object that can be called.
Parameters
[in]funcFunction to check.
Returns
An output stream to write optional messages.

Definition at line 217 of file micro-test-plus.h.

◆ throws() [1/2]

template<class Exception_T , class Callable_T >
constexpr auto micro_os_plus::micro_test_plus::throws ( const Callable_T & func)
constexpr

Check if a callable throws a specific exception.

Template Parameters
Exception_TType of the exception.
Callable_TThe type of an object that can be called.
Parameters
[in]funcFunction to check.
Returns
An output stream to write optional messages.

Definition at line 189 of file micro-test-plus.h.

◆ throws() [2/2]

template<class Callable_T >
constexpr auto micro_os_plus::micro_test_plus::throws ( const Callable_T & func)
constexpr

Check if a callable throws an exception (any exception).

Template Parameters
Callable_TThe type of an object that can be called.
Parameters
[in]funcFunction to check.
Returns
An output stream to write optional messages.

Definition at line 203 of file micro-test-plus.h.