From 694fe61ae3f3d36b9fc11300eb133b467e7de8a5 Mon Sep 17 00:00:00 2001 From: Baruch Burstein Date: Sun, 2 Jul 2017 11:52:29 +0300 Subject: [PATCH 1/4] Add ability to format any streamable class This should probably be done at the source of this external file, but I couldn't find where that is. Perhaps it should be mentioned in the file header comment? --- include/external/tbc_text_format.h | 85 +++++++++++++++++------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/include/external/tbc_text_format.h b/include/external/tbc_text_format.h index 5209d804..39030ab0 100644 --- a/include/external/tbc_text_format.h +++ b/include/external/tbc_text_format.h @@ -53,13 +53,55 @@ namespace Tbc { public: Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() ) : attr( _attr ) + { + init( _str ); + } + + template + Text( T const& _val, TextAttributes const& _attr = TextAttributes() ) + : attr( _attr ) + { + std::ostringstream oss; + oss << _val; + init( oss.str() ); + } + + typedef std::vector::const_iterator const_iterator; + + const_iterator begin() const { return lines.begin(); } + const_iterator end() const { return lines.end(); } + std::string const& last() const { return lines.back(); } + std::size_t size() const { return lines.size(); } + std::string const& operator[]( std::size_t _index ) const { return lines[_index]; } + std::string toString() const { + std::ostringstream oss; + oss << *this; + return oss.str(); + } + + inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) { + for( Text::const_iterator it = _text.begin(), itEnd = _text.end(); + it != itEnd; ++it ) { + if( it != _text.begin() ) + _stream << "\n"; + _stream << *it; + } + return _stream; + } + + + private: + TextAttributes attr; + std::vector lines; + + void init( std::string const& _str ) { const std::string wrappableBeforeChars = "[({<\t"; const std::string wrappableAfterChars = "])}>-,./|\\"; const std::string wrappableInsteadOfChars = " \n\r"; - std::string indent = _attr.initialIndent != std::string::npos - ? std::string( _attr.initialIndent, ' ' ) - : std::string( _attr.indent, ' ' ); + std::string indent = attr.initialIndent != std::string::npos + ? std::string( attr.initialIndent, ' ' ) + : std::string( attr.indent, ' ' ); typedef std::string::const_iterator iterator; iterator it = _str.begin(); @@ -74,7 +116,7 @@ namespace Tbc { std::string suffix; - std::size_t width = (std::min)( static_cast( strEnd-it ), _attr.width-static_cast( indent.size() ) ); + std::size_t width = (std::min)( static_cast( strEnd-it ), attr.width-static_cast( indent.size() ) ); iterator itEnd = it+width; iterator itNext = _str.end(); @@ -121,42 +163,11 @@ namespace Tbc { } lines.push_back( indent + std::string( it, itEnd ) + suffix ); - if( indent.size() != _attr.indent ) - indent = std::string( _attr.indent, ' ' ); + if( indent.size() != attr.indent ) + indent = std::string( attr.indent, ' ' ); it = itNext; } } - - - - typedef std::vector::const_iterator const_iterator; - - const_iterator begin() const { return lines.begin(); } - const_iterator end() const { return lines.end(); } - std::string const& last() const { return lines.back(); } - std::size_t size() const { return lines.size(); } - std::string const& operator[]( std::size_t _index ) const { return lines[_index]; } - std::string toString() const { - std::ostringstream oss; - oss << *this; - return oss.str(); - } - - inline friend std::ostream& operator << ( std::ostream& _stream, Text const& _text ) { - for( Text::const_iterator it = _text.begin(), itEnd = _text.end(); - it != itEnd; ++it ) { - if( it != _text.begin() ) - _stream << "\n"; - _stream << *it; - } - return _stream; - } - - - private: - std::string str; - TextAttributes attr; - std::vector lines; }; } // end namespace Tbc From bb9f2bb3ad293971b376cd78a55b947ee62f07aa Mon Sep 17 00:00:00 2001 From: Baruch Burstein Date: Tue, 4 Jul 2017 18:16:42 +0300 Subject: [PATCH 2/4] Use new formatting capabilities --- include/internal/catch_list.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/internal/catch_list.hpp b/include/internal/catch_list.hpp index 5d9d8ed4..a423f3b4 100644 --- a/include/internal/catch_list.hpp +++ b/include/internal/catch_list.hpp @@ -45,7 +45,7 @@ namespace Catch { Catch::cout() << Text( testCaseInfo.name, nameAttr ) << std::endl; if( config.verbosity() >= Verbosity::High ) { - Catch::cout() << " " << testCaseInfo.lineInfo << std::endl; + Catch::cout() << Text( testCaseInfo.lineInfo, descAttr ) << std::endl; std::string description = testCaseInfo.description; if( description == "" ) description = "(NO DESCRIPTION)"; From f7493475230aa25a4086bbd44af59f9705aea493 Mon Sep 17 00:00:00 2001 From: Baruch Burstein Date: Thu, 6 Jul 2017 01:25:49 +0300 Subject: [PATCH 3/4] Check that reporter supports requested verbosity --- include/internal/catch_config.hpp | 3 +-- include/internal/catch_interfaces_config.h | 1 + include/internal/catch_interfaces_reporter.h | 3 +++ include/reporters/catch_reporter_bases.hpp | 12 ++++++++++++ include/reporters/catch_reporter_multi.hpp | 18 ++++++++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/include/internal/catch_config.hpp b/include/internal/catch_config.hpp index bc1f5b26..3f967b93 100644 --- a/include/internal/catch_config.hpp +++ b/include/internal/catch_config.hpp @@ -86,8 +86,6 @@ namespace Catch { bool listTags() const { return m_data.listTags; } bool listReporters() const { return m_data.listReporters; } - Verbosity verbosity() const { return m_data.verbosity; } - std::string getProcessName() const { return m_data.processName; } std::vector const& getReporterNames() const { return m_data.reporterNames; } @@ -110,6 +108,7 @@ namespace Catch { virtual bool shouldDebugBreak() const override { return m_data.shouldDebugBreak; } virtual int abortAfter() const override { return m_data.abortAfter; } virtual bool showInvisibles() const override { return m_data.showInvisibles; } + virtual Verbosity verbosity() const override { return m_data.verbosity; } private: diff --git a/include/internal/catch_interfaces_config.h b/include/internal/catch_interfaces_config.h index 75499014..d2d7602c 100644 --- a/include/internal/catch_interfaces_config.h +++ b/include/internal/catch_interfaces_config.h @@ -64,6 +64,7 @@ namespace Catch { virtual unsigned int rngSeed() const = 0; virtual UseColour::YesOrNo useColour() const = 0; virtual std::vector const& getSectionsToRun() const = 0; + virtual Verbosity verbosity() const = 0; }; using IConfigPtr = std::shared_ptr; diff --git a/include/internal/catch_interfaces_reporter.h b/include/internal/catch_interfaces_reporter.h index 7a42f097..ea68aa54 100644 --- a/include/internal/catch_interfaces_reporter.h +++ b/include/internal/catch_interfaces_reporter.h @@ -20,6 +20,7 @@ #include #include #include +#include #include namespace Catch @@ -212,6 +213,8 @@ namespace Catch virtual ReporterPreferences getPreferences() const = 0; + virtual std::set const& getSupportedVerbosities() const = 0; + virtual void noMatchingTestCases( std::string const& spec ) = 0; virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0; diff --git a/include/reporters/catch_reporter_bases.hpp b/include/reporters/catch_reporter_bases.hpp index 27767060..f6a8d28e 100644 --- a/include/reporters/catch_reporter_bases.hpp +++ b/include/reporters/catch_reporter_bases.hpp @@ -49,12 +49,18 @@ namespace Catch { stream( _config.stream() ) { m_reporterPrefs.shouldRedirectStdOut = false; + CATCH_ENFORCE( getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" ); } virtual ReporterPreferences getPreferences() const override { return m_reporterPrefs; } + virtual std::set const& getSupportedVerbosities() const override { + static std::set supported{ Verbosity::Normal }; + return supported; + } + virtual ~StreamingReporterBase() override; virtual void noMatchingTestCases( std::string const& ) override {} @@ -155,6 +161,7 @@ namespace Catch { stream( _config.stream() ) { m_reporterPrefs.shouldRedirectStdOut = false; + CATCH_ENFORCE( getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" ); } ~CumulativeReporterBase(); @@ -162,6 +169,11 @@ namespace Catch { return m_reporterPrefs; } + virtual std::set const& getSupportedVerbosities() const override { + static std::set supported{ Verbosity::Normal }; + return supported; + } + virtual void testRunStarting( TestRunInfo const& ) override {} virtual void testGroupStarting( GroupInfo const& ) override {} diff --git a/include/reporters/catch_reporter_multi.hpp b/include/reporters/catch_reporter_multi.hpp index 589c34e0..57f0974c 100644 --- a/include/reporters/catch_reporter_multi.hpp +++ b/include/reporters/catch_reporter_multi.hpp @@ -15,9 +15,23 @@ namespace Catch { class MultipleReporters : public IStreamingReporter { typedef std::vector Reporters; Reporters m_reporters; + std::set m_verbosities; public: void add( IStreamingReporterPtr&& reporter ) { + if( m_reporters.empty() ) { + m_verbosities = reporter->getSupportedVerbosities(); + } + else { + for( auto it = m_verbosities.cbegin(); it != m_verbosities.cend(); ) { + if( reporter->getSupportedVerbosities().count( *it ) == 0 ) { + it = m_verbosities.erase(it); + } + else { + ++it; + } + } + } m_reporters.push_back( std::move( reporter ) ); } @@ -27,6 +41,10 @@ public: // IStreamingReporter return m_reporters[0]->getPreferences(); } + virtual std::set const& getSupportedVerbosities() const override { + return m_verbosities; + } + virtual void noMatchingTestCases( std::string const& spec ) override { for( auto const& reporter : m_reporters ) reporter->noMatchingTestCases( spec ); From 058b21e604dd3486fe4f7957e4a4485104943905 Mon Sep 17 00:00:00 2001 From: Baruch Burstein Date: Sun, 9 Jul 2017 12:46:53 +0300 Subject: [PATCH 4/4] Previous implementation didn't work It relied on calling a virtual method from a base constructer --- include/internal/catch_impl.hpp | 10 +++++---- include/internal/catch_interfaces_reporter.h | 5 ++--- include/reporters/catch_reporter_automake.hpp | 2 +- include/reporters/catch_reporter_bases.hpp | 21 +++++++++---------- include/reporters/catch_reporter_compact.hpp | 2 +- include/reporters/catch_reporter_console.hpp | 2 +- include/reporters/catch_reporter_junit.hpp | 2 +- include/reporters/catch_reporter_multi.hpp | 18 ++-------------- include/reporters/catch_reporter_tap.hpp | 2 +- include/reporters/catch_reporter_teamcity.hpp | 2 +- include/reporters/catch_reporter_xml.hpp | 2 +- 11 files changed, 27 insertions(+), 41 deletions(-) diff --git a/include/internal/catch_impl.hpp b/include/internal/catch_impl.hpp index 2306af7f..242d9f82 100644 --- a/include/internal/catch_impl.hpp +++ b/include/internal/catch_impl.hpp @@ -77,10 +77,12 @@ namespace Catch { TestCaseStats::~TestCaseStats() {} TestGroupStats::~TestGroupStats() {} TestRunStats::~TestRunStats() {} - CumulativeReporterBase::SectionNode::~SectionNode() {} - CumulativeReporterBase::~CumulativeReporterBase() {} - - StreamingReporterBase::~StreamingReporterBase() {} + template + CumulativeReporterBase::SectionNode::~SectionNode() {} + template + CumulativeReporterBase::~CumulativeReporterBase() {} + template + StreamingReporterBase::~StreamingReporterBase() {} ConsoleReporter::~ConsoleReporter() {} CompactReporter::~CompactReporter() {} IRunner::~IRunner() {} diff --git a/include/internal/catch_interfaces_reporter.h b/include/internal/catch_interfaces_reporter.h index ea68aa54..5eae83cc 100644 --- a/include/internal/catch_interfaces_reporter.h +++ b/include/internal/catch_interfaces_reporter.h @@ -208,13 +208,12 @@ namespace Catch struct IStreamingReporter { virtual ~IStreamingReporter(); - // Implementing class must also provide the following static method: + // Implementing class must also provide the following static methods: // static std::string getDescription(); + // static std::set getSupportedVerbosities() virtual ReporterPreferences getPreferences() const = 0; - virtual std::set const& getSupportedVerbosities() const = 0; - virtual void noMatchingTestCases( std::string const& spec ) = 0; virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0; diff --git a/include/reporters/catch_reporter_automake.hpp b/include/reporters/catch_reporter_automake.hpp index 00478e1f..7a841a1a 100644 --- a/include/reporters/catch_reporter_automake.hpp +++ b/include/reporters/catch_reporter_automake.hpp @@ -16,7 +16,7 @@ namespace Catch { - struct AutomakeReporter : StreamingReporterBase { + struct AutomakeReporter : StreamingReporterBase { AutomakeReporter( ReporterConfig const& _config ) : StreamingReporterBase( _config ) {} diff --git a/include/reporters/catch_reporter_bases.hpp b/include/reporters/catch_reporter_bases.hpp index f6a8d28e..9089b616 100644 --- a/include/reporters/catch_reporter_bases.hpp +++ b/include/reporters/catch_reporter_bases.hpp @@ -41,7 +41,7 @@ namespace Catch { } } - + template struct StreamingReporterBase : IStreamingReporter { StreamingReporterBase( ReporterConfig const& _config ) @@ -49,16 +49,15 @@ namespace Catch { stream( _config.stream() ) { m_reporterPrefs.shouldRedirectStdOut = false; - CATCH_ENFORCE( getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" ); + CATCH_ENFORCE( DerivedT::getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" ); } virtual ReporterPreferences getPreferences() const override { return m_reporterPrefs; } - virtual std::set const& getSupportedVerbosities() const override { - static std::set supported{ Verbosity::Normal }; - return supported; + static std::set getSupportedVerbosities() { + return { Verbosity::Normal }; } virtual ~StreamingReporterBase() override; @@ -110,6 +109,7 @@ namespace Catch { ReporterPreferences m_reporterPrefs; }; + template struct CumulativeReporterBase : IStreamingReporter { template struct Node { @@ -161,7 +161,7 @@ namespace Catch { stream( _config.stream() ) { m_reporterPrefs.shouldRedirectStdOut = false; - CATCH_ENFORCE( getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" ); + CATCH_ENFORCE( DerivedT::getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" ); } ~CumulativeReporterBase(); @@ -169,9 +169,8 @@ namespace Catch { return m_reporterPrefs; } - virtual std::set const& getSupportedVerbosities() const override { - static std::set supported{ Verbosity::Normal }; - return supported; + static std::set getSupportedVerbosities() { + return { Verbosity::Normal }; } virtual void testRunStarting( TestRunInfo const& ) override {} @@ -189,7 +188,7 @@ namespace Catch { } else { SectionNode& parentNode = *m_sectionStack.back(); - SectionNode::ChildSections::const_iterator it = + typename SectionNode::ChildSections::const_iterator it = std::find_if( parentNode.childSections.begin(), parentNode.childSections.end(), BySectionInfo( sectionInfo ) ); @@ -284,7 +283,7 @@ namespace Catch { } - struct TestEventListenerBase : StreamingReporterBase { + struct TestEventListenerBase : StreamingReporterBase { TestEventListenerBase( ReporterConfig const& _config ) : StreamingReporterBase( _config ) {} diff --git a/include/reporters/catch_reporter_compact.hpp b/include/reporters/catch_reporter_compact.hpp index 1579a608..ff461f14 100644 --- a/include/reporters/catch_reporter_compact.hpp +++ b/include/reporters/catch_reporter_compact.hpp @@ -15,7 +15,7 @@ namespace Catch { - struct CompactReporter : StreamingReporterBase { + struct CompactReporter : StreamingReporterBase { using StreamingReporterBase::StreamingReporterBase; diff --git a/include/reporters/catch_reporter_console.hpp b/include/reporters/catch_reporter_console.hpp index bbe38a87..3c2130f8 100644 --- a/include/reporters/catch_reporter_console.hpp +++ b/include/reporters/catch_reporter_console.hpp @@ -19,7 +19,7 @@ namespace Catch { - struct ConsoleReporter : StreamingReporterBase { + struct ConsoleReporter : StreamingReporterBase { using StreamingReporterBase::StreamingReporterBase; virtual ~ConsoleReporter() override; diff --git a/include/reporters/catch_reporter_junit.hpp b/include/reporters/catch_reporter_junit.hpp index cebdf3c7..08652565 100644 --- a/include/reporters/catch_reporter_junit.hpp +++ b/include/reporters/catch_reporter_junit.hpp @@ -47,7 +47,7 @@ namespace Catch { } - class JunitReporter : public CumulativeReporterBase { + class JunitReporter : public CumulativeReporterBase { public: JunitReporter( ReporterConfig const& _config ) : CumulativeReporterBase( _config ), diff --git a/include/reporters/catch_reporter_multi.hpp b/include/reporters/catch_reporter_multi.hpp index 57f0974c..36d56584 100644 --- a/include/reporters/catch_reporter_multi.hpp +++ b/include/reporters/catch_reporter_multi.hpp @@ -15,23 +15,9 @@ namespace Catch { class MultipleReporters : public IStreamingReporter { typedef std::vector Reporters; Reporters m_reporters; - std::set m_verbosities; public: void add( IStreamingReporterPtr&& reporter ) { - if( m_reporters.empty() ) { - m_verbosities = reporter->getSupportedVerbosities(); - } - else { - for( auto it = m_verbosities.cbegin(); it != m_verbosities.cend(); ) { - if( reporter->getSupportedVerbosities().count( *it ) == 0 ) { - it = m_verbosities.erase(it); - } - else { - ++it; - } - } - } m_reporters.push_back( std::move( reporter ) ); } @@ -41,8 +27,8 @@ public: // IStreamingReporter return m_reporters[0]->getPreferences(); } - virtual std::set const& getSupportedVerbosities() const override { - return m_verbosities; + static std::set getSupportedVerbosities() { + return { }; } virtual void noMatchingTestCases( std::string const& spec ) override { diff --git a/include/reporters/catch_reporter_tap.hpp b/include/reporters/catch_reporter_tap.hpp index b34acac3..40106649 100644 --- a/include/reporters/catch_reporter_tap.hpp +++ b/include/reporters/catch_reporter_tap.hpp @@ -19,7 +19,7 @@ namespace Catch { - struct TAPReporter : StreamingReporterBase { + struct TAPReporter : StreamingReporterBase { using StreamingReporterBase::StreamingReporterBase; diff --git a/include/reporters/catch_reporter_teamcity.hpp b/include/reporters/catch_reporter_teamcity.hpp index 623ae6a3..33548987 100644 --- a/include/reporters/catch_reporter_teamcity.hpp +++ b/include/reporters/catch_reporter_teamcity.hpp @@ -23,7 +23,7 @@ namespace Catch { - struct TeamCityReporter : StreamingReporterBase { + struct TeamCityReporter : StreamingReporterBase { TeamCityReporter( ReporterConfig const& _config ) : StreamingReporterBase( _config ) { diff --git a/include/reporters/catch_reporter_xml.hpp b/include/reporters/catch_reporter_xml.hpp index b99ec11f..d685e979 100644 --- a/include/reporters/catch_reporter_xml.hpp +++ b/include/reporters/catch_reporter_xml.hpp @@ -16,7 +16,7 @@ #include "../internal/catch_timer.h" namespace Catch { - class XmlReporter : public StreamingReporterBase { + class XmlReporter : public StreamingReporterBase { public: XmlReporter( ReporterConfig const& _config ) : StreamingReporterBase( _config ),