mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 20:27:11 +01:00 
			
		
		
		
	Simplified test case registration and started using StringRefs
This commit is contained in:
		| @@ -12,13 +12,17 @@ | ||||
|  | ||||
| #include <cstring> | ||||
| #include <ostream> | ||||
| #include <cassert> | ||||
|  | ||||
| namespace Catch { | ||||
|      | ||||
|     StringRef StringRef::s_emptyStringRef = ""; | ||||
|  | ||||
|     auto getEmptyStringRef() -> StringRef { | ||||
|         static StringRef s_emptyStringRef(""); | ||||
|         return s_emptyStringRef; | ||||
|     } | ||||
|      | ||||
|     StringRef::StringRef() noexcept | ||||
|     :   StringRef( s_emptyStringRef ) | ||||
|     :   StringRef( getEmptyStringRef() ) | ||||
|     {} | ||||
|      | ||||
|     StringRef::StringRef( StringRef const& other ) noexcept | ||||
| @@ -41,7 +45,9 @@ namespace Catch { | ||||
|     StringRef::StringRef( char const* rawChars ) noexcept | ||||
|     :   m_start( rawChars ), | ||||
|         m_size( static_cast<size_type>( std::strlen( rawChars ) ) ) | ||||
|     {} | ||||
|     { | ||||
|         assert( rawChars != nullptr ); | ||||
|     } | ||||
|      | ||||
|     StringRef::StringRef( char const* rawChars, size_type size ) noexcept | ||||
|     :   m_start( rawChars ), | ||||
|   | ||||
| @@ -31,8 +31,6 @@ namespace Catch { | ||||
|          | ||||
|         StringData const* m_data = nullptr; | ||||
|          | ||||
|         static StringRef s_emptyStringRef; | ||||
|          | ||||
|         void takeOwnership(); | ||||
|          | ||||
|     public: // construction/ assignment | ||||
|   | ||||
| @@ -134,6 +134,10 @@ namespace Catch { | ||||
|             m_testAsFunction(); | ||||
|         } | ||||
|     }; | ||||
|     auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker* { | ||||
|         return new TestInvokerAsFunction( testAsFunction ); | ||||
|     } | ||||
|  | ||||
|  | ||||
|     inline std::string extractClassName( std::string const& classOrQualifiedMethodName ) { | ||||
|         std::string className = classOrQualifiedMethodName; | ||||
| @@ -148,40 +152,24 @@ namespace Catch { | ||||
|         return className; | ||||
|     } | ||||
|  | ||||
|     void registerTestCase | ||||
|         (   ITestInvoker* testCase, | ||||
|             char const* classOrQualifiedMethodName, | ||||
|             NameAndDesc const& nameAndDesc, | ||||
|             SourceLineInfo const& lineInfo ) { | ||||
|     /////////////////////////////////////////////////////////////////////////// | ||||
|  | ||||
|     AutoReg::AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags ) | ||||
|     { | ||||
|         try { | ||||
|             getMutableRegistryHub().registerTest | ||||
|             (makeTestCase | ||||
|             (testCase, | ||||
|              extractClassName(classOrQualifiedMethodName), | ||||
|              nameAndDesc.name, | ||||
|              nameAndDesc.description, | ||||
|              lineInfo)); | ||||
|             getMutableRegistryHub() | ||||
|                     .registerTest( | ||||
|                         makeTestCase( | ||||
|                             invoker, | ||||
|                             extractClassName( classOrMethod.c_str() ), | ||||
|                             nameAndTags.name.c_str(), | ||||
|                             nameAndTags.tags.c_str(), | ||||
|                             lineInfo)); | ||||
|         } catch (...) { | ||||
|             // Do not throw when constructing global objects, instead register the exception to be processed later | ||||
|             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() {} | ||||
|  | ||||
| } // end namespace Catch | ||||
|   | ||||
| @@ -11,6 +11,7 @@ | ||||
| #include "catch_common.h" | ||||
| #include "catch_interfaces_testcase.h" | ||||
| #include "catch_compiler_capabilities.h" | ||||
| #include "catch_stringref.h" | ||||
|  | ||||
| namespace Catch { | ||||
|  | ||||
| @@ -26,62 +27,33 @@ public: | ||||
|     } | ||||
| }; | ||||
|  | ||||
| typedef void(*TestFunction)(); | ||||
| auto makeTestInvoker( void(*testAsFunction)() ) -> ITestInvoker*; | ||||
|  | ||||
| struct NameAndDesc { | ||||
|     NameAndDesc( const char* _name = "", const char* _description= "" ) | ||||
|     : name( _name ), description( _description ) | ||||
|     {} | ||||
| template<typename C> | ||||
| auto makeTestInvoker( void (C::*testAsMethod)() ) -> ITestInvoker* { | ||||
|     return new TestInvokerAsMethod<C>( testAsMethod ); | ||||
| } | ||||
|  | ||||
|     const char* name; | ||||
|     const char* description; | ||||
| struct NameAndTags { | ||||
|  | ||||
|     NameAndTags( StringRef name_ = "", StringRef tags_ = "" ) : name( name_ ), tags( tags_ ) {} | ||||
|  | ||||
|     StringRef name; | ||||
|     StringRef tags; | ||||
| }; | ||||
|  | ||||
| void registerTestCase | ||||
|     (   ITestInvoker* testCase, | ||||
|         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 ); | ||||
|     } | ||||
|  | ||||
| struct AutoReg : NonCopyable { | ||||
|     AutoReg( ITestInvoker* invoker, SourceLineInfo const& lineInfo, StringRef classOrMethod, NameAndTags const& nameAndTags ); | ||||
|     ~AutoReg(); | ||||
|  | ||||
|     AutoReg( AutoReg const& ) = delete; | ||||
|     AutoReg& operator = (AutoReg const& ) = delete; | ||||
| }; | ||||
|  | ||||
| void registerTestCaseFunction | ||||
|     (   TestFunction function, | ||||
|         SourceLineInfo const& lineInfo, | ||||
|         NameAndDesc const& nameAndDesc ); | ||||
|  | ||||
| } // end namespace Catch | ||||
|  | ||||
|     /////////////////////////////////////////////////////////////////////////////// | ||||
|     #define INTERNAL_CATCH_TESTCASE2( TestName, ... ) \ | ||||
|         static void TestName(); \ | ||||
|         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 \ | ||||
|         static void TestName() | ||||
|     #define INTERNAL_CATCH_TESTCASE( ... ) \ | ||||
| @@ -90,7 +62,7 @@ void registerTestCaseFunction | ||||
|     /////////////////////////////////////////////////////////////////////////////// | ||||
|     #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, ... ) \ | ||||
|         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 | ||||
|  | ||||
|     /////////////////////////////////////////////////////////////////////////////// | ||||
| @@ -100,7 +72,7 @@ void registerTestCaseFunction | ||||
|             struct TestName : ClassName{ \ | ||||
|                 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 \ | ||||
|         void TestName::test() | ||||
| @@ -110,7 +82,7 @@ void registerTestCaseFunction | ||||
|     /////////////////////////////////////////////////////////////////////////////// | ||||
|     #define INTERNAL_CATCH_REGISTER_TESTCASE( Function, ... ) \ | ||||
|         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 | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash