blob: a8af1d9e5425fa958d55b9c60a563ba605854fa9 [file] [log] [blame]
Phil Nashf3d1f082013-07-03 19:14:59 +01001/*
Phil Nasha976c072012-05-05 19:32:52 +01002 * 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 Wozniskif29c8982012-09-17 01:42:29 -04008#ifndef TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED
9#define TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED
Phil Nasha976c072012-05-05 19:32:52 +010010
Phil Nashddfe9632012-08-14 19:30:30 +010011#include "catch_test_case_info.h"
Phil Nasha976c072012-05-05 19:32:52 +010012#include "catch_section_info.hpp"
13
Phil Nashc67a7ee2012-05-15 23:58:23 +010014namespace Catch {
15
16 class RunningTest {
Phil Nashf3d1f082013-07-03 19:14:59 +010017
Phil Nashc67a7ee2012-05-15 23:58:23 +010018 enum RunStatus {
Phil Nasha976c072012-05-05 19:32:52 +010019 NothingRun,
20 EncounteredASection,
21 RanAtLeastOneSection,
22 RanToCompletionWithSections,
23 RanToCompletionWithNoSections
24 };
Phil Nashf3d1f082013-07-03 19:14:59 +010025
Phil Nasha976c072012-05-05 19:32:52 +010026 public:
Phil Nash2a9d8d92013-04-23 18:58:56 +010027 explicit RunningTest( TestCase const& info )
Phil Nasha976c072012-05-05 19:32:52 +010028 : m_info( info ),
29 m_runStatus( RanAtLeastOneSection ),
Phil Nashc4ba6752012-11-29 09:05:51 +000030 m_rootSection( info.getTestCaseInfo().name ),
Phil Nasha976c072012-05-05 19:32:52 +010031 m_currentSection( &m_rootSection ),
32 m_changed( false )
Phil Nashc67a7ee2012-05-15 23:58:23 +010033 {}
Phil Nashf3d1f082013-07-03 19:14:59 +010034
Phil Nashc67a7ee2012-05-15 23:58:23 +010035 bool wasSectionSeen() const {
Phil Nashf3d1f082013-07-03 19:14:59 +010036 return m_runStatus == RanAtLeastOneSection ||
Phil Nasha976c072012-05-05 19:32:52 +010037 m_runStatus == RanToCompletionWithSections;
38 }
Phil Nashf3d1f082013-07-03 19:14:59 +010039
Phil Nasha70fbe32012-08-31 08:10:36 +010040 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 Nashc67a7ee2012-05-15 23:58:23 +010051 void reset() {
Phil Nasha976c072012-05-05 19:32:52 +010052 m_runStatus = NothingRun;
53 m_changed = false;
54 m_lastSectionToRun = NULL;
55 }
Phil Nashf3d1f082013-07-03 19:14:59 +010056
Phil Nashc67a7ee2012-05-15 23:58:23 +010057 void ranToCompletion() {
Phil Nash3d6be032012-11-29 20:11:46 +000058 if( m_runStatus != RanAtLeastOneSection && m_runStatus != EncounteredASection )
Phil Nasha976c072012-05-05 19:32:52 +010059 m_runStatus = RanToCompletionWithNoSections;
Phil Nash3d6be032012-11-29 20:11:46 +000060 m_runStatus = RanToCompletionWithSections;
61 if( m_lastSectionToRun ) {
62 m_lastSectionToRun->ranToCompletion();
63 m_changed = true;
Phil Nasha976c072012-05-05 19:32:52 +010064 }
65 }
Phil Nashf3d1f082013-07-03 19:14:59 +010066
Phil Nash2a9d8d92013-04-23 18:58:56 +010067 bool addSection( std::string const& name ) {
Phil Nasha976c072012-05-05 19:32:52 +010068 if( m_runStatus == NothingRun )
69 m_runStatus = EncounteredASection;
Phil Nashf3d1f082013-07-03 19:14:59 +010070
Phil Nash95df6762012-11-29 20:31:17 +000071 RunningSection* thisSection = m_currentSection->findOrAddSubSection( name, m_changed );
Phil Nashc4ba6752012-11-29 09:05:51 +000072
Phil Nashc67a7ee2012-05-15 23:58:23 +010073 if( !wasSectionSeen() && thisSection->shouldRun() ) {
Phil Nasha976c072012-05-05 19:32:52 +010074 m_currentSection = thisSection;
75 m_lastSectionToRun = NULL;
76 return true;
77 }
78 return false;
79 }
Phil Nashf3d1f082013-07-03 19:14:59 +010080
Phil Nash2a9d8d92013-04-23 18:58:56 +010081 void endSection( std::string const&, bool stealth ) {
Phil Nashc67a7ee2012-05-15 23:58:23 +010082 if( m_currentSection->ran() ) {
Phil Nash767f1582013-03-04 12:19:15 +010083 if( !stealth )
84 m_runStatus = RanAtLeastOneSection;
Phil Nasha976c072012-05-05 19:32:52 +010085 m_changed = true;
86 }
Phil Nashc67a7ee2012-05-15 23:58:23 +010087 else if( m_runStatus == EncounteredASection ) {
Phil Nash767f1582013-03-04 12:19:15 +010088 if( !stealth )
89 m_runStatus = RanAtLeastOneSection;
Phil Nasha976c072012-05-05 19:32:52 +010090 m_lastSectionToRun = m_currentSection;
Phil Nasha976c072012-05-05 19:32:52 +010091 }
92 m_currentSection = m_currentSection->getParent();
93 }
Phil Nashf3d1f082013-07-03 19:14:59 +010094
Phil Nash2a9d8d92013-04-23 18:58:56 +010095 TestCase const& getTestCase() const {
Phil Nashc4ba6752012-11-29 09:05:51 +000096 return m_info;
Phil Nasha976c072012-05-05 19:32:52 +010097 }
Phil Nashc4ba6752012-11-29 09:05:51 +000098
Phil Nashc67a7ee2012-05-15 23:58:23 +010099 bool hasUntestedSections() const {
Phil Nasha976c072012-05-05 19:32:52 +0100100 return m_runStatus == RanAtLeastOneSection ||
101 ( m_rootSection.hasUntestedSections() && m_changed );
102 }
Phil Nashf3d1f082013-07-03 19:14:59 +0100103
Phil Nasha976c072012-05-05 19:32:52 +0100104 private:
Phil Nash503d5d02013-07-03 08:25:11 +0100105 RunningTest( RunningTest const& );
106 void operator=( RunningTest const& );
Phil Nash2e3c5fa2013-03-25 08:46:48 +0000107
Phil Nash2a9d8d92013-04-23 18:58:56 +0100108 TestCase const& m_info;
Phil Nasha976c072012-05-05 19:32:52 +0100109 RunStatus m_runStatus;
Phil Nash95df6762012-11-29 20:31:17 +0000110 RunningSection m_rootSection;
111 RunningSection* m_currentSection;
112 RunningSection* m_lastSectionToRun;
Phil Nasha976c072012-05-05 19:32:52 +0100113 bool m_changed;
114 };
115}
116
Matt Wozniskif29c8982012-09-17 01:42:29 -0400117#endif // TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED