blob: 2bda148cf6bde9fe142dcb54d5af68a343cac670 [file] [log] [blame]
Phil Nasha976c072012-05-05 19:32:52 +01001/*
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 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 {
17
18 enum RunStatus {
Phil Nasha976c072012-05-05 19:32:52 +010019 NothingRun,
20 EncounteredASection,
21 RanAtLeastOneSection,
22 RanToCompletionWithSections,
23 RanToCompletionWithNoSections
24 };
25
26 public:
Phil Nash06a671a2012-11-22 19:17:20 +000027 explicit RunningTest( const TestCase* info = NULL )
Phil Nasha976c072012-05-05 19:32:52 +010028 : m_info( info ),
29 m_runStatus( RanAtLeastOneSection ),
30 m_currentSection( &m_rootSection ),
31 m_changed( false )
Phil Nashc67a7ee2012-05-15 23:58:23 +010032 {}
Phil Nasha976c072012-05-05 19:32:52 +010033
Phil Nashc67a7ee2012-05-15 23:58:23 +010034 bool wasSectionSeen() const {
Phil Nasha976c072012-05-05 19:32:52 +010035 return m_runStatus == RanAtLeastOneSection ||
36 m_runStatus == RanToCompletionWithSections;
37 }
38
Phil Nasha70fbe32012-08-31 08:10:36 +010039 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 Nashc67a7ee2012-05-15 23:58:23 +010050 void reset() {
Phil Nasha976c072012-05-05 19:32:52 +010051 m_runStatus = NothingRun;
52 m_changed = false;
53 m_lastSectionToRun = NULL;
54 }
55
Phil Nashc67a7ee2012-05-15 23:58:23 +010056 void ranToCompletion() {
Phil Nasha976c072012-05-05 19:32:52 +010057 if( m_runStatus == RanAtLeastOneSection ||
Phil Nashc67a7ee2012-05-15 23:58:23 +010058 m_runStatus == EncounteredASection ) {
Phil Nasha976c072012-05-05 19:32:52 +010059 m_runStatus = RanToCompletionWithSections;
Phil Nashc67a7ee2012-05-15 23:58:23 +010060 if( m_lastSectionToRun ) {
Phil Nasha976c072012-05-05 19:32:52 +010061 m_lastSectionToRun->ranToCompletion();
62 m_changed = true;
63 }
64 }
Phil Nashc67a7ee2012-05-15 23:58:23 +010065 else {
Phil Nasha976c072012-05-05 19:32:52 +010066 m_runStatus = RanToCompletionWithNoSections;
67 }
68 }
69
Phil Nashc67a7ee2012-05-15 23:58:23 +010070 bool addSection( const std::string& name ) {
Phil Nasha976c072012-05-05 19:32:52 +010071 if( m_runStatus == NothingRun )
72 m_runStatus = EncounteredASection;
73
74 SectionInfo* thisSection = m_currentSection->findSubSection( name );
Phil Nashc67a7ee2012-05-15 23:58:23 +010075 if( !thisSection ) {
Phil Nasha976c072012-05-05 19:32:52 +010076 thisSection = m_currentSection->addSubSection( name );
77 m_changed = true;
78 }
79
Phil Nashc67a7ee2012-05-15 23:58:23 +010080 if( !wasSectionSeen() && thisSection->shouldRun() ) {
Phil Nasha976c072012-05-05 19:32:52 +010081 m_currentSection = thisSection;
82 m_lastSectionToRun = NULL;
83 return true;
84 }
85 return false;
86 }
87
Phil Nashc67a7ee2012-05-15 23:58:23 +010088 void endSection( const std::string& ) {
89 if( m_currentSection->ran() ) {
Phil Nasha976c072012-05-05 19:32:52 +010090 m_runStatus = RanAtLeastOneSection;
91 m_changed = true;
92 }
Phil Nashc67a7ee2012-05-15 23:58:23 +010093 else if( m_runStatus == EncounteredASection ) {
Phil Nasha976c072012-05-05 19:32:52 +010094 m_runStatus = RanAtLeastOneSection;
95 m_lastSectionToRun = m_currentSection;
Phil Nasha976c072012-05-05 19:32:52 +010096 }
97 m_currentSection = m_currentSection->getParent();
98 }
99
Phil Nash06a671a2012-11-22 19:17:20 +0000100 const TestCase& getTestCase() const {
Phil Nasha976c072012-05-05 19:32:52 +0100101 return *m_info;
102 }
103
Phil Nashc67a7ee2012-05-15 23:58:23 +0100104 bool hasUntestedSections() const {
Phil Nasha976c072012-05-05 19:32:52 +0100105 return m_runStatus == RanAtLeastOneSection ||
106 ( m_rootSection.hasUntestedSections() && m_changed );
107 }
108
109 private:
Phil Nash06a671a2012-11-22 19:17:20 +0000110 const TestCase* m_info;
Phil Nasha976c072012-05-05 19:32:52 +0100111 RunStatus m_runStatus;
112 SectionInfo m_rootSection;
113 SectionInfo* m_currentSection;
114 SectionInfo* m_lastSectionToRun;
115 bool m_changed;
116 };
117}
118
Matt Wozniskif29c8982012-09-17 01:42:29 -0400119#endif // TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED