2012-05-08 09:10:49 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 8/5/2012.
|
|
|
|
* Copyright 2012 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_common.h"
|
2013-04-16 23:55:31 +02:00
|
|
|
#include "catch_sfinae.hpp"
|
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
#include <sstream>
|
2013-03-04 12:19:15 +01:00
|
|
|
#include <iomanip>
|
|
|
|
#include <limits>
|
2013-11-07 11:35:59 +01:00
|
|
|
#include <vector>
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2012-08-01 09:17:07 +02:00
|
|
|
#ifdef __OBJC__
|
|
|
|
#include "catch_objc_arc.hpp"
|
|
|
|
#endif
|
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
namespace Catch {
|
2013-04-21 00:12:17 +02:00
|
|
|
namespace Detail {
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-04-21 00:18:44 +02:00
|
|
|
// SFINAE is currently disabled by default for all compilers.
|
|
|
|
// If the non SFINAE version of IsStreamInsertable is ambiguous for you
|
2013-04-22 09:19:17 +02:00
|
|
|
// and your compiler supports SFINAE, try #defining CATCH_CONFIG_SFINAE
|
|
|
|
#ifdef CATCH_CONFIG_SFINAE
|
2013-04-16 23:55:31 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class IsStreamInsertableHelper {
|
|
|
|
template<int N> struct TrueIfSizeable : TrueType {};
|
|
|
|
|
|
|
|
template<typename T2>
|
|
|
|
static TrueIfSizeable<sizeof((*(std::ostream*)0) << *((T2 const*)0))> dummy(T2*);
|
|
|
|
static FalseType dummy(...);
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef SizedIf<sizeof(dummy((T*)0))> type;
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-16 23:55:31 +02:00
|
|
|
template<typename T>
|
|
|
|
struct IsStreamInsertable : IsStreamInsertableHelper<T>::type {};
|
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
#else
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
struct BorgType {
|
|
|
|
template<typename T> BorgType( T const& );
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
TrueType& testStreamable( std::ostream& );
|
|
|
|
FalseType testStreamable( FalseType );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
FalseType operator<<( std::ostream const&, BorgType const& );
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
template<typename T>
|
|
|
|
struct IsStreamInsertable {
|
|
|
|
static std::ostream &s;
|
2013-04-23 19:58:56 +02:00
|
|
|
static T const&t;
|
2013-04-21 00:12:17 +02:00
|
|
|
enum { value = sizeof( testStreamable(s << t) ) == sizeof( TrueType ) };
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
#endif
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
template<bool C>
|
|
|
|
struct StringMakerBase {
|
|
|
|
template<typename T>
|
|
|
|
static std::string convert( T const& ) { return "{?}"; }
|
|
|
|
};
|
2012-05-15 08:42:26 +02:00
|
|
|
|
2013-04-21 00:12:17 +02:00
|
|
|
template<>
|
|
|
|
struct StringMakerBase<true> {
|
|
|
|
template<typename T>
|
|
|
|
static std::string convert( T const& _value ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << _value;
|
|
|
|
return oss.str();
|
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2013-03-04 12:19:15 +01:00
|
|
|
} // end namespace Detail
|
|
|
|
|
2013-09-14 20:58:45 +02:00
|
|
|
template<typename T>
|
|
|
|
std::string toString( T const& value );
|
|
|
|
|
2013-03-04 12:19:15 +01:00
|
|
|
template<typename T>
|
2013-04-21 00:12:17 +02:00
|
|
|
struct StringMaker :
|
|
|
|
Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {};
|
2013-04-16 23:55:31 +02:00
|
|
|
|
2013-03-04 12:19:15 +01:00
|
|
|
template<typename T>
|
|
|
|
struct StringMaker<T*> {
|
2013-07-03 09:25:11 +02:00
|
|
|
template<typename U>
|
|
|
|
static std::string convert( U* p ) {
|
2012-05-08 09:10:49 +02:00
|
|
|
if( !p )
|
|
|
|
return INTERNAL_CATCH_STRINGIFY( NULL );
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << p;
|
|
|
|
return oss.str();
|
2013-03-04 12:19:15 +01:00
|
|
|
}
|
|
|
|
};
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2013-09-17 23:22:47 +02:00
|
|
|
namespace Detail {
|
|
|
|
template<typename InputIterator>
|
2013-09-21 19:45:42 +02:00
|
|
|
std::string rangeToString( InputIterator first, InputIterator last );
|
2013-09-17 23:22:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename Allocator>
|
|
|
|
struct StringMaker<std::vector<T, Allocator> > {
|
|
|
|
static std::string convert( std::vector<T,Allocator> const& v ) {
|
|
|
|
return Detail::rangeToString( v.begin(), v.end() );
|
|
|
|
}
|
2013-03-04 12:19:15 +01:00
|
|
|
};
|
2012-05-08 09:10:49 +02:00
|
|
|
|
2013-03-04 12:19:15 +01:00
|
|
|
namespace Detail {
|
|
|
|
template<typename T>
|
2013-04-23 19:58:56 +02:00
|
|
|
inline std::string makeString( T const& value ) {
|
2013-03-04 12:19:15 +01:00
|
|
|
return StringMaker<T>::convert( value );
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
} // end namespace Detail
|
|
|
|
|
|
|
|
/// \brief converts any type to a string
|
|
|
|
///
|
2013-07-03 20:14:59 +02:00
|
|
|
/// The default template forwards on to ostringstream - except when an
|
|
|
|
/// ostringstream overload does not exist - in which case it attempts to detect
|
2012-05-08 09:10:49 +02:00
|
|
|
/// that and writes {?}.
|
|
|
|
/// Overload (not specialise) this template for custom typs that you don't want
|
|
|
|
/// to provide an ostream overload for.
|
|
|
|
template<typename T>
|
2013-04-23 19:58:56 +02:00
|
|
|
std::string toString( T const& value ) {
|
2013-03-04 12:19:15 +01:00
|
|
|
return StringMaker<T>::convert( value );
|
2012-05-08 09:10:49 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
// Built in overloads
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
inline std::string toString( std::string const& value ) {
|
2012-05-08 09:10:49 +02:00
|
|
|
return "\"" + value + "\"";
|
|
|
|
}
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
inline std::string toString( std::wstring const& value ) {
|
2012-05-08 09:10:49 +02:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "\"";
|
|
|
|
for(size_t i = 0; i < value.size(); ++i )
|
|
|
|
oss << static_cast<char>( value[i] <= 0xff ? value[i] : '?');
|
|
|
|
oss << "\"";
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string toString( const char* const value ) {
|
|
|
|
return value ? Catch::toString( std::string( value ) ) : std::string( "{null string}" );
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
|
|
|
|
inline std::string toString( char* const value ) {
|
|
|
|
return Catch::toString( static_cast<const char*>( value ) );
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
inline std::string toString( unsigned int value ) {
|
2012-05-11 20:05:53 +02:00
|
|
|
return toString( static_cast<unsigned long>( value ) );
|
2012-05-08 09:10:49 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
inline std::string toString( const double value ) {
|
|
|
|
std::ostringstream oss;
|
2013-08-15 19:39:55 +02:00
|
|
|
oss << std::setprecision( 10 )
|
|
|
|
<< std::fixed
|
2013-03-04 12:19:15 +01:00
|
|
|
<< value;
|
2013-08-15 19:39:55 +02:00
|
|
|
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;
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2012-05-08 09:10:49 +02:00
|
|
|
|
|
|
|
inline std::string toString( bool value ) {
|
|
|
|
return value ? "true" : "false";
|
|
|
|
}
|
|
|
|
|
2012-06-06 09:06:40 +02:00
|
|
|
inline std::string toString( char value ) {
|
|
|
|
return value < ' '
|
2012-12-14 08:49:18 +01:00
|
|
|
? toString( static_cast<unsigned int>( value ) )
|
2012-06-06 09:06:40 +02:00
|
|
|
: Detail::makeString( value );
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string toString( signed char value ) {
|
|
|
|
return toString( static_cast<char>( value ) );
|
|
|
|
}
|
|
|
|
|
2012-12-14 08:49:18 +01:00
|
|
|
inline std::string toString( unsigned char value ) {
|
|
|
|
return toString( static_cast<char>( value ) );
|
|
|
|
}
|
|
|
|
|
2012-05-10 08:58:48 +02:00
|
|
|
#ifdef CATCH_CONFIG_CPP11_NULLPTR
|
2012-05-23 12:22:49 +02:00
|
|
|
inline std::string toString( std::nullptr_t ) {
|
2012-05-10 08:58:48 +02:00
|
|
|
return "nullptr";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-01 09:17:07 +02:00
|
|
|
#ifdef __OBJC__
|
|
|
|
inline std::string toString( NSString const * const& nsstring ) {
|
2013-03-12 19:49:22 +01:00
|
|
|
if( !nsstring )
|
|
|
|
return "nil";
|
2012-08-01 09:17:07 +02:00
|
|
|
return std::string( "@\"" ) + [nsstring UTF8String] + "\"";
|
|
|
|
}
|
|
|
|
inline std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) {
|
2013-03-12 19:49:22 +01:00
|
|
|
if( !nsstring )
|
|
|
|
return "nil";
|
2012-08-01 09:17:07 +02:00
|
|
|
return std::string( "@\"" ) + [nsstring UTF8String] + "\"";
|
|
|
|
}
|
|
|
|
inline std::string toString( NSObject* const& nsObject ) {
|
|
|
|
return toString( [nsObject description] );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-21 19:45:42 +02:00
|
|
|
namespace Detail {
|
|
|
|
template<typename InputIterator>
|
|
|
|
std::string rangeToString( InputIterator first, InputIterator last ) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "{ ";
|
|
|
|
if( first != last ) {
|
|
|
|
oss << toString( *first );
|
|
|
|
for( ++first ; first != last ; ++first ) {
|
|
|
|
oss << ", " << toString( *first );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oss << " }";
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 09:10:49 +02:00
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_TOSTRING_HPP_INCLUDED
|