Reworked stringification layer, removed Catch::toString

Now the order of stringification checks is

1) StringMaker specialization
2) operator<<

toString overloads and specializations have been removed.
This commit is contained in:
Martin Hořeňovský 2017-05-02 23:51:03 +02:00
parent 40130e59b4
commit 33ed1773f4
20 changed files with 626 additions and 557 deletions

View File

@ -127,8 +127,8 @@ LeakDetector leakDetector;
#define CATCH_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg ) #define CATCH_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg )
#define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( "CATCH_WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) #define CATCH_WARN( msg ) INTERNAL_CATCH_MSG( "CATCH_WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg )
#define CATCH_SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg ) #define CATCH_SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg )
#define CATCH_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CATCH_CAPTURE", #msg " := " << Catch::toString(msg) ) #define CATCH_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CATCH_CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) )
#define CATCH_SCOPED_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CATCH_CAPTURE", #msg " := " << Catch::toString(msg) ) #define CATCH_SCOPED_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CATCH_CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) )
#define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) #define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
#define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) #define CATCH_TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ )
@ -194,8 +194,8 @@ LeakDetector leakDetector;
#define INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg ) #define INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg )
#define WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) #define WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg )
#define SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg ) #define SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg )
#define CAPTURE( msg ) INTERNAL_CATCH_INFO( "CAPTURE", #msg " := " << Catch::toString(msg) ) #define CAPTURE( msg ) INTERNAL_CATCH_INFO( "CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) )
#define SCOPED_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CAPTURE", #msg " := " << Catch::toString(msg) ) #define SCOPED_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CAPTURE", #msg " := " << ::Catch::Detail::stringify(msg) )
#define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) #define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ )
#define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) #define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ )

View File

@ -114,7 +114,7 @@ namespace Detail {
std::string toString() const { std::string toString() const {
std::ostringstream oss; std::ostringstream oss;
oss << "Approx( " << Catch::toString( m_value ) << " )"; oss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )";
return oss.str(); return oss.str();
} }
@ -127,9 +127,11 @@ namespace Detail {
} }
template<> template<>
inline std::string toString<Detail::Approx>( Detail::Approx const& value ) { struct StringMaker<Catch::Detail::Approx> {
std::string operator()(Catch::Detail::Approx const& value) {
return value.toString(); return value.toString();
} }
};
} // end namespace Catch } // end namespace Catch

View File

@ -81,7 +81,7 @@ public:
} }
virtual void reconstructExpression( std::string& dest ) const override { virtual void reconstructExpression( std::string& dest ) const override {
dest = Catch::toString( m_truthy ); dest = ::Catch::Detail::stringify( m_truthy );
} }
private: private:
@ -120,8 +120,8 @@ public:
} }
virtual void reconstructExpression( std::string& dest ) const override { virtual void reconstructExpression( std::string& dest ) const override {
std::string lhs = Catch::toString( m_lhs ); std::string lhs = ::Catch::Detail::stringify( m_lhs );
std::string rhs = Catch::toString( m_rhs ); std::string rhs = ::Catch::Detail::stringify( m_rhs );
char delim = lhs.size() + rhs.size() < 40 && char delim = lhs.size() + rhs.size() < 40 &&
lhs.find('\n') == std::string::npos && lhs.find('\n') == std::string::npos &&
rhs.find('\n') == std::string::npos ? ' ' : '\n'; rhs.find('\n') == std::string::npos ? ' ' : '\n';
@ -155,7 +155,7 @@ public:
virtual void reconstructExpression( std::string& dest ) const override { virtual void reconstructExpression( std::string& dest ) const override {
std::string matcherAsString = m_matcher.toString(); std::string matcherAsString = m_matcher.toString();
dest = Catch::toString( m_arg ); dest = ::Catch::Detail::stringify( m_arg );
dest += ' '; dest += ' ';
if( matcherAsString == Detail::unprintableString ) if( matcherAsString == Detail::unprintableString )
dest += m_matcherString; dest += m_matcherString;

View File

@ -25,7 +25,7 @@ namespace Matchers {
} }
virtual std::string describe() const override { virtual std::string describe() const override {
return "Contains: " + Catch::toString( m_comparator ); return "Contains: " + ::Catch::Detail::stringify( m_comparator );
} }
T const& m_comparator; T const& m_comparator;
@ -46,7 +46,7 @@ namespace Matchers {
return true; return true;
} }
virtual std::string describe() const override { virtual std::string describe() const override {
return "Contains: " + Catch::toString( m_comparator ); return "Contains: " + ::Catch::Detail::stringify( m_comparator );
} }
std::vector<T> const& m_comparator; std::vector<T> const& m_comparator;
@ -70,7 +70,7 @@ namespace Matchers {
return true; return true;
} }
virtual std::string describe() const override { virtual std::string describe() const override {
return "Equals: " + Catch::toString( m_comparator ); return "Equals: " + ::Catch::Detail::stringify( m_comparator );
} }
std::vector<T> const& m_comparator; std::vector<T> const& m_comparator;
}; };

View File

