mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
split test registry code out
This commit is contained in:
parent
430733c918
commit
95627c40cf
@ -13,6 +13,7 @@
|
||||
#include "../catch.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -11,6 +11,7 @@
|
||||
*/
|
||||
|
||||
#include "../catch.hpp"
|
||||
#include <iostream>
|
||||
|
||||
TEST_CASE( "succeeding/Misc/Sections", "random SECTION tests" )
|
||||
{
|
||||
|
@ -42,6 +42,7 @@
|
||||
4A992A6512B2156C002B7B66 /* catch_xmlwriter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = catch_xmlwriter.hpp; path = ../internal/catch_xmlwriter.hpp; sourceTree = SOURCE_ROOT; };
|
||||
4A992A6612B21582002B7B66 /* catch_reporter_junit.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = catch_reporter_junit.hpp; path = ../catch_reporter_junit.hpp; sourceTree = SOURCE_ROOT; };
|
||||
4AA7EA9112A438C7005A0B97 /* MiscTests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MiscTests.cpp; sourceTree = "<group>"; };
|
||||
4AD6775912D71DA0005AAF59 /* catch_test_case_registry_impl.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = catch_test_case_registry_impl.hpp; path = ../internal/catch_test_case_registry_impl.hpp; sourceTree = SOURCE_ROOT; };
|
||||
4AFC341512809A36003A0C29 /* catch_capture.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = catch_capture.hpp; path = ../internal/catch_capture.hpp; sourceTree = SOURCE_ROOT; };
|
||||
4AFC341612809A36003A0C29 /* catch_common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = catch_common.h; path = ../internal/catch_common.h; sourceTree = SOURCE_ROOT; };
|
||||
4AFC341712809A36003A0C29 /* catch_test_registry.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = catch_test_registry.hpp; path = ../internal/catch_test_registry.hpp; sourceTree = SOURCE_ROOT; };
|
||||
@ -128,6 +129,7 @@
|
||||
4A302E3912D5160400C84B67 /* Hub */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
4AD6775912D71DA0005AAF59 /* catch_test_case_registry_impl.hpp */,
|
||||
4A33BCE512CE7F500052A211 /* catch_hub.h */,
|
||||
4A33BF0D12CEAC0C0052A211 /* catch_hub_impl.hpp */,
|
||||
);
|
||||
|
@ -13,9 +13,10 @@
|
||||
#ifndef TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_RUNNER_HPP_INCLUDED
|
||||
|
||||
#include "internal/catch_hub_impl.hpp"
|
||||
|
||||
#include "internal/catch_commandline.hpp"
|
||||
#include "internal/catch_list.hpp"
|
||||
#include "internal/catch_hub_impl.hpp"
|
||||
#include "catch_reporter_basic.hpp"
|
||||
#include "catch_reporter_xml.hpp"
|
||||
#include "catch_reporter_junit.hpp"
|
||||
|
@ -9,45 +9,39 @@
|
||||
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
*
|
||||
*/
|
||||
#ifndef TWOBLUECUBES_CATCH_HUB_IMPL_HPP_INCLUDED
|
||||
#define TWOBLUECUBES_CATCH_HUB_IMPL_HPP_INCLUDED
|
||||
|
||||
#include "catch_hub.h"
|
||||
#include "catch_reporter_registry.hpp"a
|
||||
#include "catch_reporter_registry.hpp"
|
||||
#include "catch_test_case_registry_impl.hpp"
|
||||
|
||||
namespace Catch
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
Hub::Hub()
|
||||
Hub::Hub
|
||||
()
|
||||
: m_reporterRegistry( new ReporterRegistry ),
|
||||
m_testCaseRegistry( new TestRegistry )
|
||||
{
|
||||
}
|
||||
|
||||
Hub& Hub::me()
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
Hub& Hub::me
|
||||
()
|
||||
{
|
||||
static Hub hub;
|
||||
return hub;
|
||||
}
|
||||
|
||||
IReporterRegistry& Hub::getReporterRegistry()
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
IReporterRegistry& Hub::getReporterRegistry
|
||||
()
|
||||
{
|
||||
return *me().m_reporterRegistry.get();
|
||||
}
|
||||
|
||||
ITestCaseRegistry& Hub::getTestCaseRegistry()
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
ITestCaseRegistry& Hub::getTestCaseRegistry
|
||||
()
|
||||
{
|
||||
return *me().m_testCaseRegistry.get();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
AutoReg::AutoReg( TestFunction function, const char* name, const char* description )
|
||||
{
|
||||
Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new FreeFunctionTestCase( function ), name, description ) );
|
||||
}
|
||||
AutoReg::~AutoReg()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#endif // TWOBLUECUBES_CATCH_HUB_IMPL_HPP_INCLUDED
|
111
internal/catch_test_case_registry_impl.hpp
Normal file
111
internal/catch_test_case_registry_impl.hpp
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* catch_test_case_registry_impl.hpp
|
||||
* Catch
|
||||
*
|
||||
* Created by Phil on 7/1/2011
|
||||
* 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)
|
||||
*
|
||||
*/
|
||||
|
||||
#include "catch_test_registry.hpp"
|
||||
#include "catch_hub.h"
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
|
||||
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<TestCaseInfo>& getAllTests() const
|
||||
{
|
||||
return m_functionsInOrder;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::set<TestCaseInfo> m_functions;
|
||||
std::vector<TestCaseInfo> 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<const FreeFunctionTestCase*> ( &other );
|
||||
return ffOther && fun == ffOther->fun;
|
||||
}
|
||||
|
||||
virtual bool operator < ( const ITestCase& other ) const
|
||||
{
|
||||
const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
|
||||
return ffOther && fun < ffOther->fun;
|
||||
}
|
||||
|
||||
private:
|
||||
TestFunction fun;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
AutoReg::AutoReg
|
||||
(
|
||||
TestFunction function,
|
||||
const char* name,
|
||||
const char* description
|
||||
)
|
||||
{
|
||||
registerTestCase( new FreeFunctionTestCase( function ), name, description );
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
AutoReg::~AutoReg
|
||||
()
|
||||
{
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
void AutoReg::registerTestCase
|
||||
(
|
||||
ITestCase* testCase,
|
||||
const char* name,
|
||||
const char* description
|
||||
)
|
||||
{
|
||||
Hub::getTestCaseRegistry().registerTest( TestCaseInfo( testCase, name, description ) );
|
||||
}
|
||||
|
||||
} // end namespace Catch
|
||||
|
@ -15,72 +15,10 @@
|
||||
#include "catch_testcase.hpp"
|
||||
#include "catch_common.h"
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<TestCaseInfo>& getAllTests() const
|
||||
{
|
||||
return m_functionsInOrder;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::set<TestCaseInfo> m_functions;
|
||||
std::vector<TestCaseInfo> 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<const FreeFunctionTestCase*> ( &other );
|
||||
return ffOther && fun == ffOther->fun;
|
||||
}
|
||||
|
||||
virtual bool operator < ( const ITestCase& other ) const
|
||||
{
|
||||
const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
|
||||
return ffOther && fun < ffOther->fun;
|
||||
}
|
||||
|
||||
private:
|
||||
TestFunction fun;
|
||||
};
|
||||
|
||||
template<typename C>
|
||||
struct MethodTestCase : ITestCase
|
||||
{
|
||||
@ -122,9 +60,11 @@ struct AutoReg
|
||||
template<typename C>
|
||||
AutoReg( void (C::*method)(), const char* name, const char* description )
|
||||
{
|
||||
Hub::getTestCaseRegistry().registerTest( TestCaseInfo( new MethodTestCase<C>( method ), name, description ) );
|
||||
registerTestCase( new MethodTestCase<C>( method ), name, description );
|
||||
}
|
||||
|
||||
void registerTestCase( ITestCase* testCase, const char* name, const char* description );
|
||||
|
||||
~AutoReg();
|
||||
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user