catch_run_context.hpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 #ifndef CATCH_RUN_CONTEXT_HPP_INCLUDED 9 #define CATCH_RUN_CONTEXT_HPP_INCLUDED 10 11 #include <catch2/interfaces/catch_interfaces_capture.hpp> 12 #include <catch2/internal/catch_test_registry.hpp> 13 #include <catch2/internal/catch_test_run_info.hpp> 14 #include <catch2/internal/catch_fatal_condition_handler.hpp> 15 #include <catch2/catch_test_case_info.hpp> 16 #include <catch2/catch_message.hpp> 17 #include <catch2/catch_totals.hpp> 18 #include <catch2/internal/catch_test_case_tracker.hpp> 19 #include <catch2/catch_assertion_info.hpp> 20 #include <catch2/catch_assertion_result.hpp> 21 #include <catch2/internal/catch_optional.hpp> 22 #include <catch2/internal/catch_move_and_forward.hpp> 23 24 #include <string> 25 26 namespace Catch { 27 28 class IGeneratorTracker; 29 class IConfig; 30 class IEventListener; 31 using IEventListenerPtr = Detail::unique_ptr<IEventListener>; 32 33 /////////////////////////////////////////////////////////////////////////// 34 35 class RunContext final : public IResultCapture { 36 37 public: 38 RunContext( RunContext const& ) = delete; 39 RunContext& operator =( RunContext const& ) = delete; 40 41 explicit RunContext( IConfig const* _config, IEventListenerPtr&& reporter ); 42 43 ~RunContext() override; 44 45 Totals runTest(TestCaseHandle const& testCase); 46 47 public: // IResultCapture 48 49 // Assertion handlers 50 void handleExpr 51 ( AssertionInfo const& info, 52 ITransientExpression const& expr, 53 AssertionReaction& reaction ) override; 54 void handleMessage 55 ( AssertionInfo const& info, 56 ResultWas::OfType resultType, 57 StringRef message, 58 AssertionReaction& reaction ) override; 59 void handleUnexpectedExceptionNotThrown 60 ( AssertionInfo const& info, 61 AssertionReaction& reaction ) override; 62 void handleUnexpectedInflightException 63 ( AssertionInfo const& info, 64 std::string&& message, 65 AssertionReaction& reaction ) override; 66 void handleIncomplete 67 ( AssertionInfo const& info ) override; 68 void handleNonExpr 69 ( AssertionInfo const &info, 70 ResultWas::OfType resultType, 71 AssertionReaction &reaction ) override; 72 73 void notifyAssertionStarted( AssertionInfo const& info ) override; 74 bool sectionStarted( StringRef sectionName, 75 SourceLineInfo const& sectionLineInfo, 76 Counts& assertions ) override; 77 78 void sectionEnded( SectionEndInfo&& endInfo ) override; 79 void sectionEndedEarly( SectionEndInfo&& endInfo ) override; 80 81 IGeneratorTracker* 82 acquireGeneratorTracker( StringRef generatorName, 83 SourceLineInfo const& lineInfo ) override; 84 IGeneratorTracker* createGeneratorTracker( 85 StringRef generatorName, 86 SourceLineInfo lineInfo, 87 Generators::GeneratorBasePtr&& generator ) override; 88 89 90 void benchmarkPreparing( StringRef name ) override; 91 void benchmarkStarting( BenchmarkInfo const& info ) override; 92 void benchmarkEnded( BenchmarkStats<> const& stats ) override; 93 void benchmarkFailed( StringRef error ) override; 94 95 void pushScopedMessage( MessageInfo const& message ) override; 96 void popScopedMessage( MessageInfo const& message ) override; 97 98 void emplaceUnscopedMessage( MessageBuilder&& builder ) override; 99 100 std::string getCurrentTestName() const override; 101 102 const AssertionResult* getLastResult() const override; 103 104 void exceptionEarlyReported() override; 105 106 void handleFatalErrorCondition( StringRef message ) override; 107 108 bool lastAssertionPassed() override; 109 110 void assertionPassed() override; 111 112 public: 113 // !TBD We need to do this another way! 114 bool aborting() const; 115 116 private: 117 118 void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ); 119 void invokeActiveTestCase(); 120 121 void resetAssertionInfo(); 122 bool testForMissingAssertions( Counts& assertions ); 123 124 void assertionEnded( AssertionResult&& result ); 125 void reportExpr 126 ( AssertionInfo const &info, 127 ResultWas::OfType resultType, 128 ITransientExpression const *expr, 129 bool negated ); 130 131 void populateReaction( AssertionReaction& reaction ); 132 133 private: 134 135 void handleUnfinishedSections(); 136 137 TestRunInfo m_runInfo; 138 TestCaseHandle const* m_activeTestCase = nullptr; 139 ITracker* m_testCaseTracker = nullptr; 140 Optional<AssertionResult> m_lastResult; 141 142 IConfig const* m_config; 143 Totals m_totals; 144 IEventListenerPtr m_reporter; 145 std::vector<MessageInfo> m_messages; 146 std::vector<ScopedMessage> m_messageScopes; /* Keeps owners of so-called unscoped messages. */ 147 AssertionInfo m_lastAssertionInfo; 148 std::vector<SectionEndInfo> m_unfinishedSections; 149 std::vector<ITracker*> m_activeSections; 150 TrackerContext m_trackerContext; 151 FatalConditionHandler m_fatalConditionhandler; 152 bool m_lastAssertionPassed = false; 153 bool m_shouldReportUnexpected = true; 154 bool m_includeSuccessfulResults; 155 }; 156 157 void seedRng(IConfig const& config); 158 unsigned int rngSeed(); 159 } // end namespace Catch 160 161 #endif // CATCH_RUN_CONTEXT_HPP_INCLUDED