@ -24,45 +24,23 @@
#include <type_traits> #include <type_traits>
#include <string>
// We need a dummy global operator<< so we can bring it into Catch namespace later
struct Catch_global_namespace_dummy;
std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy);
namespace Catch { namespace Catch {
// Bring in operator<< from global namespace into Catch namespace
// Why we're here. using ::operator<<;
template<typename T>
std::string toString( T const& value );
// Built in overloads
std::string toString( std::string const& value );
std::string toString( std::wstring const& value );
std::string toString( const char* const value );
std::string toString( char* const value );
std::string toString( const wchar_t* const value );
std::string toString( wchar_t* const value );
std::string toString( int value );
std::string toString( unsigned long value );
std::string toString( unsigned int value );
std::string toString( const double value );
std::string toString( const float value );
std::string toString( bool value );
std::string toString( char value );
std::string toString( signed char value );
std::string toString( unsigned char value );
std::string toString( long long value );
std::string toString( unsigned long long value );
std::string toString( std::nullptr_t );
#ifdef __OBJC__
std::string toString( NSString const * const& nsstring );
std::string toString( NSString * CATCH_ARC_STRONG const& nsstring );
std::string toString( NSObject* const& nsObject );
#endif
namespace Detail { namespace Detail {
std::string rawMemoryToString( const void *object, std::size_t size );
extern const std::string unprintableString; template<typename T>
std::string rawMemoryToString( const T& object ) {
return rawMemoryToString( &object, sizeof(object) );
}
template<typename T> template<typename T>
class IsStreamInsertable { class IsStreamInsertable {
@ -77,173 +55,265 @@ namespace Detail {
static const bool value = decltype(test<std::ostream, const T&>(0))::value; static const bool value = decltype(test<std::ostream, const T&>(0))::value;
}; };
} // namespace Detail
template<typename T, // If we decide for C++14, change these to enable_if_ts
bool IsEnum = std::is_enum<T>::value
>
struct EnumStringMaker
{
static std::string convert( T const& ) { return unprintableString; }
};
template <typename T> template <typename T>
struct EnumStringMaker<T,true> struct StringMaker {
{ template <typename Fake = T>
static std::string convert( T const& v ) typename std::enable_if<::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
{ operator()(const Fake& t) {
return ::Catch::toString( std::stringstream sstr;
static_cast<typename std::underlying_type<T>::type>(v) sstr << t;
); return sstr.str();
}
template <typename Fake = T>
typename std::enable_if<!::Catch::Detail::IsStreamInsertable<Fake>::value, std::string>::type
operator()(const Fake&) {
return "{?}";
} }
}; };
template<bool C> namespace Detail {
struct StringMakerBase {
extern const std::string unprintableString;
// This function dispatches all stringification requests inside of Catch.
// Should be preferably called fully qualified, like ::Catch::Detail::stringify
template <typename T> template <typename T>
static std::string convert( T const& v ) std::string stringify(const T& e) {
{ return ::Catch::StringMaker<typename std::remove_cv<typename std::remove_reference<T>::type>::type>{}(e);
return EnumStringMaker<T>::convert( v ); }
} // namespace Detail
// Some predefined specializations
template<>
struct StringMaker<std::string> {
std::string operator()(const std::string& str);
};
template<>
struct StringMaker<std::wstring> {
std::string operator()(const std::wstring& wstr);
};
template<>
struct StringMaker<char const *> {
std::string operator()(char const * str);
};
template<>
struct StringMaker<char *> {
std::string operator()(char * str);
};
template<>
struct StringMaker<wchar_t const *> {
std::string operator()(wchar_t const * str);
};
template<>
struct StringMaker<wchar_t *> {
std::string operator()(wchar_t * str);
};
template<int SZ>
struct StringMaker<char[SZ]> {
std::string operator()(const char* str) {
return ::Catch::Detail::stringify(std::string{ str });
}
};
template<int SZ>
struct StringMaker<signed char[SZ]> {
std::string operator()(const char* str) {
return ::Catch::Detail::stringify(std::string{ str });
}
};
template<int SZ>
struct StringMaker<unsigned char[SZ]> {
std::string operator()(const char* str) {
return ::Catch::Detail::stringify(std::string{ str });
} }
}; };
template<> template<>
struct StringMakerBase<true> { struct StringMaker<int> {
template<typename T> std::string operator()(int value);
static std::string convert( T const& _value ) { };
std::ostringstream oss; template<>
oss << _value; struct StringMaker<long> {
return oss.str(); std::string operator()(long value);
} };
template<>
struct StringMaker<long long> {
std::string operator()(long long value);
};
template<>
struct StringMaker<unsigned int> {
std::string operator()(unsigned int value);
};
template<>
struct StringMaker<unsigned long> {
std::string operator()(unsigned long value);
};
template<>
struct StringMaker<unsigned long long> {
std::string operator()(unsigned long long value);
}; };
std::string rawMemoryToString( const void *object, std::size_t size ); template<>
struct StringMaker<bool> {
std::string operator()(bool b);
};
template<typename T> template<>
inline std::string rawMemoryToString( const T& object ) { struct StringMaker<char> {
return rawMemoryToString( &object, sizeof(object) ); std::string operator()(char c);
} };
template<>
struct StringMaker<signed char> {
std::string operator()(signed char c);
};
template<>
struct StringMaker<unsigned char> {
std::string operator()(unsigned char c);
};
} // end namespace Detail template<>
struct StringMaker<nullptr_t> {
std::string operator()(std::nullptr_t);
};
template<typename T> template<>
struct StringMaker : struct StringMaker<float> {
Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {}; std::string operator()(float value);
};
template<>
struct StringMaker<double> {
std::string operator()(double value);
};
template <typename T> template <typename T>
struct StringMaker<T*> { struct StringMaker<T*> {
template <typename U> template <typename U>
static std::string convert( U* p ) { std::string operator()(U* p) {
if( !p ) if (p) {
return "NULL"; return ::Catch::Detail::rawMemoryToString(p);
else } else {
return Detail::rawMemoryToString( p ); return "nullptr";
}
} }
}; };
template <typename R, typename C> template <typename R, typename C>
struct StringMaker<R C::*> { struct StringMaker<R C::*> {
static std::string convert( R C::* p ) { std::string operator()(R C::* p) {
if( !p ) if (p) {
return "NULL"; return ::Catch::Detail::rawMemoryToString(p);
else } else {
return Detail::rawMemoryToString( p ); return "nullptr";
}
} }
}; };
namespace Detail {
template<typename InputIterator>
std::string rangeToString( InputIterator first, InputIterator last );
}
//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() );
// }
//};
template<typename T, typename Allocator>
std::string toString( std::vector<T,Allocator> const& v ) {
return Detail::rangeToString( v.begin(), v.end() );
}
// toString for tuples
namespace TupleDetail {
template<
typename Tuple,
std::size_t N = 0,
bool = (N < std::tuple_size<Tuple>::value)
>
struct ElementPrinter {
static void print( const Tuple& tuple, std::ostream& os )
{
os << ( N ? ", " : " " )
<< Catch::toString(std::get<N>(tuple));
ElementPrinter<Tuple,N+1>::print(tuple,os);
}
};
template<
typename Tuple,
std::size_t N
>
struct ElementPrinter<Tuple,N,false> {
static void print( const Tuple&, std::ostream& ) {}
};
}
template<typename ...Types>
struct StringMaker<std::tuple<Types...>> {
static std::string convert( const std::tuple<Types...>& tuple )
{
std::ostringstream os;
os << '{';
TupleDetail::ElementPrinter<std::tuple<Types...>>::print( tuple, os );
os << " }";
return os.str();
}
};
namespace Detail {
template<typename T>
std::string makeString( T const& value ) {
return StringMaker<T>::convert( value );
}
} // end namespace Detail
/// \brief converts any type to a string
///
/// The default template forwards on to ostringstream - except when an
/// ostringstream overload does not exist - in which case it attempts to detect
/// 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>
std::string toString( T const& value ) {
return StringMaker<T>::convert( value );
}
namespace Detail { namespace Detail {
template<typename InputIterator> template<typename InputIterator>
std::string rangeToString(InputIterator first, InputIterator last) { std::string rangeToString(InputIterator first, InputIterator last) {
std::ostringstream oss; std::ostringstream oss;
oss << "{ "; oss << "{ ";
if (first != last) { if (first != last) {
oss << Catch::toString( *first ); oss << ::Catch::Detail::stringify(*first);
for (++first; first != last; ++first) for (++first; first != last; ++first)
oss << ", " << Catch::toString( *first ); oss << ", " << ::Catch::Detail::stringify(*first);
} }
oss << " }"; oss << " }";
return oss.str(); return oss.str();
} }
} }
} // end namespace Catch template<typename T, typename Allocator>
struct StringMaker<std::vector<T, Allocator> > {
std::string operator()( std::vector<T,Allocator> const& v ) {
return ::Catch::Detail::rangeToString( v.begin(), v.end() );
}
};
// === Pair ===
template<typename T1, typename T2>
struct StringMaker<std::pair<T1, T2> > {
std::string operator()(const std::pair<T1, T2>& pair) {
std::ostringstream oss;
oss << "{ "
<< ::Catch::Detail::stringify(pair.first)
<< ", "
<< ::Catch::Detail::stringify(pair.second)
<< " }";
return oss.str();
}
};
namespace Detail {
template<
typename Tuple,
std::size_t N = 0,
bool = (N < std::tuple_size<Tuple>::value)
>
struct TupleElementPrinter {
static void print(const Tuple& tuple, std::ostream& os) {
os << (N ? ", " : " ")
<< ::Catch::Detail::stringify(std::get<N>(tuple));
TupleElementPrinter<Tuple, N + 1>::print(tuple, os);
}
};
template<
typename Tuple,
std::size_t N
>
struct TupleElementPrinter<Tuple, N, false> {
static void print(const Tuple&, std::ostream&) {}
};
}
template<typename ...Types>
struct StringMaker<std::tuple<Types...>> {
std::string operator()(const std::tuple<Types...>& tuple) {
std::ostringstream os;
os << '{';
Detail::TupleElementPrinter<std::tuple<Types...>>::print(tuple, os);
os << " }";
return os.str();
}
};
template<typename T>
struct EnumStringMaker {
std::string operator()(const T& t) {
return ::Catch::Detail::stringify(static_cast<typename std::underlying_type<T>::type>(t));
}
};
#ifdef __OBJC__
template<>
struct StringMaker<NSString const *> {
std::string operator()(NSString const* const& nsstring);
};
template<>
struct StringMaker<NSString * CATCH_ARC_STRONG> {
std::string operator()(NSString * CATCH_ARC_STRONG const& nsstring);
};
template<>
struct StringMaker<NSObject *> {
std::string operator()(NSObject* const& nsObject);
};
#endif
} // namespace Catch
#endif // TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED #endif // TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED

View File

@ -35,8 +35,7 @@ namespace Detail {
}; };
} }
std::string rawMemoryToString( const void *object, std::size_t size ) std::string rawMemoryToString( const void *object, std::size_t size ) {
{
// Reverse order for little endian architectures // Reverse order for little endian architectures
int i = 0, end = static_cast<int>( size ), inc = 1; int i = 0, end = static_cast<int>( size ), inc = 1;
if( Endianness::which() == Endianness::Little ) { if( Endianness::which() == Endianness::Little ) {
@ -53,70 +52,6 @@ namespace Detail {
} }
} }
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 Catch::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( const wchar_t* const value )
{
return value ? Catch::toString( std::wstring(value) ) : std::string( "{null string}" );
}
std::string toString( wchar_t* const value )
{
return Catch::toString( static_cast<const wchar_t*>( value ) );
}
std::string toString( int value ) {
std::ostringstream oss;
oss << value;
if( value > Detail::hexThreshold )
oss << " (0x" << std::hex << value << ')';
return oss.str();
}
std::string toString( unsigned long value ) {
std::ostringstream oss;
oss << value;
if( value > Detail::hexThreshold )
oss << " (0x" << std::hex << value << ')';
return oss.str();
}
std::string toString( unsigned int value ) {
return Catch::toString( static_cast<unsigned long>( value ) );
}
template<typename T> template<typename T>
std::string fpToString( T value, int precision ) { std::string fpToString( T value, int precision ) {
@ -134,73 +69,159 @@ std::string fpToString( T value, int precision ) {
return d; return d;
} }
std::string toString( const double value ) {
return fpToString( value, 10 ); //// ======================================================= ////
} //
std::string toString( const float value ) { // Out-of-line defs for full specialization of StringMaker
return fpToString( value, 5 ) + 'f'; //
//// ======================================================= ////
std::string StringMaker<std::string>::operator()(const std::string& str) {
if (!getCurrentContext().getConfig()->showInvisibles()) {
return '"' + str + '"';
} }
std::string toString( bool value ) { std::string s("\"");
return value ? "true" : "false"; for (char c : str) {
switch (c) {
case '\n':
s.append("\\n");
break;
case '\t':
s.append("\\t");
break;
default:
s.push_back(c);
break;
}
}
s.append("\"");
return s;
} }
std::string toString( char value ) { std::string StringMaker<std::wstring>::operator()(const std::wstring& wstr) {
if ( value == '\r' ) std::string s;
s.reserve(wstr.size());
for (auto c : wstr) {
s += (c <= 0xff) ? static_cast<char>(c) : '?';
}
return ::Catch::Detail::stringify(s);
}
std::string StringMaker<char const*>::operator()(char const* str) {
if (str) {
return ::Catch::Detail::stringify(std::string{ str });
} else {
return{ "{null string}" };
}
}
std::string StringMaker<char*>::operator()(char* str) {
if (str) {
return ::Catch::Detail::stringify(std::string{ str });
} else {
return{ "{null string}" };
}
}
std::string StringMaker<wchar_t const*>::operator()(wchar_t const * str) {
if (str) {
return ::Catch::Detail::stringify(std::wstring{ str });
} else {
return{ "{null string}" };
}
}
std::string StringMaker<wchar_t *>::operator()(wchar_t * str) {
if (str) {
return ::Catch::Detail::stringify(std::wstring{ str });
} else {
return{ "{null string}" };
}
}
std::string StringMaker<int>::operator()(int value) {
return ::Catch::Detail::stringify(static_cast<long long>(value));
}
std::string StringMaker<long>::operator()(long value) {
return ::Catch::Detail::stringify(static_cast<long long>(value));
}
std::string StringMaker<long long>::operator()(long long value) {
std::ostringstream oss;
oss << value;
if (value > Detail::hexThreshold) {
oss << " (0x" << std::hex << value << ')';
}
return oss.str();
}
std::string StringMaker<unsigned int>::operator()(unsigned int value) {
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
}
std::string StringMaker<unsigned long>::operator()(unsigned long value) {
return ::Catch::Detail::stringify(static_cast<unsigned long long>(value));
}
std::string StringMaker<unsigned long long>::operator()(unsigned long long value) {
std::ostringstream oss;
oss << value;
if (value > Detail::hexThreshold) {
oss << " (0x" << std::hex << value << ')';
}
return oss.str();
}
std::string StringMaker<bool>::operator()(bool b) {
return b ? "true" : "false";
}
std::string StringMaker<char>::operator()(char value) {
if (value == '\r') {
return "'\\r'"; return "'\\r'";
if ( value == '\f' ) } else if (value == '\f') {
return "'\\f'"; return "'\\f'";
if ( value == '\n' ) } else if (value == '\n') {
return "'\\n'"; return "'\\n'";
if ( value == '\t' ) } else if (value == '\t') {
return "'\\t'"; return "'\\t'";
if ( '\0' <= value && value < ' ' ) } else if ('\0' <= value && value < ' ') {
return toString( static_cast<unsigned int>( value ) ); return ::Catch::Detail::stringify(static_cast<unsigned int>(value));
} else {
char chstr[] = "' '"; char chstr[] = "' '";
chstr[1] = value; chstr[1] = value;
return chstr; return chstr;
} }
}
std::string toString( signed char value ) { std::string StringMaker<signed char>::operator()(signed char c) {
return toString( static_cast<char>( value ) ); return ::Catch::Detail::stringify(static_cast<char>(c));
}
std::string StringMaker<unsigned char>::operator()(unsigned char c) {
return ::Catch::Detail::stringify(static_cast<char>(c));
} }
std::string toString( unsigned char value ) { std::string StringMaker<nullptr_t>::operator()(std::nullptr_t) {
return toString( static_cast<char>( value ) );
}
std::string toString( long long value ) {
std::ostringstream oss;
oss << value;
if( value > Detail::hexThreshold )
oss << " (0x" << std::hex << value << ')';
return oss.str();
}
std::string toString( unsigned long long value ) {
std::ostringstream oss;
oss << value;
if( value > Detail::hexThreshold )
oss << " (0x" << std::hex << value << ')';
return oss.str();
}
std::string toString( std::nullptr_t ) {
return "nullptr"; return "nullptr";
} }
std::string StringMaker<float>::operator()(float value) {
return fpToString(value, 5) + 'f';
}
std::string StringMaker<double>::operator()(double value) {
return fpToString(value, 10);
}
#ifdef __OBJC__ #ifdef __OBJC__
std::string toString( NSString const * const& nsstring ) { std::string StringMaker<NSString const *>::operator()(NSString const * const& nsstring) {
if (!nsstring) if (!nsstring)
return "nil"; return "nil";
return "@" + toString([nsstring UTF8String]); return "@" + toString([nsstring UTF8String]);
} }
std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) { std::string StringMaker<NSString * CATCH_ARC_STRONG>::operator()(NSString * CATCH_ARC_STRONG const& nsstring) {
if (!nsstring) if (!nsstring)
return "nil"; return "nil";
return "@" + toString([nsstring UTF8String]); return "@" + toString([nsstring UTF8String]);
} }
std::string toString( NSObject* const& nsObject ) { std::string StringMaker<NSObject *>::operator()(NSObject * const& nsObject) {
return toString( [nsObject description] ); return ::Catch::Detail::stringify([nsObject description]);
} }
#endif #endif

View File

@ -160,7 +160,7 @@ namespace Catch {
xml.writeAttribute( "classname", className ); xml.writeAttribute( "classname", className );
xml.writeAttribute( "name", name ); xml.writeAttribute( "name", name );
} }
xml.writeAttribute( "time", Catch::toString( sectionNode.stats.durationInSeconds ) ); xml.writeAttribute( "time", ::Catch::Detail::stringify( sectionNode.stats.durationInSeconds ) );
writeAssertions( sectionNode ); writeAssertions( sectionNode );

