/* * catch_test_registry.hpp * 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 #include "catch_testcase.hpp" #include "catch_common.h" #include #include #include #include namespace Catch { class TestRegistry : public ITestCaseRegistry { public: virtual void registerTest( const TestCaseInfo& testInfo ) { if( m_functions.find( testInfo ) == m_functions.end() ) { m_functions.insert( testInfo ); m_functionsInOrder.push_back( testInfo ); } } virtual const std::vector& getAllTests() const { return m_functionsInOrder; } private: std::set m_functions; std::vector m_functionsInOrder; }; typedef void(*TestFunction)(); struct FreeFunctionTestCase : ITestCase { FreeFunctionTestCase( TestFunction fun ) : fun( fun ) {} virtual void invoke() const { fun(); } virtual ITestCase* clone() const { return new FreeFunctionTestCase( fun ); } virtual bool operator == ( const ITestCase& other ) const { const FreeFunctionTestCase* ffOther = dynamic_cast ( &other ); return ffOther && fun == ffOther->fun; } virtual bool operator < ( const ITestCase& other ) const { const FreeFunctionTestCase* ffOther = dynamic_cast ( &other ); return ffOther && fun < ffOther->fun; } private: TestFunction fun; }; template struct MethodTestCase : ITestCase { MethodTestCase( void (C::*method)() ) : method( method ) {} virtual void invoke() const { C obj; (obj.*method)(); } virtual ITestCase* clone() const { return new MethodTestCase( method ); } virtual bool operator == ( const ITestCase& other ) const { const MethodTestCase* mtOther = dynamic_cast( &other ); return mtOther && method == mtOther->method; } virtual bool operator < ( const ITestCase& other ) const { const MethodTestCase* mtOther = dynamic_cast( &other ); return mtOther && &method < &mtOther->method; } private: void (C::*method)(); }; struct AutoReg { AutoReg( TestFunction function, const char* name, const char* description ); template AutoReg( void (C::*method)(), const char* name, const char* description ) { Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new MethodTestCase( method ), name, description ) ); } ~AutoReg(); private: AutoReg( const AutoReg& ); void operator=( const AutoReg& ); }; template struct FixtureWrapper{}; } // 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 )() #define CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \ namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, Name, Desc ); } #define TEST_CASE_METHOD( ClassName, TestName, Desc )\ namespace Catch{ template<> struct FixtureWrapper : ClassName \ { \ void test(); \ }; }\ namespace { Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &Catch::FixtureWrapper::test, TestName, Desc ); } \ void Catch::FixtureWrapper::test() #endif // TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED