mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
stdout/ stderr capturing
This commit is contained in:
@@ -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
|
@@ -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;
|
||||
};
|
||||
|
@@ -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 );
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user