mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 12:17:11 +01:00 
			
		
		
		
	Implemented wait-for-keypress option
This commit is contained in:
		| @@ -142,7 +142,6 @@ namespace Catch { | |||||||
|                     << std::left << std::setw(16) << "version: " << libraryVersion() << std::endl; |                     << std::left << std::setw(16) << "version: " << libraryVersion() << std::endl; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |  | ||||||
|         int applyCommandLine( int argc, char const* const* const argv, OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) { |         int applyCommandLine( int argc, char const* const* const argv, OnUnusedOptions::DoWhat unusedOptionBehaviour = OnUnusedOptions::Fail ) { | ||||||
|             try { |             try { | ||||||
|                 m_cli.setThrowOnUnrecognisedTokens( unusedOptionBehaviour == OnUnusedOptions::Fail ); |                 m_cli.setThrowOnUnrecognisedTokens( unusedOptionBehaviour == OnUnusedOptions::Fail ); | ||||||
| @@ -207,6 +206,35 @@ namespace Catch { | |||||||
|     #endif |     #endif | ||||||
|  |  | ||||||
|         int run() { |         int run() { | ||||||
|  |             if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeStart ) != 0 ) { | ||||||
|  |                 Catch::cout() << "...waiting for enter/ return before starting" << std::endl; | ||||||
|  |                 std::getchar(); | ||||||
|  |             } | ||||||
|  |             int exitCode = runInternal(); | ||||||
|  |             if( ( m_configData.waitForKeypress & WaitForKeypress::BeforeExit ) != 0 ) { | ||||||
|  |                 Catch::cout() << "...waiting for enter/ return before exiting, with code: " << exitCode << std::endl; | ||||||
|  |                 std::getchar(); | ||||||
|  |             } | ||||||
|  |             return exitCode; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         Clara::CommandLine<ConfigData> const& cli() const { | ||||||
|  |             return m_cli; | ||||||
|  |         } | ||||||
|  |         std::vector<Clara::Parser::Token> const& unusedTokens() const { | ||||||
|  |             return m_unusedTokens; | ||||||
|  |         } | ||||||
|  |         ConfigData& configData() { | ||||||
|  |             return m_configData; | ||||||
|  |         } | ||||||
|  |         Config& config() { | ||||||
|  |             if( !m_config ) | ||||||
|  |                 m_config = new Config( m_configData ); | ||||||
|  |             return *m_config; | ||||||
|  |         } | ||||||
|  |     private: | ||||||
|  |  | ||||||
|  |         int runInternal() { | ||||||
|             if( m_configData.showHelp || m_configData.libIdentify ) |             if( m_configData.showHelp || m_configData.libIdentify ) | ||||||
|                 return 0; |                 return 0; | ||||||
|  |  | ||||||
| @@ -231,21 +259,6 @@ namespace Catch { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         Clara::CommandLine<ConfigData> const& cli() const { |  | ||||||
|             return m_cli; |  | ||||||
|         } |  | ||||||
|         std::vector<Clara::Parser::Token> const& unusedTokens() const { |  | ||||||
|             return m_unusedTokens; |  | ||||||
|         } |  | ||||||
|         ConfigData& configData() { |  | ||||||
|             return m_configData; |  | ||||||
|         } |  | ||||||
|         Config& config() { |  | ||||||
|             if( !m_config ) |  | ||||||
|                 m_config = new Config( m_configData ); |  | ||||||
|             return *m_config; |  | ||||||
|         } |  | ||||||
|     private: |  | ||||||
|         Clara::CommandLine<ConfigData> m_cli; |         Clara::CommandLine<ConfigData> m_cli; | ||||||
|         std::vector<Clara::Parser::Token> m_unusedTokens; |         std::vector<Clara::Parser::Token> m_unusedTokens; | ||||||
|         ConfigData m_configData; |         ConfigData m_configData; | ||||||
|   | |||||||
| @@ -76,6 +76,19 @@ namespace Catch { | |||||||
|         else |         else | ||||||
|             throw std::runtime_error( "colour mode must be one of: auto, yes or no" ); |             throw std::runtime_error( "colour mode must be one of: auto, yes or no" ); | ||||||
|     } |     } | ||||||
|  |     inline void setWaitForKeypress( ConfigData& config, std::string const& keypress ) { | ||||||
|  |         auto keypressLc = toLower( keypress ); | ||||||
|  |         if( keypressLc == "start" ) | ||||||
|  |             config.waitForKeypress = WaitForKeypress::BeforeStart; | ||||||
|  |         else if( keypressLc == "exit" ) | ||||||
|  |             config.waitForKeypress = WaitForKeypress::BeforeExit; | ||||||
|  |         else if( keypressLc == "both" ) | ||||||
|  |             config.waitForKeypress = WaitForKeypress::BeforeStartAndExit; | ||||||
|  |         else | ||||||
|  |             throw std::runtime_error( "keypress argument must be one of: start, exit or both. '" + keypress + "' not recognised" ); | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |  | ||||||
|     inline void forceColour( ConfigData& config ) { |     inline void forceColour( ConfigData& config ) { | ||||||
|         config.useColour = UseColour::Yes; |         config.useColour = UseColour::Yes; | ||||||
|     } |     } | ||||||
| @@ -219,6 +232,10 @@ namespace Catch { | |||||||
|             .describe( "report name and version according to libidentify standard" ) |             .describe( "report name and version according to libidentify standard" ) | ||||||
|             .bind( &ConfigData::libIdentify ); |             .bind( &ConfigData::libIdentify ); | ||||||
|  |  | ||||||
|  |         cli["--wait-for-keypress"] | ||||||
|  |                 .describe( "waits for a keypress before exiting" ) | ||||||
|  |                 .bind( &setWaitForKeypress, "start|exit|both" ); | ||||||
|  |  | ||||||
|         return cli; |         return cli; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -45,7 +45,8 @@ namespace Catch { | |||||||
|             warnings( WarnAbout::Nothing ), |             warnings( WarnAbout::Nothing ), | ||||||
|             showDurations( ShowDurations::DefaultForReporter ), |             showDurations( ShowDurations::DefaultForReporter ), | ||||||
|             runOrder( RunTests::InDeclarationOrder ), |             runOrder( RunTests::InDeclarationOrder ), | ||||||
|             useColour( UseColour::Auto ) |             useColour( UseColour::Auto ), | ||||||
|  |             waitForKeypress( WaitForKeypress::Never ) | ||||||
|         {} |         {} | ||||||
|  |  | ||||||
|         bool listTests; |         bool listTests; | ||||||
| @@ -70,6 +71,7 @@ namespace Catch { | |||||||
|         ShowDurations::OrNot showDurations; |         ShowDurations::OrNot showDurations; | ||||||
|         RunTests::InWhatOrder runOrder; |         RunTests::InWhatOrder runOrder; | ||||||
|         UseColour::YesOrNo useColour; |         UseColour::YesOrNo useColour; | ||||||
|  |         WaitForKeypress::When waitForKeypress; | ||||||
|  |  | ||||||
|         std::string outputFilename; |         std::string outputFilename; | ||||||
|         std::string name; |         std::string name; | ||||||
|   | |||||||
| @@ -42,6 +42,12 @@ namespace Catch { | |||||||
|         Yes, |         Yes, | ||||||
|         No |         No | ||||||
|     }; }; |     }; }; | ||||||
|  |     struct WaitForKeypress { enum When { | ||||||
|  |         Never, | ||||||
|  |         BeforeStart = 1, | ||||||
|  |         BeforeExit = 2, | ||||||
|  |         BeforeStartAndExit = BeforeStart | BeforeExit | ||||||
|  |     }; }; | ||||||
|  |  | ||||||
|     class TestSpec; |     class TestSpec; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash