Moved tostring impl into catch_tostring.hpp

This commit is contained in:
Phil Nash 2014-04-23 07:03:15 +01:00
parent 97150f27ac
commit 328a469c03
4 changed files with 150 additions and 110 deletions

View File

@ -34,6 +34,7 @@
#include "catch_common.hpp" #include "catch_common.hpp"
#include "catch_section.hpp" #include "catch_section.hpp"
#include "catch_debugger.hpp" #include "catch_debugger.hpp"
#include "catch_tostring.hpp"
#include "../reporters/catch_reporter_xml.hpp" #include "../reporters/catch_reporter_xml.hpp"
#include "../reporters/catch_reporter_junit.hpp" #include "../reporters/catch_reporter_junit.hpp"

View File

@ -5,12 +5,11 @@
* 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_TOSTRING_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED #define TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED
#include "catch_common.h" #include "catch_common.h"
#include "catch_sfinae.hpp" #include "catch_sfinae.hpp"
#include "catch_interfaces_config.h"
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
@ -161,7 +160,7 @@ struct StringMaker<std::vector<T, Allocator> > {
namespace Detail { namespace Detail {
template<typename T> template<typename T>
inline std::string makeString( T const& value ) { std::string makeString( T const& value ) {
return StringMaker<T>::convert( value ); return StringMaker<T>::convert( value );
} }
} // end namespace Detail } // end namespace Detail
@ -180,116 +179,27 @@ std::string toString( T const& value ) {
// Built in overloads // Built in overloads
///////// !TBD: move this into impl file std::string toString( std::string const& value );
std::string toString( std::wstring const& value );
inline std::string toString( std::string const& value ) { std::string toString( const char* const value );
std::string s = value; std::string toString( char* const value );
if( getCurrentContext().getConfig()->showInvisibles() ) { std::string toString( int value );
for(size_t i = 0; i < s.size(); ++i ) { std::string toString( unsigned long value );
std::string subs; std::string toString( unsigned int value );
switch( s[i] ) { std::string toString( const double value );
case '\n': subs = "\\n"; break; std::string toString( bool value );
case '\t': subs = "\\t"; break; std::string toString( char value );
default: break; std::string toString( signed char value );
} std::string toString( unsigned char value );
if( !subs.empty() ) {
s = s.substr( 0, i ) + subs + s.substr( i+1 );
++i;
}
}
}
return "\"" + s + "\"";
}
inline std::string toString( std::wstring const& value ) {
std::string s;
s.reserve( value.size() );
for(size_t i = 0; i < value.size(); ++i )
s += value[i] <= 0xff ? static_cast<char>( value[i] ) : '?';
return toString( s );
}
inline std::string toString( const char* const value ) {
return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" );
}
inline std::string toString( char* const value ) {
return Catch::toString( static_cast<const char*>( value ) );
}
inline std::string toString( int value ) {
std::ostringstream oss;
oss << value;
return oss.str();
}
inline std::string toString( unsigned long value ) {
std::ostringstream oss;
if( value > 8192 )
oss << "0x" << std::hex << value;
else
oss << value;
return oss.str();
}
inline std::string toString( unsigned int value ) {
return toString( static_cast<unsigned long>( value ) );
}
inline std::string toString( const double value ) {
std::ostringstream oss;
oss << std::setprecision( 10 )
<< std::fixed
<< value;
std::string d = oss.str();
std::size_t i = d.find_last_not_of( '0' );
if( i != std::string::npos && i != d.size()-1 ) {
if( d[i] == '.' )
i++;
d = d.substr( 0, i+1 );
}
return d;
}
inline std::string toString( bool value ) {
return value ? "true" : "false";
}
inline std::string toString( char value ) {
return value < ' '
? toString( static_cast<unsigned int>( value ) )
: Detail::makeString( value );
}
inline std::string toString( signed char value ) {
return toString( static_cast<char>( value ) );
}
inline std::string toString( unsigned char value ) {
return toString( static_cast<char>( value ) );
}
#ifdef CATCH_CONFIG_CPP11_NULLPTR #ifdef CATCH_CONFIG_CPP11_NULLPTR
inline std::string toString( std::nullptr_t ) { std::string toString( std::nullptr_t );
return "nullptr";
}
#endif #endif
#ifdef __OBJC__ #ifdef __OBJC__
inline std::string toString( NSString const * const& nsstring ) { std::string toString( NSString const * const& nsstring );
if( !nsstring ) std::string toString( NSString * CATCH_ARC_STRONG const& nsstring );
return "nil"; std::string toString( NSObject* const& nsObject );
return std::string( "@\"" ) + [nsstring UTF8String] + "\"";
}
inline std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) {
if( !nsstring )
return "nil";
return std::string( "@\"" ) + [nsstring UTF8String] + "\"";
}
inline std::string toString( NSObject* const& nsObject ) {
return toString( [nsObject description] );
}
#endif #endif
namespace Detail { namespace Detail {
@ -310,4 +220,4 @@ inline std::string toString( std::nullptr_t ) {
} // end namespace Catch } // end namespace Catch
#endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED #endif // TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED

View File

@ -0,0 +1,127 @@
/*
* Created by Phil on 23/4/2014.
* Copyright 2014 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)
*/
#ifndef TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
#include "catch_tostring.h"
#include "catch_interfaces_config.h"
namespace Catch {
std::string toString( std::string const& value ) {
std::string s = value;
if( getCurrentContext().getConfig()->showInvisibles() ) {
for(size_t i = 0; i < s.size(); ++i ) {
std::string subs;
switch( s[i] ) {
case '\n': subs = "\\n"; break;
case '\t': subs = "\\t"; break;
default: break;
}
if( !subs.empty() ) {
s = s.substr( 0, i ) + subs + s.substr( i+1 );
++i;
}
}
}
return "\"" + s + "\"";
}
std::string toString( std::wstring const& value ) {
std::string s;
s.reserve( value.size() );
for(size_t i = 0; i < value.size(); ++i )
s += value[i] <= 0xff ? static_cast<char>( value[i] ) : '?';
return toString( s );
}
std::string toString( const char* const value ) {
return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" );
}
std::string toString( char* const value ) {
return Catch::toString( static_cast<const char*>( value ) );
}
std::string toString( int value ) {
std::ostringstream oss;
oss << value;
return oss.str();
}
std::string toString( unsigned long value ) {
std::ostringstream oss;
if( value > 8192 )
oss << "0x" << std::hex << value;
else
oss << value;
return oss.str();
}
std::string toString( unsigned int value ) {
return toString( static_cast<unsigned long>( value ) );
}
std::string toString( const double value ) {
std::ostringstream oss;
oss << std::setprecision( 10 )
<< std::fixed
<< value;
std::string d = oss.str();
std::size_t i = d.find_last_not_of( '0' );
if( i != std::string::npos && i != d.size()-1 ) {
if( d[i] == '.' )
i++;
d = d.substr( 0, i+1 );
}
return d;
}
std::string toString( bool value ) {
return value ? "true" : "false";
}
std::string toString( char value ) {
return value < ' '
? toString( static_cast<unsigned int>( value ) )
: Detail::makeString( value );
}
std::string toString( signed char value ) {
return toString( static_cast<char>( value ) );
}
std::string toString( unsigned char value ) {
return toString( static_cast<char>( value ) );
}
#ifdef CATCH_CONFIG_CPP11_NULLPTR
std::string toString( std::nullptr_t ) {
return "nullptr";
}
#endif
#ifdef __OBJC__
std::string toString( NSString const * const& nsstring ) {
if( !nsstring )
return "nil";
return std::string( "@\"" ) + [nsstring UTF8String] + "\"";
}
std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) {
if( !nsstring )
return "nil";
return std::string( "@\"" ) + [nsstring UTF8String] + "\"";
}
std::string toString( NSObject* const& nsObject ) {
return toString( [nsObject description] );
}
#endif
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED

View File

@ -81,6 +81,7 @@
26948287179EF7F900ED166E /* catch_test_case_tracker.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_test_case_tracker.hpp; sourceTree = "<group>"; }; 26948287179EF7F900ED166E /* catch_test_case_tracker.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_test_case_tracker.hpp; sourceTree = "<group>"; };
2694A1FB16A0000E004816E3 /* catch_text.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = catch_text.cpp; sourceTree = "<group>"; }; 2694A1FB16A0000E004816E3 /* catch_text.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = catch_text.cpp; sourceTree = "<group>"; };
269831E519078C1600BB0CE0 /* catch_tostring.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_tostring.h; sourceTree = "<group>"; }; 269831E519078C1600BB0CE0 /* catch_tostring.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_tostring.h; sourceTree = "<group>"; };
269831E619078CA200BB0CE0 /* catch_tostring.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_tostring.hpp; sourceTree = "<group>"; };
26AEAF1617BEA18E009E32C9 /* catch_platform.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = catch_platform.h; sourceTree = "<group>"; }; 26AEAF1617BEA18E009E32C9 /* catch_platform.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = catch_platform.h; sourceTree = "<group>"; };
26DACF2F17206D3400A21326 /* catch_text.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = catch_text.h; sourceTree = "<group>"; }; 26DACF2F17206D3400A21326 /* catch_text.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = catch_text.h; sourceTree = "<group>"; };
4A084F1C15DACEEA0027E631 /* catch_test_case_info.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_test_case_info.hpp; sourceTree = "<group>"; }; 4A084F1C15DACEEA0027E631 /* catch_test_case_info.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_test_case_info.hpp; sourceTree = "<group>"; };
@ -345,6 +346,7 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
269831E519078C1600BB0CE0 /* catch_tostring.h */, 269831E519078C1600BB0CE0 /* catch_tostring.h */,
269831E619078CA200BB0CE0 /* catch_tostring.hpp */,
4A6D0C4D149B3E3D00DB3EAA /* catch_evaluate.hpp */, 4A6D0C4D149B3E3D00DB3EAA /* catch_evaluate.hpp */,
4A6D0C4F149B3E3D00DB3EAA /* catch_generators.hpp */, 4A6D0C4F149B3E3D00DB3EAA /* catch_generators.hpp */,
4A6D0C5C149B3E3D00DB3EAA /* catch_result_type.h */, 4A6D0C5C149B3E3D00DB3EAA /* catch_result_type.h */,