Do not assume that CATCH_CONFIG_CONSOLE_WIDTH is always a constant

This commit is contained in:
Nic Ramage 2021-07-26 13:15:15 +02:00
parent a9bbc42e9c
commit dc18c0556d
3 changed files with 15 additions and 10 deletions

View File

@ -120,6 +120,7 @@ namespace Catch {
} }
} }
auto consoleWidth = static_cast<size_t> (CATCH_CONFIG_CONSOLE_WIDTH);
for( auto const& tagCount : tagCounts ) { for( auto const& tagCount : tagCounts ) {
ReusableStringStream rss; ReusableStringStream rss;
rss << " " << std::setw(2) << tagCount.second.count << " "; rss << " " << std::setw(2) << tagCount.second.count << " ";
@ -127,7 +128,7 @@ namespace Catch {
auto wrapper = Column( tagCount.second.all() ) auto wrapper = Column( tagCount.second.all() )
.initialIndent( 0 ) .initialIndent( 0 )
.indent( str.size() ) .indent( str.size() )
.width( CATCH_CONFIG_CONSOLE_WIDTH-10 ); .width( consoleWidth-10 );
Catch::cout() << str << wrapper << '\n'; Catch::cout() << str << wrapper << '\n';
} }
Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl; Catch::cout() << pluralise( tagCounts.size(), "tag" ) << '\n' << std::endl;
@ -141,6 +142,7 @@ namespace Catch {
for( auto const& factoryKvp : factories ) for( auto const& factoryKvp : factories )
maxNameLen = (std::max)( maxNameLen, factoryKvp.first.size() ); maxNameLen = (std::max)( maxNameLen, factoryKvp.first.size() );
auto consoleWidth = static_cast<size_t> (CATCH_CONFIG_CONSOLE_WIDTH);
for( auto const& factoryKvp : factories ) { for( auto const& factoryKvp : factories ) {
Catch::cout() Catch::cout()
<< Column( factoryKvp.first + ":" ) << Column( factoryKvp.first + ":" )
@ -149,7 +151,7 @@ namespace Catch {
+ Column( factoryKvp.second->getDescription() ) + Column( factoryKvp.second->getDescription() )
.initialIndent(0) .initialIndent(0)
.indent(2) .indent(2)
.width( CATCH_CONFIG_CONSOLE_WIDTH - maxNameLen-8 ) .width( consoleWidth - maxNameLen-8 )
<< "\n"; << "\n";
} }
Catch::cout() << std::endl; Catch::cout() << std::endl;

View File

@ -264,8 +264,9 @@ namespace Catch {
char const* getLineOfChars() { char const* getLineOfChars() {
static char line[CATCH_CONFIG_CONSOLE_MAXIMUM_WIDTH] = {0}; static char line[CATCH_CONFIG_CONSOLE_MAXIMUM_WIDTH] = {0};
if( !*line ) { if( !*line ) {
std::memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 ); auto consoleWidth = static_cast<size_t> (CATCH_CONFIG_CONSOLE_WIDTH);
line[CATCH_CONFIG_CONSOLE_WIDTH-1] = 0; std::memset( line, C, consoleWidth-1 );
line[consoleWidth-1] = 0;
} }
return line; return line;
} }

View File

@ -174,7 +174,7 @@ private:
}; };
std::size_t makeRatio(std::size_t number, std::size_t total) { std::size_t makeRatio(std::size_t number, std::size_t total) {
std::size_t ratio = total > 0 ? CATCH_CONFIG_CONSOLE_WIDTH * number / total : 0; std::size_t ratio = total > 0 ? static_cast<size_t> (CATCH_CONFIG_CONSOLE_WIDTH) * number / total : 0;
return (ratio == 0 && number > 0) ? 1 : ratio; return (ratio == 0 && number > 0) ? 1 : ratio;
} }
@ -350,10 +350,11 @@ ConsoleReporter::ConsoleReporter(ReporterConfig const& config)
: StreamingReporterBase(config), : StreamingReporterBase(config),
m_tablePrinter(new TablePrinter(config.stream(), m_tablePrinter(new TablePrinter(config.stream(),
[&config]() -> std::vector<ColumnInfo> { [&config]() -> std::vector<ColumnInfo> {
auto consoleWidth = static_cast<int> (CATCH_CONFIG_CONSOLE_WIDTH);
if (config.fullConfig()->benchmarkNoAnalysis()) if (config.fullConfig()->benchmarkNoAnalysis())
{ {
return{ return{
{ "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 43, ColumnInfo::Left }, { "benchmark name", consoleWidth - 43, ColumnInfo::Left },
{ " samples", 14, ColumnInfo::Right }, { " samples", 14, ColumnInfo::Right },
{ " iterations", 14, ColumnInfo::Right }, { " iterations", 14, ColumnInfo::Right },
{ " mean", 14, ColumnInfo::Right } { " mean", 14, ColumnInfo::Right }
@ -362,7 +363,7 @@ ConsoleReporter::ConsoleReporter(ReporterConfig const& config)
else else
{ {
return{ return{
{ "benchmark name", CATCH_CONFIG_CONSOLE_WIDTH - 43, ColumnInfo::Left }, { "benchmark name", consoleWidth - 43, ColumnInfo::Left },
{ "samples mean std dev", 14, ColumnInfo::Right }, { "samples mean std dev", 14, ColumnInfo::Right },
{ "iterations low mean low std dev", 14, ColumnInfo::Right }, { "iterations low mean low std dev", 14, ColumnInfo::Right },
{ "estimated high mean high std dev", 14, ColumnInfo::Right } { "estimated high mean high std dev", 14, ColumnInfo::Right }
@ -656,13 +657,14 @@ void ConsoleReporter::printSummaryRow(std::string const& label, std::vector<Summ
} }
void ConsoleReporter::printTotalsDivider(Totals const& totals) { void ConsoleReporter::printTotalsDivider(Totals const& totals) {
auto consoleWidth = static_cast<size_t> (CATCH_CONFIG_CONSOLE_WIDTH);
if (totals.testCases.total() > 0) { if (totals.testCases.total() > 0) {
std::size_t failedRatio = makeRatio(totals.testCases.failed, totals.testCases.total()); std::size_t failedRatio = makeRatio(totals.testCases.failed, totals.testCases.total());
std::size_t failedButOkRatio = makeRatio(totals.testCases.failedButOk, totals.testCases.total()); std::size_t failedButOkRatio = makeRatio(totals.testCases.failedButOk, totals.testCases.total());
std::size_t passedRatio = makeRatio(totals.testCases.passed, totals.testCases.total()); std::size_t passedRatio = makeRatio(totals.testCases.passed, totals.testCases.total());
while (failedRatio + failedButOkRatio + passedRatio < CATCH_CONFIG_CONSOLE_WIDTH - 1) while (failedRatio + failedButOkRatio + passedRatio < consoleWidth - 1)
findMax(failedRatio, failedButOkRatio, passedRatio)++; findMax(failedRatio, failedButOkRatio, passedRatio)++;
while (failedRatio + failedButOkRatio + passedRatio > CATCH_CONFIG_CONSOLE_WIDTH - 1) while (failedRatio + failedButOkRatio + passedRatio > consoleWidth - 1)
findMax(failedRatio, failedButOkRatio, passedRatio)--; findMax(failedRatio, failedButOkRatio, passedRatio)--;
stream << Colour(Colour::Error) << std::string(failedRatio, '='); stream << Colour(Colour::Error) << std::string(failedRatio, '=');
@ -672,7 +674,7 @@ void ConsoleReporter::printTotalsDivider(Totals const& totals) {
else else
stream << Colour(Colour::Success) << std::string(passedRatio, '='); stream << Colour(Colour::Success) << std::string(passedRatio, '=');
} else { } else {
stream << Colour(Colour::Warning) << std::string(CATCH_CONFIG_CONSOLE_WIDTH - 1, '='); stream << Colour(Colour::Warning) << std::string(consoleWidth - 1, '=');
} }
stream << '\n'; stream << '\n';
} }