2013-12-03 19:52:41 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 27/11/2013.
|
|
|
|
* Copyright 2013 Two Blue Cubes Ltd. 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_BASES_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "../internal/catch_interfaces_reporter.h"
|
|
|
|
|
2014-12-22 08:42:25 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
struct StreamingReporterBase : SharedImpl<IStreamingReporter> {
|
|
|
|
|
|
|
|
StreamingReporterBase( ReporterConfig const& _config )
|
|
|
|
: m_config( _config.fullConfig() ),
|
|
|
|
stream( _config.stream() )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ReporterPreferences getPreferences() const CATCH_OVERRIDE {
|
|
|
|
return m_reporterPrefs;
|
|
|
|
}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual ~StreamingReporterBase() CATCH_OVERRIDE;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void noMatchingTestCases( std::string const& ) CATCH_OVERRIDE {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testRunStarting( TestRunInfo const& _testRunInfo ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestRunInfo = _testRunInfo;
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testGroupStarting( GroupInfo const& _groupInfo ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentGroupInfo = _groupInfo;
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseStarting( TestCaseInfo const& _testInfo ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo = _testInfo;
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionStarting( SectionInfo const& _sectionInfo ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
m_sectionStack.push_back( _sectionInfo );
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionEnded( SectionStats const& /* _sectionStats */ ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
m_sectionStack.pop_back();
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& /* _testCaseStats */ ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo.reset();
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testGroupEnded( TestGroupStats const& /* _testGroupStats */ ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentGroupInfo.reset();
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testRunEnded( TestRunStats const& /* _testRunStats */ ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo.reset();
|
|
|
|
currentGroupInfo.reset();
|
|
|
|
currentTestRunInfo.reset();
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void skipTest( TestCaseInfo const& ) CATCH_OVERRIDE {
|
2014-12-22 21:10:33 +01:00
|
|
|
// Don't do anything with this by default.
|
|
|
|
// It can optionally be overridden in the derived class.
|
|
|
|
}
|
|
|
|
|
2015-09-28 10:09:06 +02:00
|
|
|
Ptr<IConfig const> m_config;
|
2013-12-03 19:52:41 +01:00
|
|
|
std::ostream& stream;
|
|
|
|
|
|
|
|
LazyStat<TestRunInfo> currentTestRunInfo;
|
|
|
|
LazyStat<GroupInfo> currentGroupInfo;
|
|
|
|
LazyStat<TestCaseInfo> currentTestCaseInfo;
|
|
|
|
|
|
|
|
std::vector<SectionInfo> m_sectionStack;
|
2015-08-07 09:20:56 +02:00
|
|
|
ReporterPreferences m_reporterPrefs;
|
2013-12-03 19:52:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CumulativeReporterBase : SharedImpl<IStreamingReporter> {
|
|
|
|
template<typename T, typename ChildNodeT>
|
|
|
|
struct Node : SharedImpl<> {
|
|
|
|
explicit Node( T const& _value ) : value( _value ) {}
|
|
|
|
virtual ~Node() {}
|
|
|
|
|
|
|
|
typedef std::vector<Ptr<ChildNodeT> > ChildNodes;
|
|
|
|
T value;
|
|
|
|
ChildNodes children;
|
|
|
|
};
|
|
|
|
struct SectionNode : SharedImpl<> {
|
|
|
|
explicit SectionNode( SectionStats const& _stats ) : stats( _stats ) {}
|
|
|
|
virtual ~SectionNode();
|
|
|
|
|
|
|
|
bool operator == ( SectionNode const& other ) const {
|
|
|
|
return stats.sectionInfo.lineInfo == other.stats.sectionInfo.lineInfo;
|
|
|
|
}
|
|
|
|
bool operator == ( Ptr<SectionNode> const& other ) const {
|
|
|
|
return operator==( *other );
|
|
|
|
}
|
|
|
|
|
|
|
|
SectionStats stats;
|
|
|
|
typedef std::vector<Ptr<SectionNode> > ChildSections;
|
|
|
|
typedef std::vector<AssertionStats> Assertions;
|
|
|
|
ChildSections childSections;
|
|
|
|
Assertions assertions;
|
|
|
|
std::string stdOut;
|
|
|
|
std::string stdErr;
|
|
|
|
};
|
2013-12-18 09:37:23 +01:00
|
|
|
|
|
|
|
struct BySectionInfo {
|
|
|
|
BySectionInfo( SectionInfo const& other ) : m_other( other ) {}
|
2014-05-19 20:04:38 +02:00
|
|
|
BySectionInfo( BySectionInfo const& other ) : m_other( other.m_other ) {}
|
2013-12-18 09:37:23 +01:00
|
|
|
bool operator() ( Ptr<SectionNode> const& node ) const {
|
|
|
|
return node->stats.sectionInfo.lineInfo == m_other.lineInfo;
|
|
|
|
}
|
|
|
|
private:
|
2014-05-19 20:04:38 +02:00
|
|
|
void operator=( BySectionInfo const& );
|
2013-12-18 09:37:23 +01:00
|
|
|
SectionInfo const& m_other;
|
|
|
|
};
|
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
|
|
|
|
typedef Node<TestCaseStats, SectionNode> TestCaseNode;
|
|
|
|
typedef Node<TestGroupStats, TestCaseNode> TestGroupNode;
|
|
|
|
typedef Node<TestRunStats, TestGroupNode> TestRunNode;
|
|
|
|
|
|
|
|
CumulativeReporterBase( ReporterConfig const& _config )
|
|
|
|
: m_config( _config.fullConfig() ),
|
|
|
|
stream( _config.stream() )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = false;
|
|
|
|
}
|
2013-12-03 19:52:41 +01:00
|
|
|
~CumulativeReporterBase();
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual ReporterPreferences getPreferences() const CATCH_OVERRIDE {
|
|
|
|
return m_reporterPrefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void testRunStarting( TestRunInfo const& ) CATCH_OVERRIDE {}
|
|
|
|
virtual void testGroupStarting( GroupInfo const& ) CATCH_OVERRIDE {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseStarting( TestCaseInfo const& ) CATCH_OVERRIDE {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionStarting( SectionInfo const& sectionInfo ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
SectionStats incompleteStats( sectionInfo, Counts(), 0, false );
|
|
|
|
Ptr<SectionNode> node;
|
|
|
|
if( m_sectionStack.empty() ) {
|
|
|
|
if( !m_rootSection )
|
|
|
|
m_rootSection = new SectionNode( incompleteStats );
|
|
|
|
node = m_rootSection;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SectionNode& parentNode = *m_sectionStack.back();
|
|
|
|
SectionNode::ChildSections::const_iterator it =
|
2013-12-18 09:37:23 +01:00
|
|
|
std::find_if( parentNode.childSections.begin(),
|
|
|
|
parentNode.childSections.end(),
|
|
|
|
BySectionInfo( sectionInfo ) );
|
2013-12-03 19:52:41 +01:00
|
|
|
if( it == parentNode.childSections.end() ) {
|
|
|
|
node = new SectionNode( incompleteStats );
|
|
|
|
parentNode.childSections.push_back( node );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
node = *it;
|
|
|
|
}
|
|
|
|
m_sectionStack.push_back( node );
|
|
|
|
m_deepestSection = node;
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2016-03-29 18:03:09 +02:00
|
|
|
virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
assert( !m_sectionStack.empty() );
|
|
|
|
SectionNode& sectionNode = *m_sectionStack.back();
|
|
|
|
sectionNode.assertions.push_back( assertionStats );
|
2017-01-09 13:23:10 +01:00
|
|
|
// AssertionResult holds a pointer to a temporary DecomposedExpression,
|
|
|
|
// which getExpandedExpression() calls to build the expression string.
|
|
|
|
// Our section stack copy of the assertionResult will likely outlive the
|
|
|
|
// temporary, so it must be expanded or discarded now to avoid calling
|
|
|
|
// a destroyed object later.
|
|
|
|
prepareExpandedExpression( sectionNode.assertions.back().assertionResult );
|
2013-12-03 19:52:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionEnded( SectionStats const& sectionStats ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
assert( !m_sectionStack.empty() );
|
|
|
|
SectionNode& node = *m_sectionStack.back();
|
|
|
|
node.stats = sectionStats;
|
|
|
|
m_sectionStack.pop_back();
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& testCaseStats ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
Ptr<TestCaseNode> node = new TestCaseNode( testCaseStats );
|
|
|
|
assert( m_sectionStack.size() == 0 );
|
|
|
|
node->children.push_back( m_rootSection );
|
|
|
|
m_testCases.push_back( node );
|
|
|
|
m_rootSection.reset();
|
|
|
|
|
|
|
|
assert( m_deepestSection );
|
|
|
|
m_deepestSection->stdOut = testCaseStats.stdOut;
|
|
|
|
m_deepestSection->stdErr = testCaseStats.stdErr;
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testGroupEnded( TestGroupStats const& testGroupStats ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
Ptr<TestGroupNode> node = new TestGroupNode( testGroupStats );
|
|
|
|
node->children.swap( m_testCases );
|
|
|
|
m_testGroups.push_back( node );
|
|
|
|
}
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testRunEnded( TestRunStats const& testRunStats ) CATCH_OVERRIDE {
|
2013-12-03 19:52:41 +01:00
|
|
|
Ptr<TestRunNode> node = new TestRunNode( testRunStats );
|
|
|
|
node->children.swap( m_testGroups );
|
|
|
|
m_testRuns.push_back( node );
|
2013-12-07 10:08:01 +01:00
|
|
|
testRunEndedCumulative();
|
2013-12-03 19:52:41 +01:00
|
|
|
}
|
2013-12-07 10:08:01 +01:00
|
|
|
virtual void testRunEndedCumulative() = 0;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void skipTest( TestCaseInfo const& ) CATCH_OVERRIDE {}
|
2014-12-22 21:10:33 +01:00
|
|
|
|
2017-01-09 13:23:10 +01:00
|
|
|
virtual void prepareExpandedExpression( AssertionResult& result ) const {
|
|
|
|
if( result.isOk() )
|
|
|
|
result.discardDecomposedExpression();
|
|
|
|
else
|
|
|
|
result.expandDecomposedExpression();
|
|
|
|
}
|
|
|
|
|
2015-09-28 10:09:06 +02:00
|
|
|
Ptr<IConfig const> m_config;
|
2013-12-03 19:52:41 +01:00
|
|
|
std::ostream& stream;
|
|
|
|
std::vector<AssertionStats> m_assertions;
|
|
|
|
std::vector<std::vector<Ptr<SectionNode> > > m_sections;
|
|
|
|
std::vector<Ptr<TestCaseNode> > m_testCases;
|
|
|
|
std::vector<Ptr<TestGroupNode> > m_testGroups;
|
|
|
|
|
|
|
|
std::vector<Ptr<TestRunNode> > m_testRuns;
|
|
|
|
|
|
|
|
Ptr<SectionNode> m_rootSection;
|
|
|
|
Ptr<SectionNode> m_deepestSection;
|
|
|
|
std::vector<Ptr<SectionNode> > m_sectionStack;
|
2015-08-07 09:20:56 +02:00
|
|
|
ReporterPreferences m_reporterPrefs;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-12-20 01:46:02 +01:00
|
|
|
template<char C>
|
|
|
|
char const* getLineOfChars() {
|
|
|
|
static char line[CATCH_CONFIG_CONSOLE_WIDTH] = {0};
|
|
|
|
if( !*line ) {
|
|
|
|
memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 );
|
|
|
|
line[CATCH_CONFIG_CONSOLE_WIDTH-1] = 0;
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
struct TestEventListenerBase : StreamingReporterBase {
|
|
|
|
TestEventListenerBase( ReporterConfig const& _config )
|
|
|
|
: StreamingReporterBase( _config )
|
|
|
|
{}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE {}
|
2015-11-04 19:11:54 +01:00
|
|
|
virtual bool assertionEnded( AssertionStats const& ) CATCH_OVERRIDE {
|
2015-08-07 09:20:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
|