Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

print messages when unexpected exceptions are thrown

Commit
9a07dde16dc8fab02edfac3984fb204dc61b2ba4
Author
dvirtz ·
Committer
Martin Hořeňovský
Parents
6e091d399128

Files · Download .tar.gz

Changes

 include/internal/catch_message.hpp           |  5 ++++-
 include/reporters/catch_reporter_console.hpp |  6 +++++-
 projects/SelfTest/ExceptionTests.cpp         | 15 +++++++++++++++
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/include/internal/catch_message.hpp b/include/internal/catch_message.hpp
index 42866be8..1b72afec 100644
--- a/include/internal/catch_message.hpp
+++ b/include/internal/catch_message.hpp
@@ -38,7 +38,10 @@ namespace Catch {
     {}
 
     ScopedMessage::~ScopedMessage() {
-        getResultCapture().popScopedMessage( m_info );
+        if (!std::uncaught_exception())
+        {
+            getResultCapture().popScopedMessage(m_info);
+        }
     }
 
 
diff --git a/include/reporters/catch_reporter_console.hpp b/include/reporters/catch_reporter_console.hpp
index fed97072..5f181241 100644
--- a/include/reporters/catch_reporter_console.hpp
+++ b/include/reporters/catch_reporter_console.hpp
@@ -138,7 +138,11 @@ namespace Catch {
                     case ResultWas::ThrewException:
                         colour = Colour::Error;
                         passOrFail = "FAILED";
-                        messageLabel = "due to unexpected exception with message";
+                        messageLabel = "due to unexpected exception with ";
+                        if (_stats.infoMessages.size() == 1)
+                            messageLabel += "message";
+                        if (_stats.infoMessages.size() > 1)
+                            messageLabel += "messages";
                         break;
                     case ResultWas::FatalErrorCondition:
                         colour = Colour::Error;
diff --git a/projects/SelfTest/ExceptionTests.cpp b/projects/SelfTest/ExceptionTests.cpp
index 69eba2a0..0f363da4 100644
--- a/projects/SelfTest/ExceptionTests.cpp
+++ b/projects/SelfTest/ExceptionTests.cpp
@@ -209,3 +209,18 @@ TEST_CASE( "Mismatching exception messages failing the test", "[.][failing][!thr
     REQUIRE_THROWS_WITH( thisThrows(), "should fail" );
     REQUIRE_THROWS_WITH( thisThrows(), "expected exception" );
 }
+
+TEST_CASE( "#748 - captures with unexpected exceptions", "[!shouldfail]" ) {
+    int answer = 42;
+    CAPTURE(answer);
+    // the message should be printed on the first two sections but not on the third
+    SECTION( "outside assertions" ) {
+        thisThrows();
+    }
+    SECTION( "inside REQUIRE_NOTHROW" ) {
+        REQUIRE_NOTHROW(thisThrows());
+    }
+    SECTION( "inside REQUIRE_THROWS" ) {
+        REQUIRE_THROWS(thisThrows());
+    }
+}
\ No newline at end of file