WIP: devirtualize RunContext

This commit is contained in:
Martin Hořeňovský 2023-03-16 23:49:24 +01:00
parent f3960c02ce
commit 5db31e587e
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
23 changed files with 236 additions and 263 deletions

View File

@ -55,6 +55,7 @@ set(IMPL_HEADERS
${SOURCES_DIR}/catch_template_test_macros.hpp
${SOURCES_DIR}/catch_test_case_info.hpp
${SOURCES_DIR}/catch_test_macros.hpp
${SOURCES_DIR}/catch_test_run_info.hpp
${SOURCES_DIR}/catch_test_spec.hpp
${SOURCES_DIR}/catch_timer.hpp
${SOURCES_DIR}/catch_tostring.hpp
@ -63,6 +64,7 @@ set(IMPL_HEADERS
${SOURCES_DIR}/catch_version.hpp
${SOURCES_DIR}/catch_version_macros.hpp
${SOURCES_DIR}/internal/catch_assertion_handler.hpp
${SOURCES_DIR}/internal/catch_benchmark_stats_fwd.hpp
${SOURCES_DIR}/internal/catch_case_insensitive_comparisons.hpp
${SOURCES_DIR}/internal/catch_case_sensitive.hpp
${SOURCES_DIR}/internal/catch_clara.hpp
@ -210,7 +212,6 @@ set(INTERNAL_FILES ${IMPL_SOURCES} ${IMPL_HEADERS})
set(INTERFACE_HEADERS
${SOURCES_DIR}/interfaces/catch_interfaces_all.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_capture.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_config.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_exception.hpp
${SOURCES_DIR}/interfaces/catch_interfaces_generatortracker.hpp
@ -219,7 +220,6 @@ set(INTERFACE_HEADERS
${SOURCES_DIR}/interfaces/catch_interfaces_testcase.hpp
)
set(INTERFACE_SOURCES
${SOURCES_DIR}/interfaces/catch_interfaces_capture.cpp
${SOURCES_DIR}/interfaces/catch_interfaces_config.cpp
${SOURCES_DIR}/interfaces/catch_interfaces_exception.cpp
${SOURCES_DIR}/interfaces/catch_interfaces_generatortracker.cpp

View File

@ -11,9 +11,10 @@
#define CATCH_BENCHMARK_HPP_INCLUDED
#include <catch2/interfaces/catch_interfaces_config.hpp>
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/internal/catch_context.hpp>
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_run_context.hpp>
#include <catch2/internal/catch_registry_hub.hpp>
#include <catch2/internal/catch_unique_name.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>

View File

@ -12,7 +12,6 @@
#include <catch2/internal/catch_test_failure_exception.hpp>
#include <catch2/internal/catch_meta.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <type_traits>

View File

@ -36,6 +36,7 @@
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/catch_test_run_info.hpp>
#include <catch2/catch_test_spec.hpp>
#include <catch2/catch_timer.hpp>
#include <catch2/catch_tostring.hpp>
@ -46,6 +47,7 @@
#include <catch2/generators/catch_generators_all.hpp>
#include <catch2/interfaces/catch_interfaces_all.hpp>
#include <catch2/internal/catch_assertion_handler.hpp>
#include <catch2/internal/catch_benchmark_stats_fwd.hpp>
#include <catch2/internal/catch_case_insensitive_comparisons.hpp>
#include <catch2/internal/catch_case_sensitive.hpp>
#include <catch2/internal/catch_clara.hpp>

View File

@ -6,10 +6,10 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/catch_message.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_uncaught_exceptions.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/internal/catch_run_context.hpp>
#include <cassert>
#include <stack>

View File

@ -12,7 +12,7 @@
#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <catch2/internal/catch_stream_end_stop.hpp>
#include <catch2/internal/catch_message_info.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_run_context.hpp>
#include <catch2/catch_tostring.hpp>
#include <string>
@ -61,7 +61,7 @@ namespace Catch {
class Capturer {
std::vector<MessageInfo> m_messages;
IResultCapture& m_resultCapture = getResultCapture();
RunContext& m_resultCapture = getResultCapture();
size_t m_captured = 0;
public:
Capturer( StringRef macroName, SourceLineInfo const& lineInfo, ResultWas::OfType resultType, StringRef names );

View File

@ -25,7 +25,8 @@
#include <catch2/internal/catch_stdstreams.hpp>
#include <catch2/internal/catch_istream.hpp>
#include <catch2/internal/catch_test_case_registry_impl.hpp>
#include <catch2/internal/catch_registry_hub.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <algorithm>
#include <cassert>

View File

@ -0,0 +1,23 @@
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_TEST_RUN_INFO_HPP_INCLUDED
#define CATCH_TEST_RUN_INFO_HPP_INCLUDED
#include <catch2/internal/catch_stringref.hpp>
namespace Catch {
struct TestRunInfo {
constexpr TestRunInfo(StringRef _name) : name(_name) {}
StringRef name;
};
} // end namespace Catch
#endif // CATCH_TEST_RUN_INFO_HPP_INCLUDED

View File

@ -9,7 +9,7 @@
#include <catch2/generators/catch_generators.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/generators/catch_generator_exception.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_run_context.hpp>
namespace Catch {

View File

@ -22,7 +22,6 @@
#ifndef CATCH_INTERFACES_ALL_HPP_INCLUDED
#define CATCH_INTERFACES_ALL_HPP_INCLUDED
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
#include <catch2/interfaces/catch_interfaces_exception.hpp>
#include <catch2/interfaces/catch_interfaces_generatortracker.hpp>

View File

@ -1,13 +0,0 @@
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#include <catch2/interfaces/catch_interfaces_capture.hpp>
namespace Catch {
IResultCapture::~IResultCapture() = default;
}

View File

@ -1,110 +0,0 @@
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_INTERFACES_CAPTURE_HPP_INCLUDED
#define CATCH_INTERFACES_CAPTURE_HPP_INCLUDED
#include <string>
#include <chrono>
#include <catch2/internal/catch_stringref.hpp>
#include <catch2/internal/catch_result_type.hpp>
#include <catch2/internal/catch_unique_ptr.hpp>
namespace Catch {
class AssertionResult;
struct AssertionInfo;
struct SectionInfo;
struct SectionEndInfo;
struct MessageInfo;
struct MessageBuilder;
struct Counts;
struct AssertionReaction;
struct SourceLineInfo;
class ITransientExpression;
class IGeneratorTracker;
struct BenchmarkInfo;
template <typename Duration = std::chrono::duration<double, std::nano>>
struct BenchmarkStats;
namespace Generators {
class GeneratorUntypedBase;
using GeneratorBasePtr = Catch::Detail::unique_ptr<GeneratorUntypedBase>;
}
class IResultCapture {
public:
virtual ~IResultCapture();
virtual bool sectionStarted( StringRef sectionName,
SourceLineInfo const& sectionLineInfo,
Counts& assertions ) = 0;
virtual void sectionEnded( SectionEndInfo&& endInfo ) = 0;
virtual void sectionEndedEarly( SectionEndInfo&& endInfo ) = 0;
virtual IGeneratorTracker*
acquireGeneratorTracker( StringRef generatorName,
SourceLineInfo const& lineInfo ) = 0;
virtual IGeneratorTracker*
createGeneratorTracker( StringRef generatorName,
SourceLineInfo lineInfo,
Generators::GeneratorBasePtr&& generator ) = 0;
virtual void benchmarkPreparing( StringRef name ) = 0;
virtual void benchmarkStarting( BenchmarkInfo const& info ) = 0;
virtual void benchmarkEnded( BenchmarkStats<> const& stats ) = 0;
virtual void benchmarkFailed( StringRef error ) = 0;
virtual void pushScopedMessage( MessageInfo const& message ) = 0;
virtual void popScopedMessage( MessageInfo const& message ) = 0;
virtual void emplaceUnscopedMessage( MessageBuilder&& builder ) = 0;
virtual void handleFatalErrorCondition( StringRef message ) = 0;
virtual void handleExpr
( AssertionInfo const& info,
ITransientExpression const& expr,
AssertionReaction& reaction ) = 0;
virtual void handleMessage
( AssertionInfo const& info,
ResultWas::OfType resultType,
StringRef message,
AssertionReaction& reaction ) = 0;
virtual void handleUnexpectedExceptionNotThrown
( AssertionInfo const& info,
AssertionReaction& reaction ) = 0;
virtual void handleUnexpectedInflightException
( AssertionInfo const& info,
std::string&& message,
AssertionReaction& reaction ) = 0;
virtual void handleIncomplete
( AssertionInfo const& info ) = 0;
virtual void handleNonExpr
( AssertionInfo const &info,
ResultWas::OfType resultType,
AssertionReaction &reaction ) = 0;
virtual bool lastAssertionPassed() = 0;
virtual void assertionPassed() = 0;
// Deprecated, do not use:
virtual std::string getCurrentTestName() const = 0;
virtual const AssertionResult* getLastResult() const = 0;
virtual void exceptionEarlyReported() = 0;
};
IResultCapture& getResultCapture();
}
#endif // CATCH_INTERFACES_CAPTURE_HPP_INCLUDED

View File

@ -9,6 +9,7 @@
#define CATCH_INTERFACES_REPORTER_HPP_INCLUDED
#include <catch2/catch_section_info.hpp>
#include <catch2/catch_test_run_info.hpp>
#include <catch2/catch_totals.hpp>
#include <catch2/catch_assertion_result.hpp>
#include <catch2/internal/catch_message_info.hpp>
@ -17,15 +18,18 @@
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/benchmark/catch_estimate.hpp>
#include <catch2/benchmark/catch_outlier_classification.hpp>
#include <catch2/internal/catch_benchmark_stats_fwd.hpp>
#include <map>
#include <string>
#include <vector>
#include <iosfwd>
#include <chrono>
namespace Catch {
struct BenchmarkInfo;
struct ReporterDescription;
struct ListenerDescription;
struct TagInfo;
@ -57,11 +61,6 @@ namespace Catch {
std::map<std::string, std::string> m_customOptions;
};
struct TestRunInfo {
constexpr TestRunInfo(StringRef _name) : name(_name) {}
StringRef name;
};
struct AssertionStats {
AssertionStats( AssertionResult const& _assertionResult,
std::vector<MessageInfo> const& _infoMessages,

View File

@ -10,14 +10,14 @@
#include <catch2/catch_assertion_info.hpp>
#include <catch2/internal/catch_decomposer.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_run_context.hpp>
#include <catch2/internal/catch_lazy_expr.hpp>
#include <string>
namespace Catch {
class IResultCapture;
class RunContext;
struct AssertionReaction {
bool shouldDebugBreak = false;
@ -29,7 +29,7 @@ namespace Catch {
AssertionInfo m_assertionInfo;
AssertionReaction m_reaction;
bool m_completed = false;
IResultCapture& m_resultCapture;
RunContext& m_resultCapture;
public:
AssertionHandler

View File

@ -0,0 +1,23 @@
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_BENCHMARK_STATS_FWD_HPP_INCLUDED
#define CATCH_BENCHMARK_STATS_FWD_HPP_INCLUDED
#include <chrono>
namespace Catch {
// We cannot forward declare the type with default template argument
// multiple times, so it is split out into a separate header so that
// we can prevent multiple declarations in dependees
template <typename Duration = std::chrono::duration<double, std::nano>>
struct BenchmarkStats;
} // end namespace Catch
#endif // CATCH_BENCHMARK_STATS_FWD_HPP_INCLUDED

View File

@ -27,7 +27,7 @@ namespace Catch {
return *Context::currentContext;
}
void Context::setResultCapture( IResultCapture* resultCapture ) {
void Context::setResultCapture( RunContext* resultCapture ) {
m_resultCapture = resultCapture;
}

View File

@ -12,12 +12,12 @@
namespace Catch {
class IResultCapture;
class RunContext;
class IConfig;
class Context {
IConfig const* m_config = nullptr;
IResultCapture* m_resultCapture = nullptr;
RunContext* m_resultCapture = nullptr;
CATCH_EXPORT static Context* currentContext;
friend Context& getCurrentMutableContext();
@ -26,9 +26,9 @@ namespace Catch {
friend void cleanUpContext();
public:
IResultCapture* getResultCapture() const { return m_resultCapture; }
RunContext* getResultCapture() const { return m_resultCapture; }
IConfig const* getConfig() const { return m_config; }
void setResultCapture( IResultCapture* resultCapture );
void setResultCapture( RunContext* resultCapture );
void setConfig( IConfig const* config );
};

View File

@ -28,7 +28,7 @@
#include <catch2/internal/catch_context.hpp>
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_run_context.hpp>
#include <catch2/internal/catch_windows_h_proxy.hpp>
#include <catch2/internal/catch_stdstreams.hpp>

View File

@ -10,7 +10,7 @@
#include <catch2/internal/catch_result_type.hpp>
#include <catch2/internal/catch_source_line_info.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_stringref.hpp>
#include <string>

View File

@ -7,7 +7,11 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/internal/catch_run_context.hpp>
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/catch_assertion_result.hpp>
#include <catch2/catch_user_config.hpp>
#include <catch2/catch_timer.hpp>
#include <catch2/interfaces/catch_interfaces_generatortracker.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
@ -15,10 +19,14 @@
#include <catch2/internal/catch_enforce.hpp>
#include <catch2/internal/catch_fatal_condition_handler.hpp>
#include <catch2/internal/catch_random_number_generator.hpp>
#include <catch2/catch_timer.hpp>
#include <catch2/internal/catch_output_redirect.hpp>
#include <catch2/internal/catch_assertion_handler.hpp>
#include <catch2/internal/catch_test_failure_exception.hpp>
#include <catch2/internal/catch_optional.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/internal/catch_fatal_condition_handler.hpp>
#include <catch2/internal/catch_test_case_tracker.hpp>
#include <catch2/catch_message.hpp>
#include <cassert>
#include <algorithm>
@ -164,30 +172,44 @@ namespace Catch {
} // namespace
}
RunContext::RunContext(IConfig const* _config, IEventListenerPtr&& reporter)
: m_runInfo(_config->name()),
struct RunContext::RunContextImpl {
Optional<AssertionResult> lastResult;
FatalConditionHandler fatalConditionhandler;
TrackerContext trackerContext;
std::vector<MessageInfo> messages;
// Fake owners for unscoped messages
std::vector<ScopedMessage> messageScopes;
IEventListenerPtr reporter;
std::vector<SectionEndInfo> unfinishedSections;
std::vector<ITracker*> activeSections;
};
RunContext::RunContext(IConfig const* _config, IEventListenerPtr&& reporter):
m_impl( Detail::make_unique<RunContextImpl>() ),
m_runInfo(_config->name()),
m_config(_config),
m_reporter(CATCH_MOVE(reporter)),
m_lastAssertionInfo{ StringRef(), SourceLineInfo("",0), StringRef(), ResultDisposition::Normal },
m_includeSuccessfulResults( m_config->includeSuccessfulResults() || m_reporter->getPreferences().shouldReportAllAssertions )
m_includeSuccessfulResults( m_config->includeSuccessfulResults() || reporter->getPreferences().shouldReportAllAssertions )
{
getCurrentMutableContext().setResultCapture( this );
m_reporter->testRunStarting(m_runInfo);
getCurrentMutableContext().setResultCapture(this);
m_impl->reporter = CATCH_MOVE( reporter );
m_impl->reporter->testRunStarting(m_runInfo);
}
RunContext::~RunContext() {
m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, aborting()));
m_impl->reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, aborting()));
}
Totals RunContext::runTest(TestCaseHandle const& testCase) {
const Totals prevTotals = m_totals;
auto const& testInfo = testCase.getTestCaseInfo();
m_reporter->testCaseStarting(testInfo);
m_impl->reporter->testCaseStarting( testInfo );
m_activeTestCase = &testCase;
ITracker& rootTracker = m_trackerContext.startRun();
ITracker& rootTracker = m_impl->trackerContext.startRun();
assert(rootTracker.isSectionTracker());
static_cast<SectionTracker&>(rootTracker).addInitialFilters(m_config->getSectionsToRun());
@ -228,10 +250,10 @@ namespace Catch {
std::string redirectedCout;
std::string redirectedCerr;
do {
m_trackerContext.startCycle();
m_testCaseTracker = &SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocationRef(testInfo.name, testInfo.lineInfo));
m_impl->trackerContext.startCycle();
m_testCaseTracker = &SectionTracker::acquire(m_impl->trackerContext, TestCaseTracking::NameAndLocationRef(testInfo.name, testInfo.lineInfo));
m_reporter->testCasePartialStarting(testInfo, testRuns);
m_impl->reporter->testCasePartialStarting( testInfo, testRuns );
const auto beforeRunTotals = m_totals;
std::string oneRunCout, oneRunCerr;
@ -242,7 +264,7 @@ namespace Catch {
const auto singleRunTotals = m_totals.delta(beforeRunTotals);
auto statsForOneRun = TestCaseStats(testInfo, singleRunTotals, CATCH_MOVE(oneRunCout), CATCH_MOVE(oneRunCerr), aborting());
m_reporter->testCasePartialEnded(statsForOneRun, testRuns);
m_impl->reporter->testCasePartialEnded( statsForOneRun, testRuns );
++testRuns;
} while (!m_testCaseTracker->isSuccessfullyCompleted() && !aborting());
@ -253,11 +275,12 @@ namespace Catch {
deltaTotals.testCases.failed++;
}
m_totals.testCases += deltaTotals.testCases;
m_reporter->testCaseEnded(TestCaseStats(testInfo,
deltaTotals,
CATCH_MOVE(redirectedCout),
CATCH_MOVE(redirectedCerr),
aborting()));
m_impl->reporter->testCaseEnded(
TestCaseStats( testInfo,
deltaTotals,
CATCH_MOVE( redirectedCout ),
CATCH_MOVE( redirectedCerr ),
aborting() ) );
m_activeTestCase = nullptr;
m_testCaseTracker = nullptr;
@ -286,14 +309,15 @@ namespace Catch {
m_lastAssertionPassed = true;
}
m_reporter->assertionEnded(AssertionStats(result, m_messages, m_totals));
m_impl->reporter->assertionEnded(AssertionStats(result, m_impl->messages, m_totals));
if (result.getResultType() != ResultWas::Warning)
m_messageScopes.clear();
if ( result.getResultType() != ResultWas::Warning ) {
m_impl->messageScopes.clear();
}
// Reset working state
resetAssertionInfo();
m_lastResult = result;
m_impl->lastResult = result;
}
void RunContext::resetAssertionInfo() {
m_lastAssertionInfo.macroName = StringRef();
@ -302,18 +326,18 @@ namespace Catch {
bool RunContext::sectionStarted(StringRef sectionName, SourceLineInfo const& sectionLineInfo, Counts & assertions) {
ITracker& sectionTracker =
SectionTracker::acquire( m_trackerContext,
SectionTracker::acquire( m_impl->trackerContext,
TestCaseTracking::NameAndLocationRef(
sectionName, sectionLineInfo ) );
if (!sectionTracker.isOpen())
return false;
m_activeSections.push_back(&sectionTracker);
m_impl->activeSections.push_back(&sectionTracker);
SectionInfo sectionInfo( sectionLineInfo, static_cast<std::string>(sectionName) );
m_lastAssertionInfo.lineInfo = sectionInfo.lineInfo;
m_reporter->sectionStarting(sectionInfo);
m_impl->reporter->sectionStarting( sectionInfo );
assertions = m_totals.assertions;
@ -324,7 +348,7 @@ namespace Catch {
SourceLineInfo const& lineInfo ) {
using namespace Generators;
GeneratorTracker* tracker = GeneratorTracker::acquire(
m_trackerContext,
m_impl->trackerContext,
TestCaseTracking::NameAndLocationRef(
generatorName, lineInfo ) );
m_lastAssertionInfo.lineInfo = lineInfo;
@ -337,13 +361,13 @@ namespace Catch {
Generators::GeneratorBasePtr&& generator ) {
auto nameAndLoc = TestCaseTracking::NameAndLocation( static_cast<std::string>( generatorName ), lineInfo );
auto& currentTracker = m_trackerContext.currentTracker();
auto& currentTracker = m_impl->trackerContext.currentTracker();
assert(
currentTracker.nameAndLocation() != nameAndLoc &&
"Trying to create tracker for a genreator that already has one" );
auto newTracker = Catch::Detail::make_unique<Generators::GeneratorTracker>(
CATCH_MOVE(nameAndLoc), m_trackerContext, &currentTracker );
CATCH_MOVE(nameAndLoc), m_impl->trackerContext, &currentTracker );
auto ret = newTracker.get();
currentTracker.addChild( CATCH_MOVE( newTracker ) );
@ -357,7 +381,7 @@ namespace Catch {
return false;
if (!m_config->warnAboutMissingAssertions())
return false;
if (m_trackerContext.currentTracker().hasChildren())
if (m_impl->trackerContext.currentTracker().hasChildren())
return false;
m_totals.assertions.failed++;
assertions.failed++;
@ -368,50 +392,57 @@ namespace Catch {
Counts assertions = m_totals.assertions - endInfo.prevAssertions;
bool missingAssertions = testForMissingAssertions(assertions);
if (!m_activeSections.empty()) {
m_activeSections.back()->close();
m_activeSections.pop_back();
if (!m_impl->activeSections.empty()) {
m_impl->activeSections.back()->close();
m_impl->activeSections.pop_back();
}
m_reporter->sectionEnded(SectionStats(CATCH_MOVE(endInfo.sectionInfo), assertions, endInfo.durationInSeconds, missingAssertions));
m_messages.clear();
m_messageScopes.clear();
m_impl->reporter->sectionEnded(
SectionStats( CATCH_MOVE( endInfo.sectionInfo ),
assertions,
endInfo.durationInSeconds,
missingAssertions ) );
m_impl->messages.clear();
m_impl->messageScopes.clear();
}
void RunContext::sectionEndedEarly(SectionEndInfo&& endInfo) {
if ( m_unfinishedSections.empty() ) {
m_activeSections.back()->fail();
if ( m_impl->unfinishedSections.empty() ) {
m_impl->activeSections.back()->fail();
} else {
m_activeSections.back()->close();
m_impl->activeSections.back()->close();
}
m_activeSections.pop_back();
m_impl->activeSections.pop_back();
m_unfinishedSections.push_back(CATCH_MOVE(endInfo));
m_impl->unfinishedSections.push_back(CATCH_MOVE(endInfo));
}
void RunContext::benchmarkPreparing( StringRef name ) {
m_reporter->benchmarkPreparing(name);
m_impl->reporter->benchmarkPreparing( name );
}
void RunContext::benchmarkStarting( BenchmarkInfo const& info ) {
m_reporter->benchmarkStarting( info );
m_impl->reporter->benchmarkStarting( info );
}
void RunContext::benchmarkEnded( BenchmarkStats<> const& stats ) {
m_reporter->benchmarkEnded( stats );
m_impl->reporter->benchmarkEnded( stats );
}
void RunContext::benchmarkFailed( StringRef error ) {
m_reporter->benchmarkFailed( error );
m_impl->reporter->benchmarkFailed( error );
}
void RunContext::pushScopedMessage(MessageInfo const & message) {
m_messages.push_back(message);
m_impl->messages.push_back(message);
}
void RunContext::popScopedMessage(MessageInfo const & message) {
m_messages.erase(std::remove(m_messages.begin(), m_messages.end(), message), m_messages.end());
m_impl->messages.erase( std::remove( m_impl->messages.begin(),
m_impl->messages.end(),
message ),
m_impl->messages.end() );
}
void RunContext::emplaceUnscopedMessage( MessageBuilder&& builder ) {
m_messageScopes.emplace_back( CATCH_MOVE(builder) );
m_impl->messageScopes.emplace_back( CATCH_MOVE(builder) );
}
std::string RunContext::getCurrentTestName() const {
@ -421,7 +452,7 @@ namespace Catch {
}
const AssertionResult * RunContext::getLastResult() const {
return &(*m_lastResult);
return &(*m_impl->lastResult);
}
void RunContext::exceptionEarlyReported() {
@ -430,7 +461,7 @@ namespace Catch {
void RunContext::handleFatalErrorCondition( StringRef message ) {
// First notify reporter that bad things happened
m_reporter->fatalErrorEncountered(message);
m_impl->reporter->fatalErrorEncountered(message);
// Don't rebuild the result -- the stringification itself can cause more fatal errors
// Instead, fake a result data.
@ -449,20 +480,20 @@ namespace Catch {
Counts assertions;
assertions.failed = 1;
SectionStats testCaseSectionStats(CATCH_MOVE(testCaseSection), assertions, 0, false);
m_reporter->sectionEnded(testCaseSectionStats);
m_impl->reporter->sectionEnded(testCaseSectionStats);
auto const& testInfo = m_activeTestCase->getTestCaseInfo();
Totals deltaTotals;
deltaTotals.testCases.failed = 1;
deltaTotals.assertions.failed = 1;
m_reporter->testCaseEnded(TestCaseStats(testInfo,
m_impl->reporter->testCaseEnded(TestCaseStats(testInfo,
deltaTotals,
std::string(),
std::string(),
false));
m_totals.testCases.failed++;
m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, false));
m_impl->reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, false));
}
bool RunContext::lastAssertionPassed() {
@ -473,7 +504,7 @@ namespace Catch {
m_lastAssertionPassed = true;
++m_totals.assertions.passed;
resetAssertionInfo();
m_messageScopes.clear();
m_impl->messageScopes.clear();
}
bool RunContext::aborting() const {
@ -483,7 +514,7 @@ namespace Catch {
void RunContext::runCurrentTest(std::string & redirectedCout, std::string & redirectedCerr) {
auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo();
SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name);
m_reporter->sectionStarting(testCaseSection);
m_impl->reporter->sectionStarting(testCaseSection);
Counts prevAssertions = m_totals.assertions;
double duration = 0;
m_shouldReportUnexpected = true;
@ -491,7 +522,7 @@ namespace Catch {
Timer timer;
CATCH_TRY {
if (m_reporter->getPreferences().shouldRedirectStdOut) {
if (m_impl->reporter->getPreferences().shouldRedirectStdOut) {
#if !defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT)
RedirectedStreams redirectedStreams(redirectedCout, redirectedCerr);
@ -524,18 +555,18 @@ namespace Catch {
m_testCaseTracker->close();
handleUnfinishedSections();
m_messages.clear();
m_messageScopes.clear();
m_impl->messages.clear();
m_impl->messageScopes.clear();
SectionStats testCaseSectionStats(CATCH_MOVE(testCaseSection), assertions, duration, missingAssertions);
m_reporter->sectionEnded(testCaseSectionStats);
m_impl->reporter->sectionEnded(testCaseSectionStats);
}
void RunContext::invokeActiveTestCase() {
// We need to engage a handler for signals/structured exceptions
// before running the tests themselves, or the binary can crash
// without failed test being reported.
FatalConditionHandlerGuard _(&m_fatalConditionhandler);
FatalConditionHandlerGuard _(&m_impl->fatalConditionhandler);
// We keep having issue where some compilers warn about an unused
// variable, even though the type has non-trivial constructor and
// destructor. This is annoying and ugly, but it makes them stfu.
@ -547,12 +578,12 @@ namespace Catch {
void RunContext::handleUnfinishedSections() {
// If sections ended prematurely due to an exception we stored their
// infos here so we can tear them down outside the unwind process.
for (auto it = m_unfinishedSections.rbegin(),
itEnd = m_unfinishedSections.rend();
for (auto it = m_impl->unfinishedSections.rbegin(),
itEnd = m_impl->unfinishedSections.rend();
it != itEnd;
++it)
sectionEnded(CATCH_MOVE(*it));
m_unfinishedSections.clear();
m_impl->unfinishedSections.clear();
}
void RunContext::handleExpr(
@ -560,7 +591,7 @@ namespace Catch {
ITransientExpression const& expr,
AssertionReaction& reaction
) {
m_reporter->assertionStarting( info );
m_impl->reporter->assertionStarting( info );
bool negated = isFalseTest( info.resultDisposition );
bool result = expr.getResult() != negated;
@ -599,7 +630,7 @@ namespace Catch {
StringRef message,
AssertionReaction& reaction
) {
m_reporter->assertionStarting( info );
m_impl->reporter->assertionStarting( info );
m_lastAssertionInfo = info;
@ -669,7 +700,7 @@ namespace Catch {
}
IResultCapture& getResultCapture() {
RunContext& getResultCapture() {
if (auto* capture = getCurrentContext().getResultCapture())
return *capture;
else

View File

@ -8,36 +8,60 @@
#ifndef CATCH_RUN_CONTEXT_HPP_INCLUDED
#define CATCH_RUN_CONTEXT_HPP_INCLUDED
#include <catch2/interfaces/catch_interfaces_reporter.hpp>
#include <catch2/internal/catch_test_registry.hpp>
#include <catch2/internal/catch_fatal_condition_handler.hpp>
#include <catch2/catch_test_case_info.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/catch_test_run_info.hpp>
#include <catch2/catch_totals.hpp>
#include <catch2/internal/catch_test_case_tracker.hpp>
#include <catch2/catch_assertion_info.hpp>
#include <catch2/catch_assertion_result.hpp>
#include <catch2/internal/catch_optional.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/internal/catch_benchmark_stats_fwd.hpp>
#include <catch2/internal/catch_unique_ptr.hpp>
#include <string>
#include <chrono>
namespace Catch {
class TestCaseHandle;
class AssertionResult;
struct AssertionInfo;
struct SectionInfo;
struct SectionEndInfo;
struct MessageInfo;
struct MessageBuilder;
struct Counts;
struct AssertionReaction;
struct SourceLineInfo;
class ITransientExpression;
class IGeneratorTracker;
struct BenchmarkInfo;
namespace Generators {
class GeneratorUntypedBase;
using GeneratorBasePtr = Catch::Detail::unique_ptr<GeneratorUntypedBase>;
}
class IGeneratorTracker;
class IConfig;
// Fixme: Take out the namespace?
namespace TestCaseTracking {
class ITracker;
}
using TestCaseTracking::ITracker;
///////////////////////////////////////////////////////////////////////////
class RunContext final : public IResultCapture {
class IEventListener;
using IEventListenerPtr = Detail::unique_ptr<IEventListener>;
class RunContext {
struct RunContextImpl;
Detail::unique_ptr<RunContextImpl> m_impl;
public:
RunContext( RunContext const& ) = delete;
RunContext& operator =( RunContext const& ) = delete;
explicit RunContext( IConfig const* _config, IEventListenerPtr&& reporter );
~RunContext() override;
~RunContext();
Totals runTest(TestCaseHandle const& testCase);
@ -47,63 +71,63 @@ namespace Catch {
void handleExpr
( AssertionInfo const& info,
ITransientExpression const& expr,
AssertionReaction& reaction ) override;
AssertionReaction& reaction );
void handleMessage
( AssertionInfo const& info,
ResultWas::OfType resultType,
StringRef message,
AssertionReaction& reaction ) override;
AssertionReaction& reaction );
void handleUnexpectedExceptionNotThrown
( AssertionInfo const& info,
AssertionReaction& reaction ) override;
AssertionReaction& reaction );
void handleUnexpectedInflightException
( AssertionInfo const& info,
std::string&& message,
AssertionReaction& reaction ) override;
AssertionReaction& reaction );
void handleIncomplete
( AssertionInfo const& info ) override;
( AssertionInfo const& info );
void handleNonExpr
( AssertionInfo const &info,
ResultWas::OfType resultType,
AssertionReaction &reaction ) override;
AssertionReaction &reaction );
bool sectionStarted( StringRef sectionName,
SourceLineInfo const& sectionLineInfo,
Counts& assertions ) override;
Counts& assertions );
void sectionEnded( SectionEndInfo&& endInfo ) override;
void sectionEndedEarly( SectionEndInfo&& endInfo ) override;
void sectionEnded( SectionEndInfo&& endInfo );
void sectionEndedEarly( SectionEndInfo&& endInfo );
IGeneratorTracker*
acquireGeneratorTracker( StringRef generatorName,
SourceLineInfo const& lineInfo ) override;
SourceLineInfo const& lineInfo );
IGeneratorTracker* createGeneratorTracker(
StringRef generatorName,
SourceLineInfo lineInfo,
Generators::GeneratorBasePtr&& generator ) override;
Generators::GeneratorBasePtr&& generator );
void benchmarkPreparing( StringRef name ) override;
void benchmarkStarting( BenchmarkInfo const& info ) override;
void benchmarkEnded( BenchmarkStats<> const& stats ) override;
void benchmarkFailed( StringRef error ) override;
void benchmarkPreparing( StringRef name );
void benchmarkStarting( BenchmarkInfo const& info );
void benchmarkEnded( BenchmarkStats<> const& stats );
void benchmarkFailed( StringRef error );
void pushScopedMessage( MessageInfo const& message ) override;
void popScopedMessage( MessageInfo const& message ) override;
void pushScopedMessage( MessageInfo const& message );
void popScopedMessage( MessageInfo const& message );
void emplaceUnscopedMessage( MessageBuilder&& builder ) override;
void emplaceUnscopedMessage( MessageBuilder&& builder );
std::string getCurrentTestName() const override;
std::string getCurrentTestName() const;
const AssertionResult* getLastResult() const override;
const AssertionResult* getLastResult() const;
void exceptionEarlyReported() override;
void exceptionEarlyReported();
void handleFatalErrorCondition( StringRef message ) override;
void handleFatalErrorCondition( StringRef message );
bool lastAssertionPassed() override;
bool lastAssertionPassed();
void assertionPassed() override;
void assertionPassed();
public:
// !TBD We need to do this another way!
@ -133,18 +157,10 @@ namespace Catch {
TestRunInfo m_runInfo;
TestCaseHandle const* m_activeTestCase = nullptr;
ITracker* m_testCaseTracker = nullptr;
Optional<AssertionResult> m_lastResult;
IConfig const* m_config;
Totals m_totals;
IEventListenerPtr m_reporter;
std::vector<MessageInfo> m_messages;
std::vector<ScopedMessage> m_messageScopes; /* Keeps owners of so-called unscoped messages. */
AssertionInfo m_lastAssertionInfo;
std::vector<SectionEndInfo> m_unfinishedSections;
std::vector<ITracker*> m_activeSections;
TrackerContext m_trackerContext;
FatalConditionHandler m_fatalConditionhandler;
bool m_lastAssertionPassed = false;
bool m_shouldReportUnexpected = true;
bool m_includeSuccessfulResults;
@ -152,6 +168,9 @@ namespace Catch {
void seedRng(IConfig const& config);
unsigned int rngSeed();
RunContext& getResultCapture();
} // end namespace Catch
#endif // CATCH_RUN_CONTEXT_HPP_INCLUDED

View File

@ -10,7 +10,6 @@
#include <catch2/catch_user_config.hpp>
#include <catch2/internal/catch_assertion_handler.hpp>
#include <catch2/interfaces/catch_interfaces_capture.hpp>
#include <catch2/internal/catch_stringref.hpp>
#include <catch2/internal/catch_source_line_info.hpp>

View File

@ -56,7 +56,6 @@ internal_headers = [
'generators/catch_generators_random.hpp',
'generators/catch_generators_range.hpp',
'interfaces/catch_interfaces_all.hpp',
'interfaces/catch_interfaces_capture.hpp',
'interfaces/catch_interfaces_config.hpp',
'interfaces/catch_interfaces_exception.hpp',
'interfaces/catch_interfaces_generatortracker.hpp',
@ -64,6 +63,7 @@ internal_headers = [
'interfaces/catch_interfaces_reporter_factory.hpp',
'interfaces/catch_interfaces_testcase.hpp',
'internal/catch_assertion_handler.hpp',
'internal/catch_benchmark_stats_fwd.hpp',
'internal/catch_case_insensitive_comparisons.hpp',
'internal/catch_case_sensitive.hpp',
'internal/catch_clara.hpp',
@ -165,6 +165,7 @@ internal_headers = [
'catch_template_test_macros.hpp',
'catch_test_case_info.hpp',
'catch_test_macros.hpp',
'catch_test_run_info.hpp',
'catch_test_spec.hpp',
'catch_timer.hpp',
'catch_tostring.hpp',
@ -178,7 +179,6 @@ internal_sources = files(
'generators/catch_generator_exception.cpp',
'generators/catch_generators.cpp',
'generators/catch_generators_random.cpp',
'interfaces/catch_interfaces_capture.cpp',
'interfaces/catch_interfaces_config.cpp',
'interfaces/catch_interfaces_exception.cpp',
'interfaces/catch_interfaces_generatortracker.cpp',