mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-04 05:09:53 +01:00
General cleanup for C++11
Also less allocations and less stack usage on the fatal condition path
This commit is contained in:
parent
e871742534
commit
e8ec6bd73c
@ -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;
|
||||||
|
@ -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;
|
||||||
|
@ -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"
|
||||||
|
@ -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");
|
||||||
|
@ -22,14 +22,16 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iomanip>
|
#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 = Catch::getRegistryHub().getReporterRegistry().create(reporterName, config);
|
||||||
IStreamingReporterPtr createReporter( std::string const& reporterName, IConfigPtr const& config ) {
|
CATCH_ENFORCE(reporter, "No reporter registered with name: '" << reporterName << "'");
|
||||||
auto reporter = getRegistryHub().getReporterRegistry().create( reporterName, config );
|
|
||||||
CATCH_ENFORCE( reporter, "No reporter registered with name: '" << reporterName << "'" );
|
|
||||||
|
|
||||||
return reporter;
|
return reporter;
|
||||||
}
|
}
|
||||||
@ -38,69 +40,79 @@ namespace Catch {
|
|||||||
#define CATCH_CONFIG_DEFAULT_REPORTER "console"
|
#define CATCH_CONFIG_DEFAULT_REPORTER "console"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
IStreamingReporterPtr makeReporter( std::shared_ptr<Config> const& config ) {
|
IStreamingReporterPtr makeReporter(std::shared_ptr<Config> const& config) {
|
||||||
auto const& reporterNames = config->getReporterNames();
|
auto const& reporterNames = config->getReporterNames();
|
||||||
if( reporterNames.empty() )
|
if (reporterNames.empty())
|
||||||
return createReporter(CATCH_CONFIG_DEFAULT_REPORTER, config );
|
return createReporter(CATCH_CONFIG_DEFAULT_REPORTER, config);
|
||||||
|
|
||||||
IStreamingReporterPtr reporter;
|
IStreamingReporterPtr reporter;
|
||||||
for( auto const& name : reporterNames )
|
for (auto const& name : reporterNames)
|
||||||
addReporter( reporter, createReporter( name, config ) );
|
addReporter(reporter, createReporter(name, config));
|
||||||
return reporter;
|
return reporter;
|
||||||
}
|
}
|
||||||
void addListeners( IStreamingReporterPtr& reporters, IConfigPtr const& config ) {
|
|
||||||
auto const& listeners = getRegistryHub().getReporterRegistry().getListeners();
|
#undef CATCH_CONFIG_DEFAULT_REPORTER
|
||||||
for( auto const& listener : listeners )
|
|
||||||
addReporter(reporters, listener->create( ReporterConfig( config ) ) );
|
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 );
|
RunContext context(config, std::move(reporter));
|
||||||
addListeners( reporter, config );
|
|
||||||
|
|
||||||
RunContext context( config, std::move( reporter ) );
|
|
||||||
|
|
||||||
Totals totals;
|
Totals totals;
|
||||||
|
|
||||||
context.testGroupStarting( config->name(), 1, 1 );
|
context.testGroupStarting(config->name(), 1, 1);
|
||||||
|
|
||||||
TestSpec testSpec = config->testSpec();
|
TestSpec testSpec = config->testSpec();
|
||||||
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);
|
||||||
else
|
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;
|
return totals;
|
||||||
}
|
}
|
||||||
|
|
||||||
void applyFilenamesAsTags( IConfig const& config ) {
|
void applyFilenamesAsTags(Catch::IConfig const& config) {
|
||||||
auto& tests = const_cast<std::vector<TestCase>&>( getAllTestCasesSorted( config ) );
|
using namespace Catch;
|
||||||
for( auto& testCase : tests ) {
|
auto& tests = const_cast<std::vector<TestCase>&>(getAllTestCasesSorted(config));
|
||||||
|
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user