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
|
|
|
|
|
2018-09-03 10:03:47 +02:00
|
|
|
#include "../internal/catch_enforce.h"
|
2013-12-03 19:52:41 +01:00
|
|
|
#include "../internal/catch_interfaces_reporter.h"
|
|
|
|
|
2017-07-25 21:57:35 +02:00
|
|
|
#include <algorithm>
|
2014-12-22 08:42:25 +01:00
|
|
|
#include <cstring>
|
2017-03-02 16:16:17 +01:00
|
|
|
#include <cfloat>
|
|
|
|
#include <cstdio>
|
2018-04-18 14:59:10 +02:00
|
|
|
#include <cassert>
|
2017-04-26 18:04:00 +02:00
|
|
|
#include <memory>
|
2017-11-07 19:01:10 +01:00
|
|
|
#include <ostream>
|
2014-12-22 08:42:25 +01:00
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
namespace Catch {
|
2017-07-25 17:16:28 +02:00
|
|
|
void prepareExpandedExpression(AssertionResult& result);
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-10 11:33:18 +02:00
|
|
|
// Returns double formatted as %.3f (format expected on output)
|
|
|
|
std::string getFormattedDuration( double duration );
|
2017-03-02 16:16:17 +01:00
|
|
|
|
2020-07-06 20:33:08 +02:00
|
|
|
//! Should the reporter show
|
|
|
|
bool shouldShowDuration( IConfig const& config, double duration );
|
2020-04-13 14:34:27 +02:00
|
|
|
|
2019-04-01 21:33:57 +02:00
|
|
|
std::string serializeFilters( std::vector<std::string> const& container );
|
|
|
|
|
2017-07-09 11:46:53 +02:00
|
|
|
template<typename DerivedT>
|
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-11-07 19:01:10 +01:00
|
|
|
if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) )
|
2018-09-03 10:03:47 +02:00
|
|
|
CATCH_ERROR( "Verbosity level not supported by this reporter" );
|
2015-08-07 09:20:56 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
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-09 11:46:53 +02:00
|
|
|
static std::set<Verbosity> getSupportedVerbosities() {
|
|
|
|
return { Verbosity::Normal };
|
2017-07-06 00:25:49 +02:00
|
|
|
}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
~StreamingReporterBase() override = default;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void noMatchingTestCases(std::string const&) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2019-10-19 15:50:46 +02:00
|
|
|
void reportInvalidArguments(std::string const&) override {}
|
2020-07-06 20:33:08 +02:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testRunStarting(TestRunInfo const& _testRunInfo) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestRunInfo = _testRunInfo;
|
|
|
|
}
|
2019-04-01 21:33:57 +02:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testGroupStarting(GroupInfo const& _groupInfo) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentGroupInfo = _groupInfo;
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testCaseStarting(TestCaseInfo const& _testInfo) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo = _testInfo;
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void sectionStarting(SectionInfo const& _sectionInfo) override {
|
2017-07-19 23:15:54 +02:00
|
|
|
m_sectionStack.push_back(_sectionInfo);
|
2013-12-03 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void sectionEnded(SectionStats const& /* _sectionStats */) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
m_sectionStack.pop_back();
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void testCaseEnded(TestCaseStats const& /* _testCaseStats */) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo.reset();
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void testGroupEnded(TestGroupStats const& /* _testGroupStats */) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentGroupInfo.reset();
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void testRunEnded(TestRunStats const& /* _testRunStats */) override {
|
2013-12-03 19:52:41 +01:00
|
|
|
currentTestCaseInfo.reset();
|
|
|
|
currentGroupInfo.reset();
|
|
|
|
currentTestRunInfo.reset();
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
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-07-09 11:46:53 +02:00
|
|
|
template<typename DerivedT>
|
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 {
|
2017-07-19 23:15:54 +02:00
|
|
|
explicit SectionNode(SectionStats const& _stats) : stats(_stats) {}
|
|
|
|
virtual ~SectionNode() = default;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-19 23:15:54 +02:00
|
|
|
bool operator == (SectionNode const& other) const {
|
2013-12-03 19:52:41 +01:00
|
|
|
return stats.sectionInfo.lineInfo == other.stats.sectionInfo.lineInfo;
|
|
|
|
}
|
2017-07-19 23:15:54 +02:00
|
|
|
bool operator == (std::shared_ptr<SectionNode> const& other) const {
|
|
|
|
return operator==(*other);
|
2013-12-03 19:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-07-23 17:13:44 +02:00
|
|
|
BySectionInfo( SectionInfo const& other ) : m_other( other ) {}
|
|
|
|
BySectionInfo( BySectionInfo const& other ) : m_other( other.m_other ) {}
|
2017-07-19 23:15:54 +02:00
|
|
|
bool operator() (std::shared_ptr<SectionNode> const& node) const {
|
2017-07-23 17:13:44 +02:00
|
|
|
return ((node->stats.sectionInfo.name == m_other.name) &&
|
|
|
|
(node->stats.sectionInfo.lineInfo == m_other.lineInfo));
|
2013-12-18 09:37:23 +01:00
|
|
|
}
|
2017-07-10 11:33:18 +02:00
|
|
|
void operator=(BySectionInfo const&) = delete;
|
|
|
|
|
2013-12-18 09:37:23 +01:00
|
|
|
private:
|
|
|
|
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-11-07 19:01:10 +01:00
|
|
|
if( !DerivedT::getSupportedVerbosities().count( m_config->verbosity() ) )
|
2018-09-03 10:03:47 +02:00
|
|
|
CATCH_ERROR( "Verbosity level not supported by this reporter" );
|
2015-08-07 09:20:56 +02:00
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
~CumulativeReporterBase() override = default;
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
ReporterPreferences getPreferences() const override {
|
2015-08-07 09:20:56 +02:00
|
|
|
return m_reporterPrefs;
|
|
|
|
}
|
2017-07-10 11:33:18 +02:00
|
|
|
|
2017-07-09 11:46:53 +02:00
|
|
|
static std::set<Verbosity> getSupportedVerbosities() {
|
|
|
|
return { Verbosity::Normal };
|
2017-07-06 00:25:49 +02:00
|
|
|
}
|
2017-07-10 11:33:18 +02:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testRunStarting( TestRunInfo const& ) override {}
|
|
|
|
void testGroupStarting( GroupInfo const& ) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void testCaseStarting( TestCaseInfo const& ) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
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();
|
2017-08-29 14:02:14 +02:00
|
|
|
auto 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 );
|
2017-07-25 15:45:50 +02:00
|
|
|
m_deepestSection = std::move(node);
|
2013-12-03 19:52:41 +01:00
|
|
|
}
|
2017-07-10 11:33:18 +02:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void assertionStarting(AssertionInfo const&) override {}
|
2013-12-03 19:52:41 +01:00
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
bool assertionEnded(AssertionStats const& assertionStats) override {
|
2017-07-19 23:15:54 +02:00
|
|
|
assert(!m_sectionStack.empty());
|
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.
|
2017-08-08 18:53:01 +02:00
|
|
|
prepareExpandedExpression(const_cast<AssertionResult&>( assertionStats.assertionResult ) );
|
|
|
|
SectionNode& sectionNode = *m_sectionStack.back();
|
|
|
|
sectionNode.assertions.push_back(assertionStats);
|
2013-12-03 19:52:41 +01:00
|
|
|
return true;
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void sectionEnded(SectionStats const& sectionStats) override {
|
2017-07-19 23:15:54 +02:00
|
|
|
assert(!m_sectionStack.empty());
|
2013-12-03 19:52:41 +01:00
|
|
|
SectionNode& node = *m_sectionStack.back();
|
|
|
|
node.stats = sectionStats;
|
|
|
|
m_sectionStack.pop_back();
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void testCaseEnded(TestCaseStats const& testCaseStats) override {
|
2017-07-19 23:15:54 +02:00
|
|
|
auto node = std::make_shared<TestCaseNode>(testCaseStats);
|
|
|
|
assert(m_sectionStack.size() == 0);
|
|
|
|
node->children.push_back(m_rootSection);
|
|
|
|
m_testCases.push_back(node);
|
2013-12-03 19:52:41 +01:00
|
|
|
m_rootSection.reset();
|
2018-11-05 20:01:25 +01:00
|
|
|
|
2017-07-19 23:15:54 +02:00
|
|
|
assert(m_deepestSection);
|
2013-12-03 19:52:41 +01:00
|
|
|
m_deepestSection->stdOut = testCaseStats.stdOut;
|
|
|
|
m_deepestSection->stdErr = testCaseStats.stdErr;
|
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void testGroupEnded(TestGroupStats const& testGroupStats) override {
|
2017-07-19 23:15:54 +02:00
|
|
|
auto node = std::make_shared<TestGroupNode>(testGroupStats);
|
|
|
|
node->children.swap(m_testCases);
|
|
|
|
m_testGroups.push_back(node);
|
2013-12-03 19:52:41 +01:00
|
|
|
}
|
2017-07-20 16:57:17 +02:00
|
|
|
void testRunEnded(TestRunStats const& testRunStats) override {
|
2017-07-19 23:15:54 +02:00
|
|
|
auto node = std::make_shared<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
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void skipTest(TestCaseInfo const&) override {}
|
2014-12-22 21:10:33 +01:00
|
|
|
|
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() {
|
2021-07-26 13:14:42 +02:00
|
|
|
static char line[CATCH_CONFIG_CONSOLE_MAXIMUM_WIDTH] = {0};
|
2014-12-20 01:46:02 +01:00
|
|
|
if( !*line ) {
|
2021-07-26 13:15:15 +02:00
|
|
|
auto consoleWidth = static_cast<size_t> (CATCH_CONFIG_CONSOLE_WIDTH);
|
|
|
|
std::memset( line, C, consoleWidth-1 );
|
|
|
|
line[consoleWidth-1] = 0;
|
2014-12-20 01:46:02 +01:00
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-07-09 11:46:53 +02:00
|
|
|
struct TestEventListenerBase : StreamingReporterBase<TestEventListenerBase> {
|
2017-07-19 23:15:54 +02:00
|
|
|
TestEventListenerBase( ReporterConfig const& _config );
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2018-11-05 20:01:25 +01:00
|
|
|
static std::set<Verbosity> getSupportedVerbosities();
|
|
|
|
|
2017-07-20 16:57:17 +02:00
|
|
|
void assertionStarting(AssertionInfo const&) override;
|
|
|
|
bool assertionEnded(AssertionStats const&) override;
|
2015-08-07 09:20:56 +02:00
|
|
|
};
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2019-10-19 15:50:46 +02:00
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
|