Fixed alternate stream bugs

This commit is contained in:
Phil Nash 2012-09-26 18:36:58 +01:00
parent 60fb60f5e0
commit c4160e9ef8
5 changed files with 43 additions and 22 deletions

View File

@ -26,7 +26,7 @@ namespace Catch {
: m_configWrapper( configWrapper ), : m_configWrapper( configWrapper ),
m_config( configWrapper.data() ) m_config( configWrapper.data() )
{ {
resolveStream(); openStream();
makeReporter(); makeReporter();
} }
@ -78,13 +78,10 @@ namespace Catch {
} }
private: private:
void resolveStream() { void openStream() {
if( !m_config.stream.empty() ) { if( !m_config.stream.empty() )
if( m_config.stream[0] == '%' ) m_configWrapper.useStream( m_config.stream );
m_configWrapper.useStream( m_config.stream.substr( 1 ) );
else
m_configWrapper.setFilename( m_config.stream );
}
// Open output file, if specified // Open output file, if specified
if( !m_config.outputFilename.empty() ) { if( !m_config.outputFilename.empty() ) {
m_ofs.open( m_config.outputFilename.c_str() ); m_ofs.open( m_config.outputFilename.c_str() );

View File

@ -11,6 +11,7 @@
#include "catch_test_spec.h" #include "catch_test_spec.h"
#include "catch_context.h" #include "catch_context.h"
#include "catch_interfaces_config.h" #include "catch_interfaces_config.h"
#include "catch_stream.hpp"
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -79,19 +80,17 @@ namespace Catch {
public: public:
Config() Config()
: m_streambuf( NULL ), : m_os( std::cout.rdbuf() )
m_os( std::cout.rdbuf() )
{} {}
Config( const ConfigData& data ) Config( const ConfigData& data )
: m_data( data ), : m_data( data ),
m_streambuf( NULL ),
m_os( std::cout.rdbuf() ) m_os( std::cout.rdbuf() )
{} {}
virtual ~Config() { virtual ~Config() {
m_os.rdbuf( std::cout.rdbuf() ); m_os.rdbuf( std::cout.rdbuf() );
delete m_streambuf; m_stream.release();
} }
void setFilename( const std::string& filename ) { void setFilename( const std::string& filename ) {
@ -131,10 +130,10 @@ namespace Catch {
} }
void useStream( const std::string& streamName ) { void useStream( const std::string& streamName ) {
std::streambuf* newBuf = createStreamBuf( streamName ); Stream stream = createStream( streamName );
setStreamBuf( newBuf ); setStreamBuf( stream.streamBuf );
delete m_streambuf; m_stream.release();
m_streambuf = newBuf; m_stream = stream;
} }
void addTestSpec( const std::string& testSpec ) { void addTestSpec( const std::string& testSpec ) {
@ -166,7 +165,7 @@ namespace Catch {
ConfigData m_data; ConfigData m_data;
// !TBD Move these out of here // !TBD Move these out of here
std::streambuf* m_streambuf; Stream m_stream;
mutable std::ostream m_os; mutable std::ostream m_os;
}; };

View File

@ -17,6 +17,7 @@
namespace Catch { namespace Catch {
class TestCaseInfo; class TestCaseInfo;
class Stream;
struct IResultCapture; struct IResultCapture;
struct IRunner; struct IRunner;
struct IGeneratorsForTest; struct IGeneratorsForTest;
@ -49,7 +50,7 @@ namespace Catch {
IContext& getCurrentContext(); IContext& getCurrentContext();
IMutableContext& getCurrentMutableContext(); IMutableContext& getCurrentMutableContext();
void cleanUpContext(); void cleanUpContext();
std::streambuf* createStreamBuf( const std::string& streamName ); Stream createStream( const std::string& streamName );
} }

View File

@ -92,10 +92,10 @@ namespace Catch {
return getCurrentMutableContext(); return getCurrentMutableContext();
} }
std::streambuf* createStreamBuf( const std::string& streamName ) { Stream createStream( const std::string& streamName ) {
if( streamName == "stdout" ) return std::cout.rdbuf(); if( streamName == "stdout" ) return Stream( std::cout.rdbuf(), false );
if( streamName == "stderr" ) return std::cerr.rdbuf(); if( streamName == "stderr" ) return Stream( std::cerr.rdbuf(), false );
if( streamName == "debug" ) return new StreamBufImpl<OutputDebugWriter>; if( streamName == "debug" ) return Stream( new StreamBufImpl<OutputDebugWriter>, true );
throw std::domain_error( "Unknown stream: " + streamName ); throw std::domain_error( "Unknown stream: " + streamName );
} }

View File

@ -58,6 +58,30 @@ namespace Catch {
writeToDebugConsole( str ); writeToDebugConsole( str );
} }
}; };
class Stream {
public:
Stream()
: streamBuf( NULL ), isOwned( false )
{}
Stream( std::streambuf* _streamBuf, bool _isOwned )
: streamBuf( _streamBuf ), isOwned( _isOwned )
{}
void release() {
if( isOwned ) {
delete streamBuf;
streamBuf = NULL;
isOwned = false;
}
}
std::streambuf* streamBuf;
private:
bool isOwned;
};
} }
#endif // TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED #endif // TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED