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_config.h"
#include <memory>
#include <vector>
#include <stdlib.h>
namespace Catch {
class TestCaseInfo;
struct IResultCapture;
struct ITestCaseRegistry;
struct IExceptionTranslatorRegistry;
struct IExceptionTranslator;

View File

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

View File

@ -33,7 +33,7 @@
namespace Catch {
class OcMethod : public ITestCase {
class OcMethod : public SharedImpl<ITestCase> {
public:
OcMethod( Class cls, SEL sel ) : m_cls( cls ), m_sel( sel ) {}
@ -47,22 +47,9 @@ namespace Catch {
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:
virtual ~OcMethod() {}
Class m_cls;
SEL m_sel;
};

View File

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

View File

@ -68,31 +68,18 @@ namespace Catch {
///////////////////////////////////////////////////////////////////////////
class FreeFunctionTestCase : public ITestCase {
class FreeFunctionTestCase : public SharedImpl<ITestCase> {
public:
FreeFunctionTestCase( TestFunction fun ) : m_fun( fun ) {}
virtual ~FreeFunctionTestCase();
virtual void invoke() const {
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:
virtual ~FreeFunctionTestCase();
TestFunction m_fun;
};

View File

@ -14,7 +14,7 @@
namespace Catch {
template<typename C>
class MethodTestCase : public ITestCase {
class MethodTestCase : public SharedImpl<ITestCase> {
public:
MethodTestCase( void (C::*method)() ) : m_method( method ) {}
@ -24,21 +24,9 @@ public:
(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:
virtual ~MethodTestCase() {}
void (C::*m_method)();
};