View File

@ -119,7 +119,7 @@ with expansion:
MiscTests.cpp:<line number>: MiscTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( (*__error()) == 1 ) REQUIRE( (*__errno_location ()) == 1 )
with expansion: with expansion:
1 == 1 1 == 1
@ -5873,7 +5873,7 @@ TrickyTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( (std::pair<int, int>( 1, 2 )) == aNicePair ) REQUIRE( (std::pair<int, int>( 1, 2 )) == aNicePair )
with expansion: with expansion:
std::pair( 1, 2 ) == std::pair( 1, 2 ) { 1, 2 } == { 1, 2 }
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Pointers can be compared to null Pointers can be compared to null
@ -5885,13 +5885,13 @@ ConditionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( p == 0 ) REQUIRE( p == 0 )
with expansion: with expansion:
NULL == 0 0 == 0
ConditionTests.cpp:<line number>: ConditionTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( p == pNULL ) REQUIRE( p == pNULL )
with expansion: with expansion:
NULL == NULL 0 == 0
ConditionTests.cpp:<line number>: ConditionTests.cpp:<line number>:
PASSED: PASSED:
@ -8864,7 +8864,7 @@ ToStringPair.cpp:<line number>
ToStringPair.cpp:<line number>: ToStringPair.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" ) REQUIRE( ::Catch::Detail::stringify( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" )
with expansion: with expansion:
"{ { 42, "Arthur" }, { "Ford", 24 } }" "{ { 42, "Arthur" }, { "Ford", 24 } }"
== ==
@ -8880,7 +8880,7 @@ TrickyTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( p == 0 ) REQUIRE( p == 0 )
with expansion: with expansion:
NULL == 0 0 == 0
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
random SECTION tests random SECTION tests
@ -9078,7 +9078,7 @@ ToStringPair.cpp:<line number>
ToStringPair.cpp:<line number>: ToStringPair.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString(value) == "{ 34, \"xyzzy\" }" ) REQUIRE( ::Catch::Detail::stringify(value) == "{ 34, \"xyzzy\" }" )
with expansion: with expansion:
"{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }"
@ -9090,7 +9090,7 @@ ToStringPair.cpp:<line number>
ToStringPair.cpp:<line number>: ToStringPair.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString( value ) == "{ 34, \"xyzzy\" }" ) REQUIRE( ::Catch::Detail::stringify( value ) == "{ 34, \"xyzzy\" }" )
with expansion: with expansion:
"{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }"
@ -9102,7 +9102,7 @@ ToStringPair.cpp:<line number>
ToStringPair.cpp:<line number>: ToStringPair.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString( pr ) == "{ { \"green\", 55 } }" ) REQUIRE( ::Catch::Detail::stringify( pr ) == "{ { \"green\", 55 } }" )
with expansion: with expansion:
"{ { "green", 55 } }" "{ { "green", 55 } }"
== ==
@ -9119,6 +9119,48 @@ TrickyTests.cpp:<line number>: FAILED:
with expansion: with expansion:
"first" == "second" "first" == "second"
-------------------------------------------------------------------------------
stringify( has_maker )
-------------------------------------------------------------------------------
ToStringWhich.cpp:<line number>
...............................................................................
ToStringWhich.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker<has_maker>" )
with expansion:
"StringMaker<has_maker>"
==
"StringMaker<has_maker>"
-------------------------------------------------------------------------------
stringify( has_maker_and_toString )
-------------------------------------------------------------------------------
ToStringWhich.cpp:<line number>
...............................................................................
ToStringWhich.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker<has_maker_and_operator>" )
with expansion:
"StringMaker<has_maker_and_operator>"
==
"StringMaker<has_maker_and_operator>"
-------------------------------------------------------------------------------
stringify( has_operator )
-------------------------------------------------------------------------------
ToStringWhich.cpp:<line number>
...............................................................................
ToStringWhich.cpp:<line number>:
PASSED:
REQUIRE( ::Catch::Detail::stringify( item ) == "operator<<( has_operator )" )
with expansion:
"operator<<( has_operator )"
==
"operator<<( has_operator )"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
toString on const wchar_t const pointer returns the string contents toString on const wchar_t const pointer returns the string contents
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -9167,48 +9209,6 @@ PASSED:
with expansion: with expansion:
""wide load"" == ""wide load"" ""wide load"" == ""wide load""
-------------------------------------------------------------------------------
toString( has_maker )
-------------------------------------------------------------------------------
ToStringWhich.cpp:<line number>
...............................................................................
ToStringWhich.cpp:<line number>:
PASSED:
REQUIRE( Catch::toString( item ) == "StringMaker<has_maker>" )
with expansion:
"StringMaker<has_maker>"
==
"StringMaker<has_maker>"
-------------------------------------------------------------------------------
toString( has_maker_and_toString )
-------------------------------------------------------------------------------
ToStringWhich.cpp:<line number>
...............................................................................
ToStringWhich.cpp:<line number>:
PASSED:
REQUIRE( Catch::toString( item ) == "toString( has_maker_and_toString )" )
with expansion:
"toString( has_maker_and_toString )"
==
"toString( has_maker_and_toString )"
-------------------------------------------------------------------------------
toString( has_toString )
-------------------------------------------------------------------------------
ToStringWhich.cpp:<line number>
...............................................................................
ToStringWhich.cpp:<line number>:
PASSED:
REQUIRE( Catch::toString( item ) == "toString( has_toString )" )
with expansion:
"toString( has_toString )"
==
"toString( has_toString )"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
toString( vectors<has_maker ) toString( vectors<has_maker )
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -9217,7 +9217,7 @@ ToStringWhich.cpp:<line number>
ToStringWhich.cpp:<line number>: ToStringWhich.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString( v ) == "{ StringMaker<has_maker> }" ) REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker> }" )
with expansion: with expansion:
"{ StringMaker<has_maker> }" "{ StringMaker<has_maker> }"
== ==
@ -9231,13 +9231,13 @@ EnumToString.cpp:<line number>
EnumToString.cpp:<line number>: EnumToString.cpp:<line number>:
PASSED: PASSED:
CHECK( Catch::toString(e0) == "E2{0}" ) CHECK( ::Catch::Detail::stringify(e0) == "E2{0}" )
with expansion: with expansion:
"E2{0}" == "E2{0}" "E2{0}" == "E2{0}"
EnumToString.cpp:<line number>: EnumToString.cpp:<line number>:
PASSED: PASSED:
CHECK( Catch::toString(e1) == "E2{1}" ) CHECK( ::Catch::Detail::stringify(e1) == "E2{1}" )
with expansion: with expansion:
"E2{1}" == "E2{1}" "E2{1}" == "E2{1}"
@ -9249,13 +9249,13 @@ EnumToString.cpp:<line number>
EnumToString.cpp:<line number>: EnumToString.cpp:<line number>:
PASSED: PASSED:
CHECK( Catch::toString(e0) == "0" ) CHECK( ::Catch::Detail::stringify(e0) == "0" )
with expansion: with expansion:
"0" == "0" "0" == "0"
EnumToString.cpp:<line number>: EnumToString.cpp:<line number>:
PASSED: PASSED:
CHECK( Catch::toString(e1) == "1" ) CHECK( ::Catch::Detail::stringify(e1) == "1" )
with expansion: with expansion:
"1" == "1" "1" == "1"
@ -9267,19 +9267,19 @@ ToStringVector.cpp:<line number>
ToStringVector.cpp:<line number>: ToStringVector.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString(vv) == "{ }" ) REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" )
with expansion: with expansion:
"{ }" == "{ }" "{ }" == "{ }"
ToStringVector.cpp:<line number>: ToStringVector.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString(vv) == "{ 42 }" ) REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" )
with expansion: with expansion:
"{ 42 }" == "{ 42 }" "{ 42 }" == "{ 42 }"
ToStringVector.cpp:<line number>: ToStringVector.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ) REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" )
with expansion: with expansion:
"{ 42, 250 }" == "{ 42, 250 }" "{ 42, 250 }" == "{ 42, 250 }"
@ -9291,19 +9291,19 @@ ToStringVector.cpp:<line number>
ToStringVector.cpp:<line number>: ToStringVector.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString(vv) == "{ }" ) REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" )
with expansion: with expansion:
"{ }" == "{ }" "{ }" == "{ }"
ToStringVector.cpp:<line number>: ToStringVector.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString(vv) == "{ \"hello\" }" ) REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" )
with expansion: with expansion:
"{ "hello" }" == "{ "hello" }" "{ "hello" }" == "{ "hello" }"
ToStringVector.cpp:<line number>: ToStringVector.cpp:<line number>:
PASSED: PASSED:
REQUIRE( Catch::toString(vv) == "{ \"hello\", \"world\" }" ) REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" )
with expansion: with expansion:
"{ "hello", "world" }" "{ "hello", "world" }"
== ==

View File

@ -119,7 +119,7 @@ with expansion:
MiscTests.cpp:<line number>: MiscTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( (*__error()) == 1 ) REQUIRE( (*__errno_location ()) == 1 )
with expansion: with expansion:
1 == 1 1 == 1

View File

@ -715,13 +715,13 @@ MessageTests.cpp:<line number>
TrickyTests.cpp:<line number> TrickyTests.cpp:<line number>
</failure> </failure>
</testcase> </testcase>
<testcase classname="global" name="stringify( has_maker )" time="{duration}"/>
<testcase classname="global" name="stringify( has_maker_and_toString )" time="{duration}"/>
<testcase classname="global" name="stringify( has_operator )" time="{duration}"/>
<testcase classname="global" name="toString on const wchar_t const pointer returns the string contents" time="{duration}"/> <testcase classname="global" name="toString on const wchar_t const pointer returns the string contents" time="{duration}"/>
<testcase classname="global" name="toString on const wchar_t pointer returns the string contents" time="{duration}"/> <testcase classname="global" name="toString on const wchar_t pointer returns the string contents" time="{duration}"/>
<testcase classname="global" name="toString on wchar_t const pointer returns the string contents" time="{duration}"/> <testcase classname="global" name="toString on wchar_t const pointer returns the string contents" time="{duration}"/>
<testcase classname="global" name="toString on wchar_t returns the string contents" time="{duration}"/> <testcase classname="global" name="toString on wchar_t returns the string contents" time="{duration}"/>
<testcase classname="global" name="toString( has_maker )" time="{duration}"/>
<testcase classname="global" name="toString( has_maker_and_toString )" time="{duration}"/>
<testcase classname="global" name="toString( has_toString )" time="{duration}"/>
<testcase classname="global" name="toString( vectors&lt;has_maker )" time="{duration}"/> <testcase classname="global" name="toString( vectors&lt;has_maker )" time="{duration}"/>
<testcase classname="global" name="toString(enum w/operator&lt;&lt;)" time="{duration}"/> <testcase classname="global" name="toString(enum w/operator&lt;&lt;)" time="{duration}"/>
<testcase classname="global" name="toString(enum)" time="{duration}"/> <testcase classname="global" name="toString(enum)" time="{duration}"/>

