Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Created by Phil Nash on 4/5/2012 |
| 3 | * Copyright 2012 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 | */ |
Matt Wozniski | f29c898 | 2012-09-17 01:42:29 -0400 | [diff] [blame] | 8 | #ifndef TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED |
| 9 | #define TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 10 | |
Phil Nash | ddfe963 | 2012-08-14 19:30:30 +0100 | [diff] [blame] | 11 | #include "catch_test_case_info.h" |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 12 | #include "catch_section_info.hpp" |
| 13 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 14 | namespace Catch { |
| 15 | |
| 16 | class RunningTest { |
| 17 | |
| 18 | enum RunStatus { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 19 | NothingRun, |
| 20 | EncounteredASection, |
| 21 | RanAtLeastOneSection, |
| 22 | RanToCompletionWithSections, |
| 23 | RanToCompletionWithNoSections |
| 24 | }; |
| 25 | |
| 26 | public: |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 27 | explicit RunningTest( const TestCase& info ) |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 28 | : m_info( info ), |
| 29 | m_runStatus( RanAtLeastOneSection ), |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 30 | m_rootSection( info.getTestCaseInfo().name ), |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 31 | m_currentSection( &m_rootSection ), |
| 32 | m_changed( false ) |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 33 | {} |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 34 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 35 | bool wasSectionSeen() const { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 36 | return m_runStatus == RanAtLeastOneSection || |
| 37 | m_runStatus == RanToCompletionWithSections; |
| 38 | } |
| 39 | |
Phil Nash | a70fbe3 | 2012-08-31 08:10:36 +0100 | [diff] [blame] | 40 | bool isBranchSection() const { |
| 41 | return m_currentSection && |
| 42 | m_currentSection->isBranch(); |
| 43 | } |
| 44 | |
| 45 | bool hasSections() const { |
| 46 | return m_runStatus == RanAtLeastOneSection || |
| 47 | m_runStatus == RanToCompletionWithSections || |
| 48 | m_runStatus == EncounteredASection; |
| 49 | } |
| 50 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 51 | void reset() { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 52 | m_runStatus = NothingRun; |
| 53 | m_changed = false; |
| 54 | m_lastSectionToRun = NULL; |
| 55 | } |
| 56 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 57 | void ranToCompletion() { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 58 | if( m_runStatus == RanAtLeastOneSection || |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 59 | m_runStatus == EncounteredASection ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 60 | m_runStatus = RanToCompletionWithSections; |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 61 | if( m_lastSectionToRun ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 62 | m_lastSectionToRun->ranToCompletion(); |
| 63 | m_changed = true; |
| 64 | } |
| 65 | } |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 66 | else { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 67 | m_runStatus = RanToCompletionWithNoSections; |
| 68 | } |
| 69 | } |
| 70 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 71 | bool addSection( const std::string& name ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 72 | if( m_runStatus == NothingRun ) |
| 73 | m_runStatus = EncounteredASection; |
| 74 | |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 75 | SectionInfo* thisSection = m_currentSection->findOrAddSubSection( name, m_changed ); |
| 76 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 77 | if( !wasSectionSeen() && thisSection->shouldRun() ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 78 | m_currentSection = thisSection; |
| 79 | m_lastSectionToRun = NULL; |
| 80 | return true; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 85 | void endSection( const std::string& ) { |
| 86 | if( m_currentSection->ran() ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 87 | m_runStatus = RanAtLeastOneSection; |
| 88 | m_changed = true; |
| 89 | } |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 90 | else if( m_runStatus == EncounteredASection ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 91 | m_runStatus = RanAtLeastOneSection; |
| 92 | m_lastSectionToRun = m_currentSection; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 93 | } |
| 94 | m_currentSection = m_currentSection->getParent(); |
| 95 | } |
| 96 | |
Phil Nash | 06a671a | 2012-11-22 19:17:20 +0000 | [diff] [blame] | 97 | const TestCase& getTestCase() const { |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 98 | return m_info; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 99 | } |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 100 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 101 | bool hasUntestedSections() const { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 102 | return m_runStatus == RanAtLeastOneSection || |
| 103 | ( m_rootSection.hasUntestedSections() && m_changed ); |
| 104 | } |
| 105 | |
| 106 | private: |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 107 | const TestCase& m_info; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 108 | RunStatus m_runStatus; |
| 109 | SectionInfo m_rootSection; |
| 110 | SectionInfo* m_currentSection; |
| 111 | SectionInfo* m_lastSectionToRun; |
| 112 | bool m_changed; |
| 113 | }; |
| 114 | } |
| 115 | |
Matt Wozniski | f29c898 | 2012-09-17 01:42:29 -0400 | [diff] [blame] | 116 | #endif // TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED |