mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 20:27:11 +01:00 
			
		
		
		
	General cleanup for C++11
Also less allocations and less stack usage on the fatal condition path
This commit is contained in:
		| @@ -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; | ||||
|   | ||||
| @@ -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 = "<unknown signal>"; | ||||
|         char const * name = "<unknown signal>"; | ||||
|         for (auto const& def : signalDefs) { | ||||
|             if (sig == def.id) { | ||||
|                 name = def.name; | ||||
|   | ||||
| @@ -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" | ||||
|   | ||||
| @@ -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"); | ||||
|   | ||||
| @@ -22,14 +22,16 @@ | ||||
| #include <cstdlib> | ||||
| #include <iomanip> | ||||
|  | ||||
| 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<Config> const& config ) { | ||||
|     IStreamingReporterPtr makeReporter(std::shared_ptr<Config> 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<Config> const& config ) { | ||||
|     Catch::Totals runTests(std::shared_ptr<Config> 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<TestCase> 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<std::vector<TestCase>&>( getAllTestCasesSorted( config ) ); | ||||
|         for( auto& testCase : tests ) { | ||||
|     void applyFilenamesAsTags(Catch::IConfig const& config) { | ||||
|         using namespace Catch; | ||||
|         auto& tests = const_cast<std::vector<TestCase>&>(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; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Martin Hořeňovský
					Martin Hořeňovský