Skip to content

Commit

Permalink
fixup! Use console reporter's totals summary for compact reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
psalz committed Oct 25, 2022
1 parent 3c7ccf9 commit f0982f6
Showing 1 changed file with 65 additions and 49 deletions.
114 changes: 65 additions & 49 deletions src/catch2/reporters/catch_reporter_helpers.cpp
Expand Up @@ -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<std::string> rows;
private:
std::string m_suffix;
Colour::Code m_colour;
std::uint64_t m_width = 0;
std::vector<std::string> m_rows;
};

static void printSummaryRow( StringRef label,
std::vector<SummaryColumn> 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<SummaryColumn> 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;
Expand All @@ -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';
Expand All @@ -287,36 +301,38 @@ 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 << " ("
<< pluralise( totals.assertions.passed, "assertion"_sr )
<< " in "
<< pluralise( totals.testCases.passed, "test case"_sr )
<< ')' << '\n';
} else {

std::vector<SummaryColumn> 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<SummaryColumn> 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

0 comments on commit f0982f6

Please sign in to comment.