View File

@ -128,7 +128,7 @@
</Expression> </Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MiscTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MiscTests.cpp" >
<Original> <Original>
(*__error()) == 1 (*__errno_location ()) == 1
</Original> </Original>
<Expanded> <Expanded>
1 == 1 1 == 1
@ -6214,7 +6214,7 @@ re>"
(std::pair&lt;int, int>( 1, 2 )) == aNicePair (std::pair&lt;int, int>( 1, 2 )) == aNicePair
</Original> </Original>
<Expanded> <Expanded>
std::pair( 1, 2 ) == std::pair( 1, 2 ) { 1, 2 } == { 1, 2 }
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
@ -6225,7 +6225,7 @@ re>"
p == 0 p == 0
</Original> </Original>
<Expanded> <Expanded>
NULL == 0 0 == 0
</Expanded> </Expanded>
</Expression> </Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ConditionTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ConditionTests.cpp" >
@ -6233,7 +6233,7 @@ re>"
p == pNULL p == pNULL
</Original> </Original>
<Expanded> <Expanded>
NULL == NULL 0 == 0
</Expanded> </Expanded>
</Expression> </Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ConditionTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ConditionTests.cpp" >
@ -9516,7 +9516,7 @@ spanner <OverallResult success="true"/>
<TestCase name="pair&lt;pair&lt;int,const char *,pair&lt;std::string,int> > -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" > <TestCase name="pair&lt;pair&lt;int,const char *,pair&lt;std::string,int> > -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" >
<Original> <Original>
Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" ::Catch::Detail::stringify( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }"
</Original> </Original>
<Expanded> <Expanded>
"{ { 42, "Arthur" }, { "Ford", 24 } }" "{ { 42, "Arthur" }, { "Ford", 24 } }"
@ -9532,7 +9532,7 @@ spanner <OverallResult success="true"/>
p == 0 p == 0
</Original> </Original>
<Expanded> <Expanded>
NULL == 0 0 == 0
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
@ -9743,7 +9743,7 @@ spanner <OverallResult success="true"/>
<TestCase name="std::pair&lt;int,const std::string> -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" > <TestCase name="std::pair&lt;int,const std::string> -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" >
<Original> <Original>
Catch::toString(value) == "{ 34, \"xyzzy\" }" ::Catch::Detail::stringify(value) == "{ 34, \"xyzzy\" }"
</Original> </Original>
<Expanded> <Expanded>
"{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }"
@ -9754,7 +9754,7 @@ spanner <OverallResult success="true"/>
<TestCase name="std::pair&lt;int,std::string> -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" > <TestCase name="std::pair&lt;int,std::string> -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" >
<Original> <Original>
Catch::toString( value ) == "{ 34, \"xyzzy\" }" ::Catch::Detail::stringify( value ) == "{ 34, \"xyzzy\" }"
</Original> </Original>
<Expanded> <Expanded>
"{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }"
@ -9765,7 +9765,7 @@ spanner <OverallResult success="true"/>
<TestCase name="std::vector&lt;std::pair&lt;std::string,int> > -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" > <TestCase name="std::vector&lt;std::pair&lt;std::string,int> > -> toString" tags="[pair][toString]" filename="projects/<exe-name>/ToStringPair.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringPair.cpp" >
<Original> <Original>
Catch::toString( pr ) == "{ { \"green\", 55 } }" ::Catch::Detail::stringify( pr ) == "{ { \"green\", 55 } }"
</Original> </Original>
<Expanded> <Expanded>
"{ { "green", 55 } }" "{ { "green", 55 } }"
@ -9786,6 +9786,45 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<OverallResult success="false"/> <OverallResult success="false"/>
</TestCase> </TestCase>
<TestCase name="stringify( has_maker )" tags="[toString]" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Original>
::Catch::Detail::stringify( item ) == "StringMaker&lt;has_maker>"
</Original>
<Expanded>
"StringMaker&lt;has_maker>"
==
"StringMaker&lt;has_maker>"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="stringify( has_maker_and_toString )" tags="[.][hide][toString]" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Original>
::Catch::Detail::stringify( item ) == "StringMaker&lt;has_maker_and_operator>"
</Original>
<Expanded>
"StringMaker&lt;has_maker_and_operator>"
==
"StringMaker&lt;has_maker_and_operator>"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="stringify( has_operator )" tags="[toString]" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Original>
::Catch::Detail::stringify( item ) == "operator&lt;&lt;( has_operator )"
</Original>
<Expanded>
"operator&lt;&lt;( has_operator )"
==
"operator&lt;&lt;( has_operator )"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="toString on const wchar_t const pointer returns the string contents" tags="[toString]" filename="projects/<exe-name>/MiscTests.cpp" > <TestCase name="toString on const wchar_t const pointer returns the string contents" tags="[toString]" filename="projects/<exe-name>/MiscTests.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/MiscTests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/MiscTests.cpp" >
<Original> <Original>
@ -9830,49 +9869,10 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="toString( has_maker )" description="toString]" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Original>
Catch::toString( item ) == "StringMaker&lt;has_maker>"
</Original>
<Expanded>
"StringMaker&lt;has_maker>"
==
"StringMaker&lt;has_maker>"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="toString( has_maker_and_toString )" tags="[.][hide][toString]" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Original>
Catch::toString( item ) == "toString( has_maker_and_toString )"
</Original>
<Expanded>
"toString( has_maker_and_toString )"
==
"toString( has_maker_and_toString )"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="toString( has_toString )" tags="[toString]" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Original>
Catch::toString( item ) == "toString( has_toString )"
</Original>
<Expanded>
"toString( has_toString )"
==
"toString( has_toString )"
</Expanded>
</Expression>
<OverallResult success="true"/>
</TestCase>
<TestCase name="toString( vectors&lt;has_maker )" tags="[toString]" filename="projects/<exe-name>/ToStringWhich.cpp" > <TestCase name="toString( vectors&lt;has_maker )" tags="[toString]" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringWhich.cpp" >
<Original> <Original>
Catch::toString( v ) == "{ StringMaker&lt;has_maker> }" ::Catch::Detail::stringify( v ) == "{ StringMaker&lt;has_maker> }"
</Original> </Original>
<Expanded> <Expanded>
"{ StringMaker&lt;has_maker> }" "{ StringMaker&lt;has_maker> }"
@ -9885,7 +9885,7 @@ spanner <OverallResult success="true"/>
<TestCase name="toString(enum w/operator&lt;&lt;)" tags="[enum][toString]" filename="projects/<exe-name>/EnumToString.cpp" > <TestCase name="toString(enum w/operator&lt;&lt;)" tags="[enum][toString]" filename="projects/<exe-name>/EnumToString.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" >
<Original> <Original>
Catch::toString(e0) == "E2{0}" ::Catch::Detail::stringify(e0) == "E2{0}"
</Original> </Original>
<Expanded> <Expanded>
"E2{0}" == "E2{0}" "E2{0}" == "E2{0}"
@ -9893,7 +9893,7 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" >
<Original> <Original>
Catch::toString(e1) == "E2{1}" ::Catch::Detail::stringify(e1) == "E2{1}"
</Original> </Original>
<Expanded> <Expanded>
"E2{1}" == "E2{1}" "E2{1}" == "E2{1}"
@ -9904,7 +9904,7 @@ spanner <OverallResult success="true"/>
<TestCase name="toString(enum)" tags="[enum][toString]" filename="projects/<exe-name>/EnumToString.cpp" > <TestCase name="toString(enum)" tags="[enum][toString]" filename="projects/<exe-name>/EnumToString.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" >
<Original> <Original>
Catch::toString(e0) == "0" ::Catch::Detail::stringify(e0) == "0"
</Original> </Original>
<Expanded> <Expanded>
"0" == "0" "0" == "0"
@ -9912,7 +9912,7 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/EnumToString.cpp" >
<Original> <Original>
Catch::toString(e1) == "1" ::Catch::Detail::stringify(e1) == "1"
</Original> </Original>
<Expanded> <Expanded>
"1" == "1" "1" == "1"
@ -9923,7 +9923,7 @@ spanner <OverallResult success="true"/>
<TestCase name="vector&lt;int> -> toString" tags="[toString][vector]" filename="projects/<exe-name>/ToStringVector.cpp" > <TestCase name="vector&lt;int> -> toString" tags="[toString][vector]" filename="projects/<exe-name>/ToStringVector.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" >
<Original> <Original>
Catch::toString(vv) == "{ }" ::Catch::Detail::stringify(vv) == "{ }"
</Original> </Original>
<Expanded> <Expanded>
"{ }" == "{ }" "{ }" == "{ }"
@ -9931,7 +9931,7 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" >
<Original> <Original>
Catch::toString(vv) == "{ 42 }" ::Catch::Detail::stringify(vv) == "{ 42 }"
</Original> </Original>
<Expanded> <Expanded>
"{ 42 }" == "{ 42 }" "{ 42 }" == "{ 42 }"
@ -9939,7 +9939,7 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" >
<Original> <Original>
Catch::toString(vv) == "{ 42, 250 }" ::Catch::Detail::stringify(vv) == "{ 42, 250 }"
</Original> </Original>
<Expanded> <Expanded>
"{ 42, 250 }" == "{ 42, 250 }" "{ 42, 250 }" == "{ 42, 250 }"
@ -9950,7 +9950,7 @@ spanner <OverallResult success="true"/>
<TestCase name="vector&lt;string> -> toString" tags="[toString][vector]" filename="projects/<exe-name>/ToStringVector.cpp" > <TestCase name="vector&lt;string> -> toString" tags="[toString][vector]" filename="projects/<exe-name>/ToStringVector.cpp" >
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" >
<Original> <Original>
Catch::toString(vv) == "{ }" ::Catch::Detail::stringify(vv) == "{ }"
</Original> </Original>
<Expanded> <Expanded>
"{ }" == "{ }" "{ }" == "{ }"
@ -9958,7 +9958,7 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" >
<Original> <Original>
Catch::toString(vv) == "{ \"hello\" }" ::Catch::Detail::stringify(vv) == "{ \"hello\" }"
</Original> </Original>
<Expanded> <Expanded>
"{ "hello" }" == "{ "hello" }" "{ "hello" }" == "{ "hello" }"
@ -9966,7 +9966,7 @@ spanner <OverallResult success="true"/>
</Expression> </Expression>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringVector.cpp" >
<Original> <Original>
Catch::toString(vv) == "{ \"hello\", \"world\" }" ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }"
</Original> </Original>
<Expanded> <Expanded>
"{ "hello", "world" }" "{ "hello", "world" }"

