Catch and register startup exceptions in autoregistrars

Previously they were registered where they would be thrown otherwise
This commit is contained in:
Martin Hořeňovský 2017-06-04 22:37:59 +02:00
parent da0edcbe25
commit 860de28b8d
3 changed files with 28 additions and 41 deletions

View File

@ -39,29 +39,13 @@ namespace Catch {
} }
void TagAliasRegistry::add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) { void TagAliasRegistry::add( std::string const& alias, std::string const& tag, SourceLineInfo const& lineInfo ) {
// Do not throw when constructing global objects, instead register the exception to be processed later CATCH_ENFORCE( startsWith(alias, "[@") && endsWith(alias, ']'),
if (!(startsWith( alias, "[@") && endsWith(alias, ']'))) { "error: tag alias, '" << alias << "' is not of the form [@alias name].\n" << lineInfo );
getMutableRegistryHub().registerStartupException(
std::make_exception_ptr(
CATCH_PREPARE_EXCEPTION( std::domain_error,
"error: tag alias, '"
<< alias
<< "' is not of the form [@alias name].\n"
<< lineInfo)
)
);
}
if (!m_registry.insert(std::make_pair(alias, TagAlias(tag, lineInfo))).second) { CATCH_ENFORCE( m_registry.insert(std::make_pair(alias, TagAlias(tag, lineInfo))).second,
getMutableRegistryHub().registerStartupException(
std::make_exception_ptr(
CATCH_PREPARE_EXCEPTION(std::domain_error,
"error: tag alias, '" << alias << "' already registered.\n" "error: tag alias, '" << alias << "' already registered.\n"
<< "\tFirst seen at: " << find(alias)->lineInfo << "\n" << "\tFirst seen at: " << find(alias)->lineInfo << "\n"
<< "\tRedefined at: " << lineInfo) << "\tRedefined at: " << lineInfo );
)
);
}
} }
ITagAliasRegistry::~ITagAliasRegistry() {} ITagAliasRegistry::~ITagAliasRegistry() {}
@ -71,7 +55,12 @@ namespace Catch {
} }
RegistrarForTagAliases::RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ) { RegistrarForTagAliases::RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ) {
getMutableRegistryHub().registerTagAlias( alias, tag, lineInfo ); try {
getMutableRegistryHub().registerTagAlias(alias, tag, lineInfo);
} catch (...) {
// Do not throw when constructing global objects, instead register the exception to be processed later
getMutableRegistryHub().registerStartupException(std::current_exception());
}
} }
} // end namespace Catch } // end namespace Catch

View File

@ -38,16 +38,10 @@ namespace Catch {
return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( tag[0] ); return parseSpecialTag( tag ) == TestCaseInfo::None && tag.size() > 0 && !std::isalnum( tag[0] );
} }
inline void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) { inline void enforceNotReservedTag( std::string const& tag, SourceLineInfo const& _lineInfo ) {
// Do not throw when constructing global objects, instead register the exception to be processed later CATCH_ENFORCE( !isReservedTag(tag),
if (isReservedTag(tag)) { "Tag name: [" << tag << "] is not allowed.\n"
getMutableRegistryHub().registerStartupException(
std::make_exception_ptr(
CATCH_PREPARE_EXCEPTION(std::domain_error, "Tag name: [" << tag << "] is not allowed.\n"
<< "Tag names starting with non alpha-numeric characters are reserved\n" << "Tag names starting with non alpha-numeric characters are reserved\n"
<< _lineInfo) << _lineInfo );
)
);
}
} }
TestCase makeTestCase( ITestCase* _testCase, TestCase makeTestCase( ITestCase* _testCase,

View File

@ -154,14 +154,18 @@ namespace Catch {
char const* classOrQualifiedMethodName, char const* classOrQualifiedMethodName,
NameAndDesc const& nameAndDesc, NameAndDesc const& nameAndDesc,
SourceLineInfo const& lineInfo ) { SourceLineInfo const& lineInfo ) {
try {
getMutableRegistryHub().registerTest getMutableRegistryHub().registerTest
( makeTestCase (makeTestCase
( testCase, (testCase,
extractClassName( classOrQualifiedMethodName ), extractClassName(classOrQualifiedMethodName),
nameAndDesc.name, nameAndDesc.name,
nameAndDesc.description, nameAndDesc.description,
lineInfo ) ); lineInfo));
} catch (...) {
// Do not throw when constructing global objects, instead register the exception to be processed later
getMutableRegistryHub().registerStartupException( std::current_exception() );
}
} }
void registerTestCaseFunction void registerTestCaseFunction
( TestFunction function, ( TestFunction function,