Simplified test case registration and started using StringRefs

This commit is contained in:
Phil Nash 2017-07-12 22:39:31 +01:00
parent 79627cdcdb
commit 720fdf1d02
4 changed files with 44 additions and 80 deletions

View File

@ -12,13 +12,17 @@
#include <cstring> #include <cstring>
#include <ostream> #include <ostream>
#include <cassert>
namespace Catch { namespace Catch {
StringRef StringRef::s_emptyStringRef = ""; auto getEmptyStringRef() -> StringRef {
static StringRef s_emptyStringRef("");
return s_emptyStringRef;
}
StringRef::StringRef() noexcept StringRef::StringRef() noexcept
: StringRef( s_emptyStringRef ) : StringRef( getEmptyStringRef() )
{} {}
StringRef::StringRef( StringRef const& other ) noexcept StringRef::StringRef( StringRef const& other ) noexcept
@ -41,7 +45,9 @@ namespace Catch {
StringRef::StringRef( char const* rawChars ) noexcept StringRef::StringRef( char const* rawChars ) noexcept
: m_start( rawChars ), : m_start( rawChars ),
m_size( static_cast<size_type>( std::strlen( rawChars ) ) ) m_size( static_cast<size_type>( std::strlen( rawChars ) ) )
{} {
assert( rawChars != nullptr );
}
StringRef::StringRef( char const* rawChars, size_type size ) noexcept StringRef::StringRef( char const* rawChars, size_type size ) noexcept
: m_start( rawChars ), : m_start( rawChars ),

View File

@ -31,8 +31,6 @@ namespace Catch {
StringData const* m_data = nullptr; StringData const* m_data = nullptr;
static StringRef s_emptyStringRef;
void takeOwnership(); void takeOwnership();
public: // construction/ assignment public: // construction/ assignment

View File

@ -134,6 +134,10 @@ namespace Catch {
m_testAsFunction(); m_testAsFunction();
} }
}; };
auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker* {
return new TestInvokerAsFunction( testAsFunction );
}
inline std::string extractClassName( std::string const& classOrQualifiedMethodName ) { inline std::string extractClassName( std::string const& classOrQualifiedMethodName ) {
std::string className = classOrQualifiedMethodName; std::string className = classOrQualifiedMethodName;
@ -148,40 +152,24 @@ namespace Catch {
return className; return className;
} }
void registerTestCase ///////////////////////////////////////////////////////////////////////////
( ITestInvoker* testCase,
char const* classOrQualifiedMethodName, AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags )
NameAndDesc const& nameAndDesc, {
SourceLineInfo const& lineInfo ) {
try { try {
getMutableRegistryHub().registerTest getMutableRegistryHub()
(makeTestCase .registerTest(
(testCase, makeTestCase(
extractClassName(classOrQualifiedMethodName), invoker,
nameAndDesc.name, extractClassName( classOrMethod.c_str() ),
nameAndDesc.description, nameAndTags.name.c_str(),
lineInfo)); nameAndTags.tags.c_str(),
lineInfo));
} catch (...) { } catch (...) {
// Do not throw when constructing global objects, instead register the exception to be processed later // Do not throw when constructing global objects, instead register the exception to be processed later
getMutableRegistryHub().registerStartupException( std::current_exception() ); getMutableRegistryHub().registerStartupException( std::current_exception() );
} }
} }
void registerTestCaseFunction
( TestFunction function,
SourceLineInfo const& lineInfo,
NameAndDesc const& nameAndDesc ) {
registerTestCase( new TestInvokerAsFunction( function ), "", nameAndDesc, lineInfo );
}
///////////////////////////////////////////////////////////////////////////
AutoReg::AutoReg
( TestFunction function,
SourceLineInfo const& lineInfo,
NameAndDesc const& nameAndDesc ) {
registerTestCaseFunction( function, lineInfo, nameAndDesc );
}
AutoReg::~AutoReg() {} AutoReg::~AutoReg() {}
} // end namespace Catch } // end namespace Catch

View File

@ -11,6 +11,7 @@
#include "catch_common.h" #include "catch_common.h"
#include "catch_interfaces_testcase.h" #include "catch_interfaces_testcase.h"
#include "catch_compiler_capabilities.h" #include "catch_compiler_capabilities.h"
#include "catch_stringref.h"
namespace Catch { namespace Catch {
@ -26,62 +27,33 @@ public:
} }
}; };
typedef void(*TestFunction)(); auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker*;
struct NameAndDesc { template<typename C>
NameAndDesc( const char* _name = "", const char* _description= "" ) auto makeTestInvoker( void (C::*testAsMethod)() ) -> ITestInvoker* {
: name( _name ), description( _description ) return new TestInvokerAsMethod<C>( testAsMethod );
{} }
const char* name; struct NameAndTags {
const char* description;
NameAndTags( StringRef name_ = "", StringRef tags_ = "" ) : name( name_ ), tags( tags_ ) {}
StringRef name;
StringRef tags;
}; };
void registerTestCase struct AutoReg : NonCopyable {
( ITestInvoker* testCase, AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags );
char const* className,
NameAndDesc const& nameAndDesc,
SourceLineInfo const& lineInfo );
struct AutoReg {
AutoReg
( TestFunction function,
SourceLineInfo const& lineInfo,
NameAndDesc const& nameAndDesc );
template<typename C>
AutoReg
( void (C::*method)(),
char const* className,
NameAndDesc const& nameAndDesc,
SourceLineInfo const& lineInfo ) {
registerTestCase
( new TestInvokerAsMethod<C>( method ),
className,
nameAndDesc,
lineInfo );
}
~AutoReg(); ~AutoReg();
AutoReg( AutoReg const& ) = delete;
AutoReg& operator = (AutoReg const& ) = delete;
}; };
void registerTestCaseFunction
( TestFunction function,
SourceLineInfo const& lineInfo,
NameAndDesc const& nameAndDesc );
} // end namespace Catch } // end namespace Catch
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \ #define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \
static void TestName(); \ static void TestName(); \
CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \ CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &TestName, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); } \ namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &TestName ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); } \
CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS \ CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS \
static void TestName() static void TestName()
#define INTERNAL_CATCH_TESTCASE( ... ) \ #define INTERNAL_CATCH_TESTCASE( ... ) \
@ -90,7 +62,7 @@ void registerTestCaseFunction
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \ #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \
CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \ CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); } \ namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( Catch::makeTestInvoker( &QualifiedMethod ), CATCH_INTERNAL_LINEINFO, "&" #QualifiedMethod, Catch::NameAndTags{ __VA_ARGS__ } ); } \
CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -100,7 +72,7 @@ void registerTestCaseFunction
struct TestName : ClassName{ \ struct TestName : ClassName{ \
void test(); \ void test(); \
}; \ }; \
Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &TestName::test, #ClassName, Catch::NameAndDesc( __VA_ARGS__ ), CATCH_INTERNAL_LINEINFO ); \ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( Catch::makeTestInvoker( &TestName::test ), CATCH_INTERNAL_LINEINFO, #ClassName, Catch::NameAndTags{ __VA_ARGS__ } ); \
} \ } \
CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS \ CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS \
void TestName::test() void TestName::test()
@ -110,7 +82,7 @@ void registerTestCaseFunction
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \ #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \
CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \ CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS \
Catch::AutoReg( Function, CATCH_INTERNAL_LINEINFO, Catch::NameAndDesc( __VA_ARGS__ ) ); \ Catch::AutoReg( Catch::makeTestInvoker( Function ), CATCH_INTERNAL_LINEINFO, "", Catch::NameAndTags{ __VA_ARGS__ } ); \
CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS