diff --git a/src/catch2/reporters/catch_reporter_helpers.cpp b/src/catch2/reporters/catch_reporter_helpers.cpp index d95ec4dd4a..9d33384783 100644 --- a/src/catch2/reporters/catch_reporter_helpers.cpp +++ b/src/catch2/reporters/catch_reporter_helpers.cpp @@ -236,36 +236,50 @@ namespace Catch { } namespace { - struct SummaryColumn { - SummaryColumn( std::string _label, Colour::Code _colour ): - label( CATCH_MOVE( _label ) ), colour( _colour ) {} - SummaryColumn addRow( std::uint64_t count ) { - ReusableStringStream rss; - rss << count; - std::string row = rss.str(); - for ( auto& oldRow : rows ) { - while ( oldRow.size() < row.size() ) - oldRow = ' ' + oldRow; - while ( oldRow.size() > row.size() ) - row = ' ' + row; + class SummaryColumn { + public: + SummaryColumn( std::string suffix, Colour::Code colour ): + m_suffix( CATCH_MOVE( suffix ) ), m_colour( colour ) {} + + SummaryColumn&& addRow( std::uint64_t count ) && { + std::string row = std::to_string(count); + auto const new_width = std::max( m_width, row.size() ); + if ( new_width > m_width ) { + for ( auto& oldRow : m_rows ) { + oldRow = std::string( new_width - m_width, ' ' ) + .append( oldRow ); + } + } else { + row = + std::string( m_width - row.size(), ' ' ).append( row ); } - rows.push_back( row ); - return *this; + m_width = new_width; + m_rows.push_back( row ); + return std::move( *this ); + } + + std::string const& getSuffix() const { return m_suffix; } + Colour::Code getColour() const { return m_colour; } + std::string const& getRow( size_t index ) const { + return m_rows[index]; } - std::string label; - Colour::Code colour; - std::vector rows; + private: + std::string m_suffix; + Colour::Code m_colour; + std::uint64_t m_width = 0; + std::vector m_rows; }; - static void printSummaryRow( StringRef label, - std::vector const& cols, - std::size_t row, - std::ostream& stream, - ColourImpl& colour ) { - for ( auto col : cols ) { - std::string const& value = col.rows[row]; - if ( col.label.empty() ) { + void printSummaryRow( std::ostream& stream, + ColourImpl& colour, + StringRef label, + std::vector const& cols, + std::size_t row ) { + for ( auto const& col : cols ) { + auto const& value = col.getRow( row ); + auto const& suffix = col.getSuffix(); + if ( suffix.empty() ) { stream << label << ": "; if ( value != "0" ) { stream << value; @@ -275,8 +289,8 @@ namespace Catch { } } else if ( value != "0" ) { stream << colour.guardColour( Colour::LightGrey ) << " | " - << colour.guardColour( col.colour ) << value << ' ' - << col.label; + << colour.guardColour( col.getColour() ) << value + << ' ' << suffix; } } stream << '\n'; @@ -287,9 +301,12 @@ namespace Catch { ColourImpl& streamColour, Totals const& totals ) { if ( totals.testCases.total() == 0 ) { - stream << streamColour.guardColour( Colour::Warning ) << "No tests ran\n"; - } else if ( totals.assertions.total() > 0 && - totals.testCases.allPassed() ) { + stream << streamColour.guardColour( Colour::Warning ) + << "No tests ran\n"; + return; + } + + if ( totals.assertions.total() > 0 && totals.testCases.allPassed() ) { stream << streamColour.guardColour( Colour::ResultSuccess ) << "All tests passed"; stream << " (" @@ -297,26 +314,25 @@ namespace Catch { << " in " << pluralise( totals.testCases.passed, "test case"_sr ) << ')' << '\n'; - } else { - - std::vector columns; - columns.push_back( SummaryColumn( "", Colour::None ) - .addRow( totals.testCases.total() ) - .addRow( totals.assertions.total() ) ); - columns.push_back( SummaryColumn( "passed", Colour::Success ) - .addRow( totals.testCases.passed ) - .addRow( totals.assertions.passed ) ); - columns.push_back( SummaryColumn( "failed", Colour::ResultError ) - .addRow( totals.testCases.failed ) - .addRow( totals.assertions.failed ) ); - columns.push_back( SummaryColumn( "failed as expected", - Colour::ResultExpectedFailure ) - .addRow( totals.testCases.failedButOk ) - .addRow( totals.assertions.failedButOk ) ); - - printSummaryRow( "test cases"_sr, columns, 0, stream, streamColour ); - printSummaryRow( "assertions"_sr, columns, 1, stream, streamColour ); + return; } + + std::vector columns; + columns.push_back( SummaryColumn( "", Colour::None ) + .addRow( totals.testCases.total() ) + .addRow( totals.assertions.total() ) ); + columns.push_back( SummaryColumn( "passed", Colour::Success ) + .addRow( totals.testCases.passed ) + .addRow( totals.assertions.passed ) ); + columns.push_back( SummaryColumn( "failed", Colour::ResultError ) + .addRow( totals.testCases.failed ) + .addRow( totals.assertions.failed ) ); + columns.push_back( + SummaryColumn( "failed as expected", Colour::ResultExpectedFailure ) + .addRow( totals.testCases.failedButOk ) + .addRow( totals.assertions.failedButOk ) ); + printSummaryRow( stream, streamColour, "test cases"_sr, columns, 0 ); + printSummaryRow( stream, streamColour, "assertions"_sr, columns, 1 ); } } // namespace Catch