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"
|
2017-03-06 21:51:22 +01:00
|
|
|
#include "../internal/catch_errno_guard.hpp"
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2014-12-22 08:42:25 +01:00
|
|
|
#include <cstring>
|
2017-03-02 16:16:17 +01:00
|
|
|
#include <cfloat>
|
|
|
|
#include <cstdio>
|
2017-02-22 13:31:51 +01:00
|
|
|
#include <assert.h>
|
2017-04-26 18:04:00 +02:00
|
|
|
#include <memory>
|
2014-12-22 08:42:25 +01:00
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
namespace Catch {
|
|
|
|
|
2017-03-02 16:16:17 +01:00
|
|
|
namespace {
|
|
|
|
// Because formatting using c++ streams is stateful, drop down to C is required
|
|
|
|
// Alternatively we could use stringstream, but its performance is... not good.
|
|
|
|
std::string getFormattedDuration( double duration ) {
|
|
|
|
// Max exponent + 1 is required to represent the whole part
|
|
|
|
// + 1 for decimal point
|
|
|
|
// + 3 for the 3 decimal places
|
|
|
|
// + 1 for null terminator
|
|
|
|
const size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1;
|
|
|
|
char buffer[maxDoubleSize];
|
2017-05-27 14:42:54 +02:00
|
|
|
|
2017-03-06 21:51:22 +01:00
|
|
|
// Save previous errno, to prevent sprintf from overwriting it
|
|
|
|
ErrnoGuard guard;
|
2017-03-02 16:16:17 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
sprintf_s(buffer, "%.3f", duration);
|
|
|
|
#else
|
|
|
|
sprintf(buffer, "%.3f", duration);
|
|
|
|
#endif
|
|
|
|
return std::string(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-25 22:51:44 +02:00
|
|
|
struct StreamingReporterBase : IStreamingReporter {
|
2013-12-03 19:52:41 +01:00
|
|
|
|
|
|
|
StreamingReporterBase( ReporterConfig const& _config )
|
|
|
|
: m_config( _config.fullConfig() ),
|
|
|
|
stream( _config.stream() )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = false;
|
2017-07-06 00:25:49 +02:00
|
|
|
CATCH_ENFORCE( getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" );
|
2015-08-07 09:20:56 +02:00
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual ReporterPreferences getPreferences() const override {
|
2015-08-07 09:20:56 +02:00
|
|
|
return m_reporterPrefs;
|
|
|
|
}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-06 00:25:49 +02:00
|
|
|
virtual std::set<Verbosity> const& getSupportedVerbosities() const override {
|
|
|
|
static std::set<Verbosity> supported{ Verbosity::Normal };
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual ~StreamingReporterBase() override;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void noMatchingTestCases( std::string const& ) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testRunStarting( TestRunInfo const& _testRunInfo ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestRunInfo = _testRunInfo;
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testGroupStarting( GroupInfo const& _groupInfo ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentGroupInfo = _groupInfo;
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testCaseStarting( TestCaseInfo const& _testInfo ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo = _testInfo;
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void sectionStarting( SectionInfo const& _sectionInfo ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
m_sectionStack.push_back( _sectionInfo );
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void sectionEnded( SectionStats const& /* _sectionStats */ ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
m_sectionStack.pop_back();
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& /* _testCaseStats */ ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo.reset();
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testGroupEnded( TestGroupStats const& /* _testGroupStats */ ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentGroupInfo.reset();
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testRunEnded( TestRunStats const& /* _testRunStats */ ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo.reset();
|
|
|
|
currentGroupInfo.reset();
|
|
|
|
currentTestRunInfo.reset();
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void skipTest( TestCaseInfo const& ) 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.
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:18:02 +02:00
|
|
|
IConfigPtr 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
|
|
|
};
|
|
|
|
|
2017-04-25 22:51:44 +02:00
|
|
|
struct CumulativeReporterBase : IStreamingReporter {
|
2013-12-03 19:52:41 +01:00
|
|
|
template<typename T, typename ChildNodeT>
|
2017-04-25 22:08:41 +02:00
|
|
|
struct Node {
|
2013-12-03 19:52:41 +01:00
|
|
|
explicit Node( T const& _value ) : value( _value ) {}
|
|
|
|
virtual ~Node() {}
|
|
|
|
|
2017-04-25 22:08:41 +02:00
|
|
|
using ChildNodes = std::vector<std::shared_ptr<ChildNodeT>>;
|
2013-12-03 19:52:41 +01:00
|
|
|
T value;
|
|
|
|
ChildNodes children;
|
|
|
|
};
|
2017-04-25 22:08:41 +02:00
|
|
|
struct SectionNode {
|
2013-12-03 19:52:41 +01:00
|
|
|
explicit SectionNode( SectionStats const& _stats ) : stats( _stats ) {}
|
|
|
|
virtual ~SectionNode();
|
|
|
|
|
|
|
|
bool operator == ( SectionNode const& other ) const {
|
|
|
|
return stats.sectionInfo.lineInfo == other.stats.sectionInfo.lineInfo;
|
|
|
|
}
|
2017-04-25 22:08:41 +02:00
|
|
|
bool operator == ( std::shared_ptr<SectionNode> const& other ) const {
|
2013-12-03 19:52:41 +01:00
|
|
|
return operator==( *other );
|
|
|
|
}
|
|
|
|
|
|
|
|
SectionStats stats;
|
2017-04-25 22:08:41 +02:00
|
|
|
using ChildSections = std::vector<std::shared_ptr<SectionNode>>;
|
|
|
|
using Assertions = std::vector<AssertionStats>;
|
2013-12-03 19:52:41 +01:00
|
|
|
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 ) {}
|
2017-01-26 23:13:12 +01:00
|
|
|
BySectionInfo( BySectionInfo const& other ) : m_other( other.m_other ) {}
|
2017-04-25 22:08:41 +02:00
|
|
|
bool operator() ( std::shared_ptr<SectionNode> const& node ) const {
|
2013-12-18 09:37:23 +01:00
|
|
|
return node->stats.sectionInfo.lineInfo == m_other.lineInfo;
|
|
|
|
}
|
|
|
|
private:
|
2017-01-26 23:13:12 +01: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
|
|
|
|
2017-04-25 22:08:41 +02:00
|
|
|
using TestCaseNode = Node<TestCaseStats, SectionNode>;
|
|
|
|
using TestGroupNode = Node<TestGroupStats, TestCaseNode>;
|
|
|
|
using TestRunNode = Node<TestRunStats, TestGroupNode>;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
|
|
|
CumulativeReporterBase( ReporterConfig const& _config )
|
|
|
|
: m_config( _config.fullConfig() ),
|
|
|
|
stream( _config.stream() )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = false;
|
2017-07-06 00:25:49 +02:00
|
|
|
CATCH_ENFORCE( getSupportedVerbosities().count( m_config->verbosity() ), "Verbosity level not supported by this reporter" );
|
2015-08-07 09:20:56 +02:00
|
|
|
}
|
2013-12-03 19:52:41 +01:00
|
|
|
~CumulativeReporterBase();
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual ReporterPreferences getPreferences() const override {
|
2015-08-07 09:20:56 +02:00
|
|
|
return m_reporterPrefs;
|
|
|
|
}
|
|
|
|
|
2017-07-06 00:25:49 +02:00
|
|
|
virtual std::set<Verbosity> const& getSupportedVerbosities() const override {
|
|
|
|
static std::set<Verbosity> supported{ Verbosity::Normal };
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testRunStarting( TestRunInfo const& ) override {}
|
|
|
|
virtual void testGroupStarting( GroupInfo const& ) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testCaseStarting( TestCaseInfo const& ) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void sectionStarting( SectionInfo const& sectionInfo ) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
SectionStats incompleteStats( sectionInfo, Counts(), 0, false );
|
2017-04-25 22:08:41 +02:00
|
|
|
std::shared_ptr<SectionNode> node;
|
2013-12-03 19:52:41 +01:00
|
|
|
if( m_sectionStack.empty() ) {
|
|
|
|
if( !m_rootSection )
|
2017-04-25 22:08:41 +02:00
|
|
|
m_rootSection = std::make_shared<SectionNode>( incompleteStats );
|
2013-12-03 19:52:41 +01:00
|
|
|
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() ) {
|
2017-04-25 22:08:41 +02:00
|
|
|
node = std::make_shared<SectionNode>( incompleteStats );
|
2013-12-03 19:52:41 +01:00
|
|
|
parentNode.childSections.push_back( node );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
node = *it;
|
|
|
|
}
|
|
|
|
m_sectionStack.push_back( node );
|
|
|
|
m_deepestSection = node;
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual bool assertionEnded( AssertionStats const& assertionStats ) 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;
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void sectionEnded( SectionStats const& sectionStats ) 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();
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& testCaseStats ) override {
|
2017-04-25 22:08:41 +02:00
|
|
|
auto node = std::make_shared<TestCaseNode>( testCaseStats );
|
2013-12-03 19:52:41 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testGroupEnded( TestGroupStats const& testGroupStats ) override {
|
2017-04-25 22:08:41 +02:00
|
|
|
auto node = std::make_shared<TestGroupNode>( testGroupStats );
|
2013-12-03 19:52:41 +01:00
|
|
|
node->children.swap( m_testCases );
|
|
|
|
m_testGroups.push_back( node );
|
|
|
|
}
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void testRunEnded( TestRunStats const& testRunStats ) override {
|
2017-04-25 22:08:41 +02:00
|
|
|
auto node = std::make_shared<TestRunNode>( testRunStats );
|
2013-12-03 19:52:41 +01:00
|
|
|
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
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void skipTest( TestCaseInfo const& ) 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();
|
|
|
|
}
|
|
|
|
|
2017-04-25 21:18:02 +02:00
|
|
|
IConfigPtr m_config;
|
2013-12-03 19:52:41 +01:00
|
|
|
std::ostream& stream;
|
|
|
|
std::vector<AssertionStats> m_assertions;
|
2017-04-25 22:08:41 +02:00
|
|
|
std::vector<std::vector<std::shared_ptr<SectionNode>>> m_sections;
|
|
|
|
std::vector<std::shared_ptr<TestCaseNode>> m_testCases;
|
|
|
|
std::vector<std::shared_ptr<TestGroupNode>> m_testGroups;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-04-25 22:08:41 +02:00
|
|
|
std::vector<std::shared_ptr<TestRunNode>> m_testRuns;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-04-25 22:08:41 +02:00
|
|
|
std::shared_ptr<SectionNode> m_rootSection;
|
|
|
|
std::shared_ptr<SectionNode> m_deepestSection;
|
|
|
|
std::vector<std::shared_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 ) {
|
2017-01-26 19:11:20 +01:00
|
|
|
std::memset( line, C, CATCH_CONFIG_CONSOLE_WIDTH-1 );
|
2014-12-20 01:46:02 +01:00
|
|
|
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
|
|
|
|
2017-04-25 12:41:30 +02:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) override {}
|
|
|
|
virtual bool assertionEnded( AssertionStats const& ) 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
|