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

console_reporter refactoring

Commit
3682433c2ff9266c4ed53dcc41ddcc31be2967dc
Author
Phil Nash ·
Parents
3ce320db4f79

Files · Download .tar.gz

Changes

 include/internal/catch_common.h              |   2 +-
 include/reporters/catch_reporter_console.hpp | 205 ++++++++++++++-------------
 2 files changed, 104 insertions(+), 103 deletions(-)

diff --git a/include/internal/catch_common.h b/include/internal/catch_common.h
index a575b4fa..2eeb572a 100644
--- a/include/internal/catch_common.h
+++ b/include/internal/catch_common.h
@@ -119,7 +119,7 @@ namespace Catch {
     
     inline std::ostream& operator << ( std::ostream& os, const SourceLineInfo& info ) {
 #ifndef __GNUG__
-        os << info.file << "(" << info.line << ")";
+        os << info.file << "(" << info.line << "):";
 #else                
         os << info.file << ":" << info.line;
 #endif            
diff --git a/include/reporters/catch_reporter_console.hpp b/include/reporters/catch_reporter_console.hpp
index 292b354b..e6c58ceb 100644
--- a/include/reporters/catch_reporter_console.hpp
+++ b/include/reporters/catch_reporter_console.hpp
@@ -43,37 +43,66 @@ namespace Catch {
             
             lazyPrint();
 
-            ResultComponents components( result );
-            bool endsWithNewLine = false;
-            if( _assertionStats.totals.assertions.total() > 0 ) {
-                printResultType( components );
-                printOriginalExpression( result );
-                endsWithNewLine = printReconstructedExpression( result );
-            }
-            endsWithNewLine |= printMessage( components );
-            
-            printSourceInfo( result );
+            AssertionPrinter printer( stream, _assertionStats );
+            printer.print();
             stream << std::endl;
         }
-        void printSourceInfo( AssertionResult const& _result ) {
-            TextColour colour( TextColour::FileName );
-            stream << _result.getSourceInfo() << ":\n";
+                        
+        virtual void sectionEnded( SectionStats const& _sectionStats ) {
+            if( _sectionStats.missingAssertions ) {
+                lazyPrint();
+                TextColour colour( TextColour::ResultError );
+                stream << "\nNo assertions in section, '" << _sectionStats.sectionInfo.name << "'\n" << std::endl;
+            }
+            StreamingReporterBase::sectionEnded( _sectionStats );
+        }
+
+        virtual void testCaseEnded( TestCaseStats const& _testCaseStats ) {
+
+            if( _testCaseStats.missingAssertions ) {
+                lazyPrint();
+                TextColour colour( TextColour::ResultError );
+                stream << "\nNo assertions in test case, '" << _testCaseStats.testInfo.name << "'\n" << std::endl;
+            }
+            StreamingReporterBase::testCaseEnded( _testCaseStats );
+        }
+        virtual void testGroupEnded( TestGroupStats const& _testGroupStats ) {
+            if( !unusedGroupInfo ) {
+                printSummaryDivider();
+                stream << "Summary for group '" << _testGroupStats.groupInfo.name << "':\n";
+                printTotals( _testGroupStats.totals );
+                stream << "\n" << std::endl;
+            }
+            StreamingReporterBase::testGroupEnded( _testGroupStats );
+        }
+        virtual void testRunEnded( TestRunStats const& _testRunStats ) {
+            if( m_atLeastOneTestCasePrinted )
+                printTotalsDivider();
+            printTotals( _testRunStats.totals );
+            stream << "\n" << std::endl;
+            StreamingReporterBase::testRunEnded( _testRunStats );
         }
+
+    private:
         
-        struct ResultComponents {
-            ResultComponents( AssertionResult const& _result )
-            :   colour( TextColour::None ),
-                message( _result.getMessage() )
+        class AssertionPrinter {
+        public:
+            AssertionPrinter( std::ostream& _stream, AssertionStats const& _stats )
+            :   stream( _stream ),
+                stats( _stats ),
+                result( _stats.assertionResult ),
+                colour( TextColour::None ),
+                message( result.getMessage() )
             {
-                switch( _result.getResultType() ) {
+                switch( result.getResultType() ) {
                     case ResultWas::Ok:
                         colour = TextColour::Success;
                         passOrFail = "PASSED";
-                        if( _result.hasMessage() )
+                        if( result.hasMessage() )
                             messageLabel = "with message";
                         break;
                     case ResultWas::ExpressionFailed:
-                        if( _result.isOk() ) {
+                        if( result.isOk() ) {
                             colour = TextColour::Success;
                             passOrFail = "FAILED - but was ok";
                         }
@@ -81,7 +110,7 @@ namespace Catch {
                             colour = TextColour::Error;
                             passOrFail = "FAILED";
                         }
-                        if( _result.hasMessage() ){
+                        if( result.hasMessage() ){
                             messageLabel = "with message";
                         }
                         break;
@@ -109,7 +138,7 @@ namespace Catch {
                     case ResultWas::Exception:
                         passOrFail = "FAILED";
                         colour = TextColour::Error;
-                        if( _result.hasMessage() )
+                        if( result.hasMessage() )
                             messageLabel = "with message";
                         break;
                         
@@ -122,93 +151,65 @@ namespace Catch {
                 }
             }
             
-            TextColour::Colours colour;
-            std::string passOrFail;
-            std::string messageLabel;
-            std::string message;
-        };
-
-        void printResultType( ResultComponents const& _components ) {
-            if( !_components.passOrFail.empty() ) {
-                TextColour colour( _components.colour );
-                stream << _components.passOrFail << ":\n";
-            }
-        }
-        bool printOriginalExpression( AssertionResult const& _result ) {
-            if( _result.hasExpression() ) {
-                TextColour colour( TextColour::OriginalExpression );
-                stream  << "  ";
-                if( !_result.getTestMacroName().empty() )
-                    stream << _result.getTestMacroName() << "( ";
-                stream << _result.getExpression();
-                if( !_result.getTestMacroName().empty() )
-                    stream << " )";
-                stream << "\n";
-                return true;
+            void print() const {
+                if( stats.totals.assertions.total() > 0 ) {
+                    printResultType();
+                    printOriginalExpression();
+                    printReconstructedExpression();
+                }
+                printMessage();
+                printSourceInfo();
             }
-            return false;
-        }
-        bool printReconstructedExpression( AssertionResult const& _result ) {
-            if( _result.hasExpandedExpression() ) {
-                stream << "with expansion:\n";
-                TextColour colour( TextColour::ReconstructedExpression );
-                stream << wrapLongStrings( _result.getExpandedExpression() ) << "\n";
-                return true;
+            
+        private:
+            void printResultType() const {
+                if( !passOrFail.empty() ) {
+                    TextColour colourGuard( colour );
+                    stream << passOrFail << ":\n";
+                }
             }
-            return false;
-        }        
-        bool printMessage( ResultComponents const& _components ) {
-            bool endsWithNewLine = false;
-            if( !_components.messageLabel.empty() ) {
-                stream << _components.messageLabel << ":" << "\n";
-                endsWithNewLine = true;
+            void printOriginalExpression() const {
+                if( result.hasExpression() ) {
+                    TextColour colourGuard( TextColour::OriginalExpression );
+                    stream  << "  ";
+                    if( !result.getTestMacroName().empty() )
+                        stream << result.getTestMacroName() << "( ";
+                    stream << result.getExpression();
+                    if( !result.getTestMacroName().empty() )
+                        stream << " )";
+                    stream << "\n";
+                }
             }
-            if( !_components.message.empty() ) {
-                stream << wrapLongStrings( _components.message ) << "\n";
-                endsWithNewLine = true;
+            void printReconstructedExpression() const {
+                if( result.hasExpandedExpression() ) {
+                    stream << "with expansion:\n";
+                    TextColour colourGuard( TextColour::ReconstructedExpression );
+                    stream << wrapLongStrings( result.getExpandedExpression() ) << "\n";
+                }
             }
-            return endsWithNewLine;
-        }
-                
-        virtual void sectionEnded( SectionStats const& _sectionStats ) {
-            if( _sectionStats.missingAssertions ) {
-                lazyPrint();
-                TextColour colour( TextColour::ResultError );
-                stream << "\nNo assertions in section, '" << _sectionStats.sectionInfo.name << "'\n" << std::endl;
+            void printMessage() const {
+                if( !messageLabel.empty() )
+                    stream << messageLabel << ":" << "\n";
+                if( !message.empty() )
+                    stream << wrapLongStrings( message ) << "\n";
             }
-            StreamingReporterBase::sectionEnded( _sectionStats );
-        }
-
-        virtual void testCaseEnded( TestCaseStats const& _testCaseStats ) {
-
-            if( _testCaseStats.missingAssertions ) {
-                lazyPrint();
-                TextColour colour( TextColour::ResultError );
-                stream << "\nNo assertions in test case, '" << _testCaseStats.testInfo.name << "'\n" << std::endl;
+            void printSourceInfo() const {
+                TextColour colourGuard( TextColour::FileName );
+                stream << result.getSourceInfo() << ":\n";
             }
-            StreamingReporterBase::testCaseEnded( _testCaseStats );
-        }
-        virtual void testGroupEnded( TestGroupStats const& _testGroupStats ) {
-            if( !unusedGroupInfo ) {
-                printSummaryDivider();
-                stream << "Summary for group '" << _testGroupStats.groupInfo.name << "':\n";
-                printTotals( _testGroupStats.totals );
-                stream << "\n" << std::endl;
+            
+            static std::string wrapLongStrings( std::string const& _string ){
+                return Catch::wrapLongStrings( _string, 70, 2 );
             }
-            StreamingReporterBase::testGroupEnded( _testGroupStats );
-        }
-        virtual void testRunEnded( TestRunStats const& _testRunStats ) {
-            if( m_atLeastOneTestCasePrinted )
-                printTotalsDivider();
-            printTotals( _testRunStats.totals );
-            stream << "\n" << std::endl;
-            StreamingReporterBase::testRunEnded( _testRunStats );
-        }
-
-    private:
-        std::string wrapLongStrings( std::string const& _string ) {
-            return Catch::wrapLongStrings( _string, 70, 2 );
-        }
+            
+            std::ostream& stream;
+            AssertionStats const& stats;
+            AssertionResult const& result;
+            TextColour::Colours colour;
+            std::string passOrFail;
+            std::string messageLabel;
+            std::string message;
+        };
         
         void lazyPrint() {