mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 11:43:29 +01:00
Simplified test case registration and started using StringRefs
This commit is contained in:
parent
79627cdcdb
commit
720fdf1d02
@ -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 ),
|
||||||
|
@ -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
|
||||||
|
@ -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(),
|
||||||
|
nameAndTags.tags.c_str(),
|
||||||
lineInfo));
|
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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user