1 /* 2 * Created by Phil on 08/11/2010. 3 * Copyright 2010 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 */ 8 #ifndef TWOBLUECUBES_CATCH_CONFIG_HPP_INCLUDED 9 #define TWOBLUECUBES_CATCH_CONFIG_HPP_INCLUDED 10 11 #include "catch_test_spec_parser.hpp" 12 #include "catch_context.h" 13 #include "catch_interfaces_config.h" 14 #include "catch_stream.h" 15 16 #include <memory> 17 #include <vector> 18 #include <string> 19 #include <stdexcept> 20 21 #ifndef CATCH_CONFIG_CONSOLE_WIDTH 22 #define CATCH_CONFIG_CONSOLE_WIDTH 80 23 #endif 24 25 namespace Catch { 26 27 struct ConfigData { 28 ConfigDataCatch::ConfigData29 ConfigData() 30 : listTests( false ), 31 listTags( false ), 32 listReporters( false ), 33 listTestNamesOnly( false ), 34 listExtraInfo( false ), 35 showSuccessfulTests( false ), 36 shouldDebugBreak( false ), 37 noThrow( false ), 38 showHelp( false ), 39 showInvisibles( false ), 40 filenamesAsTags( false ), 41 libIdentify( false ), 42 abortAfter( -1 ), 43 rngSeed( 0 ), 44 verbosity( Verbosity::Normal ), 45 warnings( WarnAbout::Nothing ), 46 showDurations( ShowDurations::DefaultForReporter ), 47 runOrder( RunTests::InDeclarationOrder ), 48 useColour( UseColour::Auto ), 49 waitForKeypress( WaitForKeypress::Never ) 50 {} 51 52 bool listTests; 53 bool listTags; 54 bool listReporters; 55 bool listTestNamesOnly; 56 bool listExtraInfo; 57 58 bool showSuccessfulTests; 59 bool shouldDebugBreak; 60 bool noThrow; 61 bool showHelp; 62 bool showInvisibles; 63 bool filenamesAsTags; 64 bool libIdentify; 65 66 int abortAfter; 67 unsigned int rngSeed; 68 69 Verbosity::Level verbosity; 70 WarnAbout::What warnings; 71 ShowDurations::OrNot showDurations; 72 RunTests::InWhatOrder runOrder; 73 UseColour::YesOrNo useColour; 74 WaitForKeypress::When waitForKeypress; 75 76 std::string outputFilename; 77 std::string name; 78 std::string processName; 79 80 std::vector<std::string> reporterNames; 81 std::vector<std::string> testsOrTags; 82 std::vector<std::string> sectionsToRun; 83 }; 84 85 86 class Config : public SharedImpl<IConfig> { 87 private: 88 Config( Config const& other ); 89 Config& operator = ( Config const& other ); 90 virtual void dummy(); 91 public: 92 Config()93 Config() 94 {} 95 Config(ConfigData const & data)96 Config( ConfigData const& data ) 97 : m_data( data ), 98 m_stream( openStream() ) 99 { 100 if( !data.testsOrTags.empty() ) { 101 TestSpecParser parser( ITagAliasRegistry::get() ); 102 for( std::size_t i = 0; i < data.testsOrTags.size(); ++i ) 103 parser.parse( data.testsOrTags[i] ); 104 m_testSpec = parser.testSpec(); 105 } 106 } 107 ~Config()108 virtual ~Config() {} 109 getFilename() const110 std::string const& getFilename() const { 111 return m_data.outputFilename ; 112 } 113 listTests() const114 bool listTests() const { return m_data.listTests; } listTestNamesOnly() const115 bool listTestNamesOnly() const { return m_data.listTestNamesOnly; } listTags() const116 bool listTags() const { return m_data.listTags; } listReporters() const117 bool listReporters() const { return m_data.listReporters; } listExtraInfo() const118 bool listExtraInfo() const { return m_data.listExtraInfo; } 119 getProcessName() const120 std::string getProcessName() const { return m_data.processName; } 121 getReporterNames() const122 std::vector<std::string> const& getReporterNames() const { return m_data.reporterNames; } getSectionsToRun() const123 std::vector<std::string> const& getSectionsToRun() const CATCH_OVERRIDE { return m_data.sectionsToRun; } 124 testSpec() const125 virtual TestSpec const& testSpec() const CATCH_OVERRIDE { return m_testSpec; } 126 showHelp() const127 bool showHelp() const { return m_data.showHelp; } 128 129 // IConfig interface allowThrows() const130 virtual bool allowThrows() const CATCH_OVERRIDE { return !m_data.noThrow; } stream() const131 virtual std::ostream& stream() const CATCH_OVERRIDE { return m_stream->stream(); } name() const132 virtual std::string name() const CATCH_OVERRIDE { return m_data.name.empty() ? m_data.processName : m_data.name; } includeSuccessfulResults() const133 virtual bool includeSuccessfulResults() const CATCH_OVERRIDE { return m_data.showSuccessfulTests; } warnAboutMissingAssertions() const134 virtual bool warnAboutMissingAssertions() const CATCH_OVERRIDE { return m_data.warnings & WarnAbout::NoAssertions; } showDurations() const135 virtual ShowDurations::OrNot showDurations() const CATCH_OVERRIDE { return m_data.showDurations; } runOrder() const136 virtual RunTests::InWhatOrder runOrder() const CATCH_OVERRIDE { return m_data.runOrder; } rngSeed() const137 virtual unsigned int rngSeed() const CATCH_OVERRIDE { return m_data.rngSeed; } useColour() const138 virtual UseColour::YesOrNo useColour() const CATCH_OVERRIDE { return m_data.useColour; } shouldDebugBreak() const139 virtual bool shouldDebugBreak() const CATCH_OVERRIDE { return m_data.shouldDebugBreak; } abortAfter() const140 virtual int abortAfter() const CATCH_OVERRIDE { return m_data.abortAfter; } showInvisibles() const141 virtual bool showInvisibles() const CATCH_OVERRIDE { return m_data.showInvisibles; } 142 143 private: 144 openStream()145 IStream const* openStream() { 146 if( m_data.outputFilename.empty() ) 147 return new CoutStream(); 148 else if( m_data.outputFilename[0] == '%' ) { 149 if( m_data.outputFilename == "%debug" ) 150 return new DebugOutStream(); 151 else 152 throw std::domain_error( "Unrecognised stream: " + m_data.outputFilename ); 153 } 154 else 155 return new FileStream( m_data.outputFilename ); 156 } 157 ConfigData m_data; 158 159 CATCH_AUTO_PTR( IStream const ) m_stream; 160 TestSpec m_testSpec; 161 }; 162 163 } // end namespace Catch 164 165 #endif // TWOBLUECUBES_CATCH_CONFIG_HPP_INCLUDED 166