catch2/internal/catch_registry.hpp

149 lines
3.8 KiB
C++
Raw Normal View History

2010-11-10 00:24:00 +01:00
/*
* catch_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_testcaseinfo.hpp"
#include "catch_common.h"
#include <vector>
2010-11-14 23:47:30 +01:00
#include <set>
2010-11-10 00:24:00 +01:00
#include <stdexcept>
2010-11-14 23:47:30 +01:00
#include <iostream>
2010-11-10 00:24:00 +01:00
namespace Catch
{
class TestRegistry
{
public:
static TestRegistry& instance()
{
static TestRegistry reg;
return reg;
}
void registerTest( const TestCaseInfo& testInfo )
{
2010-11-14 23:47:30 +01:00
if( m_functions.find( testInfo ) == m_functions.end() )
{
m_functions.insert( testInfo );
m_functionsInOrder.push_back( testInfo );
}
2010-11-10 00:24:00 +01:00
}
std::vector<TestCaseInfo> getAllTests() const
{
2010-11-14 23:47:30 +01:00
return m_functionsInOrder;
2010-11-10 00:24:00 +01:00
}
private:
2010-11-14 23:47:30 +01:00
std::set<TestCaseInfo> m_functions;
std::vector<TestCaseInfo> m_functionsInOrder;
2010-11-10 00:24:00 +01:00
};
typedef void(*TestFunction)();
struct FreeFunctionTestCase : TestCase
{
FreeFunctionTestCase( TestFunction fun )
: fun( fun )
{}
virtual void invoke() const
{
fun();
}
virtual TestCase* clone() const
{
return new FreeFunctionTestCase( fun );
}
2010-11-14 23:47:30 +01:00
virtual bool operator == ( const TestCase& other ) const
{
const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
return ffOther && fun == ffOther->fun;
}
virtual bool operator < ( const TestCase& other ) const
{
const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
return ffOther && fun < ffOther->fun;
}
2010-11-10 00:24:00 +01:00
private:
TestFunction fun;
};
template<typename C>
struct MethodTestCase : TestCase
{
MethodTestCase( void (C::*method)() )
: method( method )
{}
virtual void invoke() const
{
C obj;
(obj.*method)();
}
virtual TestCase* clone() const
{
return new MethodTestCase<C>( method );
}
2010-11-14 23:47:30 +01:00
virtual bool operator == ( const TestCase& other ) const
{
const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
return mtOther && method == mtOther->method;
}
virtual bool operator < ( const TestCase& other ) const
{
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
{
AutoReg( TestFunction function, const std::string& name, const std::string& description )
{
TestRegistry::instance().registerTest( TestCaseInfo( new FreeFunctionTestCase( function ), name, description ) );
}
template<typename C>
AutoReg( void (C::*method)(), const std::string& name, const std::string& description )
{
TestRegistry::instance().registerTest( TestCaseInfo( new MethodTestCase<C>( method ), name, description ) );
}
};
} // 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
#endif // TWOBLUECUBES_CATCH_REGISTRY_HPP_INCLUDED