From cefdde35fa562f2a8d453db5a7dc14160c4a9fd6 Mon Sep 17 00:00:00 2001 From: Rasmus Thomsen Date: Tue, 25 Feb 2020 18:01:37 +0100 Subject: [PATCH 1/2] Print description of unexpected exception in AssertThrows This makes it easier to debug the failure fixes #47 --- include/snowhouse/exceptions.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/snowhouse/exceptions.h b/include/snowhouse/exceptions.h index 5cbd0f2..4da7667 100644 --- a/include/snowhouse/exceptions.h +++ b/include/snowhouse/exceptions.h @@ -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; \ @@ -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 (...) {} \ + } \ } \ if (SNOWHOUSE_TEMPVAR(no_exception)) \ { \ @@ -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(SNOWHOUSE_TEMPVAR(stm).str()); \ } \ do {} while (false) From 78f6b9647ef62da8e6f956b2729400866a361baa Mon Sep 17 00:00:00 2001 From: Rasmus Thomsen Date: Tue, 25 Feb 2020 18:05:27 +0100 Subject: [PATCH 2/2] Test printing unexpected exception in AssertThrows --- example/exceptions_tests.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/example/exceptions_tests.cpp b/example/exceptions_tests.cpp index d050a07..6402abd 100644 --- a/example/exceptions_tests.cpp +++ b/example/exceptions_tests.cpp @@ -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; @@ -89,4 +97,9 @@ void ExceptionTests() AssertThrows(AssertionException, LastException()); AssertThat(LastException().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!"); + } }