catch2/include/internal/catch_test_case_tracker.hpp

188 lines
5.2 KiB
C++
Raw Normal View History

/*
* Created by Phil Nash on 23/7/2013
* Copyright 2013 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)
*/
#ifndef TWOBLUECUBES_CATCH_TEST_CASE_TRACKER_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_TEST_CASE_TRACKER_HPP_INCLUDED
#include "catch_compiler_capabilities.h"
2017-04-25 22:51:44 +02:00
#include "catch_common.h"
#include <string>
#include <vector>
2017-04-26 18:04:00 +02:00
#include <memory>
CATCH_INTERNAL_SUPPRESS_ETD_WARNINGS
namespace Catch {
namespace TestCaseTracking {
struct NameAndLocation {
std::string name;
SourceLineInfo location;
2017-07-19 10:13:47 +02:00
NameAndLocation( std::string const& _name, SourceLineInfo const& _location );
};
2017-04-25 21:57:43 +02:00
struct ITracker;
using ITrackerPtr = std::shared_ptr<ITracker>;
struct ITracker {
2017-07-19 10:13:47 +02:00
virtual ~ITracker() = default;
// static queries
virtual NameAndLocation const& nameAndLocation() const = 0;
// dynamic queries
virtual bool isComplete() const = 0; // Successfully completed or failed
virtual bool isSuccessfullyCompleted() const = 0;
virtual bool isOpen() const = 0; // Started but not complete
2015-11-02 20:21:46 +01:00
virtual bool hasChildren() const = 0;
virtual ITracker& parent() = 0;
// actions
virtual void close() = 0; // Successfully complete
virtual void fail() = 0;
virtual void markAsNeedingAnotherRun() = 0;
2017-04-25 21:57:43 +02:00
virtual void addChild( ITrackerPtr const& child ) = 0;
virtual ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) = 0;
virtual void openChild() = 0;
// Debug/ checking
virtual bool isSectionTracker() const = 0;
virtual bool isIndexTracker() const = 0;
};
class TrackerContext {
enum RunState {
NotStarted,
Executing,
CompletedCycle
};
2017-04-25 21:57:43 +02:00
ITrackerPtr m_rootTracker;
ITracker* m_currentTracker = nullptr;
RunState m_runState = NotStarted;
public:
2017-07-19 10:13:47 +02:00
static TrackerContext& instance();
ITracker& startRun();
2017-07-19 10:13:47 +02:00
void endRun();
2017-07-19 10:13:47 +02:00
void startCycle();
void completeCycle();
bool completedCycle() const;
ITracker& currentTracker();
void setCurrentTracker( ITracker* tracker );
};
class TrackerBase : public ITracker {
protected:
enum CycleState {
NotStarted,
Executing,
ExecutingChildren,
NeedsAnotherRun,
CompletedSuccessfully,
Failed
};
2017-07-19 10:13:47 +02:00
class TrackerHasName {
NameAndLocation m_nameAndLocation;
public:
2017-07-19 10:13:47 +02:00
TrackerHasName( NameAndLocation const& nameAndLocation );
bool operator ()( ITrackerPtr const& tracker ) const;
};
2017-07-19 10:13:47 +02:00
2017-04-25 21:57:43 +02:00
typedef std::vector<ITrackerPtr> Children;
NameAndLocation m_nameAndLocation;
TrackerContext& m_ctx;
ITracker* m_parent;
Children m_children;
CycleState m_runState = NotStarted;
2017-07-19 10:13:47 +02:00
public:
2017-07-19 10:13:47 +02:00
TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent );
NameAndLocation const& nameAndLocation() const override;
bool isComplete() const override;
bool isSuccessfullyCompleted() const override;
bool isOpen() const override;
bool hasChildren() const override;
2017-07-19 10:13:47 +02:00
void addChild( ITrackerPtr const& child ) override;
2017-07-19 10:13:47 +02:00
ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) override;
ITracker& parent() override;
2017-07-19 10:13:47 +02:00
void openChild() override;
2017-07-19 10:13:47 +02:00
bool isSectionTracker() const override;
bool isIndexTracker() const override;
2017-07-19 10:13:47 +02:00
void open();
void close() override;
void fail() override;
void markAsNeedingAnotherRun() override;
2017-07-19 10:13:47 +02:00
private:
2017-07-19 10:13:47 +02:00
void moveToParent();
void moveToThis();
};
class SectionTracker : public TrackerBase {
std::vector<std::string> m_filters;
public:
2017-07-19 10:13:47 +02:00
SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent );
bool isSectionTracker() const override;
2017-07-19 10:13:47 +02:00
static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation );
void tryOpen();
void addInitialFilters( std::vector<std::string> const& filters );
void addNextFilters( std::vector<std::string> const& filters );
};
class IndexTracker : public TrackerBase {
int m_size;
int m_index = -1;
public:
2017-07-19 10:13:47 +02:00
IndexTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent, int size );
bool isIndexTracker() const override;
void close() override;
2017-07-19 10:13:47 +02:00
static IndexTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation, int size );
int index() const;
void moveNext();
};
} // namespace TestCaseTracking
using TestCaseTracking::ITracker;
using TestCaseTracking::TrackerContext;
using TestCaseTracking::SectionTracker;
using TestCaseTracking::IndexTracker;
} // namespace Catch
CATCH_INTERNAL_UNSUPPRESS_ETD_WARNINGS
#endif // TWOBLUECUBES_CATCH_TEST_CASE_TRACKER_HPP_INCLUDED