2017-07-19 10:13:47 +02:00
|
|
|
/*
|
|
|
|
* Created by Martin on 19/07/2017
|
|
|
|
*
|
|
|
|
* 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-09-07 12:24:33 +02:00
|
|
|
#include "catch_test_case_tracker.h"
|
2017-07-19 10:13:47 +02:00
|
|
|
|
2017-08-01 18:46:33 +02:00
|
|
|
#include "catch_enforce.h"
|
2019-09-09 11:30:45 +02:00
|
|
|
#include "catch_string_manip.h"
|
2017-08-01 18:46:33 +02:00
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
#include <algorithm>
|
2018-04-18 14:59:10 +02:00
|
|
|
#include <cassert>
|
2017-07-19 10:13:47 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <memory>
|
2017-11-07 19:01:10 +01:00
|
|
|
#include <sstream>
|
2017-07-19 10:13:47 +02:00
|
|
|
|
2017-09-07 16:51:33 +02:00
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic push
|
|
|
|
# pragma clang diagnostic ignored "-Wexit-time-destructors"
|
|
|
|
#endif
|
2017-07-19 10:13:47 +02:00
|
|
|
|
|
|
|
namespace Catch {
|
|
|
|
namespace TestCaseTracking {
|
|
|
|
|
|
|
|
NameAndLocation::NameAndLocation( std::string const& _name, SourceLineInfo const& _location )
|
|
|
|
: name( _name ),
|
|
|
|
location( _location )
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2017-09-07 16:51:33 +02:00
|
|
|
ITracker::~ITracker() = default;
|
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
|
|
|
|
ITracker& TrackerContext::startRun() {
|
|
|
|
m_rootTracker = std::make_shared<SectionTracker>( NameAndLocation( "{root}", CATCH_INTERNAL_LINEINFO ), *this, nullptr );
|
|
|
|
m_currentTracker = nullptr;
|
|
|
|
m_runState = Executing;
|
|
|
|
return *m_rootTracker;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackerContext::endRun() {
|
|
|
|
m_rootTracker.reset();
|
|
|
|
m_currentTracker = nullptr;
|
|
|
|
m_runState = NotStarted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackerContext::startCycle() {
|
|
|
|
m_currentTracker = m_rootTracker.get();
|
|
|
|
m_runState = Executing;
|
|
|
|
}
|
|
|
|
void TrackerContext::completeCycle() {
|
|
|
|
m_runState = CompletedCycle;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TrackerContext::completedCycle() const {
|
|
|
|
return m_runState == CompletedCycle;
|
|
|
|
}
|
|
|
|
ITracker& TrackerContext::currentTracker() {
|
|
|
|
return *m_currentTracker;
|
|
|
|
}
|
|
|
|
void TrackerContext::setCurrentTracker( ITracker* tracker ) {
|
|
|
|
m_currentTracker = tracker;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TrackerBase::TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent )
|
|
|
|
: m_nameAndLocation( nameAndLocation ),
|
|
|
|
m_ctx( ctx ),
|
|
|
|
m_parent( parent )
|
|
|
|
{}
|
|
|
|
|
|
|
|
NameAndLocation const& TrackerBase::nameAndLocation() const {
|
|
|
|
return m_nameAndLocation;
|
|
|
|
}
|
|
|
|
bool TrackerBase::isComplete() const {
|
|
|
|
return m_runState == CompletedSuccessfully || m_runState == Failed;
|
|
|
|
}
|
|
|
|
bool TrackerBase::isSuccessfullyCompleted() const {
|
|
|
|
return m_runState == CompletedSuccessfully;
|
|
|
|
}
|
|
|
|
bool TrackerBase::isOpen() const {
|
|
|
|
return m_runState != NotStarted && !isComplete();
|
|
|
|
}
|
|
|
|
bool TrackerBase::hasChildren() const {
|
|
|
|
return !m_children.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TrackerBase::addChild( ITrackerPtr const& child ) {
|
|
|
|
m_children.push_back( child );
|
|
|
|
}
|
|
|
|
|
|
|
|
ITrackerPtr TrackerBase::findChild( NameAndLocation const& nameAndLocation ) {
|
2018-06-12 18:50:39 +02:00
|
|
|
auto it = std::find_if( m_children.begin(), m_children.end(),
|
|
|
|
[&nameAndLocation]( ITrackerPtr const& tracker ){
|
|
|
|
return
|
|
|
|
tracker->nameAndLocation().location == nameAndLocation.location &&
|
|
|
|
tracker->nameAndLocation().name == nameAndLocation.name;
|
|
|
|
} );
|
2017-07-19 10:13:47 +02:00
|
|
|
return( it != m_children.end() )
|
|
|
|
? *it
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
ITracker& TrackerBase::parent() {
|
|
|
|
assert( m_parent ); // Should always be non-null except for root
|
|
|
|
return *m_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackerBase::openChild() {
|
|
|
|
if( m_runState != ExecutingChildren ) {
|
|
|
|
m_runState = ExecutingChildren;
|
|
|
|
if( m_parent )
|
|
|
|
m_parent->openChild();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TrackerBase::isSectionTracker() const { return false; }
|
2019-01-27 19:46:28 +01:00
|
|
|
bool TrackerBase::isGeneratorTracker() const { return false; }
|
2017-07-19 10:13:47 +02:00
|
|
|
|
|
|
|
void TrackerBase::open() {
|
|
|
|
m_runState = Executing;
|
|
|
|
moveToThis();
|
|
|
|
if( m_parent )
|
|
|
|
m_parent->openChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackerBase::close() {
|
|
|
|
|
|
|
|
// Close any still open children (e.g. generators)
|
|
|
|
while( &m_ctx.currentTracker() != this )
|
|
|
|
m_ctx.currentTracker().close();
|
|
|
|
|
|
|
|
switch( m_runState ) {
|
|
|
|
case NeedsAnotherRun:
|
2017-07-25 17:16:28 +02:00
|
|
|
break;
|
2017-07-19 10:13:47 +02:00
|
|
|
|
|
|
|
case Executing:
|
|
|
|
m_runState = CompletedSuccessfully;
|
|
|
|
break;
|
|
|
|
case ExecutingChildren:
|
2019-06-29 18:02:28 +02:00
|
|
|
if( std::all_of(m_children.begin(), m_children.end(), [](ITrackerPtr const& t){ return t->isComplete(); }) )
|
2017-07-19 10:13:47 +02:00
|
|
|
m_runState = CompletedSuccessfully;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NotStarted:
|
|
|
|
case CompletedSuccessfully:
|
|
|
|
case Failed:
|
|
|
|
CATCH_INTERNAL_ERROR( "Illogical state: " << m_runState );
|
|
|
|
|
|
|
|
default:
|
|
|
|
CATCH_INTERNAL_ERROR( "Unknown state: " << m_runState );
|
|
|
|
}
|
|
|
|
moveToParent();
|
|
|
|
m_ctx.completeCycle();
|
|
|
|
}
|
|
|
|
void TrackerBase::fail() {
|
|
|
|
m_runState = Failed;
|
|
|
|
if( m_parent )
|
|
|
|
m_parent->markAsNeedingAnotherRun();
|
|
|
|
moveToParent();
|
|
|
|
m_ctx.completeCycle();
|
|
|
|
}
|
|
|
|
void TrackerBase::markAsNeedingAnotherRun() {
|
|
|
|
m_runState = NeedsAnotherRun;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackerBase::moveToParent() {
|
|
|
|
assert( m_parent );
|
|
|
|
m_ctx.setCurrentTracker( m_parent );
|
|
|
|
}
|
|
|
|
void TrackerBase::moveToThis() {
|
|
|
|
m_ctx.setCurrentTracker( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
SectionTracker::SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent )
|
2019-09-09 11:30:45 +02:00
|
|
|
: TrackerBase( nameAndLocation, ctx, parent ),
|
|
|
|
m_trimmed_name(trim(nameAndLocation.name))
|
2017-07-19 10:13:47 +02:00
|
|
|
{
|
|
|
|
if( parent ) {
|
|
|
|
while( !parent->isSectionTracker() )
|
|
|
|
parent = &parent->parent();
|
|
|
|
|
|
|
|
SectionTracker& parentSection = static_cast<SectionTracker&>( *parent );
|
|
|
|
addNextFilters( parentSection.m_filters );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 16:40:23 +01:00
|
|
|
bool SectionTracker::isComplete() const {
|
|
|
|
bool complete = true;
|
|
|
|
|
2019-09-09 11:30:45 +02:00
|
|
|
if ((m_filters.empty() || m_filters[0] == "")
|
|
|
|
|| std::find(m_filters.begin(), m_filters.end(), m_trimmed_name) != m_filters.end()) {
|
2019-01-07 16:40:23 +01:00
|
|
|
complete = TrackerBase::isComplete();
|
2019-09-09 11:30:45 +02:00
|
|
|
}
|
2019-01-07 16:40:23 +01:00
|
|
|
return complete;
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:13:47 +02:00
|
|
|
bool SectionTracker::isSectionTracker() const { return true; }
|
|
|
|
|
|
|
|
SectionTracker& SectionTracker::acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation ) {
|
|
|
|
std::shared_ptr<SectionTracker> section;
|
|
|
|
|
|
|
|
ITracker& currentTracker = ctx.currentTracker();
|
|
|
|
if( ITrackerPtr childTracker = currentTracker.findChild( nameAndLocation ) ) {
|
|
|
|
assert( childTracker );
|
|
|
|
assert( childTracker->isSectionTracker() );
|
|
|
|
section = std::static_pointer_cast<SectionTracker>( childTracker );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
section = std::make_shared<SectionTracker>( nameAndLocation, ctx, ¤tTracker );
|
|
|
|
currentTracker.addChild( section );
|
|
|
|
}
|
|
|
|
if( !ctx.completedCycle() )
|
|
|
|
section->tryOpen();
|
|
|
|
return *section;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SectionTracker::tryOpen() {
|
2019-09-09 11:30:45 +02:00
|
|
|
if( !isComplete() )
|
2017-07-19 10:13:47 +02:00
|
|
|
open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SectionTracker::addInitialFilters( std::vector<std::string> const& filters ) {
|
|
|
|
if( !filters.empty() ) {
|
2019-09-09 11:30:45 +02:00
|
|
|
m_filters.reserve( m_filters.size() + filters.size() + 2 );
|
2020-01-21 21:04:42 +01:00
|
|
|
m_filters.emplace_back(""); // Root - should never be consulted
|
|
|
|
m_filters.emplace_back(""); // Test Case - not a section filter
|
2017-07-19 10:13:47 +02:00
|
|
|
m_filters.insert( m_filters.end(), filters.begin(), filters.end() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void SectionTracker::addNextFilters( std::vector<std::string> const& filters ) {
|
|
|
|
if( filters.size() > 1 )
|
2019-09-09 11:30:45 +02:00
|
|
|
m_filters.insert( m_filters.end(), filters.begin()+1, filters.end() );
|
2017-07-19 10:13:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace TestCaseTracking
|
|
|
|
|
|
|
|
using TestCaseTracking::ITracker;
|
|
|
|
using TestCaseTracking::TrackerContext;
|
|
|
|
using TestCaseTracking::SectionTracker;
|
|
|
|
|
|
|
|
} // namespace Catch
|
|
|
|
|
2017-09-07 16:51:33 +02:00
|
|
|
#if defined(__clang__)
|
|
|
|
# pragma clang diagnostic pop
|
|
|
|
#endif
|