General cleanup for C++11

Also less allocations and less stack usage on the fatal condition path
This commit is contained in:
Martin Hořeňovský 2017-08-31 11:46:37 +02:00
parent e871742534
commit e8ec6bd73c
5 changed files with 63 additions and 62 deletions

View File

@ -23,7 +23,7 @@ namespace Catch {
AssertionResultData( ResultWas::OfType _resultType, LazyExpression const& _lazyExpression ); AssertionResultData( ResultWas::OfType _resultType, LazyExpression const& _lazyExpression );
ResultWas::OfType resultType = ResultWas::Unknown; ResultWas::OfType resultType;
std::string message; std::string message;
LazyExpression lazyExpression; LazyExpression lazyExpression;

View File

@ -12,16 +12,12 @@
#include "catch_context.h" #include "catch_context.h"
#include "catch_interfaces_capture.h" #include "catch_interfaces_capture.h"
namespace Catch { namespace {
// Report the error condition // Report the error condition
void reportFatal( std::string const& message ) { void reportFatal( char const * const message ) {
IContext& context = Catch::getCurrentContext(); Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message );
IResultCapture* resultCapture = context.getResultCapture(); }
resultCapture->handleFatalErrorCondition( message );
} }
} // namespace Catch
#if defined ( CATCH_PLATFORM_WINDOWS ) ///////////////////////////////////////// #if defined ( CATCH_PLATFORM_WINDOWS ) /////////////////////////////////////////
@ -122,7 +118,7 @@ namespace Catch {
void FatalConditionHandler::handleSignal( int sig ) { void FatalConditionHandler::handleSignal( int sig ) {
std::string name = "<unknown signal>"; char const * name = "<unknown signal>";
for (auto const& def : signalDefs) { for (auto const& def : signalDefs) {
if (sig == def.id) { if (sig == def.id) {
name = def.name; name = def.name;

View File

@ -13,12 +13,6 @@
#include "catch_platform.h" #include "catch_platform.h"
#include "catch_compiler_capabilities.h" #include "catch_compiler_capabilities.h"
namespace Catch {
// Report the error condition
void reportFatal( std::string const& message );
} // namespace Catch
#if defined ( CATCH_PLATFORM_WINDOWS ) ///////////////////////////////////////// #if defined ( CATCH_PLATFORM_WINDOWS ) /////////////////////////////////////////
#include "catch_windows_h_proxy.h" #include "catch_windows_h_proxy.h"

View File

@ -211,8 +211,7 @@ namespace Catch {
void RunContext::handleFatalErrorCondition(std::string const & message) { void RunContext::handleFatalErrorCondition(std::string const & message) {
// Don't rebuild the result -- the stringification itself can cause more fatal errors // Don't rebuild the result -- the stringification itself can cause more fatal errors
// Instead, fake a result data. // Instead, fake a result data.
AssertionResultData tempResult( ResultWas::Unknown, { false } ); AssertionResultData tempResult( ResultWas::FatalErrorCondition, { false } );
tempResult.resultType = ResultWas::FatalErrorCondition;
tempResult.message = message; tempResult.message = message;
AssertionResult result(m_lastAssertionInfo, tempResult); AssertionResult result(m_lastAssertionInfo, tempResult);
@ -221,7 +220,7 @@ namespace Catch {
handleUnfinishedSections(); handleUnfinishedSections();
// Recreate section for test case (as we will lose the one that was in scope) // Recreate section for test case (as we will lose the one that was in scope)
TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo();
SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description); SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description);
Counts assertions; Counts assertions;
@ -229,7 +228,7 @@ namespace Catch {
SectionStats testCaseSectionStats(testCaseSection, assertions, 0, false); SectionStats testCaseSectionStats(testCaseSection, assertions, 0, false);
m_reporter->sectionEnded(testCaseSectionStats); m_reporter->sectionEnded(testCaseSectionStats);
TestCaseInfo testInfo = m_activeTestCase->getTestCaseInfo(); auto const& testInfo = m_activeTestCase->getTestCaseInfo();
Totals deltaTotals; Totals deltaTotals;
deltaTotals.testCases.failed = 1; deltaTotals.testCases.failed = 1;
@ -263,7 +262,7 @@ namespace Catch {
} }
void RunContext::runCurrentTest(std::string & redirectedCout, std::string & redirectedCerr) { void RunContext::runCurrentTest(std::string & redirectedCout, std::string & redirectedCerr) {
TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); auto const& testCaseInfo = m_activeTestCase->getTestCaseInfo();
SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description); SectionInfo testCaseSection(testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description);
m_reporter->sectionStarting(testCaseSection); m_reporter->sectionStarting(testCaseSection);
Counts prevAssertions = m_totals.assertions; Counts prevAssertions = m_totals.assertions;
@ -325,7 +324,7 @@ namespace Catch {
} }
IResultCapture& getResultCapture() { IResultCapture& getResultCapture() {
if (IResultCapture* capture = getCurrentContext().getResultCapture()) if (auto* capture = getCurrentContext().getResultCapture())
return *capture; return *capture;
else else
CATCH_INTERNAL_ERROR("No result capture instance"); CATCH_INTERNAL_ERROR("No result capture instance");

View File

@ -22,13 +22,15 @@
#include <cstdlib> #include <cstdlib>
#include <iomanip> #include <iomanip>
static const int MaxExitCode = 255;
namespace {
namespace Catch { const int MaxExitCode = 255;
using Catch::IStreamingReporterPtr;
using Catch::IConfigPtr;
using Catch::Config;
IStreamingReporterPtr createReporter(std::string const& reporterName, IConfigPtr const& config) { IStreamingReporterPtr createReporter(std::string const& reporterName, IConfigPtr const& config) {
auto reporter = getRegistryHub().getReporterRegistry().create( reporterName, config ); auto reporter = Catch::getRegistryHub().getReporterRegistry().create(reporterName, config);
CATCH_ENFORCE(reporter, "No reporter registered with name: '" << reporterName << "'"); CATCH_ENFORCE(reporter, "No reporter registered with name: '" << reporterName << "'");
return reporter; return reporter;
@ -48,15 +50,18 @@ namespace Catch {
addReporter(reporter, createReporter(name, config)); addReporter(reporter, createReporter(name, config));
return reporter; return reporter;
} }
#undef CATCH_CONFIG_DEFAULT_REPORTER
void addListeners(IStreamingReporterPtr& reporters, IConfigPtr const& config) { void addListeners(IStreamingReporterPtr& reporters, IConfigPtr const& config) {
auto const& listeners = getRegistryHub().getReporterRegistry().getListeners(); auto const& listeners = Catch::getRegistryHub().getReporterRegistry().getListeners();
for (auto const& listener : listeners) for (auto const& listener : listeners)
addReporter(reporters, listener->create( ReporterConfig( config ) ) ); addReporter(reporters, listener->create(Catch::ReporterConfig(config)));
} }
Totals runTests( std::shared_ptr<Config> const& config ) { Catch::Totals runTests(std::shared_ptr<Config> const& config) {
using namespace Catch;
IStreamingReporterPtr reporter = makeReporter(config); IStreamingReporterPtr reporter = makeReporter(config);
addListeners(reporter, config); addListeners(reporter, config);
@ -70,7 +75,7 @@ namespace Catch {
if (!testSpec.hasFilters()) if (!testSpec.hasFilters())
testSpec = TestSpecParser(ITagAliasRegistry::get()).parse("~[.]").testSpec(); // All not hidden tests testSpec = TestSpecParser(ITagAliasRegistry::get()).parse("~[.]").testSpec(); // All not hidden tests
std::vector<TestCase> const& allTestCases = getAllTestCasesSorted( *config ); auto const& allTestCases = getAllTestCasesSorted(*config);
for (auto const& testCase : allTestCases) { for (auto const& testCase : allTestCases) {
if (!context.aborting() && matchTest(testCase, testSpec, *config)) if (!context.aborting() && matchTest(testCase, testSpec, *config))
totals += context.runTest(testCase); totals += context.runTest(testCase);
@ -82,25 +87,32 @@ namespace Catch {
return totals; return totals;
} }
void applyFilenamesAsTags( IConfig const& config ) { void applyFilenamesAsTags(Catch::IConfig const& config) {
using namespace Catch;
auto& tests = const_cast<std::vector<TestCase>&>(getAllTestCasesSorted(config)); auto& tests = const_cast<std::vector<TestCase>&>(getAllTestCasesSorted(config));
for (auto& testCase : tests) { for (auto& testCase : tests) {
auto tags = testCase.tags; auto tags = testCase.tags;
std::string filename = testCase.lineInfo.file; std::string filename = testCase.lineInfo.file;
std::string::size_type lastSlash = filename.find_last_of( "\\/" ); auto lastSlash = filename.find_last_of("\\/");
if( lastSlash != std::string::npos ) if (lastSlash != std::string::npos) {
filename = filename.substr( lastSlash+1 ); filename.erase(0, lastSlash);
filename[0] = '#';
}
std::string::size_type lastDot = filename.find_last_of( '.' ); auto lastDot = filename.find_last_of('.');
if( lastDot != std::string::npos ) if (lastDot != std::string::npos) {
filename = filename.substr( 0, lastDot ); filename.erase(lastDot);
}
tags.push_back( '#' + filename ); tags.push_back(std::move(filename));
setTags(testCase, tags); setTags(testCase, tags);
} }
} }
}
namespace Catch {
Session::Session() { Session::Session() {
static bool alreadyInstantiated = false; static bool alreadyInstantiated = false;