mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 12:17:11 +01:00 
			
		
		
		
	Started adding support for isolated sections
This commit is contained in:
		| @@ -10,9 +10,10 @@ | |||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| #include "../internal/catch_self_test.hpp" |  | ||||||
| #include "../catch_with_main.hpp" | #include "../catch_with_main.hpp" | ||||||
|  |  | ||||||
|  | #include "../internal/catch_self_test.hpp" | ||||||
|  |  | ||||||
| TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" ) | TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" ) | ||||||
| { | { | ||||||
|     using namespace Catch; |     using namespace Catch; | ||||||
|   | |||||||
| @@ -48,6 +48,48 @@ namespace Catch | |||||||
|         bool m_isWildcarded; |         bool m_isWildcarded; | ||||||
|     }; |     }; | ||||||
|      |      | ||||||
|  |     class RunningTest | ||||||
|  |     { | ||||||
|  |     public: | ||||||
|  |         explicit RunningTest( const TestCaseInfo* info = NULL ) | ||||||
|  |         :   m_info( info ), | ||||||
|  |             m_sectionSeen( false ) | ||||||
|  |         { | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         size_t sectionsSeenCount() const | ||||||
|  |         { | ||||||
|  |             return m_sectionsSeen.size(); | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         bool wasSectionSeen() const | ||||||
|  |         { | ||||||
|  |             return m_sectionSeen; | ||||||
|  |         } | ||||||
|  |         void resetSectionSeen() | ||||||
|  |         { | ||||||
|  |             m_sectionSeen = false; | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         bool addSection( const std::string& name ) | ||||||
|  |         { | ||||||
|  |             if( m_sectionsSeen.find( name ) != m_sectionsSeen.end() ) | ||||||
|  |                 return false; | ||||||
|  |             m_sectionsSeen.insert( name ); | ||||||
|  |             return m_sectionSeen = true; | ||||||
|  |         } | ||||||
|  |         const TestCaseInfo& getTestCaseInfo() const | ||||||
|  |         { | ||||||
|  |             return *m_info; | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |     private: | ||||||
|  |         const TestCaseInfo* m_info; | ||||||
|  |         bool m_sectionSeen; | ||||||
|  |         std::set<std::string> m_sectionsSeen; | ||||||
|  |          | ||||||
|  |     }; | ||||||
|  |      | ||||||
|     class StreamRedirect |     class StreamRedirect | ||||||
|     { |     { | ||||||
|     public: |     public: | ||||||
| @@ -145,36 +187,16 @@ namespace Catch | |||||||
|             const TestCaseInfo& testInfo |             const TestCaseInfo& testInfo | ||||||
|         ) |         ) | ||||||
|         { |         { | ||||||
|             std::size_t prevSuccessCount = m_successes; |             m_runningTest = RunningTest( &testInfo ); | ||||||
|             std::size_t prevFailureCount = m_failures; |  | ||||||
|              |  | ||||||
|             m_reporter->StartTestCase( testInfo ); |  | ||||||
|              |  | ||||||
|             std::string redirectedCout; |  | ||||||
|             std::string redirectedCerr; |  | ||||||
|  |  | ||||||
|             try |             do | ||||||
|             { |             { | ||||||
|                 StreamRedirect coutRedir( std::cout, redirectedCout ); |                 m_runningTest.resetSectionSeen(); | ||||||
|                 StreamRedirect cerrRedir( std::cerr, redirectedCerr ); |                 runCurrentTest(); | ||||||
|                 testInfo.invoke();                 |  | ||||||
|             } |             } | ||||||
|             catch( TestFailureException& ) |             while( m_runningTest.wasSectionSeen() ); | ||||||
|             { |  | ||||||
|                 // This just means the test was aborted due to failure |             m_runningTest = RunningTest(); | ||||||
|             } |  | ||||||
|             catch( std::exception& ex ) |  | ||||||
|             { |  | ||||||
|                 acceptMessage( ex.what() ); |  | ||||||
|                 acceptResult( ResultWas::ThrewException ); |  | ||||||
|             } |  | ||||||
|             catch(...) |  | ||||||
|             { |  | ||||||
|                 acceptMessage( "unknown exception" ); |  | ||||||
|                 acceptResult( ResultWas::ThrewException ); |  | ||||||
|             } |  | ||||||
|             m_info.clear(); |  | ||||||
|             m_reporter->EndTestCase( testInfo, m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr ); |  | ||||||
|         } |         } | ||||||
|          |          | ||||||
|         /////////////////////////////////////////////////////////////////////////// |         /////////////////////////////////////////////////////////////////////////// | ||||||
| @@ -278,6 +300,9 @@ namespace Catch | |||||||
|             std::size_t& failures  |             std::size_t& failures  | ||||||
|         ) |         ) | ||||||
|         { |         { | ||||||
|  |             if( m_runningTest.wasSectionSeen() || !m_runningTest.addSection( name ) ) | ||||||
|  |                 return false; | ||||||
|  |  | ||||||
|             m_reporter->StartSection( name, description ); |             m_reporter->StartSection( name, description ); | ||||||
|             successes = m_successes; |             successes = m_successes; | ||||||
|             failures = m_failures; |             failures = m_failures; | ||||||
| @@ -323,8 +348,46 @@ namespace Catch | |||||||
|         { |         { | ||||||
|             return m_config.shouldDebugBreak(); |             return m_config.shouldDebugBreak(); | ||||||
|         } |         } | ||||||
|  |     private: | ||||||
|  |          | ||||||
|  |         /////////////////////////////////////////////////////////////////////////// | ||||||
|  |         void runCurrentTest | ||||||
|  |         () | ||||||
|  |         { | ||||||
|  |             std::size_t prevSuccessCount = m_successes; | ||||||
|  |             std::size_t prevFailureCount = m_failures; | ||||||
|  |              | ||||||
|  |             m_reporter->StartTestCase( m_runningTest.getTestCaseInfo() ); | ||||||
|  |              | ||||||
|  |             std::string redirectedCout; | ||||||
|  |             std::string redirectedCerr; | ||||||
|  |              | ||||||
|  |             try | ||||||
|  |             { | ||||||
|  |                 StreamRedirect coutRedir( std::cout, redirectedCout ); | ||||||
|  |                 StreamRedirect cerrRedir( std::cerr, redirectedCerr ); | ||||||
|  |                 m_runningTest.getTestCaseInfo().invoke();                 | ||||||
|  |             } | ||||||
|  |             catch( TestFailureException& ) | ||||||
|  |             { | ||||||
|  |                 // This just means the test was aborted due to failure | ||||||
|  |             } | ||||||
|  |             catch( std::exception& ex ) | ||||||
|  |             { | ||||||
|  |                 acceptMessage( ex.what() ); | ||||||
|  |                 acceptResult( ResultWas::ThrewException ); | ||||||
|  |             } | ||||||
|  |             catch(...) | ||||||
|  |             { | ||||||
|  |                 acceptMessage( "unknown exception" ); | ||||||
|  |                 acceptResult( ResultWas::ThrewException ); | ||||||
|  |             } | ||||||
|  |             m_info.clear(); | ||||||
|  |             m_reporter->EndTestCase( m_runningTest.getTestCaseInfo(), m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr ); | ||||||
|  |         } | ||||||
|          |          | ||||||
|     private: |     private: | ||||||
|  |         RunningTest m_runningTest; | ||||||
|         MutableResultInfo m_currentResult; |         MutableResultInfo m_currentResult; | ||||||
|  |  | ||||||
|         const Config& m_config; |         const Config& m_config; | ||||||
|   | |||||||
| @@ -30,7 +30,8 @@ namespace Catch | |||||||
|          |          | ||||||
|         ~Section() |         ~Section() | ||||||
|         { |         { | ||||||
|             Hub::getResultCapture().sectionEnded( m_name, m_successes, m_failures ); |             if( m_sectionIncluded ) | ||||||
|  |                 Hub::getResultCapture().sectionEnded( m_name, m_successes, m_failures ); | ||||||
|         } |         } | ||||||
|          |          | ||||||
|         // This indicates whether the section should be executed or not |         // This indicates whether the section should be executed or not | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash