Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If we catch the wrong exception in AssertThrows, print the description of the exception #48

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions example/exceptions_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ struct ClassWithExceptions
}
};

struct ExpectedException : public std::exception
{
const char* what() const throw()
{
return "Description of the exception we expected";
}
};

void ExceptionTests()
{
ClassWithExceptions objectUnderTest;
Expand Down Expand Up @@ -89,4 +97,9 @@ void ExceptionTests()
AssertThrows(AssertionException, LastException<std::logic_error>());
AssertThat(LastException<AssertionException>().GetMessage(), Contains("No exception was stored"));
}

it("prints description of unwanted exception");
{
AssertTestFails(AssertThrows(ExpectedException, objectUnderTest.LogicError()), "Expected ExpectedException. Wrong exception was thrown. Description of unwanted exception: not logical!");
}
}
Cogitri marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions include/snowhouse/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ namespace snowhouse
SNOWHOUSE_TEMPVAR(storage).compiler_thinks_i_am_unused(); \
bool SNOWHOUSE_TEMPVAR(wrong_exception) = false; \
bool SNOWHOUSE_TEMPVAR(no_exception) = false; \
bool SNOWHOUSE_TEMPVAR(more_info) = true; \
std::string SNOWHOUSE_TEMPVAR(info_string); \
try \
{ \
METHOD; \
Expand All @@ -97,6 +99,14 @@ namespace snowhouse
catch (...) \
{ \
SNOWHOUSE_TEMPVAR(wrong_exception) = true; \
if (auto eptr = std::current_exception()) { \
try { \
std::rethrow_exception(eptr); \
} catch (const std::exception& e) { \
SNOWHOUSE_TEMPVAR(more_info) = true; \
SNOWHOUSE_TEMPVAR(info_string) = e.what(); \
} catch (...) {} \
Cogitri marked this conversation as resolved.
Show resolved Hide resolved
} \
} \
if (SNOWHOUSE_TEMPVAR(no_exception)) \
{ \
Expand All @@ -108,6 +118,9 @@ namespace snowhouse
{ \
::std::ostringstream SNOWHOUSE_TEMPVAR(stm); \
SNOWHOUSE_TEMPVAR(stm) << "Expected " #EXCEPTION_TYPE ". Wrong exception was thrown."; \
if (SNOWHOUSE_TEMPVAR(more_info)) { \
SNOWHOUSE_TEMPVAR(stm) << " Description of unwanted exception: " << SNOWHOUSE_TEMPVAR(info_string); \
} \
::snowhouse::ConfigurableAssert<FAILURE_HANDLER_TYPE>::Failure(SNOWHOUSE_TEMPVAR(stm).str()); \
} \
do {} while (false)
Expand Down