Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 1 | /* |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 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 { |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 17 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 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 | }; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 25 | |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 26 | public: |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 27 | explicit RunningTest( TestCase const& 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 | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 34 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 35 | bool wasSectionSeen() const { |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 36 | return m_runStatus == RanAtLeastOneSection || |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 37 | m_runStatus == RanToCompletionWithSections; |
| 38 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 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 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 56 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 57 | void ranToCompletion() { |
Phil Nash | 3d6be03 | 2012-11-29 20:11:46 +0000 | [diff] [blame] | 58 | if( m_runStatus != RanAtLeastOneSection && m_runStatus != EncounteredASection ) |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 59 | m_runStatus = RanToCompletionWithNoSections; |
Phil Nash | 3d6be03 | 2012-11-29 20:11:46 +0000 | [diff] [blame] | 60 | m_runStatus = RanToCompletionWithSections; |
| 61 | if( m_lastSectionToRun ) { |
| 62 | m_lastSectionToRun->ranToCompletion(); |
| 63 | m_changed = true; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 64 | } |
| 65 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 66 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 67 | bool addSection( std::string const& name ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 68 | if( m_runStatus == NothingRun ) |
| 69 | m_runStatus = EncounteredASection; |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 70 | |
Phil Nash | 95df676 | 2012-11-29 20:31:17 +0000 | [diff] [blame] | 71 | RunningSection* thisSection = m_currentSection->findOrAddSubSection( name, m_changed ); |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 72 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 73 | if( !wasSectionSeen() && thisSection->shouldRun() ) { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 74 | m_currentSection = thisSection; |
| 75 | m_lastSectionToRun = NULL; |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
| 79 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 80 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 81 | void endSection( std::string const&, bool stealth ) { |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 82 | if( m_currentSection->ran() ) { |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 83 | if( !stealth ) |
| 84 | m_runStatus = RanAtLeastOneSection; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 85 | m_changed = true; |
| 86 | } |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 87 | else if( m_runStatus == EncounteredASection ) { |
Phil Nash | 767f158 | 2013-03-04 12:19:15 +0100 | [diff] [blame] | 88 | if( !stealth ) |
| 89 | m_runStatus = RanAtLeastOneSection; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 90 | m_lastSectionToRun = m_currentSection; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 91 | } |
| 92 | m_currentSection = m_currentSection->getParent(); |
| 93 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 94 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 95 | TestCase const& getTestCase() const { |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 96 | return m_info; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 97 | } |
Phil Nash | c4ba675 | 2012-11-29 09:05:51 +0000 | [diff] [blame] | 98 | |
Phil Nash | c67a7ee | 2012-05-15 23:58:23 +0100 | [diff] [blame] | 99 | bool hasUntestedSections() const { |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 100 | return m_runStatus == RanAtLeastOneSection || |
| 101 | ( m_rootSection.hasUntestedSections() && m_changed ); |
| 102 | } |
Phil Nash | f3d1f08 | 2013-07-03 19:14:59 +0100 | [diff] [blame^] | 103 | |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 104 | private: |
Phil Nash | 503d5d0 | 2013-07-03 08:25:11 +0100 | [diff] [blame] | 105 | RunningTest( RunningTest const& ); |
| 106 | void operator=( RunningTest const& ); |
Phil Nash | 2e3c5fa | 2013-03-25 08:46:48 +0000 | [diff] [blame] | 107 | |
Phil Nash | 2a9d8d9 | 2013-04-23 18:58:56 +0100 | [diff] [blame] | 108 | TestCase const& m_info; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 109 | RunStatus m_runStatus; |
Phil Nash | 95df676 | 2012-11-29 20:31:17 +0000 | [diff] [blame] | 110 | RunningSection m_rootSection; |
| 111 | RunningSection* m_currentSection; |
| 112 | RunningSection* m_lastSectionToRun; |
Phil Nash | a976c07 | 2012-05-05 19:32:52 +0100 | [diff] [blame] | 113 | bool m_changed; |
| 114 | }; |
| 115 | } |
| 116 | |
Matt Wozniski | f29c898 | 2012-09-17 01:42:29 -0400 | [diff] [blame] | 117 | #endif // TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED |