Made ITestCase a shared object

This commit is contained in:
Phil Nash 2012-08-14 08:38:22 +01:00
parent a67d833091
commit 9c6ce97f01
6 changed files with 19 additions and 79 deletions

View File

@ -11,14 +11,11 @@
#include "catch_interfaces_reporter.h" #include "catch_interfaces_reporter.h"
#include "catch_interfaces_config.h" #include "catch_interfaces_config.h"
#include <memory>
#include <vector> #include <vector>
#include <stdlib.h>
namespace Catch { namespace Catch {
class TestCaseInfo; class TestCaseInfo;
struct IResultCapture;
struct ITestCaseRegistry; struct ITestCaseRegistry;
struct IExceptionTranslatorRegistry; struct IExceptionTranslatorRegistry;
struct IExceptionTranslator; struct IExceptionTranslator;

View File

@ -11,12 +11,10 @@
#include <vector> #include <vector>
namespace Catch { namespace Catch {
struct ITestCase { struct ITestCase : IShared {
virtual ~ITestCase();
virtual void invoke () const = 0; virtual void invoke () const = 0;
virtual ITestCase* clone() const = 0; protected:
virtual bool operator == ( const ITestCase& other ) const = 0; virtual ~ITestCase();
virtual bool operator < ( const ITestCase& other ) const = 0;
}; };
class TestCaseInfo; class TestCaseInfo;

View File

@ -33,7 +33,7 @@
namespace Catch { namespace Catch {
class OcMethod : public ITestCase { class OcMethod : public SharedImpl<ITestCase> {
public: public:
OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {} OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {}
@ -47,22 +47,9 @@ namespace Catch {
arcSafeRelease( obj ); arcSafeRelease( obj );
} }
virtual ITestCase* clone() const {
return new OcMethod( m_cls, m_sel );
}
virtual bool operator == ( const ITestCase& other ) const {
const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other );
return ocmOther && ocmOther->m_sel == m_sel;
}
virtual bool operator < ( const ITestCase& other ) const {
const OcMethod* ocmOther = dynamic_cast<const OcMethod*> ( &other );
return ocmOther && ocmOther->m_sel < m_sel;
}
private: private:
virtual ~OcMethod() {}
Class m_cls; Class m_cls;
SEL m_sel; SEL m_sel;
}; };

View File

@ -33,30 +33,13 @@ namespace Catch {
m_description() m_description()
{} {}
TestCaseInfo( const TestCaseInfo& other )
: m_test( other.m_test->clone() ),
m_name( other.m_name ),
m_description( other.m_description ),
m_lineInfo( other.m_lineInfo )
{}
TestCaseInfo( const TestCaseInfo& other, const std::string& name ) TestCaseInfo( const TestCaseInfo& other, const std::string& name )
: m_test( other.m_test->clone() ), : m_test( other.m_test ),
m_name( name ), m_name( name ),
m_description( other.m_description ), m_description( other.m_description ),
m_lineInfo( other.m_lineInfo ) m_lineInfo( other.m_lineInfo )
{} {}
TestCaseInfo& operator = ( const TestCaseInfo& other ) {
TestCaseInfo temp( other );
swap( temp );
return *this;
}
~TestCaseInfo() {
delete m_test;
}
void invoke() const { void invoke() const {
m_test->invoke(); m_test->invoke();
} }
@ -78,14 +61,14 @@ namespace Catch {
} }
void swap( TestCaseInfo& other ) { void swap( TestCaseInfo& other ) {
std::swap( m_test, other.m_test ); m_test.swap( other.m_test );
m_name.swap( other.m_name ); m_name.swap( other.m_name );
m_description.swap( other.m_description ); m_description.swap( other.m_description );
m_lineInfo.swap( other.m_lineInfo ); m_lineInfo.swap( other.m_lineInfo );
} }
bool operator == ( const TestCaseInfo& other ) const { bool operator == ( const TestCaseInfo& other ) const {
return *m_test == *other.m_test && m_name == other.m_name; return m_test.get() == other.m_test.get() && m_name == other.m_name;
} }
bool operator < ( const TestCaseInfo& other ) const { bool operator < ( const TestCaseInfo& other ) const {
@ -93,7 +76,7 @@ namespace Catch {
} }
private: private:
ITestCase* m_test; Ptr<ITestCase> m_test;
std::string m_name; std::string m_name;
std::string m_description; std::string m_description;
SourceLineInfo m_lineInfo; SourceLineInfo m_lineInfo;

View File

@ -68,31 +68,18 @@ namespace Catch {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
class FreeFunctionTestCase : public ITestCase { class FreeFunctionTestCase : public SharedImpl<ITestCase> {
public: public:
FreeFunctionTestCase( TestFunction fun ) : m_fun( fun ) {} FreeFunctionTestCase( TestFunction fun ) : m_fun( fun ) {}
virtual ~FreeFunctionTestCase();
virtual void invoke() const { virtual void invoke() const {
m_fun(); m_fun();
} }
virtual ITestCase* clone() const {
return new FreeFunctionTestCase( m_fun );
}
virtual bool operator == ( const ITestCase& other ) const {
const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
return ffOther && m_fun == ffOther->m_fun;
}
virtual bool operator < ( const ITestCase& other ) const {
const FreeFunctionTestCase* ffOther = dynamic_cast<const FreeFunctionTestCase*> ( &other );
return ffOther && m_fun < ffOther->m_fun;
}
private: private:
virtual ~FreeFunctionTestCase();
TestFunction m_fun; TestFunction m_fun;
}; };

View File

@ -14,7 +14,7 @@
namespace Catch { namespace Catch {
template<typename C> template<typename C>
class MethodTestCase : public ITestCase { class MethodTestCase : public SharedImpl<ITestCase> {
public: public:
MethodTestCase( void (C::*method)() ) : m_method( method ) {} MethodTestCase( void (C::*method)() ) : m_method( method ) {}
@ -24,21 +24,9 @@ public:
(obj.*m_method)(); (obj.*m_method)();
} }
virtual ITestCase* clone() const {
return new MethodTestCase<C>( m_method );
}
virtual bool operator == ( const ITestCase& other ) const {
const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
return mtOther && m_method == mtOther->m_method;
}
virtual bool operator < ( const ITestCase& other ) const {
const MethodTestCase* mtOther = dynamic_cast<const MethodTestCase*>( &other );
return mtOther && &m_method < &mtOther->m_method;
}
private: private:
virtual ~MethodTestCase() {}
void (C::*m_method)(); void (C::*m_method)();
}; };