Files
Catch2/include/internal/catch_section_info.cpp
T

105 lines
2.5 KiB
C++
Raw Normal View History

2013-07-03 19:14:59 +01:00
/*
2012-05-05 19:32:52 +01:00
* Created by Phil Nash on 4/5/2012
* Copyright 2012 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)
*/
2017-02-12 13:34:55 +01:00
#include "catch_section_info.h"
2012-05-05 19:32:52 +01:00
#include <vector>
2012-05-15 23:58:23 +01:00
namespace Catch {
2012-11-29 20:31:17 +00:00
class RunningSection {
2012-05-05 19:32:52 +01:00
public:
2012-11-29 09:17:08 +00:00
2012-11-29 20:31:17 +00:00
typedef std::vector<RunningSection*> SubSections;
2013-07-03 19:14:59 +01:00
enum State {
2012-05-05 19:32:52 +01:00
Root,
Unknown,
Branch,
TestedBranch,
TestedLeaf
};
2013-07-03 19:14:59 +01:00
2013-04-23 18:58:56 +01:00
RunningSection( RunningSection* parent, std::string const& name )
: m_state( Unknown ),
m_parent( parent ),
m_name( name )
2012-05-15 23:58:23 +01:00
{}
2013-07-03 19:14:59 +01:00
2013-04-23 18:58:56 +01:00
RunningSection( std::string const& name )
: m_state( Root ),
2017-04-25 12:41:30 +02:00
m_parent( nullptr ),
m_name( name )
2012-05-15 23:58:23 +01:00
{}
2013-07-03 19:14:59 +01:00
2012-11-29 20:31:17 +00:00
~RunningSection() {
2012-11-29 09:17:08 +00:00
deleteAll( m_subSections );
2012-05-05 19:32:52 +01:00
}
2012-11-29 20:31:17 +00:00
std::string getName() const {
return m_name;
}
2012-05-15 23:58:23 +01:00
bool shouldRun() const {
return m_state < TestedBranch;
2012-05-05 19:32:52 +01:00
}
2013-07-03 19:14:59 +01:00
bool isBranch() const {
return m_state == Branch;
}
2012-11-29 20:31:17 +00:00
const RunningSection* getParent() const {
2012-05-05 19:32:52 +01:00
return m_parent;
}
2012-05-15 23:58:23 +01:00
bool hasUntestedSections() const {
if( m_state == Unknown )
2012-05-05 19:32:52 +01:00
return true;
for( auto subSection : m_subSections )
if( subSection->hasUntestedSections() )
2012-05-05 19:32:52 +01:00
return true;
return false;
}
// Mutable methods:
2012-11-29 20:31:17 +00:00
RunningSection* getParent() {
return m_parent;
}
2013-04-23 18:58:56 +01:00
RunningSection* findOrAddSubSection( std::string const& name, bool& changed ) {
for( auto subSection : m_subSections )
if( subSection->getName() == name )
return subSection;
2012-11-29 20:31:17 +00:00
RunningSection* subSection = new RunningSection( this, name );
2012-11-29 09:17:08 +00:00
m_subSections.push_back( subSection );
m_state = Branch;
changed = true;
return subSection;
}
bool ran() {
2012-11-29 20:11:46 +00:00
if( m_state >= Branch )
return false;
m_state = TestedLeaf;
return true;
}
void ranToCompletion() {
if( m_state == Branch && !hasUntestedSections() )
m_state = TestedBranch;
}
2012-05-05 19:32:52 +01:00
private:
State m_state;
2012-11-29 20:31:17 +00:00
RunningSection* m_parent;
std::string m_name;
2012-11-29 09:17:08 +00:00
SubSections m_subSections;
2012-05-05 19:32:52 +01:00
};
}