2011-01-18 10:20:06 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 17/01/2011.
|
|
|
|
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-07-06 22:28:42 +02:00
|
|
|
#include "catch_common.h"
|
2017-08-01 18:46:33 +02:00
|
|
|
#include "catch_enforce.h"
|
2013-12-03 19:52:41 +01:00
|
|
|
#include "catch_stream.h"
|
2017-08-29 13:51:55 +02:00
|
|
|
#include "catch_debug_console.h"
|
2017-11-07 16:55:09 +01:00
|
|
|
#include "catch_stringref.h"
|
2018-06-15 22:39:52 +02:00
|
|
|
#include "catch_singletons.hpp"
|
2012-09-28 20:21:14 +02:00
|
|
|
|
2011-02-02 20:45:59 +01:00
|
|
|
#include <cstdio>
|
2014-10-02 20:08:19 +02:00
|
|
|
#include <iostream>
|
2017-11-07 16:55:09 +01:00
|
|
|
#include <fstream>
|
2017-11-07 19:01:10 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
2017-11-07 19:48:57 +01:00
|
|
|
#include <memory>
|
2017-11-07 19:01:10 +01:00
|
|
|
|
2012-05-15 09:02:36 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
Catch::IStream::~IStream() = default;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2019-04-23 23:41:13 +02:00
|
|
|
namespace Detail { namespace {
|
2017-11-07 16:55:09 +01:00
|
|
|
template<typename WriterF, std::size_t bufferSize=256>
|
|
|
|
class StreamBufImpl : public std::streambuf {
|
|
|
|
char data[bufferSize];
|
|
|
|
WriterF m_writer;
|
2011-02-03 21:00:46 +01:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
public:
|
|
|
|
StreamBufImpl() {
|
|
|
|
setp( data, data + sizeof(data) );
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
~StreamBufImpl() noexcept {
|
|
|
|
StreamBufImpl::sync();
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
private:
|
|
|
|
int overflow( int c ) override {
|
|
|
|
sync();
|
|
|
|
|
|
|
|
if( c != EOF ) {
|
|
|
|
if( pbase() == epptr() )
|
|
|
|
m_writer( std::string( 1, static_cast<char>( c ) ) );
|
|
|
|
else
|
|
|
|
sputc( static_cast<char>( c ) );
|
|
|
|
}
|
|
|
|
return 0;
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
int sync() override {
|
|
|
|
if( pbase() != pptr() ) {
|
|
|
|
m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) );
|
|
|
|
setp( pbase(), epptr() );
|
|
|
|
}
|
|
|
|
return 0;
|
2011-01-18 10:20:06 +01:00
|
|
|
}
|
2017-11-07 16:55:09 +01:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2011-02-03 21:00:46 +01:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
struct OutputDebugWriter {
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
void operator()( std::string const&str ) {
|
|
|
|
writeToDebugConsole( str );
|
|
|
|
}
|
|
|
|
};
|
2017-09-07 16:51:33 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
class FileStream : public IStream {
|
|
|
|
mutable std::ofstream m_ofs;
|
|
|
|
public:
|
|
|
|
FileStream( StringRef filename ) {
|
|
|
|
m_ofs.open( filename.c_str() );
|
|
|
|
CATCH_ENFORCE( !m_ofs.fail(), "Unable to open file: '" << filename << "'" );
|
|
|
|
}
|
|
|
|
~FileStream() override = default;
|
|
|
|
public: // IStream
|
|
|
|
std::ostream& stream() const override {
|
|
|
|
return m_ofs;
|
|
|
|
}
|
|
|
|
};
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
class CoutStream : public IStream {
|
|
|
|
mutable std::ostream m_os;
|
|
|
|
public:
|
|
|
|
// Store the streambuf from cout up-front because
|
|
|
|
// cout may get redirected when running tests
|
|
|
|
CoutStream() : m_os( Catch::cout().rdbuf() ) {}
|
|
|
|
~CoutStream() override = default;
|
2012-09-26 19:36:58 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
public: // IStream
|
|
|
|
std::ostream& stream() const override { return m_os; }
|
|
|
|
};
|
2012-09-26 19:36:58 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class DebugOutStream : public IStream {
|
|
|
|
std::unique_ptr<StreamBufImpl<OutputDebugWriter>> m_streamBuf;
|
|
|
|
mutable std::ostream m_os;
|
|
|
|
public:
|
|
|
|
DebugOutStream()
|
|
|
|
: m_streamBuf( new StreamBufImpl<OutputDebugWriter>() ),
|
|
|
|
m_os( m_streamBuf.get() )
|
|
|
|
{}
|
|
|
|
|
|
|
|
~DebugOutStream() override = default;
|
|
|
|
|
|
|
|
public: // IStream
|
|
|
|
std::ostream& stream() const override { return m_os; }
|
|
|
|
};
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
}} // namespace anon::detail
|
2012-09-26 19:36:58 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
auto makeStream( StringRef const &filename ) -> IStream const* {
|
|
|
|
if( filename.empty() )
|
2019-04-23 23:41:13 +02:00
|
|
|
return new Detail::CoutStream();
|
2017-11-07 16:55:09 +01:00
|
|
|
else if( filename[0] == '%' ) {
|
|
|
|
if( filename == "%debug" )
|
2019-04-23 23:41:13 +02:00
|
|
|
return new Detail::DebugOutStream();
|
2017-11-07 16:55:09 +01:00
|
|
|
else
|
|
|
|
CATCH_ERROR( "Unrecognised stream: '" << filename << "'" );
|
|
|
|
}
|
|
|
|
else
|
2019-04-23 23:41:13 +02:00
|
|
|
return new Detail::FileStream( filename );
|
2013-12-03 19:52:41 +01:00
|
|
|
}
|
2014-10-02 20:08:19 +02:00
|
|
|
|
2017-11-07 16:55:09 +01:00
|
|
|
|
2017-11-07 19:01:10 +01:00
|
|
|
// This class encapsulates the idea of a pool of ostringstreams that can be reused.
|
|
|
|
struct StringStreams {
|
|
|
|
std::vector<std::unique_ptr<std::ostringstream>> m_streams;
|
|
|
|
std::vector<std::size_t> m_unused;
|
|
|
|
std::ostringstream m_referenceStream; // Used for copy state/ flags from
|
2017-11-07 16:55:09 +01:00
|
|
|
|
2017-11-07 19:01:10 +01:00
|
|
|
auto add() -> std::size_t {
|
|
|
|
if( m_unused.empty() ) {
|
|
|
|
m_streams.push_back( std::unique_ptr<std::ostringstream>( new std::ostringstream ) );
|
|
|
|
return m_streams.size()-1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
auto index = m_unused.back();
|
|
|
|
m_unused.pop_back();
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void release( std::size_t index ) {
|
|
|
|
m_streams[index]->copyfmt( m_referenceStream ); // Restore initial flags and other state
|
|
|
|
m_unused.push_back(index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ReusableStringStream::ReusableStringStream()
|
2018-06-15 22:39:52 +02:00
|
|
|
: m_index( Singleton<StringStreams>::getMutable().add() ),
|
|
|
|
m_oss( Singleton<StringStreams>::getMutable().m_streams[m_index].get() )
|
2017-11-07 19:01:10 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
ReusableStringStream::~ReusableStringStream() {
|
|
|
|
static_cast<std::ostringstream*>( m_oss )->str("");
|
|
|
|
m_oss->clear();
|
2018-06-15 22:39:52 +02:00
|
|
|
Singleton<StringStreams>::getMutable().release( m_index );
|
2014-10-02 20:08:19 +02:00
|
|
|
}
|
2017-11-07 19:01:10 +01:00
|
|
|
|
|
|
|
auto ReusableStringStream::str() const -> std::string {
|
|
|
|
return static_cast<std::ostringstream*>( m_oss )->str();
|
2017-08-09 15:28:40 +02:00
|
|
|
}
|
2017-11-07 19:01:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CATCH_CONFIG_NOSTDOUT // If you #define this you must implement these functions
|
|
|
|
std::ostream& cout() { return std::cout; }
|
|
|
|
std::ostream& cerr() { return std::cerr; }
|
|
|
|
std::ostream& clog() { return std::clog; }
|
2014-10-02 20:08:19 +02:00
|
|
|
#endif
|
2011-01-18 10:20:06 +01:00
|
|
|
}
|