catch_test_case_tracker.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Created by Phil Nash on 23/7/2013
  3. * Copyright 2013 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef TWOBLUECUBES_CATCH_TEST_CASE_TRACKER_HPP_INCLUDED
  9. #define TWOBLUECUBES_CATCH_TEST_CASE_TRACKER_HPP_INCLUDED
  10. #include "catch_compiler_capabilities.h"
  11. #include "catch_common.h"
  12. #include <string>
  13. #include <vector>
  14. #include <memory>
  15. namespace Catch {
  16. namespace TestCaseTracking {
  17. struct NameAndLocation {
  18. std::string name;
  19. SourceLineInfo location;
  20. NameAndLocation( std::string const& _name, SourceLineInfo const& _location );
  21. friend bool operator==(NameAndLocation const& lhs, NameAndLocation const& rhs) {
  22. return lhs.name == rhs.name
  23. && lhs.location == rhs.location;
  24. }
  25. };
  26. class ITracker;
  27. using ITrackerPtr = std::shared_ptr<ITracker>;
  28. class ITracker {
  29. NameAndLocation m_nameAndLocation;
  30. public:
  31. ITracker(NameAndLocation const& nameAndLoc) :
  32. m_nameAndLocation(nameAndLoc)
  33. {}
  34. // static queries
  35. NameAndLocation const& nameAndLocation() const {
  36. return m_nameAndLocation;
  37. }
  38. virtual ~ITracker();
  39. // dynamic queries
  40. virtual bool isComplete() const = 0; // Successfully completed or failed
  41. virtual bool isSuccessfullyCompleted() const = 0;
  42. virtual bool isOpen() const = 0; // Started but not complete
  43. virtual bool hasChildren() const = 0;
  44. virtual bool hasStarted() const = 0;
  45. virtual ITracker& parent() = 0;
  46. // actions
  47. virtual void close() = 0; // Successfully complete
  48. virtual void fail() = 0;
  49. virtual void markAsNeedingAnotherRun() = 0;
  50. virtual void addChild( ITrackerPtr const& child ) = 0;
  51. virtual ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) = 0;
  52. virtual void openChild() = 0;
  53. // Debug/ checking
  54. virtual bool isSectionTracker() const = 0;
  55. virtual bool isGeneratorTracker() const = 0;
  56. };
  57. class TrackerContext {
  58. enum RunState {
  59. NotStarted,
  60. Executing,
  61. CompletedCycle
  62. };
  63. ITrackerPtr m_rootTracker;
  64. ITracker* m_currentTracker = nullptr;
  65. RunState m_runState = NotStarted;
  66. public:
  67. ITracker& startRun();
  68. void endRun();
  69. void startCycle();
  70. void completeCycle();
  71. bool completedCycle() const;
  72. ITracker& currentTracker();
  73. void setCurrentTracker( ITracker* tracker );
  74. };
  75. class TrackerBase : public ITracker {
  76. protected:
  77. enum CycleState {
  78. NotStarted,
  79. Executing,
  80. ExecutingChildren,
  81. NeedsAnotherRun,
  82. CompletedSuccessfully,
  83. Failed
  84. };
  85. using Children = std::vector<ITrackerPtr>;
  86. TrackerContext& m_ctx;
  87. ITracker* m_parent;
  88. Children m_children;
  89. CycleState m_runState = NotStarted;
  90. public:
  91. TrackerBase( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent );
  92. bool isComplete() const override;
  93. bool isSuccessfullyCompleted() const override;
  94. bool isOpen() const override;
  95. bool hasChildren() const override;
  96. bool hasStarted() const override {
  97. return m_runState != NotStarted;
  98. }
  99. void addChild( ITrackerPtr const& child ) override;
  100. ITrackerPtr findChild( NameAndLocation const& nameAndLocation ) override;
  101. ITracker& parent() override;
  102. void openChild() override;
  103. bool isSectionTracker() const override;
  104. bool isGeneratorTracker() const override;
  105. void open();
  106. void close() override;
  107. void fail() override;
  108. void markAsNeedingAnotherRun() override;
  109. private:
  110. void moveToParent();
  111. void moveToThis();
  112. };
  113. class SectionTracker : public TrackerBase {
  114. std::vector<std::string> m_filters;
  115. std::string m_trimmed_name;
  116. public:
  117. SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent );
  118. bool isSectionTracker() const override;
  119. bool isComplete() const override;
  120. static SectionTracker& acquire( TrackerContext& ctx, NameAndLocation const& nameAndLocation );
  121. void tryOpen();
  122. void addInitialFilters( std::vector<std::string> const& filters );
  123. void addNextFilters( std::vector<std::string> const& filters );
  124. //! Returns filters active in this tracker
  125. std::vector<std::string> const& getFilters() const;
  126. //! Returns whitespace-trimmed name of the tracked section
  127. std::string const& trimmedName() const;
  128. };
  129. } // namespace TestCaseTracking
  130. using TestCaseTracking::ITracker;
  131. using TestCaseTracking::TrackerContext;
  132. using TestCaseTracking::SectionTracker;
  133. } // namespace Catch
  134. #endif // TWOBLUECUBES_CATCH_TEST_CASE_TRACKER_HPP_INCLUDED