the Compartmented Robust Posix C++ Unit Test system |
|
---|
Describe an exception type instance with a human readable string representation
Used in: Global scope, preferably in the translation unit containing the
main
() function
Requirement: signature
is a function parameter list with
one parameter being the type of the exception to describe
Requirement: A code block follows immediately after.
The code block returns a std::string that describes, in a human readable form, the exception object in the parameter list.
The macro
CRPCUT_DESCRIBE_EXCEPTION
(signature)
may be invoked any number of times, even using the same signature.
When an unexpected exception is encountered, the describers will be
tried, one by one, in the order they are defined, until one matches.
Note | |
---|---|
The order that exception describers from different translation units are tried is undefined. It is thus a bad idea to add them in more than one file. |
Example: The test program
#include <crpcut.hpp> #include <vector> #include <limits> CRPCUT_DESCRIBE_EXCEPTION(std::out_of_range &r) { return std::string("a reference to an out_of_range(\"") + r.what() + "\") object"; } TEST(make_mighty_big) { std::vector<int> v; v.reserve(std::numeric_limits<size_t>::max()); } TEST(access_past_end) { std::vector<int> v; v.at(3); } int main(int argc, char *argv[]) { return crpcut::run(argc, argv); }
reports two failed tests:
FAILED!: make_mighty_big phase="running" -------------------------------------------------------------- samples/describe_exception.cpp:37 Unexpectedly caught std::exception what()=vector::reserve ------------------------------------------------------------------------------- =============================================================================== FAILED!: access_past_end phase="running" -------------------------------------------------------------- samples/describe_exception.cpp:43 Unexpectedly caught a reference to an out_of_range("vector::_M_range_check: __n (which is 3) >= this->size() (which is 0)") object ------------------------------------------------------------------------------- =============================================================================== 2 test cases selected Sum Critical Non-critical FAILED : 2 2 0
The first exception type does not have a custom describer, but since it inherits from std::exception, some information is shown anyway. The second exception matches the signature used for the custom describer, and thus the message looks as expected.