From e8ec6bd73c305399e2395a6aa8271d94f9cc0afe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Thu, 31 Aug 2017 11:46:37 +0200 Subject: [PATCH] General cleanup for C++11 Also less allocations and less stack usage on the fatal condition path --- include/internal/catch_assertionresult.h | 2 +- include/internal/catch_fatal_condition.cpp | 14 ++-- include/internal/catch_fatal_condition.h | 6 -- include/internal/catch_run_context.cpp | 11 ++- include/internal/catch_session.cpp | 92 ++++++++++++---------- 5 files changed, 63 insertions(+), 62 deletions(-) diff --git a/include/internal/catch_assertionresult.h b/include/internal/catch_assertionresult.h index dfa2fab0..6a4b61f1 100644 --- a/include/internal/catch_assertionresult.h +++ b/include/internal/catch_assertionresult.h @@ -23,7 +23,7 @@ namespace Catch { AssertionResultData( ResultWas::OfType _resultType, LazyExpression const& _lazyExpression ); - ResultWas::OfType resultType = ResultWas::Unknown; + ResultWas::OfType resultType; std::string message; LazyExpression lazyExpression; diff --git a/include/internal/catch_fatal_condition.cpp b/include/internal/catch_fatal_condition.cpp index 3de65f73..be1c333e 100644 --- a/include/internal/catch_fatal_condition.cpp +++ b/include/internal/catch_fatal_condition.cpp @@ -12,16 +12,12 @@ #include "catch_context.h" #include "catch_interfaces_capture.h" -namespace Catch { - +namespace { // Report the error condition - void reportFatal( std::string const& message ) { - IContext& context = Catch::getCurrentContext(); - IResultCapture* resultCapture = context.getResultCapture(); - resultCapture->handleFatalErrorCondition( message ); + void reportFatal( char const * const message ) { + Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message ); } - -} // namespace Catch +} #if defined ( CATCH_PLATFORM_WINDOWS ) ///////////////////////////////////////// @@ -122,7 +118,7 @@ namespace Catch { void FatalConditionHandler::handleSignal( int sig ) { - std::string name = ""; + char const * name = ""; for (auto const& def : signalDefs) { if (sig == def.id) { name = def.name; diff --git a/include/internal/catch_fatal_condition.h b/include/internal/catch_fatal_condition.h index 97f371ae..9977702a 100644 --- a/include/internal/catch_fatal_condition.h +++ b/include/internal/catch_fatal_condition.h @@ -13,12 +13,6 @@ #include "catch_platform.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 ) ///////////////////////////////////////// #include "catch_windows_h_proxy.h" diff --git a/include/internal/catch_run_context.cpp b/include/internal/catch_run_context.cpp index 8b22398c..7cb8f8dc 100644 --- a/include/internal/catch_run_context.cpp +++ b/include/internal/catch_run_context.cpp @@ -211,8 +211,7 @@ namespace Catch { void RunContext::handleFatalErrorCondition(std::string const & message) { // Don't rebuild the result -- the stringification itself can cause more fatal errors // Instead, fake a result data. - AssertionResultData tempResult( ResultWas::Unknown, { false } ); - tempResult.resultType = ResultWas::FatalErrorCondition; + AssertionResultData tempResult( ResultWas::FatalErrorCondition, { false } ); tempResult.message = message; AssertionResult result(m_lastAssertionInfo, tempResult); @@ -221,7 +220,7 @@ namespace Catch { handleUnfinishedSections(); // 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); Counts assertions; @@ -229,7 +228,7 @@ namespace Catch { SectionStats testCaseSectionStats(testCaseSection, assertions, 0, false); m_reporter->sectionEnded(testCaseSectionStats); - TestCaseInfo testInfo = m_activeTestCase->getTestCaseInfo(); + auto const& testInfo = m_activeTestCase->getTestCaseInfo(); Totals deltaTotals; deltaTotals.testCases.failed = 1; @@ -263,7 +262,7 @@ namespace Catch { } 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); m_reporter->sectionStarting(testCaseSection); Counts prevAssertions = m_totals.assertions; @@ -325,7 +324,7 @@ namespace Catch { } IResultCapture& getResultCapture() { - if (IResultCapture* capture = getCurrentContext().getResultCapture()) + if (auto* capture = getCurrentContext().getResultCapture()) return *capture; else CATCH_INTERNAL_ERROR("No result capture instance"); diff --git a/include/internal/catch_session.cpp b/include/internal/catch_session.cpp index d10e9510..54c9d783 100644 --- a/include/internal/catch_session.cpp +++ b/include/internal/catch_session.cpp @@ -22,14 +22,16 @@ #include #include -static const int MaxExitCode = 255; +namespace { + const int MaxExitCode = 255; + using Catch::IStreamingReporterPtr; + using Catch::IConfigPtr; + using Catch::Config; -namespace Catch { - - IStreamingReporterPtr createReporter( std::string const& reporterName, IConfigPtr const& config ) { - auto reporter = getRegistryHub().getReporterRegistry().create( reporterName, config ); - CATCH_ENFORCE( reporter, "No reporter registered with name: '" << reporterName << "'" ); + IStreamingReporterPtr createReporter(std::string const& reporterName, IConfigPtr const& config) { + auto reporter = Catch::getRegistryHub().getReporterRegistry().create(reporterName, config); + CATCH_ENFORCE(reporter, "No reporter registered with name: '" << reporterName << "'"); return reporter; } @@ -38,69 +40,79 @@ namespace Catch { #define CATCH_CONFIG_DEFAULT_REPORTER "console" #endif - IStreamingReporterPtr makeReporter( std::shared_ptr const& config ) { + IStreamingReporterPtr makeReporter(std::shared_ptr const& config) { auto const& reporterNames = config->getReporterNames(); - if( reporterNames.empty() ) - return createReporter(CATCH_CONFIG_DEFAULT_REPORTER, config ); + if (reporterNames.empty()) + return createReporter(CATCH_CONFIG_DEFAULT_REPORTER, config); IStreamingReporterPtr reporter; - for( auto const& name : reporterNames ) - addReporter( reporter, createReporter( name, config ) ); + for (auto const& name : reporterNames) + addReporter(reporter, createReporter(name, config)); return reporter; } - void addListeners( IStreamingReporterPtr& reporters, IConfigPtr const& config ) { - auto const& listeners = getRegistryHub().getReporterRegistry().getListeners(); - for( auto const& listener : listeners ) - addReporter(reporters, listener->create( ReporterConfig( config ) ) ); + +#undef CATCH_CONFIG_DEFAULT_REPORTER + + void addListeners(IStreamingReporterPtr& reporters, IConfigPtr const& config) { + auto const& listeners = Catch::getRegistryHub().getReporterRegistry().getListeners(); + for (auto const& listener : listeners) + addReporter(reporters, listener->create(Catch::ReporterConfig(config))); } - Totals runTests( std::shared_ptr const& config ) { + Catch::Totals runTests(std::shared_ptr const& config) { + using namespace Catch; + IStreamingReporterPtr reporter = makeReporter(config); + addListeners(reporter, config); - IStreamingReporterPtr reporter = makeReporter( config ); - addListeners( reporter, config ); - - RunContext context( config, std::move( reporter ) ); + RunContext context(config, std::move(reporter)); Totals totals; - context.testGroupStarting( config->name(), 1, 1 ); + context.testGroupStarting(config->name(), 1, 1); TestSpec testSpec = config->testSpec(); - if( !testSpec.hasFilters() ) - testSpec = TestSpecParser( ITagAliasRegistry::get() ).parse( "~[.]" ).testSpec(); // All not hidden tests + if (!testSpec.hasFilters()) + testSpec = TestSpecParser(ITagAliasRegistry::get()).parse("~[.]").testSpec(); // All not hidden tests - std::vector const& allTestCases = getAllTestCasesSorted( *config ); - for( auto const& testCase : allTestCases ) { - if( !context.aborting() && matchTest( testCase, testSpec, *config ) ) - totals += context.runTest( testCase ); + auto const& allTestCases = getAllTestCasesSorted(*config); + for (auto const& testCase : allTestCases) { + if (!context.aborting() && matchTest(testCase, testSpec, *config)) + totals += context.runTest(testCase); else - context.reporter().skipTest( testCase ); + context.reporter().skipTest(testCase); } - context.testGroupEnded( config->name(), totals, 1, 1 ); + context.testGroupEnded(config->name(), totals, 1, 1); return totals; } - void applyFilenamesAsTags( IConfig const& config ) { - auto& tests = const_cast&>( getAllTestCasesSorted( config ) ); - for( auto& testCase : tests ) { + void applyFilenamesAsTags(Catch::IConfig const& config) { + using namespace Catch; + auto& tests = const_cast&>(getAllTestCasesSorted(config)); + for (auto& testCase : tests) { auto tags = testCase.tags; std::string filename = testCase.lineInfo.file; - std::string::size_type lastSlash = filename.find_last_of( "\\/" ); - if( lastSlash != std::string::npos ) - filename = filename.substr( lastSlash+1 ); + auto lastSlash = filename.find_last_of("\\/"); + if (lastSlash != std::string::npos) { + filename.erase(0, lastSlash); + filename[0] = '#'; + } - std::string::size_type lastDot = filename.find_last_of( '.' ); - if( lastDot != std::string::npos ) - filename = filename.substr( 0, lastDot ); + auto lastDot = filename.find_last_of('.'); + if (lastDot != std::string::npos) { + filename.erase(lastDot); + } - tags.push_back( '#' + filename ); - setTags( testCase, tags ); + tags.push_back(std::move(filename)); + setTags(testCase, tags); } } +} + +namespace Catch { Session::Session() { static bool alreadyInstantiated = false;