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

Improved exception reporting

Refactored exception handling (handle more in the translator)
and report exceptions in CHECKs
Commit
333e6e625484a968b97dd5dd7e2eeea800c732f8
Author
Phil Nash ·
Parents
a57b025c9c07

Files · Download .tar.gz

Changes

 include/internal/catch_capture.hpp                 | 20 +++--
 .../catch_exception_translator_registry.hpp        | 21 ++++-
 include/internal/catch_runner_impl.hpp             | 17 +---
 single_include/catch.hpp                           | 96 ++++++++++++++++------
 4 files changed, 106 insertions(+), 48 deletions(-)

diff --git a/include/internal/catch_capture.hpp b/include/internal/catch_capture.hpp
index 2449f60b..d19f4c24 100644
--- a/include/internal/catch_capture.hpp
+++ b/include/internal/catch_capture.hpp
@@ -684,8 +684,20 @@ inline bool isTrue
 
 ///////////////////////////////////////////////////////////////////////////////
 #define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
-    INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
-    if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); }
+    try \
+    { \
+        INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
+        if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } \
+    } \
+    catch( Catch::TestFailureException& ) \
+    { \
+        throw; \
+    } \
+    catch( ... ) \
+    { \
+        INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false ); \
+        throw; \
+    }
 
 ///////////////////////////////////////////////////////////////////////////////
 #define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \
@@ -704,10 +716,6 @@ inline bool isTrue
         expr; \
         INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \
     } \
-    catch( std::exception& internal_catch_exception ) \
-    { \
-        INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << internal_catch_exception.what() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
-    } \
     catch( ... ) \
     { \
         INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
diff --git a/include/internal/catch_exception_translator_registry.hpp b/include/internal/catch_exception_translator_registry.hpp
index 3c677545..45ed0971 100644
--- a/include/internal/catch_exception_translator_registry.hpp
+++ b/include/internal/catch_exception_translator_registry.hpp
@@ -32,7 +32,26 @@ namespace Catch
         ()
         const
         {
-            return tryTranslators( m_translators.begin() );
+            try
+            {
+                throw;
+            }
+            catch( std::exception& ex )
+            {
+                return ex.what();
+            }
+            catch( std::string& msg )
+            {
+                return msg;
+            }
+            catch( const char* msg )
+            {
+                return msg;
+            }
+            catch(...)
+            {
+                return tryTranslators( m_translators.begin() );
+            }
         }
         
         ///////////////////////////////////////////////////////////////////////
diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp
index d72a844f..9247cb62 100644
--- a/include/internal/catch_runner_impl.hpp
+++ b/include/internal/catch_runner_impl.hpp
@@ -391,7 +391,7 @@ namespace Catch
 
             m_reporter->EndTestCase( testInfo, m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr );
         }
-        
+
         ///////////////////////////////////////////////////////////////////////////
         virtual std::size_t getSuccessCount
         ()
@@ -601,21 +601,6 @@ namespace Catch
             {
                 // This just means the test was aborted due to failure
             }
-            catch( std::exception& ex )
-            {
-                acceptMessage( ex.what() );
-                acceptResult( ResultWas::ThrewException );
-            }
-            catch( std::string& msg )
-            {
-                acceptMessage( msg );
-                acceptResult( ResultWas::ThrewException );
-            }
-            catch( const char* msg )
-            {
-                acceptMessage( msg );
-                acceptResult( ResultWas::ThrewException );
-            }
             catch(...)
             {
                 acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );
diff --git a/single_include/catch.hpp b/single_include/catch.hpp
index 3cda3619..c2970f82 100644
--- a/single_include/catch.hpp
+++ b/single_include/catch.hpp
@@ -208,6 +208,9 @@ namespace Catch
         virtual ~IReporter
             (){}
 
+        virtual bool shouldRedirectStdout
+            () const = 0;
+
         virtual void StartTesting
             () = 0;
 
@@ -1895,8 +1898,20 @@ inline bool isTrue
 
 ///////////////////////////////////////////////////////////////////////////////
 #define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
-    INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
-    if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); }
+    try \
+    { \
+        INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr, isNot )->*expr ), stopOnFailure ); \
+        if( Catch::isTrue( false ) ){ bool internal_catch_dummyResult = ( expr ); Catch::isTrue( internal_catch_dummyResult ); } \
+    } \
+    catch( Catch::TestFailureException& ) \
+    { \
+        throw; \
+    } \
+    catch( ... ) \
+    { \
+        INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), false ); \
+        throw; \
+    }
 
 ///////////////////////////////////////////////////////////////////////////////
 #define INTERNAL_CATCH_IF( expr, isNot, stopOnFailure, macroName ) \
@@ -1915,10 +1930,6 @@ inline bool isTrue
         expr; \
         INTERNAL_CATCH_ACCEPT_EXPR( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ).setResultType( Catch::ResultWas::Ok ), stopOnFailure ); \
     } \
-    catch( std::exception& internal_catch_exception ) \
-    { \
-        INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << internal_catch_exception.what() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
-    } \
     catch( ... ) \
     { \
         INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ResultBuilder( __FILE__, __LINE__, macroName, #expr ) << Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() ).setResultType( Catch::ResultWas::ThrewException ), stopOnFailure ); \
@@ -3319,7 +3330,26 @@ namespace Catch
         ()
         const
         {
-            return tryTranslators( m_translators.begin() );
+            try
+            {
+                throw;
+            }
+            catch( std::exception& ex )
+            {
+                return ex.what();
+            }
+            catch( std::string& msg )
+            {
+                return msg;
+            }
+            catch( const char* msg )
+            {
+                return msg;
+            }
+            catch(...)
+            {
+                return tryTranslators( m_translators.begin() );
+            }
         }
 
         ///////////////////////////////////////////////////////////////////////
@@ -4220,30 +4250,22 @@ namespace Catch
             try
             {
                 m_runningTest->reset();
-                StreamRedirect coutRedir( std::cout, redirectedCout );
-                StreamRedirect cerrRedir( std::cerr, redirectedCerr );
-                m_runningTest->getTestCaseInfo().invoke();
+                if( m_reporter->shouldRedirectStdout() )
+                {
+                    StreamRedirect coutRedir( std::cout, redirectedCout );
+                    StreamRedirect cerrRedir( std::cerr, redirectedCerr );
+                    m_runningTest->getTestCaseInfo().invoke();
+                }
+                else
+                {
+                    m_runningTest->getTestCaseInfo().invoke();
+                }
                 m_runningTest->ranToCompletion();
             }
             catch( TestFailureException& )
             {
                 // This just means the test was aborted due to failure
             }
-            catch( std::exception& ex )
-            {
-                acceptMessage( ex.what() );
-                acceptResult( ResultWas::ThrewException );
-            }
-            catch( std::string& msg )
-            {
-                acceptMessage( msg );
-                acceptResult( ResultWas::ThrewException );
-            }
-            catch( const char* msg )
-            {
-                acceptMessage( msg );
-                acceptResult( ResultWas::ThrewException );
-            }
             catch(...)
             {
                 acceptMessage( Catch::Hub::getExceptionTranslatorRegistry().translateActiveException() );
@@ -5033,6 +5055,14 @@ namespace Catch
 
     private: // IReporter
 
+        ///////////////////////////////////////////////////////////////////////////
+        virtual bool shouldRedirectStdout
+        ()
+        const
+        {
+            return false;
+        }
+
         ///////////////////////////////////////////////////////////////////////////
         virtual void StartTesting
         ()
@@ -5668,6 +5698,14 @@ namespace Catch
 
     private: // IReporter
 
+        ///////////////////////////////////////////////////////////////////////////
+        virtual bool shouldRedirectStdout
+        ()
+        const
+        {
+            return true;
+        }
+
         ///////////////////////////////////////////////////////////////////////////
         virtual void StartTesting
         ()
@@ -5892,6 +5930,14 @@ namespace Catch
 
     private: // IReporter
 
+        ///////////////////////////////////////////////////////////////////////////
+        virtual bool shouldRedirectStdout
+        ()
+        const
+        {
+            return true;
+        }
+
         ///////////////////////////////////////////////////////////////////////////
         virtual void StartTesting()
         {