catch2/internal/catch_test_registry.hpp

97 lines
2.8 KiB
C++
Raw Normal View History

2010-11-10 00:24:00 +01:00
/*
2011-01-05 22:16:54 +01:00
* catch_test_registry.hpp
2010-11-10 00:24:00 +01:00
* Catch
*
* 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
2011-01-05 22:14:29 +01:00
#include "catch_testcase.hpp"
2010-11-10 00:24:00 +01:00
#include "catch_common.h"
namespace Catch
{
typedef void(*TestFunction)();
template<typename C>
struct MethodTestCase : ITestCase
2010-11-10 00:24:00 +01:00
{
MethodTestCase( void (C::*method)() )
: method( method )
{}
virtual void invoke() const
{
C obj;
(obj.*method)();
}
virtual ITestCase* clone() const
2010-11-10 00:24:00 +01:00
{
return new MethodTestCase<C>( method );
}
2010-11-14 23:47:30 +01:00
virtual bool operator == ( const ITestCase& other ) const
2010-11-14 23:47:30 +01:00
{
const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
return mtOther && method == mtOther->method;
}
virtual bool operator < ( const ITestCase& other ) const
2010-11-14 23:47:30 +01:00
{
const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
return mtOther && &method < &mtOther->method;
}
2010-11-10 00:24:00 +01:00
private:
void (C::*method)();
};
struct AutoReg
{
2011-01-07 11:01:40 +01:00
AutoReg( TestFunction function, const char* name, const char* description );
2010-11-10 00:24:00 +01:00
template<typename C>
AutoReg( void (C::*method)(), const char* name, const char* description )
2010-11-10 00:24:00 +01:00
{
2011-01-07 11:22:24 +01:00
registerTestCase( new MethodTestCase<C>( method ), name, description );
2010-11-10 00:24:00 +01:00
}
2011-01-07 11:01:40 +01:00
2011-01-07 11:22:24 +01:00
void registerTestCase( ITestCase* testCase, const char* name, const char* description );
2011-01-07 11:01:40 +01:00
~AutoReg();
private:
AutoReg( const AutoReg& );
void operator=( const AutoReg& );
2010-11-10 00:24:00 +01:00
};
template<typename T, size_t>
struct FixtureWrapper{};
2010-11-10 00:24:00 +01:00
} // end namespace Catch
#define INTERNAL_CATCH_TESTCASE( Name, Desc ) \
static void INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction )(); \
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction ), Name, Desc ); }\
static void INTERNAL_CATCH_UNIQUE_NAME( catch_internal_TestFunction )()
2010-11-10 00:24:00 +01:00
#define CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \
namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, Name, Desc ); }
2010-11-10 00:24:00 +01:00
#define TEST_CASE_METHOD( ClassName, TestName, Desc )\
namespace Catch{ template<> struct FixtureWrapper<ClassName, __LINE__> : ClassName \
{ \
void test(); \
}; }\
namespace { Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &Catch::FixtureWrapper<ClassName, __LINE__>::test, TestName, Desc ); } \
void Catch::FixtureWrapper<ClassName, __LINE__>::test()
2010-11-10 00:24:00 +01:00
#endif // TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED