Provide CompactReporter declaration with EXTERNAL_INTERFACES

Related to #991
This commit is contained in:
Martin Hořeňovský 2017-11-14 16:04:11 +01:00
parent a3cba7a0d5
commit 6acdacfde0
4 changed files with 263 additions and 228 deletions

View File

@ -247,6 +247,7 @@ CheckFileList(INTERNAL_FILES ${HEADER_DIR}/internal)
set(REPORTER_HEADERS
${HEADER_DIR}/reporters/catch_reporter_automake.hpp
${HEADER_DIR}/reporters/catch_reporter_bases.hpp
${HEADER_DIR}/reporters/catch_reporter_compact.h
${HEADER_DIR}/reporters/catch_reporter_multi.h
${HEADER_DIR}/reporters/catch_reporter_tap.hpp
${HEADER_DIR}/reporters/catch_reporter_teamcity.hpp

View File

@ -11,4 +11,7 @@
#include "catch_console_colour.h"
#include "catch_reporter_registrars.hpp"
// Allow users to base their work off existing reporters
#include "../reporters/catch_reporter_compact.h"
#endif // TWOBLUECUBES_CATCH_EXTERNAL_INTERFACES_H_INCLUDED

View File

@ -1,12 +1,11 @@
/*
* Created by Martin Moene on 2013-12-05.
* Copyright 2012 Martin Moene. All rights reserved.
* Created by Martin on 2017-11-14.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "catch_reporter_bases.hpp"
#include "catch_reporter_compact.h"
#include "../internal/catch_reporter_registrars.hpp"
#include "internal/catch_console_colour.h"
@ -28,64 +27,51 @@ namespace {
return count == 1 ? std::string() :
count == 2 ? "both " : "all " ;
}
}
} // anon namespace
namespace Catch {
struct CompactReporter : StreamingReporterBase<CompactReporter> {
using StreamingReporterBase::StreamingReporterBase;
~CompactReporter() override;
static std::string getDescription() {
return "Reports test results on a single line, suitable for IDEs";
}
ReporterPreferences getPreferences() const override {
ReporterPreferences prefs;
prefs.shouldRedirectStdOut = false;
return prefs;
}
void noMatchingTestCases( std::string const& spec ) override {
stream << "No test cases matched '" << spec << '\'' << std::endl;
}
void assertionStarting( AssertionInfo const& ) override {}
bool assertionEnded( AssertionStats const& _assertionStats ) override {
AssertionResult const& result = _assertionStats.assertionResult;
bool printInfoMessages = true;
// Drop out if result was successful and we're not printing those
if( !m_config->includeSuccessfulResults() && result.isOk() ) {
if( result.getResultType() != ResultWas::Warning )
return false;
printInfoMessages = false;
}
AssertionPrinter printer( stream, _assertionStats, printInfoMessages );
printer.print();
stream << std::endl;
return true;
}
void sectionEnded(SectionStats const& _sectionStats) override {
if (m_config->showDurations() == ShowDurations::Always) {
stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl;
namespace {
// Colour, message variants:
// - white: No tests ran.
// - red: Failed [both/all] N test cases, failed [both/all] M assertions.
// - white: Passed [both/all] N test cases (no assertions).
// - red: Failed N tests cases, failed M assertions.
// - green: Passed [both/all] N tests cases with M assertions.
void printTotals(std::ostream& out, const Totals& totals) {
if (totals.testCases.total() == 0) {
out << "No tests ran.";
} else if (totals.testCases.failed == totals.testCases.total()) {
Colour colour(Colour::ResultError);
const std::string qualify_assertions_failed =
totals.assertions.failed == totals.assertions.total() ?
bothOrAll(totals.assertions.failed) : std::string();
out <<
"Failed " << bothOrAll(totals.testCases.failed)
<< pluralise(totals.testCases.failed, "test case") << ", "
"failed " << qualify_assertions_failed <<
pluralise(totals.assertions.failed, "assertion") << '.';
} else if (totals.assertions.total() == 0) {
out <<
"Passed " << bothOrAll(totals.testCases.total())
<< pluralise(totals.testCases.total(), "test case")
<< " (no assertions).";
} else if (totals.assertions.failed) {
Colour colour(Colour::ResultError);
out <<
"Failed " << pluralise(totals.testCases.failed, "test case") << ", "
"failed " << pluralise(totals.assertions.failed, "assertion") << '.';
} else {
Colour colour(Colour::ResultSuccess);
out <<
"Passed " << bothOrAll(totals.testCases.passed)
<< pluralise(totals.testCases.passed, "test case") <<
" with " << pluralise(totals.assertions.passed, "assertion") << '.';
}
}
void testRunEnded( TestRunStats const& _testRunStats ) override {
printTotals( _testRunStats.totals );
stream << '\n' << std::endl;
StreamingReporterBase::testRunEnded( _testRunStats );
}
private:
// Implementation of CompactReporter formatting
class AssertionPrinter {
public:
AssertionPrinter& operator= (AssertionPrinter const&) = delete;
@ -95,8 +81,7 @@ namespace Catch {
, result(_stats.assertionResult)
, messages(_stats.infoMessages)
, itMessage(_stats.infoMessages.begin())
, printInfoMessages( _printInfoMessages )
{}
, printInfoMessages(_printInfoMessages) {}
void print() {
printSourceInfo();
@ -253,49 +238,54 @@ namespace Catch {
bool printInfoMessages;
};
// Colour, message variants:
// - white: No tests ran.
// - red: Failed [both/all] N test cases, failed [both/all] M assertions.
// - white: Passed [both/all] N test cases (no assertions).
// - red: Failed N tests cases, failed M assertions.
// - green: Passed [both/all] N tests cases with M assertions.
} // anon namespace
void printTotals( const Totals& totals ) const {
if( totals.testCases.total() == 0 ) {
stream << "No tests ran.";
std::string CompactReporter::getDescription() {
return "Reports test results on a single line, suitable for IDEs";
}
else if( totals.testCases.failed == totals.testCases.total() ) {
Colour colour( Colour::ResultError );
const std::string qualify_assertions_failed =
totals.assertions.failed == totals.assertions.total() ?
bothOrAll( totals.assertions.failed ) : std::string();
stream <<
"Failed " << bothOrAll( totals.testCases.failed )
<< pluralise( totals.testCases.failed, "test case" ) << ", "
"failed " << qualify_assertions_failed <<
pluralise( totals.assertions.failed, "assertion" ) << '.';
ReporterPreferences CompactReporter::getPreferences() const {
ReporterPreferences prefs;
prefs.shouldRedirectStdOut = false;
return prefs;
}
else if( totals.assertions.total() == 0 ) {
stream <<
"Passed " << bothOrAll( totals.testCases.total() )
<< pluralise( totals.testCases.total(), "test case" )
<< " (no assertions).";
void CompactReporter::noMatchingTestCases( std::string const& spec ) {
stream << "No test cases matched '" << spec << '\'' << std::endl;
}
else if( totals.assertions.failed ) {
Colour colour( Colour::ResultError );
stream <<
"Failed " << pluralise( totals.testCases.failed, "test case" ) << ", "
"failed " << pluralise( totals.assertions.failed, "assertion" ) << '.';
void CompactReporter::assertionStarting( AssertionInfo const& ) {}
bool CompactReporter::assertionEnded( AssertionStats const& _assertionStats ) {
AssertionResult const& result = _assertionStats.assertionResult;
bool printInfoMessages = true;
// Drop out if result was successful and we're not printing those
if( !m_config->includeSuccessfulResults() && result.isOk() ) {
if( result.getResultType() != ResultWas::Warning )
return false;
printInfoMessages = false;
}
else {
Colour colour( Colour::ResultSuccess );
stream <<
"Passed " << bothOrAll( totals.testCases.passed )
<< pluralise( totals.testCases.passed, "test case" ) <<
" with " << pluralise( totals.assertions.passed, "assertion" ) << '.';
AssertionPrinter printer( stream, _assertionStats, printInfoMessages );
printer.print();
stream << std::endl;
return true;
}
void CompactReporter::sectionEnded(SectionStats const& _sectionStats) {
if (m_config->showDurations() == ShowDurations::Always) {
stream << getFormattedDuration(_sectionStats.durationInSeconds) << " s: " << _sectionStats.sectionInfo.name << std::endl;
}
}
};
void CompactReporter::testRunEnded( TestRunStats const& _testRunStats ) {
printTotals( stream, _testRunStats.totals );
stream << '\n' << std::endl;
StreamingReporterBase::testRunEnded( _testRunStats );
}
CompactReporter::~CompactReporter() {}

View File

@ -0,0 +1,41 @@
/*
* Created by Martin Moene on 2013-12-05.
* Copyright 2012 Martin Moene. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_REPORTER_COMPACT_H_INCLUDED
#define TWOBLUECUBES_CATCH_REPORTER_COMPACT_H_INCLUDED
#include "catch_reporter_bases.hpp"
namespace Catch {
struct CompactReporter : StreamingReporterBase<CompactReporter> {
using StreamingReporterBase::StreamingReporterBase;
~CompactReporter() override;
static std::string getDescription();
ReporterPreferences getPreferences() const override;
void noMatchingTestCases(std::string const& spec) override;
void assertionStarting(AssertionInfo const&) override;
bool assertionEnded(AssertionStats const& _assertionStats) override;
void sectionEnded(SectionStats const& _sectionStats) override;
void testRunEnded(TestRunStats const& _testRunStats) override;
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_REPORTER_COMPACT_H_INCLUDED