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