Catch/include/internal/catch_test_registry.hpp

103 lines
3.9 KiB
C++
Raw Normal View History

2010-11-09 18:24:00 -05:00
/*
* Created by Phil on 18/10/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED
#include "catch_common.h"
#include "catch_interfaces_testcase.h"
2010-11-09 18:24:00 -05:00
2012-05-16 03:02:20 -04:00
namespace Catch {
2010-11-09 18:24:00 -05:00
template<typename C>
2012-05-16 03:02:20 -04:00
class MethodTestCase : public ITestCase {
2012-05-10 16:46:46 -04:00
public:
MethodTestCase( void (C::*method)() ) : m_method( method ) {}
2010-11-09 18:24:00 -05:00
2012-05-10 16:46:46 -04:00
virtual void invoke() const {
2010-11-09 18:24:00 -05:00
C obj;
2011-01-31 05:10:20 -05:00
(obj.*m_method)();
2010-11-09 18:24:00 -05:00
}
2012-05-10 16:46:46 -04:00
virtual ITestCase* clone() const {
2011-01-31 05:10:20 -05:00
return new MethodTestCase<C>( m_method );
2010-11-09 18:24:00 -05:00
}
2010-11-14 17:47:30 -05:00
2012-05-10 16:46:46 -04:00
virtual bool operator == ( const ITestCase& other ) const {
2010-11-14 17:47:30 -05:00
const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
2011-01-31 05:10:20 -05:00
return mtOther && m_method == mtOther->m_method;
2010-11-14 17:47:30 -05:00
}
2012-05-10 16:46:46 -04:00
virtual bool operator < ( const ITestCase& other ) const {
2010-11-14 17:47:30 -05:00
const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
2011-01-31 05:10:20 -05:00
return mtOther && &m_method < &mtOther->m_method;
2010-11-14 17:47:30 -05:00
}
2010-11-09 18:24:00 -05:00
private:
2011-01-31 05:10:20 -05:00
void (C::*m_method)();
2010-11-09 18:24:00 -05:00
};
typedef void(*TestFunction)();
2010-11-09 18:24:00 -05:00
2012-05-16 03:02:20 -04:00
struct AutoReg {
2012-05-10 16:46:46 -04:00
AutoReg( TestFunction function,
const char* name,
const char* description,
const SourceLineInfo& lineInfo );
2010-11-09 18:24:00 -05:00
template<typename C>
2012-05-10 16:46:46 -04:00
AutoReg( void (C::*method)(),
const char* name,
const char* description,
const SourceLineInfo& lineInfo ) {
registerTestCase( new MethodTestCase<C>( method ), name, description, lineInfo );
2010-11-09 18:24:00 -05:00
}
2011-01-07 05:01:40 -05:00
2012-05-10 16:46:46 -04:00
void registerTestCase( ITestCase* testCase,
const char* name,
const char* description,
const SourceLineInfo& lineInfo );
2011-01-07 05:22:24 -05:00
2012-05-10 16:46:46 -04:00
~AutoReg();
2011-01-07 05:01:40 -05:00
private:
2012-05-10 16:46:46 -04:00
AutoReg( const AutoReg& );
void operator= ( const AutoReg& );
2010-11-09 18:24:00 -05:00
};
} // end namespace Catch
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_TESTCASE( Name, Desc ) \
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )(); \
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ ), Name, Desc, CATCH_INTERNAL_LINEINFO ); }\
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )()
2010-11-09 18:24:00 -05:00
///////////////////////////////////////////////////////////////////////////////
2011-01-31 15:15:40 -05:00
#define INTERNAL_CATCH_TESTCASE_NORETURN( Name, Desc ) \
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )() ATTRIBUTE_NORETURN; \
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ ), Name, Desc, CATCH_INTERNAL_LINEINFO ); }\
static void INTERNAL_CATCH_UNIQUE_NAME( TestCaseFunction_catch_internal_ )()
2011-01-31 15:15:40 -05:00
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, Name, Desc, CATCH_INTERNAL_LINEINFO ); }
2010-11-09 18:24:00 -05:00
///////////////////////////////////////////////////////////////////////////////
#define TEST_CASE_METHOD( ClassName, TestName, Desc )\
namespace{ \
struct INTERNAL_CATCH_UNIQUE_NAME( TestCaseMethod_catch_internal_ ) : ClassName{ \
void test(); \
}; \
Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseMethod_catch_internal_ )::test, TestName, Desc, CATCH_INTERNAL_LINEINFO ); \
} \
void INTERNAL_CATCH_UNIQUE_NAME( TestCaseMethod_catch_internal_ )::test()
2010-11-09 18:24:00 -05:00
#endif // TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED