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

Added streaming to message macros and added scoped info

Commit
00256685845d8227986d1ff65d4d6a88ebaefc22
Author
Phil Nash ·
Parents
b3ef7fc0f28c

Files · Download .tar.gz

Changes

 Test/MessageTests.cpp          |  6 +++---
 catch.hpp                      |  1 +
 catch_runner.hpp               |  2 +-
 internal/catch_capture.hpp     | 49 ++++++++++++++++++++++++++++++++++++++++--
 internal/catch_runner_impl.hpp | 15 +++++++++++--
 5 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/Test/MessageTests.cpp b/Test/MessageTests.cpp
index 1a73b0ac..a75fc5bb 100644
--- a/Test/MessageTests.cpp
+++ b/Test/MessageTests.cpp
@@ -14,11 +14,11 @@
 
 TEST_CASE( "succeeding/message", "INFO and WARN do not abort tests" )
 {
-    INFO( "this is a message" );    // This should output the message but continue
-    WARN( "this is a warning" );    // This should output the message but continue
+    INFO( "this is a " << "message" );    // This should output the message but continue
+    WARN( "this is a " << "warning" );    // This should output the message but continue
 }
 
 TEST_CASE( "failing/message", "FAIL aborts the test" )
 {
-    FAIL( "This is a failure" );    // This should output the message and abort
+    FAIL( "This is a " << "failure" );    // This should output the message and abort
 }
\ No newline at end of file
diff --git a/catch.hpp b/catch.hpp
index 77022800..debc6589 100644
--- a/catch.hpp
+++ b/catch.hpp
@@ -49,6 +49,7 @@
 #define INFO( reason ) INTERNAL_CATCH_MSG( reason, Catch::ResultWas::Info, false, "INFO" )
 #define WARN( reason ) INTERNAL_CATCH_MSG( reason, Catch::ResultWas::Warning, false, "WARN" )
 #define FAIL( reason ) INTERNAL_CATCH_MSG( reason, Catch::ResultWas::ExplicitFailure, true, "FAIL" )
+#define SCOPED_INFO( log ) INTERNAL_CATCH_SCOPED_INFO( log )
 
 #define SECTION( name, description ) CATCH_SECTION( name, description )
 
diff --git a/catch_runner.hpp b/catch_runner.hpp
index 4109f560..6de96e00 100644
--- a/catch_runner.hpp
+++ b/catch_runner.hpp
@@ -76,7 +76,7 @@ namespace Catch
                 if( runner.runMatching( *it ) == 0 )
                 {
                     // Use reporter?
-                    std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl;
+//                    std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl;
                 }
                 config.getReporter()->EndGroup( *it, runner.getSuccessCount()-prevSuccess, runner.getFailureCount()-prevFail );
             }
diff --git a/internal/catch_capture.hpp b/internal/catch_capture.hpp
index 81373843..90d69365 100644
--- a/internal/catch_capture.hpp
+++ b/internal/catch_capture.hpp
@@ -190,6 +190,7 @@ private:
 };
 
 class TestCaseInfo;
+class ScopedInfo;
     
 struct IResultListener
 {
@@ -197,8 +198,10 @@ struct IResultListener
     virtual void testEnded( const ResultInfo& result ) = 0;
     virtual bool sectionStarted( const std::string& name, const std::string& description, std::size_t& successes, std::size_t& failures ) = 0;
     virtual void sectionEnded( const std::string& name, std::size_t successes, std::size_t failures ) = 0;
+    virtual void pushScopedInfo( ScopedInfo* scopedInfo ) = 0;
+    virtual void popScopedInfo( ScopedInfo* scopedInfo ) = 0;
 };
-    
+        
 class ResultsCapture
 {
 private:
@@ -274,11 +277,50 @@ public:
         instance().m_listener->sectionEnded( name, successes, failures );
     }
     
+    static void pushScopedInfo( ScopedInfo* scopedInfo )
+    {
+        instance().m_listener->pushScopedInfo( scopedInfo );
+    }
+    static void popScopedInfo( ScopedInfo* scopedInfo )
+    {
+        instance().m_listener->popScopedInfo( scopedInfo );
+    }
+    
 private:
     MutableResultInfo currentResult;
     IResultListener* m_listener;
+
 };
 
+class ScopedInfo
+{
+public:
+    ScopedInfo()
+    {
+        ResultsCapture::pushScopedInfo( this );
+    }
+    
+    ~ScopedInfo()
+    {
+        ResultsCapture::popScopedInfo( this );
+    }
+    
+    ScopedInfo& operator << ( const char* str )
+    {
+        m_oss << str;
+        return *this; 
+    }
+    
+    std::string getInfo() const
+    {
+        return m_oss.str();
+    }
+    
+private:
+    std::ostringstream m_oss;
+};
+    
+    
 // !TBD Need to clean this all up
 #define CATCH_absTol 1e-10
 #define CATCH_relTol 1e-10
@@ -349,9 +391,12 @@ catch( ... ) \
 }
 
 #define INTERNAL_CATCH_MSG( reason, resultType, stopOnFailure, macroName ) \
+    std::ostringstream INTERNAL_CATCH_UNIQUE_NAME( strm ); \
+    INTERNAL_CATCH_UNIQUE_NAME( strm ) << reason; \
     Catch::ResultsCapture::acceptExpression( Catch::MutableResultInfo( "", false, __FILE__, __LINE__, macroName ) ); \
-    Catch::ResultsCapture::acceptMessage( reason ); \
+    Catch::ResultsCapture::acceptMessage( INTERNAL_CATCH_UNIQUE_NAME( strm ).str() ); \
     Catch::ResultsCapture::acceptResult( resultType, stopOnFailure );
 
+#define INTERNAL_CATCH_SCOPED_INFO( log ) Catch::ScopedInfo INTERNAL_CATCH_UNIQUE_NAME( info ); INTERNAL_CATCH_UNIQUE_NAME( info ) << log
 
 #endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
diff --git a/internal/catch_runner_impl.hpp b/internal/catch_runner_impl.hpp
index 4023b60f..86b85ab6 100644
--- a/internal/catch_runner_impl.hpp
+++ b/internal/catch_runner_impl.hpp
@@ -157,9 +157,9 @@ namespace Catch
         
         virtual void testEnded( const ResultInfo& result )
         { 
-            if( result.ok() )
+            if( result.getResultType() == ResultWas::Ok )
                 m_successes++;
-            else
+            else if( !result.ok() )
                 m_failures++;
             
             m_reporter->Result( result );
@@ -179,11 +179,22 @@ namespace Catch
         {
             m_reporter->EndSection( name, m_successes - prevSuccesses, m_failures - prevFailures );
         }
+
+        virtual void pushScopedInfo( ScopedInfo* scopedInfo )
+        {
+            m_scopedInfos.push_back( scopedInfo );
+        }
+        virtual void popScopedInfo( ScopedInfo* scopedInfo )
+        {
+            if( m_scopedInfos.back() == scopedInfo )
+                m_scopedInfos.pop_back();
+        }
         
     private:
         std::size_t m_successes;
         std::size_t m_failures;
         ITestReporter* m_reporter;
+        std::vector<ScopedInfo*> m_scopedInfos;
     };
 }