Functions to check exceptions.
More...
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";
}
◆ 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_T | The type of an object that can be called. |
- Parameters
-
[in] | func | Function 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_T | Type of the exception. |
Callable_T | The type of an object that can be called. |
- Parameters
-
[in] | func | Function 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_T | The type of an object that can be called. |
- Parameters
-
[in] | func | Function to check. |
- Returns
- An output stream to write optional messages.
Definition at line 203 of file micro-test-plus.h.