View File

@ -1,32 +1,28 @@
#include "catch.hpp" #include "catch.hpp"
/*
TODO: maybe ought to check that user-provided specialisations of
Catch::toString also do the right thing
*/
// Enum without user-provided stream operator // Enum without user-provided stream operator
enum Enum1 { Enum1Value0, Enum1Value1 }; enum Enum1 { Enum1Value0, Enum1Value1 };
TEST_CASE( "toString(enum)", "[toString][enum]" ) { TEST_CASE( "toString(enum)", "[toString][enum]" ) {
Enum1 e0 = Enum1Value0; Enum1 e0 = Enum1Value0;
CHECK( Catch::toString(e0) == "0" ); CHECK( ::Catch::Detail::stringify(e0) == "0" );
Enum1 e1 = Enum1Value1; Enum1 e1 = Enum1Value1;
CHECK( Catch::toString(e1) == "1" ); CHECK( ::Catch::Detail::stringify(e1) == "1" );
} }
// Enum with user-provided stream operator // Enum with user-provided stream operator
enum Enum2 { Enum2Value0, Enum2Value1 }; enum Enum2 { Enum2Value0, Enum2Value1 };
inline std::ostream& operator<<( std::ostream& os, Enum2 v ) { std::ostream& operator<<( std::ostream& os, Enum2 v ) {
return os << "E2{" << static_cast<int>(v) << "}"; return os << "E2{" << static_cast<int>(v) << "}";
} }
TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) { TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) {
Enum2 e0 = Enum2Value0; Enum2 e0 = Enum2Value0;
CHECK( Catch::toString(e0) == "E2{0}" ); CHECK( ::Catch::Detail::stringify(e0) == "E2{0}" );
Enum2 e1 = Enum2Value1; Enum2 e1 = Enum2Value1;
CHECK( Catch::toString(e1) == "E2{1}" ); CHECK( ::Catch::Detail::stringify(e1) == "E2{1}" );
} }
// Enum class without user-provided stream operator // Enum class without user-provided stream operator
@ -34,15 +30,15 @@ enum class EnumClass1 { EnumClass1Value0, EnumClass1Value1 };
TEST_CASE( "toString(enum class)", "[toString][enum][enumClass][c++11][.]" ) { TEST_CASE( "toString(enum class)", "[toString][enum][enumClass][c++11][.]" ) {
EnumClass1 e0 = EnumClass1::EnumClass1Value0; EnumClass1 e0 = EnumClass1::EnumClass1Value0;
CHECK( Catch::toString(e0) == "0" ); CHECK( ::Catch::Detail::stringify(e0) == "0" );
EnumClass1 e1 = EnumClass1::EnumClass1Value1; EnumClass1 e1 = EnumClass1::EnumClass1Value1;
CHECK( Catch::toString(e1) == "1" ); CHECK( ::Catch::Detail::stringify(e1) == "1" );
} }
// Enum class with user-provided stream operator // Enum class with user-provided stream operator
enum class EnumClass2 : short { EnumClass2Value0, EnumClass2Value1 }; enum class EnumClass2 : short { EnumClass2Value0, EnumClass2Value1 };
inline std::ostream& operator<<( std::ostream& os, EnumClass2 e2 ) { std::ostream& operator<<( std::ostream& os, EnumClass2 e2 ) {
switch( static_cast<int>( e2 ) ) { switch( static_cast<int>( e2 ) ) {
case static_cast<int>( EnumClass2::EnumClass2Value0 ): case static_cast<int>( EnumClass2::EnumClass2Value0 ):
return os << "E2/V0"; return os << "E2/V0";
@ -55,11 +51,11 @@ inline std::ostream& operator<<( std::ostream& os, EnumClass2 e2 ) {
TEST_CASE( "toString(enum class w/operator<<)", "[toString][enum][enumClass][c++11][.]" ) { TEST_CASE( "toString(enum class w/operator<<)", "[toString][enum][enumClass][c++11][.]" ) {
EnumClass2 e0 = EnumClass2::EnumClass2Value0; EnumClass2 e0 = EnumClass2::EnumClass2Value0;
CHECK( Catch::toString(e0) == "E2/V0" ); CHECK( ::Catch::Detail::stringify(e0) == "E2/V0" );
EnumClass2 e1 = EnumClass2::EnumClass2Value1; EnumClass2 e1 = EnumClass2::EnumClass2Value1;
CHECK( Catch::toString(e1) == "E2/V1" ); CHECK( ::Catch::Detail::stringify(e1) == "E2/V1" );
EnumClass2 e3 = static_cast<EnumClass2>(10); EnumClass2 e3 = static_cast<EnumClass2>(10);
CHECK( Catch::toString(e3) == "Unknown enum value 10" ); CHECK( ::Catch::Detail::stringify(e3) == "Unknown enum value 10" );
} }

View File

@ -144,7 +144,7 @@ CATCH_TRANSLATE_EXCEPTION( CustomStdException& ex )
CATCH_TRANSLATE_EXCEPTION( double& ex ) CATCH_TRANSLATE_EXCEPTION( double& ex )
{ {
return Catch::toString( ex ); return Catch::Detail::stringify( ex );
} }
TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" ) TEST_CASE("Non-std exceptions can be translated", "[.][failing][!throws]" )

View File

@ -134,5 +134,5 @@ TEST_CASE( "Pointers can be converted to strings", "[messages][.]" )
{ {
int p; int p;
WARN( "actual address of p: " << &p ); WARN( "actual address of p: " << &p );
WARN( "toString(p): " << Catch::toString( &p ) ); WARN( "toString(p): " << ::Catch::Detail::stringify( &p ) );
} }

View File

@ -314,25 +314,25 @@ TEST_CASE( "Tabs and newlines show in output", "[.][whitespace][failing]" ) {
TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) { TEST_CASE( "toString on const wchar_t const pointer returns the string contents", "[toString]" ) {
const wchar_t * const s = L"wide load"; const wchar_t * const s = L"wide load";
std::string result = Catch::toString( s ); std::string result = ::Catch::Detail::stringify( s );
CHECK( result == "\"wide load\"" ); CHECK( result == "\"wide load\"" );
} }
TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) { TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) {
const wchar_t * s = L"wide load"; const wchar_t * s = L"wide load";
std::string result = Catch::toString( s ); std::string result = ::Catch::Detail::stringify( s );
CHECK( result == "\"wide load\"" ); CHECK( result == "\"wide load\"" );
} }
TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) { TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) {
wchar_t * const s = const_cast<wchar_t* const>( L"wide load" ); wchar_t * const s = const_cast<wchar_t* const>( L"wide load" );
std::string result = Catch::toString( s ); std::string result = ::Catch::Detail::stringify( s );
CHECK( result == "\"wide load\"" ); CHECK( result == "\"wide load\"" );
} }
TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) { TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) {
wchar_t * s = const_cast<wchar_t*>( L"wide load" ); wchar_t * s = const_cast<wchar_t*>( L"wide load" );
std::string result = Catch::toString( s ); std::string result = ::Catch::Detail::stringify( s );
CHECK( result == "\"wide load\"" ); CHECK( result == "\"wide load\"" );
} }

