stdout/ stderr capturing

This commit is contained in:
Phil Nash
2010-11-30 06:48:21 +00:00
parent 9a703f73d4
commit ce319b79e5
6 changed files with 50 additions and 8 deletions

View File

@@ -31,7 +31,8 @@ namespace Catch
typedef int YesType;
// create a T for use in sizeof expressions
template<typename T> T Synth();
template<typename T> T Synth();
}
#endif // TWOBLUECUBES_CATCH_COMMON_H_INCLUDED

View File

@@ -89,7 +89,7 @@ namespace Catch
virtual void EndSection( const std::string& sectionName, std::size_t succeeded, std::size_t failed ) = 0;
virtual void StartTestCase( const TestCaseInfo& testInfo ) = 0;
virtual void EndTestCase( const TestCaseInfo& testInfo ) = 0;
virtual void EndTestCase( const TestCaseInfo& testInfo, const std::string& stdOut, const std::string& stdErr ) = 0;
virtual void Result( const ResultInfo& result ) = 0;
};

View File

@@ -45,6 +45,30 @@ namespace Catch
bool m_isWildcarded;
};
class StreamRedirect
{
public:
StreamRedirect( std::ostream& stream, std::string& targetString )
: m_stream( stream ),
m_prevBuf( stream.rdbuf() ),
m_targetString( targetString )
{
stream.rdbuf( m_oss.rdbuf() );
}
~StreamRedirect()
{
m_targetString = m_oss.str();
m_stream.rdbuf( m_prevBuf );
}
private:
std::ostream& m_stream;
std::streambuf* m_prevBuf;
std::ostringstream m_oss;
std::string& m_targetString;
};
class Runner : public IResultListener
{
public:
@@ -92,9 +116,14 @@ namespace Catch
IResultListener* prevListener = ResultsCapture::setListener( this );
m_reporter->StartTestCase( testInfo );
std::string redirectedCout;
std::string redirectedCerr;
try
{
testInfo.invoke();
StreamRedirect coutRedir( std::cout, redirectedCout );
StreamRedirect cerrRedir( std::cerr, redirectedCerr );
testInfo.invoke();
}
catch( TestFailureException& )
{
@@ -111,7 +140,7 @@ namespace Catch
ResultsCapture::acceptResult( ResultWas::ThrewException );
}
m_reporter->EndTestCase( testInfo );
m_reporter->EndTestCase( testInfo, redirectedCout, redirectedCerr );
ResultsCapture::setListener( prevListener );
}