blob: 6f0ec7bfa28fa20f2d382f94ebcf6480dcb37597 [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 Nashc4ba6752012-11-29 09:05:51 +000027 explicit RunningTest( const TestCase& 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 Nasha976c072012-05-05 19:32:52 +010034
Phil Nashc67a7ee2012-05-15 23:58:23 +010035 bool wasSectionSeen() const {
Phil Nasha976c072012-05-05 19:32:52 +010036 return m_runStatus == RanAtLeastOneSection ||
37 m_runStatus == RanToCompletionWithSections;
38 }
39
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 }
56
Phil Nashc67a7ee2012-05-15 23:58:23 +010057 void ranToCompletion() {
Phil Nasha976c072012-05-05 19:32:52 +010058 if( m_runStatus == RanAtLeastOneSection ||
Phil Nashc67a7ee2012-05-15 23:58:23 +010059 m_runStatus == EncounteredASection ) {
Phil Nasha976c072012-05-05 19:32:52 +010060 m_runStatus = RanToCompletionWithSections;
Phil Nashc67a7ee2012-05-15 23:58:23 +010061 if( m_lastSectionToRun ) {
Phil Nasha976c072012-05-05 19:32:52 +010062 m_lastSectionToRun->ranToCompletion();
63 m_changed = true;
64 }
65 }
Phil Nashc67a7ee2012-05-15 23:58:23 +010066 else {
Phil Nasha976c072012-05-05 19:32:52 +010067 m_runStatus = RanToCompletionWithNoSections;
68 }
69 }
70
Phil Nashc67a7ee2012-05-15 23:58:23 +010071 bool addSection( const std::string& name ) {
Phil Nasha976c072012-05-05 19:32:52 +010072 if( m_runStatus == NothingRun )
73 m_runStatus = EncounteredASection;
74
Phil Nashc4ba6752012-11-29 09:05:51 +000075 SectionInfo* thisSection = m_currentSection->findOrAddSubSection( name, m_changed );
76
Phil Nashc67a7ee2012-05-15 23:58:23 +010077 if( !wasSectionSeen() && thisSection->shouldRun() ) {
Phil Nasha976c072012-05-05 19:32:52 +010078 m_currentSection = thisSection;
79 m_lastSectionToRun = NULL;
80 return true;
81 }
82 return false;
83 }
84
Phil Nashc67a7ee2012-05-15 23:58:23 +010085 void endSection( const std::string& ) {
86 if( m_currentSection->ran() ) {
Phil Nasha976c072012-05-05 19:32:52 +010087 m_runStatus = RanAtLeastOneSection;
88 m_changed = true;
89 }
Phil Nashc67a7ee2012-05-15 23:58:23 +010090 else if( m_runStatus == EncounteredASection ) {
Phil Nasha976c072012-05-05 19:32:52 +010091 m_runStatus = RanAtLeastOneSection;
92 m_lastSectionToRun = m_currentSection;
Phil Nasha976c072012-05-05 19:32:52 +010093 }
94 m_currentSection = m_currentSection->getParent();
95 }
96
Phil Nash06a671a2012-11-22 19:17:20 +000097 const TestCase& getTestCase() const {
Phil Nashc4ba6752012-11-29 09:05:51 +000098 return m_info;
Phil Nasha976c072012-05-05 19:32:52 +010099 }
Phil Nashc4ba6752012-11-29 09:05:51 +0000100
Phil Nashc67a7ee2012-05-15 23:58:23 +0100101 bool hasUntestedSections() const {
Phil Nasha976c072012-05-05 19:32:52 +0100102 return m_runStatus == RanAtLeastOneSection ||
103 ( m_rootSection.hasUntestedSections() && m_changed );
104 }
105
106 private:
Phil Nashc4ba6752012-11-29 09:05:51 +0000107 const TestCase& m_info;
Phil Nasha976c072012-05-05 19:32:52 +0100108 RunStatus m_runStatus;
109 SectionInfo m_rootSection;
110 SectionInfo* m_currentSection;
111 SectionInfo* m_lastSectionToRun;
112 bool m_changed;
113 };
114}
115
Matt Wozniskif29c8982012-09-17 01:42:29 -0400116#endif // TWOBLUECUBES_CATCH_RUNNING_TEST_HPP_INCLUDED