View File

@ -1,37 +1,19 @@
#include "catch.hpp" #include "catch.hpp"
// === Pair ===
namespace Catch {
// Note: If we put this in the right place in catch_tostring, then
// we can make it an overload of Catch::toString
template<typename T1, typename T2>
struct StringMaker<std::pair<T1,T2> > {
static std::string convert( const std::pair<T1,T2>& pair ) {
std::ostringstream oss;
oss << "{ "
<< toString( pair.first )
<< ", "
<< toString( pair.second )
<< " }";
return oss.str();
}
};
}
TEST_CASE( "std::pair<int,std::string> -> toString", "[toString][pair]" ) { TEST_CASE( "std::pair<int,std::string> -> toString", "[toString][pair]" ) {
std::pair<int,std::string> value( 34, "xyzzy" ); std::pair<int,std::string> value( 34, "xyzzy" );
REQUIRE( Catch::toString( value ) == "{ 34, \"xyzzy\" }" ); REQUIRE( ::Catch::Detail::stringify( value ) == "{ 34, \"xyzzy\" }" );
} }
TEST_CASE( "std::pair<int,const std::string> -> toString", "[toString][pair]" ) { TEST_CASE( "std::pair<int,const std::string> -> toString", "[toString][pair]" ) {
std::pair<int,const std::string> value( 34, "xyzzy" ); std::pair<int,const std::string> value( 34, "xyzzy" );
REQUIRE( Catch::toString(value) == "{ 34, \"xyzzy\" }" ); REQUIRE( ::Catch::Detail::stringify(value) == "{ 34, \"xyzzy\" }" );
} }
TEST_CASE( "std::vector<std::pair<std::string,int> > -> toString", "[toString][pair]" ) { TEST_CASE( "std::vector<std::pair<std::string,int> > -> toString", "[toString][pair]" ) {
std::vector<std::pair<std::string,int> > pr; std::vector<std::pair<std::string,int> > pr;
pr.push_back( std::make_pair("green", 55 ) ); pr.push_back( std::make_pair("green", 55 ) );
REQUIRE( Catch::toString( pr ) == "{ { \"green\", 55 } }" ); REQUIRE( ::Catch::Detail::stringify( pr ) == "{ { \"green\", 55 } }" );
} }
// This is pretty contrived - I figure if this works, anything will... // This is pretty contrived - I figure if this works, anything will...
@ -43,5 +25,5 @@ TEST_CASE( "pair<pair<int,const char *,pair<std::string,int> > -> toString", "[t
right_t right( "Ford", 24 ); right_t right( "Ford", 24 );
std::pair<left_t,right_t> pair( left, right ); std::pair<left_t,right_t> pair( left, right );
REQUIRE( Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" ); REQUIRE( ::Catch::Detail::stringify( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" );
} }

View File

@ -5,42 +5,42 @@
TEST_CASE( "tuple<>", "[toString][tuple][c++11][.]" ) TEST_CASE( "tuple<>", "[toString][tuple][c++11][.]" )
{ {
typedef std::tuple<> type; typedef std::tuple<> type;
CHECK( "{ }" == Catch::toString(type{}) ); CHECK( "{ }" == ::Catch::Detail::stringify(type{}) );
type value {}; type value {};
CHECK( "{ }" == Catch::toString(value) ); CHECK( "{ }" == ::Catch::Detail::stringify(value) );
} }
TEST_CASE( "tuple<int>", "[toString][tuple][c++11][.]" ) TEST_CASE( "tuple<int>", "[toString][tuple][c++11][.]" )
{ {
typedef std::tuple<int> type; typedef std::tuple<int> type;
CHECK( "{ 0 }" == Catch::toString(type{0}) ); CHECK( "{ 0 }" == ::Catch::Detail::stringify(type{0}) );
} }
TEST_CASE( "tuple<float,int>", "[toString][tuple][c++11][.]" ) TEST_CASE( "tuple<float,int>", "[toString][tuple][c++11][.]" )
{ {
typedef std::tuple<float,int> type; typedef std::tuple<float,int> type;
CHECK( "1.2f" == Catch::toString(float(1.2)) ); CHECK( "1.2f" == ::Catch::Detail::stringify(float(1.2)) );
CHECK( "{ 1.2f, 0 }" == Catch::toString(type{1.2f,0}) ); CHECK( "{ 1.2f, 0 }" == ::Catch::Detail::stringify(type{1.2f,0}) );
} }
TEST_CASE( "tuple<string,string>", "[toString][tuple][c++11][.]" ) TEST_CASE( "tuple<string,string>", "[toString][tuple][c++11][.]" )
{ {
typedef std::tuple<std::string,std::string> type; typedef std::tuple<std::string,std::string> type;
CHECK( "{ \"hello\", \"world\" }" == Catch::toString(type{"hello","world"}) ); CHECK( "{ \"hello\", \"world\" }" == ::Catch::Detail::stringify(type{"hello","world"}) );
} }
TEST_CASE( "tuple<tuple<int>,tuple<>,float>", "[toString][tuple][c++11][.]" ) TEST_CASE( "tuple<tuple<int>,tuple<>,float>", "[toString][tuple][c++11][.]" )
{ {
typedef std::tuple<std::tuple<int>,std::tuple<>,float> type; typedef std::tuple<std::tuple<int>,std::tuple<>,float> type;
type value { std::tuple<int>{42}, {}, 1.2f }; type value { std::tuple<int>{42}, {}, 1.2f };
CHECK( "{ { 42 }, { }, 1.2f }" == Catch::toString(value) ); CHECK( "{ { 42 }, { }, 1.2f }" == ::Catch::Detail::stringify(value) );
} }
TEST_CASE( "tuple<nullptr,int,const char *>", "[toString][tuple][c++11][.]" ) TEST_CASE( "tuple<nullptr,int,const char *>", "[toString][tuple][c++11][.]" )
{ {
typedef std::tuple<std::nullptr_t,int,const char *> type; typedef std::tuple<std::nullptr_t,int,const char *> type;
type value { nullptr, 42, "Catch me" }; type value { nullptr, 42, "Catch me" };
CHECK( "{ nullptr, 42, \"Catch me\" }" == Catch::toString(value) ); CHECK( "{ nullptr, 42, \"Catch me\" }" == ::Catch::Detail::stringify(value) );
} }

View File

@ -6,21 +6,21 @@
TEST_CASE( "vector<int> -> toString", "[toString][vector]" ) TEST_CASE( "vector<int> -> toString", "[toString][vector]" )
{ {
std::vector<int> vv; std::vector<int> vv;
REQUIRE( Catch::toString(vv) == "{ }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
vv.push_back( 42 ); vv.push_back( 42 );
REQUIRE( Catch::toString(vv) == "{ 42 }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
vv.push_back( 250 ); vv.push_back( 250 );
REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
} }
TEST_CASE( "vector<string> -> toString", "[toString][vector]" ) TEST_CASE( "vector<string> -> toString", "[toString][vector]" )
{ {
std::vector<std::string> vv; std::vector<std::string> vv;
REQUIRE( Catch::toString(vv) == "{ }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
vv.push_back( "hello" ); vv.push_back( "hello" );
REQUIRE( Catch::toString(vv) == "{ \"hello\" }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" );
vv.push_back( "world" ); vv.push_back( "world" );
REQUIRE( Catch::toString(vv) == "{ \"hello\", \"world\" }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" );
} }
namespace { namespace {
@ -50,19 +50,19 @@ namespace {
TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator][c++11][.]" ) { TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator][c++11][.]" ) {
std::vector<int,minimal_allocator<int> > vv; std::vector<int,minimal_allocator<int> > vv;
REQUIRE( Catch::toString(vv) == "{ }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" );
vv.push_back( 42 ); vv.push_back( 42 );
REQUIRE( Catch::toString(vv) == "{ 42 }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" );
vv.push_back( 250 ); vv.push_back( 250 );
REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" );
} }
TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator][c++11][.]" ) { TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator][c++11][.]" ) {
using inner = std::vector<std::string, minimal_allocator<std::string>>; using inner = std::vector<std::string, minimal_allocator<std::string>>;
using vector = std::vector<inner>; using vector = std::vector<inner>;
vector v; vector v;
REQUIRE( Catch::toString(v) == "{ }" ); REQUIRE( ::Catch::Detail::stringify(v) == "{ }" );
v.push_back( inner { "hello" } ); v.push_back( inner { "hello" } );
v.push_back( inner { "world" } ); v.push_back( inner { "world" } );
REQUIRE( Catch::toString(v) == "{ { \"hello\" }, { \"world\" } }" ); REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" );
} }

View File

@ -5,67 +5,69 @@
*/ */
struct has_toString { }; struct has_operator { };
struct has_maker {}; struct has_maker {};
struct has_maker_and_toString {}; struct has_maker_and_operator {};
std::ostream& operator<<(std::ostream& os, const has_operator&) {
os << "operator<<( has_operator )";
return os;
}
std::ostream& operator<<(std::ostream& os, const has_maker_and_operator&) {
os << "operator<<( has_maker_and_operator )";
return os;
}
namespace Catch { namespace Catch {
inline std::string toString( const has_toString& ) {
return "toString( has_toString )";
}
inline std::string toString( const has_maker_and_toString& ) {
return "toString( has_maker_and_toString )";
}
template<> template<>
struct StringMaker<has_maker> { struct StringMaker<has_maker> {
static std::string convert( const has_maker& ) { std::string operator()( const has_maker& ) {
return "StringMaker<has_maker>"; return "StringMaker<has_maker>";
} }
}; };
template<> template<>
struct StringMaker<has_maker_and_toString> { struct StringMaker<has_maker_and_operator> {
static std::string convert( const has_maker_and_toString& ) { std::string operator()( const has_maker_and_operator& ) {
return "StringMaker<has_maker_and_toString>"; return "StringMaker<has_maker_and_operator>";
} }
}; };
} }
// Call the overload // Call the operator
TEST_CASE( "toString( has_toString )", "[toString]" ) { TEST_CASE( "stringify( has_operator )", "[toString]" ) {
has_toString item; has_operator item;
REQUIRE( Catch::toString( item ) == "toString( has_toString )" ); REQUIRE( ::Catch::Detail::stringify( item ) == "operator<<( has_operator )" );
} }
// Call the overload // Call the stringmaker
TEST_CASE( "toString( has_maker )", "toString]" ) { TEST_CASE( "stringify( has_maker )", "[toString]" ) {
has_maker item; has_maker item;
REQUIRE( Catch::toString( item ) == "StringMaker<has_maker>" ); REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker<has_maker>" );
} }
// Call the overload // Call the stringmaker
TEST_CASE( "toString( has_maker_and_toString )", "[.][toString]" ) { TEST_CASE( "stringify( has_maker_and_toString )", "[.][toString]" ) {
has_maker_and_toString item; has_maker_and_operator item;
REQUIRE( Catch::toString( item ) == "toString( has_maker_and_toString )" ); REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker<has_maker_and_operator>" );
} }
// Vectors... // Vectors...
// Don't run this in approval tests as it is sensitive to two phase lookup differences // Don't run this in approval tests as it is sensitive to two phase lookup differences
TEST_CASE( "toString( vectors<has_toString )", "[.][toString][!nonportable]" ) { TEST_CASE( "toString( vectors<has_toString )", "[.][toString][!nonportable]" ) {
std::vector<has_toString> v(1); std::vector<has_operator> v(1);
// This invokes template<T> toString which actually gives us '{ ? }' REQUIRE( ::Catch::Detail::stringify( v ) == "{ operator<<( has_operator ) }" );
REQUIRE( Catch::toString( v ) == "{ {?} }" );
} }
TEST_CASE( "toString( vectors<has_maker )", "[toString]" ) { TEST_CASE( "toString( vectors<has_maker )", "[toString]" ) {
std::vector<has_maker> v(1); std::vector<has_maker> v(1);
REQUIRE( Catch::toString( v ) == "{ StringMaker<has_maker> }" ); REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker> }" );
} }
// Don't run this in approval tests as it is sensitive to two phase lookup differences // Don't run this in approval tests as it is sensitive to two phase lookup differences
TEST_CASE( "toString( vectors<has_maker_and_toString )", "[.][toString][!nonportable]" ) { TEST_CASE( "toString( vectors<has_maker_and_toString )", "[.][toString][!nonportable]" ) {
std::vector<has_maker_and_toString> v(1); std::vector<has_maker_and_operator> v(1);
// Note: This invokes the template<T> toString -> StringMaker REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker_and_toString> }" );
REQUIRE( Catch::toString( v ) == "{ StringMaker<has_maker_and_toString> }" );
} }

View File

@ -14,15 +14,11 @@
#include "catch.hpp" #include "catch.hpp"
namespace Catch namespace Catch {
{ std::string toString( const std::pair<int, int>& value ) {
template<>
std::string toString<std::pair<int, int> >( const std::pair<int, int>& value )
{
std::ostringstream oss; std::ostringstream oss;
oss << "std::pair( " << value.first << ", " << value.second << " )"; oss << "std::pair( " << value.first << ", " << value.second << " )";
return oss.str(); return oss.str();
} }
} }
@ -340,7 +336,7 @@ struct Awkward
TEST_CASE( "non streamable - with conv. op", "[Tricky]" ) TEST_CASE( "non streamable - with conv. op", "[Tricky]" )
{ {
Awkward awkward; Awkward awkward;
std::string s = Catch::toString( awkward ); std::string s = ::Catch::Detail::stringify( awkward );
REQUIRE( s == "7" ); REQUIRE( s == "7" );
} }