More reformatting

This commit is contained in:
Phil Nash 2012-05-15 08:02:36 +01:00
parent 2efc1146bf
commit d0be9ed5d9
11 changed files with 124 additions and 396 deletions

View File

@ -25,8 +25,8 @@
#include <stdexcept> #include <stdexcept>
#include <algorithm> #include <algorithm>
namespace Catch namespace Catch {
{
class NonCopyable { class NonCopyable {
NonCopyable( const NonCopyable& ); NonCopyable( const NonCopyable& );
void operator = ( const NonCopyable& ); void operator = ( const NonCopyable& );
@ -64,8 +64,8 @@ namespace Catch
std::for_each( container.begin(), container.end(), function ); std::for_each( container.begin(), container.end(), function );
} }
struct SourceLineInfo struct SourceLineInfo {
{
SourceLineInfo() : line( 0 ){} SourceLineInfo() : line( 0 ){}
SourceLineInfo( const std::string& _file, std::size_t _line ) SourceLineInfo( const std::string& _file, std::size_t _line )
: file( _file ), : file( _file ),

View File

@ -1,29 +1,23 @@
/* /*
* catch_console_colour.hpp
* Catch
*
* Created by Phil on 25/2/2012. * Created by Phil on 25/2/2012.
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved. * Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED #define TWOBLUECUBES_CATCH_CONSOLE_COLOUR_HPP_INCLUDED
#include "catch_common.h" #include "catch_common.h"
namespace Catch namespace Catch {
{
struct ConsoleColourImpl; struct ConsoleColourImpl;
class TextColour : NonCopyable class TextColour : NonCopyable {
{
public: public:
enum Colours enum Colours {
{
None, None,
FileName, FileName,

View File

@ -1,7 +1,4 @@
/* /*
* catch_debugger.hpp
* Catch
*
* Created by Phil on 27/12/2010. * Created by Phil on 27/12/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
* *
@ -10,7 +7,6 @@
* *
* Provides a BreakIntoDebugger() macro for Windows and Mac (so far) * Provides a BreakIntoDebugger() macro for Windows and Mac (so far)
*/ */
#ifndef TWOBLUECUBES_CATCH_DEBUGGER_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_DEBUGGER_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_DEBUGGER_HPP_INCLUDED #define TWOBLUECUBES_CATCH_DEBUGGER_HPP_INCLUDED
@ -32,15 +28,15 @@
#include <unistd.h> #include <unistd.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
namespace Catch namespace Catch{
{
// The following function is taken directly from the following technical note: // The following function is taken directly from the following technical note:
// http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html // http://developer.apple.com/library/mac/#qa/qa2004/qa1361.html
inline bool isDebuggerActive()
// Returns true if the current process is being debugged (either // Returns true if the current process is being debugged (either
// running under the debugger or has a debugger attached post facto). // running under the debugger or has a debugger attached post facto).
{ inline bool isDebuggerActive(){
int junk; int junk;
int mib[4]; int mib[4];
struct kinfo_proc info; struct kinfo_proc info;
@ -76,8 +72,7 @@
#ifdef DEBUG #ifdef DEBUG
#if defined(__ppc64__) || defined(__ppc__) #if defined(__ppc64__) || defined(__ppc__)
#define BreakIntoDebugger() \ #define BreakIntoDebugger() \
if( Catch::isDebuggerActive() ) \ if( Catch::isDebuggerActive() ) { \
{ \
__asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" \ __asm__("li r0, 20\nsc\nnop\nli r0, 37\nli r4, 2\nsc\nnop\n" \
: : : "memory","r0","r3","r4" ); \ : : : "memory","r0","r3","r4" ); \
} }
@ -91,16 +86,14 @@
#elif defined(_MSC_VER) #elif defined(_MSC_VER)
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
#define BreakIntoDebugger() if (IsDebuggerPresent() ) { __debugbreak(); } #define BreakIntoDebugger() if (IsDebuggerPresent() ) { __debugbreak(); }
inline bool isDebuggerActive() inline bool isDebuggerActive() {
{
return IsDebuggerPresent() != 0; return IsDebuggerPresent() != 0;
} }
#elif defined(__MINGW32__) #elif defined(__MINGW32__)
extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent(); extern "C" __declspec(dllimport) int __stdcall IsDebuggerPresent();
extern "C" __declspec(dllimport) void __stdcall DebugBreak(); extern "C" __declspec(dllimport) void __stdcall DebugBreak();
#define BreakIntoDebugger() if (IsDebuggerPresent() ) { DebugBreak(); } #define BreakIntoDebugger() if (IsDebuggerPresent() ) { DebugBreak(); }
inline bool isDebuggerActive() inline bool isDebuggerActive() {
{
return IsDebuggerPresent() != 0; return IsDebuggerPresent() != 0;
} }
#else #else
@ -110,13 +103,11 @@
#ifdef CATCH_PLATFORM_WINDOWS #ifdef CATCH_PLATFORM_WINDOWS
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA( const char* ); extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA( const char* );
inline void writeToDebugConsole( const std::string& text ) inline void writeToDebugConsole( const std::string& text ) {
{
::OutputDebugStringA( text.c_str() ); ::OutputDebugStringA( text.c_str() );
} }
#else #else
inline void writeToDebugConsole( const std::string& text ) inline void writeToDebugConsole( const std::string& text ) {
{
// !TBD: Need a version for Mac/ XCode and other IDEs // !TBD: Need a version for Mac/ XCode and other IDEs
std::cout << text; std::cout << text;
} }

View File

@ -1,13 +1,9 @@
/* /*
* catch_interfaces_capture.h
* Catch
*
* Created by Phil on 07/01/2011. * Created by Phil on 07/01/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED #ifndef TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED
#define TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED #define TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED
@ -16,59 +12,34 @@
#include "catch_result_type.h" #include "catch_result_type.h"
#include "catch_totals.hpp" #include "catch_totals.hpp"
namespace Catch namespace Catch {
{
class TestCaseInfo; class TestCaseInfo;
class ScopedInfo; class ScopedInfo;
class ResultInfoBuilder; class ResultInfoBuilder;
class ResultInfo; class ResultInfo;
struct IResultCapture struct IResultCapture {
{
virtual ~IResultCapture virtual ~IResultCapture(){}
()
{}
virtual void testEnded virtual void testEnded( const ResultInfo& result ) = 0;
( const ResultInfo& result virtual bool sectionStarted( const std::string& name,
) = 0; const std::string& description,
virtual bool sectionStarted const SourceLineInfo& lineInfo,
( const std::string& name, Counts& assertions ) = 0;
const std::string& description, virtual void sectionEnded( const std::string& name, const Counts& assertions ) = 0;
const SourceLineInfo& lineInfo, virtual void pushScopedInfo( ScopedInfo* scopedInfo ) = 0;
Counts& assertions virtual void popScopedInfo( ScopedInfo* scopedInfo ) = 0;
) = 0; virtual bool shouldDebugBreak() const = 0;
virtual void sectionEnded
( const std::string& name,
const Counts& assertions
) = 0;
virtual void pushScopedInfo
( ScopedInfo* scopedInfo
) = 0;
virtual void popScopedInfo
( ScopedInfo* scopedInfo
) = 0;
virtual bool shouldDebugBreak
() const = 0;
virtual ResultAction::Value acceptResult virtual ResultAction::Value acceptResult( bool result ) = 0;
( bool result virtual ResultAction::Value acceptResult( ResultWas::OfType result ) = 0;
) = 0; virtual ResultAction::Value acceptExpression( const ResultInfoBuilder& resultInfo ) = 0;
virtual ResultAction::Value acceptResult virtual void acceptMessage( const std::string& msg ) = 0;
( ResultWas::OfType result
) = 0;
virtual ResultAction::Value acceptExpression
( const ResultInfoBuilder& resultInfo
) = 0;
virtual void acceptMessage
( const std::string& msg
) = 0;
virtual std::string getCurrentTestName
() const = 0;
virtual const ResultInfo* getLastResult
() const = 0;
virtual std::string getCurrentTestName() const = 0;
virtual const ResultInfo* getLastResult() const = 0;
}; };
} }

View File

@ -1,67 +1,45 @@
/* /*
* catch_exception_interfaces.h
* Catch
*
* Created by Phil on 20/04/2011. * Created by Phil on 20/04/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED #ifndef TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
#define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED #define TWOBLUECUBES_CATCH_INTERFACES_EXCEPTIONS_H_INCLUDED
#include <string> #include <string>
namespace Catch namespace Catch {
{
typedef std::string(*exceptionTranslateFunction)(); typedef std::string(*exceptionTranslateFunction)();
struct IExceptionTranslator struct IExceptionTranslator {
{
virtual ~IExceptionTranslator(){} virtual ~IExceptionTranslator(){}
virtual std::string translate() const = 0; virtual std::string translate() const = 0;
}; };
struct IExceptionTranslatorRegistry struct IExceptionTranslatorRegistry {
{ virtual ~IExceptionTranslatorRegistry(){}
virtual ~IExceptionTranslatorRegistry
()
{}
virtual void registerTranslator
( IExceptionTranslator* translator
) = 0;
virtual std::string translateActiveException
() const = 0;
virtual void registerTranslator( IExceptionTranslator* translator ) = 0;
virtual std::string translateActiveException() const = 0;
}; };
class ExceptionTranslatorRegistrar class ExceptionTranslatorRegistrar {
{
template<typename T> template<typename T>
class ExceptionTranslator : public IExceptionTranslator class ExceptionTranslator : public IExceptionTranslator {
{
public: public:
ExceptionTranslator ExceptionTranslator( std::string(*translateFunction)( T& ) )
(
std::string(*translateFunction)( T& )
)
: m_translateFunction( translateFunction ) : m_translateFunction( translateFunction )
{} {}
virtual std::string translate virtual std::string translate() const {
() try {
const
{
try
{
throw; throw;
} }
catch( T& ex ) catch( T& ex ) {
{
return m_translateFunction( ex ); return m_translateFunction( ex );
} }
} }
@ -72,11 +50,7 @@ namespace Catch
public: public:
template<typename T> template<typename T>
ExceptionTranslatorRegistrar ExceptionTranslatorRegistrar( std::string(*translateFunction)( T& ) ) {
(
std::string(*translateFunction)( T& )
)
{
Catch::Context::getExceptionTranslatorRegistry().registerTranslator Catch::Context::getExceptionTranslatorRegistry().registerTranslator
( new ExceptionTranslator<T>( translateFunction ) ); ( new ExceptionTranslator<T>( translateFunction ) );
} }

View File

@ -57,8 +57,7 @@ namespace Catch
virtual const FactoryMap& getFactories() const = 0; virtual const FactoryMap& getFactories() const = 0;
}; };
inline std::string trim( const std::string& str ) inline std::string trim( const std::string& str ) {
{
std::string::size_type start = str.find_first_not_of( "\n\r\t " ); std::string::size_type start = str.find_first_not_of( "\n\r\t " );
std::string::size_type end = str.find_last_not_of( "\n\r\t " ); std::string::size_type end = str.find_last_not_of( "\n\r\t " );

View File

@ -1,13 +1,9 @@
/* /*
* catch_interfaces_runner.h
* Catch
*
* Created by Phil on 07/01/2011. * Created by Phil on 07/01/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_INTERNAL_CATCH_INTERFACES_RUNNER_H_INCLUDED #ifndef TWOBLUECUBES_INTERNAL_CATCH_INTERFACES_RUNNER_H_INCLUDED
#define TWOBLUECUBES_INTERNAL_CATCH_INTERFACES_RUNNER_H_INCLUDED #define TWOBLUECUBES_INTERNAL_CATCH_INTERFACES_RUNNER_H_INCLUDED
@ -16,27 +12,14 @@
#include <string> #include <string>
namespace Catch namespace Catch {
{
class TestCaseInfo; class TestCaseInfo;
struct IRunner struct IRunner {
{ virtual ~IRunner() {}
virtual ~IRunner virtual void runAll( bool runHiddenTests = false ) = 0;
() virtual std::size_t runMatching( const std::string& rawTestSpec ) = 0;
{} virtual Totals getTotals() const = 0;
virtual void runAll
( bool runHiddenTests = false
) = 0;
virtual std::size_t runMatching
( const std::string& rawTestSpec
) = 0;
virtual Totals getTotals
() const = 0;
}; };
} }

View File

@ -1,64 +1,32 @@
/* /*
* catch_interfaces_testcase.h
* Catch
*
* Created by Phil on 07/01/2011. * Created by Phil on 07/01/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED #ifndef TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED
#define TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED #define TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED
#include <vector> #include <vector>
namespace Catch namespace Catch {
{ struct ITestCase {
struct ITestCase virtual ~ITestCase(){}
{ virtual void invoke () const = 0;
virtual ~ITestCase virtual ITestCase* clone() const = 0;
() virtual bool operator == ( const ITestCase& other ) const = 0;
{} virtual bool operator < ( const ITestCase& other ) const = 0;
virtual void invoke
() const = 0;
virtual ITestCase* clone
() const = 0;
virtual bool operator ==
( const ITestCase& other
) const = 0;
virtual bool operator <
( const ITestCase& other
) const = 0;
}; };
class TestCaseInfo; class TestCaseInfo;
struct ITestCaseRegistry struct ITestCaseRegistry {
{ virtual ~ITestCaseRegistry(){}
virtual ~ITestCaseRegistry virtual void registerTest( const TestCaseInfo& testInfo ) = 0;
() virtual const std::vector<TestCaseInfo>& getAllTests() const = 0;
{} virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) = 0;
virtual void registerTest
( const TestCaseInfo& testInfo
) = 0;
virtual const std::vector<TestCaseInfo>& getAllTests
() const = 0;
virtual std::vector<TestCaseInfo> getMatchingTestCases
( const std::string& rawTestSpec
) = 0;
}; };
} }
#endif // TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED #endif // TWOBLUECUBES_CATCH_INTERFACES_TESTCASE_H_INCLUDED

View File

@ -1,27 +1,22 @@
/* /*
* catch_ptr.hpp
* Catch
*
* Created by Phil on 02/05/2012. * Created by Phil on 02/05/2012.
* Copyright 2012 Two Blue Cubes Ltd. All rights reserved. * Copyright 2012 Two Blue Cubes Ltd. All rights reserved.
* *
* Distributed under the Boost Software License, Version 1.0. (See accompanying * 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) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/ */
#ifndef TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED #define TWOBLUECUBES_CATCH_PTR_HPP_INCLUDED
#include "catch_common.h" #include "catch_common.h"
namespace Catch namespace Catch {
{
// An intrusive reference counting smart pointer. // An intrusive reference counting smart pointer.
// T must implement addRef() and release() methods // T must implement addRef() and release() methods
// typically implementing the IShared interface // typically implementing the IShared interface
template<typename T> template<typename T>
class Ptr class Ptr {
{
public: public:
Ptr() : m_p( NULL ){} Ptr() : m_p( NULL ){}
Ptr( T* p ) : m_p( p ){ Ptr( T* p ) : m_p( p ){

View File

@ -1,7 +1,4 @@
/* /*
* catch_stream.hpp
* Catch
*
* Created by Phil on 17/01/2011. * Created by Phil on 17/01/2011.
* Copyright 2011 Two Blue Cubes Ltd. All rights reserved. * Copyright 2011 Two Blue Cubes Ltd. All rights reserved.
* *
@ -9,47 +6,33 @@
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
* *
*/ */
#ifndef TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED #define TWOBLUECUBES_CATCH_STREAM_HPP_INCLUDED
#include <stdexcept> #include <stdexcept>
#include <cstdio> #include <cstdio>
namespace Catch namespace Catch {
{
template<typename WriterF, size_t bufferSize=256> template<typename WriterF, size_t bufferSize=256>
class StreamBufImpl : public StreamBufBase class StreamBufImpl : public StreamBufBase {
{
char data[bufferSize]; char data[bufferSize];
WriterF m_writer; WriterF m_writer;
public: public:
/////////////////////////////////////////////////////////////////////// StreamBufImpl() {
StreamBufImpl
()
{
setp( data, data + sizeof(data) ); setp( data, data + sizeof(data) );
} }
/////////////////////////////////////////////////////////////////////// ~StreamBufImpl() {
~StreamBufImpl
()
{
sync(); sync();
} }
private: private:
/////////////////////////////////////////////////////////////////////// int overflow( int c ) {
int overflow
(
int c
)
{
sync(); sync();
if( c != EOF ) if( c != EOF ) {
{
if( pbase() == epptr() ) if( pbase() == epptr() )
m_writer( std::string( 1, static_cast<char>( c ) ) ); m_writer( std::string( 1, static_cast<char>( c ) ) );
else else
@ -58,12 +41,8 @@ namespace Catch
return 0; return 0;
} }
/////////////////////////////////////////////////////////////////////// int sync() {
int sync if( pbase() != pptr() ) {
()
{
if( pbase() != pptr() )
{
m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) ); m_writer( std::string( pbase(), static_cast<std::string::size_type>( pptr() - pbase() ) ) );
setp( pbase(), epptr() ); setp( pbase(), epptr() );
} }
@ -71,17 +50,11 @@ namespace Catch
} }
}; };
///////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
struct OutputDebugWriter struct OutputDebugWriter {
{
/////////////////////////////////////////////////////////////////////// void operator()( const std::string &str ) {
void operator()
(
const std::string &str
)
{
writeToDebugConsole( str ); writeToDebugConsole( str );
} }
}; };

View File

@ -1,7 +1,4 @@
/* /*
* catch_xmlwriter.hpp
* Catch
*
* Created by Phil on 09/12/2010. * Created by Phil on 09/12/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
* *
@ -15,60 +12,34 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace Catch namespace Catch {
{
class XmlWriter class XmlWriter {
{
public: public:
class ScopedElement class ScopedElement {
{
public: public:
/////////////////////////////////////////////////////////////////// ScopedElement( XmlWriter* writer )
ScopedElement
(
XmlWriter* writer
)
: m_writer( writer ) : m_writer( writer )
{ {}
}
/////////////////////////////////////////////////////////////////// ScopedElement( const ScopedElement& other )
ScopedElement : m_writer( other.m_writer ){
(
const ScopedElement& other
)
: m_writer( other.m_writer )
{
other.m_writer = NULL; other.m_writer = NULL;
} }
/////////////////////////////////////////////////////////////////// ~ScopedElement() {
~ScopedElement
()
{
if( m_writer ) if( m_writer )
m_writer->endElement(); m_writer->endElement();
} }
/////////////////////////////////////////////////////////////////// ScopedElement& writeText( const std::string& text ) {
ScopedElement& writeText
(
const std::string& text
)
{
m_writer->writeText( text ); m_writer->writeText( text );
return *this; return *this;
} }
///////////////////////////////////////////////////////////////////
template<typename T> template<typename T>
ScopedElement& writeAttribute ScopedElement& writeAttribute( const std::string& name, const T& attribute ) {
(
const std::string& name,
const T& attribute
)
{
m_writer->writeAttribute( name, attribute ); m_writer->writeAttribute( name, attribute );
return *this; return *this;
} }
@ -77,53 +48,30 @@ namespace Catch
mutable XmlWriter* m_writer; mutable XmlWriter* m_writer;
}; };
/////////////////////////////////////////////////////////////////////// XmlWriter()
XmlWriter
()
: m_tagIsOpen( false ), : m_tagIsOpen( false ),
m_needsNewline( false ), m_needsNewline( false ),
m_os( &std::cout ) m_os( &std::cout )
{ {}
}
/////////////////////////////////////////////////////////////////////// XmlWriter( std::ostream& os )
XmlWriter
(
std::ostream& os
)
: m_tagIsOpen( false ), : m_tagIsOpen( false ),
m_needsNewline( false ), m_needsNewline( false ),
m_os( &os ) m_os( &os )
{ {}
}
/////////////////////////////////////////////////////////////////////// ~XmlWriter() {
~XmlWriter
()
{
while( !m_tags.empty() ) while( !m_tags.empty() )
{
endElement(); endElement();
}
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& operator = ( const XmlWriter& other ) {
XmlWriter& operator =
(
const XmlWriter& other
)
{
XmlWriter temp( other ); XmlWriter temp( other );
swap( temp ); swap( temp );
return *this; return *this;
} }
/////////////////////////////////////////////////////////////////////// void swap( XmlWriter& other ) {
void swap
(
XmlWriter& other
)
{
std::swap( m_tagIsOpen, other.m_tagIsOpen ); std::swap( m_tagIsOpen, other.m_tagIsOpen );
std::swap( m_needsNewline, other.m_needsNewline ); std::swap( m_needsNewline, other.m_needsNewline );
std::swap( m_tags, other.m_tags ); std::swap( m_tags, other.m_tags );
@ -131,12 +79,7 @@ namespace Catch
std::swap( m_os, other.m_os ); std::swap( m_os, other.m_os );
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& startElement( const std::string& name ) {
XmlWriter& startElement
(
const std::string& name
)
{
ensureTagClosed(); ensureTagClosed();
newlineIfNecessary(); newlineIfNecessary();
stream() << m_indent << "<" << name; stream() << m_indent << "<" << name;
@ -146,45 +89,28 @@ namespace Catch
return *this; return *this;
} }
/////////////////////////////////////////////////////////////////////// ScopedElement scopedElement( const std::string& name ) {
ScopedElement scopedElement
(
const std::string& name
)
{
ScopedElement scoped( this ); ScopedElement scoped( this );
startElement( name ); startElement( name );
return scoped; return scoped;
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& endElement() {
XmlWriter& endElement
()
{
newlineIfNecessary(); newlineIfNecessary();
m_indent = m_indent.substr( 0, m_indent.size()-2 ); m_indent = m_indent.substr( 0, m_indent.size()-2 );
if( m_tagIsOpen ) if( m_tagIsOpen ) {
{
stream() << "/>\n"; stream() << "/>\n";
m_tagIsOpen = false; m_tagIsOpen = false;
} }
else else {
{
stream() << m_indent << "</" << m_tags.back() << ">\n"; stream() << m_indent << "</" << m_tags.back() << ">\n";
} }
m_tags.pop_back(); m_tags.pop_back();
return *this; return *this;
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& writeAttribute( const std::string& name, const std::string& attribute ) {
XmlWriter& writeAttribute if( !name.empty() && !attribute.empty() ) {
(
const std::string& name,
const std::string& attribute
)
{
if( !name.empty() && !attribute.empty() )
{
stream() << " " << name << "=\""; stream() << " " << name << "=\"";
writeEncodedText( attribute ); writeEncodedText( attribute );
stream() << "\""; stream() << "\"";
@ -192,40 +118,20 @@ namespace Catch
return *this; return *this;
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& writeAttribute( const std::string& name, bool attribute ) {
XmlWriter& writeAttribute
(
const std::string& name,
bool attribute
)
{
stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\""; stream() << " " << name << "=\"" << ( attribute ? "true" : "false" ) << "\"";
return *this; return *this;
} }
///////////////////////////////////////////////////////////////////////
template<typename T> template<typename T>
XmlWriter& writeAttribute XmlWriter& writeAttribute( const std::string& name, const T& attribute ) {
(
const std::string& name,
const T& attribute
)
{
if( !name.empty() ) if( !name.empty() )
{
stream() << " " << name << "=\"" << attribute << "\""; stream() << " " << name << "=\"" << attribute << "\"";
}
return *this; return *this;
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& writeText( const std::string& text ) {
XmlWriter& writeText if( !text.empty() ){
(
const std::string& text
)
{
if( !text.empty() )
{
bool tagWasOpen = m_tagIsOpen; bool tagWasOpen = m_tagIsOpen;
ensureTagClosed(); ensureTagClosed();
if( tagWasOpen ) if( tagWasOpen )
@ -236,22 +142,14 @@ namespace Catch
return *this; return *this;
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& writeComment( const std::string& text ) {
XmlWriter& writeComment
(
const std::string& text
)
{
ensureTagClosed(); ensureTagClosed();
stream() << m_indent << "<!--" << text << "-->"; stream() << m_indent << "<!--" << text << "-->";
m_needsNewline = true; m_needsNewline = true;
return *this; return *this;
} }
/////////////////////////////////////////////////////////////////////// XmlWriter& writeBlankLine() {
XmlWriter& writeBlankLine
()
{
ensureTagClosed(); ensureTagClosed();
stream() << "\n"; stream() << "\n";
return *this; return *this;
@ -259,50 +157,32 @@ namespace Catch
private: private:
/////////////////////////////////////////////////////////////////////// std::ostream& stream() {
std::ostream& stream
()
{
return *m_os; return *m_os;
} }
/////////////////////////////////////////////////////////////////////// void ensureTagClosed() {
void ensureTagClosed if( m_tagIsOpen ) {
()
{
if( m_tagIsOpen )
{
stream() << ">\n"; stream() << ">\n";
m_tagIsOpen = false; m_tagIsOpen = false;
} }
} }
/////////////////////////////////////////////////////////////////////// void newlineIfNecessary() {
void newlineIfNecessary if( m_needsNewline ) {
()
{
if( m_needsNewline )
{
stream() << "\n"; stream() << "\n";
m_needsNewline = false; m_needsNewline = false;
} }
} }
/////////////////////////////////////////////////////////////////////// void writeEncodedText( const std::string& text ) {
void writeEncodedText
(
const std::string& text
)
{
static const char* charsToEncode = "<&\""; static const char* charsToEncode = "<&\"";
std::string mtext = text; std::string mtext = text;
std::string::size_type pos = mtext.find_first_of( charsToEncode ); std::string::size_type pos = mtext.find_first_of( charsToEncode );
while( pos != std::string::npos ) while( pos != std::string::npos ) {
{
stream() << mtext.substr( 0, pos ); stream() << mtext.substr( 0, pos );
switch( mtext[pos] ) switch( mtext[pos] ) {
{
case '<': case '<':
stream() << "&lt;"; stream() << "&lt;";
break; break;