This commit is contained in:
Phil Nash 2015-10-23 18:34:16 +01:00
parent 1cb993970a
commit 2c6411e70a

View File

@ -20,27 +20,27 @@
namespace Catch namespace Catch
{ {
struct IContainerPart;
struct ITrackedPart : SharedImpl<> { struct ITrackedPart : SharedImpl<> {
virtual ~ITrackedPart() {} virtual ~ITrackedPart() {}
// queries
virtual std::string name() const = 0; virtual std::string name() const = 0;
virtual bool isUnstarted() const = 0; virtual bool hasStarted() const = 0; // true even if ended
virtual bool isCompleteOrFailed() const = 0; virtual bool hasEnded() const = 0;
virtual bool isComplete() const = 0; virtual bool didCompleteSuccessfully() const = 0;
virtual bool isOpen() const = 0; virtual bool isOpen() const = 0;
virtual IContainerPart& parent() = 0; virtual ITrackedPart& parent() = 0;
// actions
virtual void close() = 0; virtual void close() = 0;
virtual void fail() = 0; virtual void fail() = 0;
};
struct IContainerPart : ITrackedPart {
virtual void addChild( Ptr<ITrackedPart> const& child ) = 0; virtual void addChild( Ptr<ITrackedPart> const& child ) = 0;
virtual ITrackedPart* findChild( std::string const& name ) = 0; virtual ITrackedPart* findChild( std::string const& name ) = 0;
virtual void openChild() = 0; virtual void openChild() = 0;
virtual void childFailed() = 0; virtual void childFailed() = 0;
}; };
@ -52,8 +52,8 @@ namespace Catch
CompletedCycle CompletedCycle
}; };
Ptr<IContainerPart> m_rootPartTracker; Ptr<ITrackedPart> m_rootPart;
IContainerPart* m_currentPart; ITrackedPart* m_currentPart;
RunState m_runState; RunState m_runState;
public: public:
@ -68,20 +68,17 @@ namespace Catch
m_runState( NotStarted ) m_runState( NotStarted )
{} {}
int i() {
return 42;
}
IContainerPart& startRun(); ITrackedPart& startRun();
void endRun() { void endRun() {
m_rootPartTracker.reset(); m_rootPart.reset();
m_currentPart = CATCH_NULL; m_currentPart = CATCH_NULL;
m_runState = NotStarted; m_runState = NotStarted;
} }
void startCycle() { void startCycle() {
m_currentPart = m_rootPartTracker.get(); m_currentPart = m_rootPart.get();
m_runState = Executing; m_runState = Executing;
} }
void completeCycle() { void completeCycle() {
@ -92,10 +89,10 @@ namespace Catch
return m_runState == CompletedCycle; return m_runState == CompletedCycle;
} }
IContainerPart& currentPart() { ITrackedPart& currentPart() {
return *m_currentPart; return *m_currentPart;
} }
void setCurrentPart( IContainerPart* part ) { void setCurrentPart( ITrackedPart* part ) {
m_currentPart = part; m_currentPart = part;
} }
@ -103,12 +100,10 @@ namespace Catch
return m_currentPart->findChild( name ); return m_currentPart->findChild( name );
} }
private:
}; };
class PartTracker : public IContainerPart { class SectionTracker : public ITrackedPart {
enum RunState { enum RunState {
NotStarted, NotStarted,
Executing, Executing,
@ -128,11 +123,11 @@ namespace Catch
typedef std::vector<Ptr<ITrackedPart> > Children; typedef std::vector<Ptr<ITrackedPart> > Children;
std::string m_name; std::string m_name;
TrackerContext& m_ctx; TrackerContext& m_ctx;
IContainerPart* m_parent; ITrackedPart* m_parent;
Children m_children; Children m_children;
RunState m_runState; RunState m_runState;
public: public:
PartTracker( std::string const& name, TrackerContext& ctx, IContainerPart* parent ) SectionTracker( std::string const& name, TrackerContext& ctx, ITrackedPart* parent )
: m_name( name ), : m_name( name ),
m_ctx( ctx ), m_ctx( ctx ),
m_parent( parent ), m_parent( parent ),
@ -142,14 +137,14 @@ namespace Catch
return m_name; return m_name;
} }
virtual bool isCompleteOrFailed() const CATCH_OVERRIDE { virtual bool hasEnded() const CATCH_OVERRIDE {
return m_runState == CompletedSuccessfully || m_runState == Failed; return m_runState == CompletedSuccessfully || m_runState == Failed;
} }
virtual bool isComplete() const CATCH_OVERRIDE { virtual bool didCompleteSuccessfully() const CATCH_OVERRIDE {
return m_runState == CompletedSuccessfully; return m_runState == CompletedSuccessfully;
} }
virtual bool isUnstarted() const CATCH_OVERRIDE { virtual bool hasStarted() const CATCH_OVERRIDE {
return m_runState == NotStarted; return m_runState != NotStarted;
} }
virtual bool isOpen() const CATCH_OVERRIDE { virtual bool isOpen() const CATCH_OVERRIDE {
return m_runState == Executing || m_runState == ExecutingChildren; return m_runState == Executing || m_runState == ExecutingChildren;
@ -167,7 +162,7 @@ namespace Catch
? it->get() ? it->get()
: CATCH_NULL; : CATCH_NULL;
} }
virtual IContainerPart& parent() CATCH_OVERRIDE { virtual ITrackedPart& parent() CATCH_OVERRIDE {
assert( m_parent ); // Should always be non-null except for root assert( m_parent ); // Should always be non-null except for root
return *m_parent; return *m_parent;
} }
@ -226,15 +221,15 @@ namespace Catch
} }
bool hasUnstartedChildren() const { bool hasUnstartedChildren() const {
return !m_children.empty() && m_children.back()->isUnstarted(); return !m_children.empty() && !m_children.back()->hasStarted();
} }
}; };
IContainerPart& TrackerContext::startRun() { ITrackedPart& TrackerContext::startRun() {
m_rootPartTracker = new PartTracker( "{root}", *this, CATCH_NULL ); m_rootPart = new SectionTracker( "{root}", *this, CATCH_NULL );
m_currentPart = CATCH_NULL; m_currentPart = CATCH_NULL;
m_runState = Executing; m_runState = Executing;
return *m_rootPartTracker; return *m_rootPart;
} }
class LocalContext { class LocalContext {
@ -245,16 +240,16 @@ namespace Catch
} }
}; };
class SectionPart : public PartTracker { class SectionPart : public SectionTracker {
public: public:
SectionPart( std::string const& name, TrackerContext& ctx, IContainerPart* parent ) SectionPart( std::string const& name, TrackerContext& ctx, ITrackedPart* parent )
: PartTracker( name, ctx, parent ) : SectionTracker( name, ctx, parent )
{} {}
static SectionPart& acquire( TrackerContext& ctx, std::string const& name ) { static SectionPart& acquire( TrackerContext& ctx, std::string const& name ) {
SectionPart* section = CATCH_NULL; SectionPart* section = CATCH_NULL;
IContainerPart& currentPart = ctx.currentPart(); ITrackedPart& currentPart = ctx.currentPart();
if( ITrackedPart* part = currentPart.findChild( name ) ) { if( ITrackedPart* part = currentPart.findChild( name ) ) {
section = dynamic_cast<SectionPart*>( part ); section = dynamic_cast<SectionPart*>( part );
assert( section ); assert( section );
@ -263,7 +258,7 @@ namespace Catch
section = new SectionPart( name, ctx, &currentPart ); section = new SectionPart( name, ctx, &currentPart );
currentPart.addChild( section ); currentPart.addChild( section );
} }
if( !ctx.completedCycle() && !section->isCompleteOrFailed() ) { if( !ctx.completedCycle() && !section->hasEnded() ) {
section->open(); section->open();
} }
return *section; return *section;
@ -284,7 +279,7 @@ using namespace Catch;
inline void testCase( Catch::LocalContext const& C_A_T_C_H_Context ) { inline void testCase( Catch::LocalContext const& C_A_T_C_H_Context ) {
REQUIRE( C_A_T_C_H_Context().i() == 42 ); // REQUIRE( C_A_T_C_H_Context().i() == 42 );
} }
TEST_CASE( "PartTracker" ) { TEST_CASE( "PartTracker" ) {
@ -294,53 +289,53 @@ TEST_CASE( "PartTracker" ) {
ctx.startCycle(); ctx.startCycle();
SectionPart& testCase = SectionPart::acquire( ctx, "Testcase" ); SectionPart& testCase = SectionPart::acquire( ctx, "Testcase" );
REQUIRE( testCase.isComplete() == false ); REQUIRE( testCase.didCompleteSuccessfully() == false );
SectionPart& s1 = SectionPart::acquire( ctx, "S1" ); SectionPart& s1 = SectionPart::acquire( ctx, "S1" );
REQUIRE( s1.isComplete() == false ); REQUIRE( s1.didCompleteSuccessfully() == false );
SECTION( "successfully close one section" ) { SECTION( "successfully close one section" ) {
s1.close(); s1.close();
REQUIRE( s1.isComplete() == true ); REQUIRE( s1.didCompleteSuccessfully() == true );
REQUIRE( testCase.isCompleteOrFailed() == false ); REQUIRE( testCase.hasEnded() == false );
testCase.close(); testCase.close();
REQUIRE( testCase.isComplete() == true ); REQUIRE( testCase.didCompleteSuccessfully() == true );
REQUIRE( ctx.completedCycle() == true ); REQUIRE( ctx.completedCycle() == true );
} }
SECTION( "fail one section" ) { SECTION( "fail one section" ) {
s1.fail(); s1.fail();
REQUIRE( s1.isComplete() == false ); REQUIRE( s1.didCompleteSuccessfully() == false );
REQUIRE( s1.isCompleteOrFailed() == true ); REQUIRE( s1.hasEnded() == true );
REQUIRE( testCase.isComplete() == false ); REQUIRE( testCase.didCompleteSuccessfully() == false );
REQUIRE( testCase.isCompleteOrFailed() == false ); REQUIRE( testCase.hasEnded() == false );
testCase.close(); testCase.close();
REQUIRE( ctx.completedCycle() == true ); REQUIRE( ctx.completedCycle() == true );
REQUIRE( testCase.isComplete() == false ); REQUIRE( testCase.didCompleteSuccessfully() == false );
SECTION( "re-enter after failed section" ) { SECTION( "re-enter after failed section" ) {
ctx.startCycle(); ctx.startCycle();
SectionPart& testCase2 = SectionPart::acquire( ctx, "Testcase" ); SectionPart& testCase2 = SectionPart::acquire( ctx, "Testcase" );
REQUIRE( testCase2.isComplete() == false ); REQUIRE( testCase2.didCompleteSuccessfully() == false );
SectionPart& s1b = SectionPart::acquire( ctx, "S1" ); SectionPart& s1b = SectionPart::acquire( ctx, "S1" );
REQUIRE( s1b.isComplete() == false ); REQUIRE( s1b.didCompleteSuccessfully() == false );
testCase2.close(); testCase2.close();
REQUIRE( ctx.completedCycle() == true ); REQUIRE( ctx.completedCycle() == true );
REQUIRE( testCase.isComplete() == true ); REQUIRE( testCase.didCompleteSuccessfully() == true );
REQUIRE( testCase.isCompleteOrFailed() == true ); REQUIRE( testCase.hasEnded() == true );
} }
SECTION( "re-enter after failed section and find next section" ) { SECTION( "re-enter after failed section and find next section" ) {
ctx.startCycle(); ctx.startCycle();
SectionPart& testCase2 = SectionPart::acquire( ctx, "Testcase" ); SectionPart& testCase2 = SectionPart::acquire( ctx, "Testcase" );
REQUIRE( testCase2.isComplete() == false ); REQUIRE( testCase2.didCompleteSuccessfully() == false );
SectionPart& s1b = SectionPart::acquire( ctx, "S1" ); SectionPart& s1b = SectionPart::acquire( ctx, "S1" );
REQUIRE( s1b.isComplete() == false ); REQUIRE( s1b.didCompleteSuccessfully() == false );
SectionPart& s2 = SectionPart::acquire( ctx, "S2" ); SectionPart& s2 = SectionPart::acquire( ctx, "S2" );
REQUIRE( s2.isOpen() ); REQUIRE( s2.isOpen() );
@ -348,8 +343,8 @@ TEST_CASE( "PartTracker" ) {
REQUIRE( ctx.completedCycle() == true ); REQUIRE( ctx.completedCycle() == true );
testCase2.close(); testCase2.close();
REQUIRE( testCase.isComplete() == true ); REQUIRE( testCase.didCompleteSuccessfully() == true );
REQUIRE( testCase.isCompleteOrFailed() == true ); REQUIRE( testCase.hasEnded() == true );
} }
} }
@ -358,22 +353,22 @@ TEST_CASE( "PartTracker" ) {
REQUIRE( ctx.completedCycle() == true ); REQUIRE( ctx.completedCycle() == true );
SectionPart& s2 = SectionPart::acquire( ctx, "S2" ); SectionPart& s2 = SectionPart::acquire( ctx, "S2" );
REQUIRE( s2.isComplete() == false ); REQUIRE( s2.didCompleteSuccessfully() == false );
REQUIRE( s2.isOpen() == false ); REQUIRE( s2.isOpen() == false );
testCase.close(); testCase.close();
REQUIRE( testCase.isComplete() == false ); REQUIRE( testCase.didCompleteSuccessfully() == false );
SECTION( "Re-enter - skip S1 and enter S2" ) { SECTION( "Re-enter - skip S1 and enter S2" ) {
ctx.startCycle(); ctx.startCycle();
SectionPart& testCase2 = SectionPart::acquire( ctx, "Testcase" ); SectionPart& testCase2 = SectionPart::acquire( ctx, "Testcase" );
REQUIRE( testCase2.isComplete() == false ); REQUIRE( testCase2.didCompleteSuccessfully() == false );
SectionPart& s1b = SectionPart::acquire( ctx, "S1" ); SectionPart& s1b = SectionPart::acquire( ctx, "S1" );
REQUIRE( s1b.isComplete() == true ); REQUIRE( s1b.didCompleteSuccessfully() == true );
SectionPart& s2b = SectionPart::acquire( ctx, "S2" ); SectionPart& s2b = SectionPart::acquire( ctx, "S2" );
REQUIRE( s2b.isComplete() == false ); REQUIRE( s2b.didCompleteSuccessfully() == false );
REQUIRE( s2b.isOpen() ); REQUIRE( s2b.isOpen() );
REQUIRE( ctx.completedCycle() == false ); REQUIRE( ctx.completedCycle() == false );
@ -382,22 +377,22 @@ TEST_CASE( "PartTracker" ) {
s2b.close(); s2b.close();
REQUIRE( ctx.completedCycle() == true ); REQUIRE( ctx.completedCycle() == true );
REQUIRE( s2b.isComplete() == true ); REQUIRE( s2b.didCompleteSuccessfully() == true );
REQUIRE( testCase2.isCompleteOrFailed() == false ); REQUIRE( testCase2.hasEnded() == false );
testCase2.close(); testCase2.close();
REQUIRE( testCase2.isComplete() == true ); REQUIRE( testCase2.didCompleteSuccessfully() == true );
} }
SECTION ("fail S2") { SECTION ("fail S2") {
s2b.fail(); s2b.fail();
REQUIRE( ctx.completedCycle() == true ); REQUIRE( ctx.completedCycle() == true );
REQUIRE( s2b.isComplete() == false ); REQUIRE( s2b.didCompleteSuccessfully() == false );
REQUIRE( s2b.isCompleteOrFailed() == true ); REQUIRE( s2b.hasEnded() == true );
REQUIRE( testCase2.isCompleteOrFailed() == false ); REQUIRE( testCase2.hasEnded() == false );
testCase2.close(); testCase2.close();
REQUIRE( testCase2.isComplete() == false ); REQUIRE( testCase2.didCompleteSuccessfully() == false );
} }
} }
} }
@ -407,15 +402,15 @@ TEST_CASE( "PartTracker" ) {
REQUIRE( s2.isOpen() == true ); REQUIRE( s2.isOpen() == true );
s2.close(); s2.close();
REQUIRE( s2.isComplete() == true ); REQUIRE( s2.didCompleteSuccessfully() == true );
REQUIRE( s1.isComplete() == false ); REQUIRE( s1.didCompleteSuccessfully() == false );
s1.close(); s1.close();
REQUIRE( s1.isComplete() == true ); REQUIRE( s1.didCompleteSuccessfully() == true );
REQUIRE( testCase.isComplete() == false ); REQUIRE( testCase.didCompleteSuccessfully() == false );
testCase.close(); testCase.close();
REQUIRE( testCase.isComplete() == true ); REQUIRE( testCase.didCompleteSuccessfully() == true );
} }