From 33ed1773f40b406dbf3b7201bf52694bd86b1503 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Tue, 2 May 2017 23:51:03 +0200 Subject: [PATCH] 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. --- include/catch.hpp | 8 +- include/internal/catch_approx.hpp | 10 +- include/internal/catch_expression_lhs.hpp | 8 +- include/internal/catch_matchers_vector.h | 6 +- include/internal/catch_tostring.h | 464 ++++++++++-------- include/internal/catch_tostring.hpp | 261 +++++----- include/reporters/catch_reporter_junit.hpp | 2 +- .../Baselines/console.sw.approved.txt | 124 ++--- .../Baselines/console.swa4.approved.txt | 2 +- .../SelfTest/Baselines/junit.sw.approved.txt | 6 +- .../SelfTest/Baselines/xml.sw.approved.txt | 118 ++--- projects/SelfTest/EnumToString.cpp | 26 +- projects/SelfTest/ExceptionTests.cpp | 2 +- projects/SelfTest/MessageTests.cpp | 2 +- projects/SelfTest/MiscTests.cpp | 8 +- projects/SelfTest/ToStringPair.cpp | 26 +- projects/SelfTest/ToStringTuple.cpp | 16 +- projects/SelfTest/ToStringVector.cpp | 22 +- projects/SelfTest/ToStringWhich.cpp | 62 +-- projects/SelfTest/TrickyTests.cpp | 10 +- 20 files changed, 626 insertions(+), 557 deletions(-) diff --git a/include/catch.hpp b/include/catch.hpp index 236ec38e..29132e0d 100644 --- a/include/catch.hpp +++ b/include/catch.hpp @@ -127,8 +127,8 @@ LeakDetector leakDetector; #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_SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( "CATCH_INFO", msg ) -#define CATCH_CAPTURE( msg ) INTERNAL_CATCH_INFO( "CATCH_CAPTURE", #msg " := " << Catch::toString(msg) ) -#define CATCH_SCOPED_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::Detail::stringify(msg) ) #define CATCH_TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __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 WARN( msg ) INTERNAL_CATCH_MSG( "WARN", Catch::ResultWas::Warning, Catch::ResultDisposition::ContinueOnFailure, msg ) #define SCOPED_INFO( msg ) INTERNAL_CATCH_INFO( "INFO", msg ) -#define CAPTURE( msg ) INTERNAL_CATCH_INFO( "CAPTURE", #msg " := " << Catch::toString(msg) ) -#define SCOPED_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::Detail::stringify(msg) ) #define TEST_CASE( ... ) INTERNAL_CATCH_TESTCASE( __VA_ARGS__ ) #define TEST_CASE_METHOD( className, ... ) INTERNAL_CATCH_TEST_CASE_METHOD( className, __VA_ARGS__ ) diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index 66576428..9c974f6c 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -114,7 +114,7 @@ namespace Detail { std::string toString() const { std::ostringstream oss; - oss << "Approx( " << Catch::toString( m_value ) << " )"; + oss << "Approx( " << ::Catch::Detail::stringify( m_value ) << " )"; return oss.str(); } @@ -127,9 +127,11 @@ namespace Detail { } template<> -inline std::string toString( Detail::Approx const& value ) { - return value.toString(); -} +struct StringMaker { + std::string operator()(Catch::Detail::Approx const& value) { + return value.toString(); + } +}; } // end namespace Catch diff --git a/include/internal/catch_expression_lhs.hpp b/include/internal/catch_expression_lhs.hpp index 756a07f6..1ac339b4 100644 --- a/include/internal/catch_expression_lhs.hpp +++ b/include/internal/catch_expression_lhs.hpp @@ -81,7 +81,7 @@ public: } virtual void reconstructExpression( std::string& dest ) const override { - dest = Catch::toString( m_truthy ); + dest = ::Catch::Detail::stringify( m_truthy ); } private: @@ -120,8 +120,8 @@ public: } virtual void reconstructExpression( std::string& dest ) const override { - std::string lhs = Catch::toString( m_lhs ); - std::string rhs = Catch::toString( m_rhs ); + std::string lhs = ::Catch::Detail::stringify( m_lhs ); + std::string rhs = ::Catch::Detail::stringify( m_rhs ); char delim = lhs.size() + rhs.size() < 40 && lhs.find('\n') == std::string::npos && rhs.find('\n') == std::string::npos ? ' ' : '\n'; @@ -155,7 +155,7 @@ public: virtual void reconstructExpression( std::string& dest ) const override { std::string matcherAsString = m_matcher.toString(); - dest = Catch::toString( m_arg ); + dest = ::Catch::Detail::stringify( m_arg ); dest += ' '; if( matcherAsString == Detail::unprintableString ) dest += m_matcherString; diff --git a/include/internal/catch_matchers_vector.h b/include/internal/catch_matchers_vector.h index 11aee020..e80f7e12 100644 --- a/include/internal/catch_matchers_vector.h +++ b/include/internal/catch_matchers_vector.h @@ -25,7 +25,7 @@ namespace Matchers { } virtual std::string describe() const override { - return "Contains: " + Catch::toString( m_comparator ); + return "Contains: " + ::Catch::Detail::stringify( m_comparator ); } T const& m_comparator; @@ -46,7 +46,7 @@ namespace Matchers { return true; } virtual std::string describe() const override { - return "Contains: " + Catch::toString( m_comparator ); + return "Contains: " + ::Catch::Detail::stringify( m_comparator ); } std::vector const& m_comparator; @@ -70,7 +70,7 @@ namespace Matchers { return true; } virtual std::string describe() const override { - return "Equals: " + Catch::toString( m_comparator ); + return "Equals: " + ::Catch::Detail::stringify( m_comparator ); } std::vector const& m_comparator; }; diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h index f72eeca3..02eb0d6f 100644 --- a/include/internal/catch_tostring.h +++ b/include/internal/catch_tostring.h @@ -24,226 +24,296 @@ #include +#include + +// 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 { + // Bring in operator<< from global namespace into Catch namespace + using ::operator<<; -// Why we're here. -template -std::string toString( T const& value ); + namespace Detail { + std::string rawMemoryToString( const void *object, std::size_t size ); -// Built in overloads + template + std::string rawMemoryToString( const T& object ) { + return rawMemoryToString( &object, sizeof(object) ); + } -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 ); + template + class IsStreamInsertable { + template + static auto test(int) + -> decltype(std::declval() << std::declval(), std::true_type()); -std::string toString( long long value ); -std::string toString( unsigned long long value ); + template + static auto test(...)->std::false_type; -std::string toString( std::nullptr_t ); + public: + static const bool value = decltype(test(0))::value; + }; + + } // namespace Detail -#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 + // If we decide for C++14, change these to enable_if_ts + template + struct StringMaker { + template + typename std::enable_if<::Catch::Detail::IsStreamInsertable::value, std::string>::type + operator()(const Fake& t) { + std::stringstream sstr; + sstr << t; + return sstr.str(); + } - -namespace Detail { - - extern const std::string unprintableString; - - template - class IsStreamInsertable { - template - static auto test(int) - -> decltype( std::declval() << std::declval(), std::true_type() ); - - template - static auto test(...) -> std::false_type; - - public: - static const bool value = decltype(test(0))::value; - }; - - - template::value - > - struct EnumStringMaker - { - static std::string convert( T const& ) { return unprintableString; } - }; - - template - struct EnumStringMaker - { - static std::string convert( T const& v ) - { - return ::Catch::toString( - static_cast::type>(v) - ); + template + typename std::enable_if::value, std::string>::type + operator()(const Fake&) { + return "{?}"; } }; - template - struct StringMakerBase { - template - static std::string convert( T const& v ) - { - return EnumStringMaker::convert( v ); + namespace Detail { + + 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 + std::string stringify(const T& e) { + return ::Catch::StringMaker::type>::type>{}(e); + } + + } // namespace Detail + + + // Some predefined specializations + + template<> + struct StringMaker { + std::string operator()(const std::string& str); + }; + template<> + struct StringMaker { + std::string operator()(const std::wstring& wstr); + }; + + template<> + struct StringMaker { + std::string operator()(char const * str); + }; + template<> + struct StringMaker { + std::string operator()(char * str); + }; + template<> + struct StringMaker { + std::string operator()(wchar_t const * str); + }; + template<> + struct StringMaker { + std::string operator()(wchar_t * str); + }; + + template + struct StringMaker { + std::string operator()(const char* str) { + return ::Catch::Detail::stringify(std::string{ str }); + } + }; + template + struct StringMaker { + std::string operator()(const char* str) { + return ::Catch::Detail::stringify(std::string{ str }); + } + }; + template + struct StringMaker { + std::string operator()(const char* str) { + return ::Catch::Detail::stringify(std::string{ str }); } }; template<> - struct StringMakerBase { - template - static std::string convert( T const& _value ) { + struct StringMaker { + std::string operator()(int value); + }; + template<> + struct StringMaker { + std::string operator()(long value); + }; + template<> + struct StringMaker { + std::string operator()(long long value); + }; + template<> + struct StringMaker { + std::string operator()(unsigned int value); + }; + template<> + struct StringMaker { + std::string operator()(unsigned long value); + }; + template<> + struct StringMaker { + std::string operator()(unsigned long long value); + }; + + template<> + struct StringMaker { + std::string operator()(bool b); + }; + + template<> + struct StringMaker { + std::string operator()(char c); + }; + template<> + struct StringMaker { + std::string operator()(signed char c); + }; + template<> + struct StringMaker { + std::string operator()(unsigned char c); + }; + + template<> + struct StringMaker { + std::string operator()(std::nullptr_t); + }; + + template<> + struct StringMaker { + std::string operator()(float value); + }; + template<> + struct StringMaker { + std::string operator()(double value); + }; + + template + struct StringMaker { + template + std::string operator()(U* p) { + if (p) { + return ::Catch::Detail::rawMemoryToString(p); + } else { + return "nullptr"; + } + } + }; + + template + struct StringMaker { + std::string operator()(R C::* p) { + if (p) { + return ::Catch::Detail::rawMemoryToString(p); + } else { + return "nullptr"; + } + } + }; + + namespace Detail { + template + std::string rangeToString(InputIterator first, InputIterator last) { std::ostringstream oss; - oss << _value; + oss << "{ "; + if (first != last) { + oss << ::Catch::Detail::stringify(*first); + for (++first; first != last; ++first) + oss << ", " << ::Catch::Detail::stringify(*first); + } + oss << " }"; + return oss.str(); + } + } + + template + struct StringMaker > { + std::string operator()( std::vector const& v ) { + return ::Catch::Detail::rangeToString( v.begin(), v.end() ); + } + }; + + // === Pair === + template + struct StringMaker > { + std::string operator()(const std::pair& pair) { + std::ostringstream oss; + oss << "{ " + << ::Catch::Detail::stringify(pair.first) + << ", " + << ::Catch::Detail::stringify(pair.second) + << " }"; return oss.str(); } }; - std::string rawMemoryToString( const void *object, std::size_t size ); - - template - inline std::string rawMemoryToString( const T& object ) { - return rawMemoryToString( &object, sizeof(object) ); - } - -} // end namespace Detail - -template -struct StringMaker : - Detail::StringMakerBase::value> {}; - -template -struct StringMaker { - template - static std::string convert( U* p ) { - if( !p ) - return "NULL"; - else - return Detail::rawMemoryToString( p ); - } -}; - -template -struct StringMaker { - static std::string convert( R C::* p ) { - if( !p ) - return "NULL"; - else - return Detail::rawMemoryToString( p ); - } -}; - -namespace Detail { - template - std::string rangeToString( InputIterator first, InputIterator last ); -} - -//template -//struct StringMaker > { -// static std::string convert( std::vector const& v ) { -// return Detail::rangeToString( v.begin(), v.end() ); -// } -//}; - -template -std::string toString( std::vector 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::value) - > - struct ElementPrinter { - static void print( const Tuple& tuple, std::ostream& os ) - { - os << ( N ? ", " : " " ) - << Catch::toString(std::get(tuple)); - ElementPrinter::print(tuple,os); - } - }; - - template< - typename Tuple, - std::size_t N - > - struct ElementPrinter { - static void print( const Tuple&, std::ostream& ) {} - }; - -} - -template -struct StringMaker> { - - static std::string convert( const std::tuple& tuple ) - { - std::ostringstream os; - os << '{'; - TupleDetail::ElementPrinter>::print( tuple, os ); - os << " }"; - return os.str(); - } -}; - - -namespace Detail { - template - std::string makeString( T const& value ) { - return StringMaker::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 -std::string toString( T const& value ) { - return StringMaker::convert( value ); -} namespace Detail { - template - std::string rangeToString( InputIterator first, InputIterator last ) { - std::ostringstream oss; - oss << "{ "; - if( first != last ) { - oss << Catch::toString( *first ); - for( ++first ; first != last ; ++first ) - oss << ", " << Catch::toString( *first ); - } - oss << " }"; - return oss.str(); - } -} + template< + typename Tuple, + std::size_t N = 0, + bool = (N < std::tuple_size::value) + > + struct TupleElementPrinter { + static void print(const Tuple& tuple, std::ostream& os) { + os << (N ? ", " : " ") + << ::Catch::Detail::stringify(std::get(tuple)); + TupleElementPrinter::print(tuple, os); + } + }; -} // end namespace Catch + template< + typename Tuple, + std::size_t N + > + struct TupleElementPrinter { + static void print(const Tuple&, std::ostream&) {} + }; + + } + + + template + struct StringMaker> { + std::string operator()(const std::tuple& tuple) { + std::ostringstream os; + os << '{'; + Detail::TupleElementPrinter>::print(tuple, os); + os << " }"; + return os.str(); + } + }; + + + template + struct EnumStringMaker { + std::string operator()(const T& t) { + return ::Catch::Detail::stringify(static_cast::type>(t)); + } + }; + +#ifdef __OBJC__ + template<> + struct StringMaker { + std::string operator()(NSString const* const& nsstring); + }; + template<> + struct StringMaker { + std::string operator()(NSString * CATCH_ARC_STRONG const& nsstring); + }; + template<> + struct StringMaker { + std::string operator()(NSObject* const& nsObject); + }; +#endif + + +} // namespace Catch #endif // TWOBLUECUBES_CATCH_TOSTRING_H_INCLUDED diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp index f20c41a2..8fc3c89e 100644 --- a/include/internal/catch_tostring.hpp +++ b/include/internal/catch_tostring.hpp @@ -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 int i = 0, end = static_cast( size ), inc = 1; 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( 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( 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( 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( value ) ); -} template std::string fpToString( T value, int precision ) { @@ -134,74 +69,160 @@ std::string fpToString( T value, int precision ) { return d; } -std::string toString( const double value ) { - return fpToString( value, 10 ); -} -std::string toString( const float value ) { - return fpToString( value, 5 ) + 'f'; + +//// ======================================================= //// +// +// Out-of-line defs for full specialization of StringMaker +// +//// ======================================================= //// + +std::string StringMaker::operator()(const std::string& str) { + if (!getCurrentContext().getConfig()->showInvisibles()) { + return '"' + str + '"'; + } + + std::string s("\""); + 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( bool value ) { - return value ? "true" : "false"; +std::string StringMaker::operator()(const std::wstring& wstr) { + std::string s; + s.reserve(wstr.size()); + for (auto c : wstr) { + s += (c <= 0xff) ? static_cast(c) : '?'; + } + return ::Catch::Detail::stringify(s); } -std::string toString( char value ) { - if ( value == '\r' ) +std::string StringMaker::operator()(char const* str) { + if (str) { + return ::Catch::Detail::stringify(std::string{ str }); + } else { + return{ "{null string}" }; + } +} +std::string StringMaker::operator()(char* str) { + if (str) { + return ::Catch::Detail::stringify(std::string{ str }); + } else { + return{ "{null string}" }; + } +} +std::string StringMaker::operator()(wchar_t const * str) { + if (str) { + return ::Catch::Detail::stringify(std::wstring{ str }); + } else { + return{ "{null string}" }; + } +} +std::string StringMaker::operator()(wchar_t * str) { + if (str) { + return ::Catch::Detail::stringify(std::wstring{ str }); + } else { + return{ "{null string}" }; + } +} + + +std::string StringMaker::operator()(int value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::operator()(long value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::operator()(long long value) { + std::ostringstream oss; + oss << value; + if (value > Detail::hexThreshold) { + oss << " (0x" << std::hex << value << ')'; + } + return oss.str(); +} + +std::string StringMaker::operator()(unsigned int value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::operator()(unsigned long value) { + return ::Catch::Detail::stringify(static_cast(value)); +} +std::string StringMaker::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::operator()(bool b) { + return b ? "true" : "false"; +} + +std::string StringMaker::operator()(char value) { + if (value == '\r') { return "'\\r'"; - if ( value == '\f' ) + } else if (value == '\f') { return "'\\f'"; - if ( value == '\n' ) + } else if (value == '\n') { return "'\\n'"; - if ( value == '\t' ) + } else if (value == '\t') { return "'\\t'"; - if ( '\0' <= value && value < ' ' ) - return toString( static_cast( value ) ); - char chstr[] = "' '"; - chstr[1] = value; - return chstr; + } else if ('\0' <= value && value < ' ') { + return ::Catch::Detail::stringify(static_cast(value)); + } else { + char chstr[] = "' '"; + chstr[1] = value; + return chstr; + } +} +std::string StringMaker::operator()(signed char c) { + return ::Catch::Detail::stringify(static_cast(c)); +} +std::string StringMaker::operator()(unsigned char c) { + return ::Catch::Detail::stringify(static_cast(c)); } -std::string toString( signed char value ) { - return toString( static_cast( value ) ); -} - -std::string toString( unsigned char value ) { - return toString( static_cast( 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 ) { +std::string StringMaker::operator()(std::nullptr_t) { return "nullptr"; } +std::string StringMaker::operator()(float value) { + return fpToString(value, 5) + 'f'; +} +std::string StringMaker::operator()(double value) { + return fpToString(value, 10); +} + + #ifdef __OBJC__ - std::string toString( NSString const * const& nsstring ) { - if( !nsstring ) - return "nil"; - return "@" + toString([nsstring UTF8String]); - } - std::string toString( NSString * CATCH_ARC_STRONG const& nsstring ) { - if( !nsstring ) - return "nil"; - return "@" + toString([nsstring UTF8String]); - } - std::string toString( NSObject* const& nsObject ) { - return toString( [nsObject description] ); - } +std::string StringMaker::operator()(NSString const * const& nsstring) { + if (!nsstring) + return "nil"; + return "@" + toString([nsstring UTF8String]); +} +std::string StringMaker::operator()(NSString * CATCH_ARC_STRONG const& nsstring) { + if (!nsstring) + return "nil"; + return "@" + toString([nsstring UTF8String]); +} +std::string StringMaker::operator()(NSObject * const& nsObject) { + return ::Catch::Detail::stringify([nsObject description]); +} #endif } // end namespace Catch diff --git a/include/reporters/catch_reporter_junit.hpp b/include/reporters/catch_reporter_junit.hpp index f5cbc47d..874b0f97 100644 --- a/include/reporters/catch_reporter_junit.hpp +++ b/include/reporters/catch_reporter_junit.hpp @@ -160,7 +160,7 @@ namespace Catch { xml.writeAttribute( "classname", className ); xml.writeAttribute( "name", name ); } - xml.writeAttribute( "time", Catch::toString( sectionNode.stats.durationInSeconds ) ); + xml.writeAttribute( "time", ::Catch::Detail::stringify( sectionNode.stats.durationInSeconds ) ); writeAssertions( sectionNode ); diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt index b62a9a3c..9b857c4f 100644 --- a/projects/SelfTest/Baselines/console.sw.approved.txt +++ b/projects/SelfTest/Baselines/console.sw.approved.txt @@ -119,7 +119,7 @@ with expansion: MiscTests.cpp:: PASSED: - REQUIRE( (*__error()) == 1 ) + REQUIRE( (*__errno_location ()) == 1 ) with expansion: 1 == 1 @@ -5873,7 +5873,7 @@ TrickyTests.cpp:: PASSED: REQUIRE( (std::pair( 1, 2 )) == aNicePair ) with expansion: - std::pair( 1, 2 ) == std::pair( 1, 2 ) + { 1, 2 } == { 1, 2 } ------------------------------------------------------------------------------- Pointers can be compared to null @@ -5885,13 +5885,13 @@ ConditionTests.cpp:: PASSED: REQUIRE( p == 0 ) with expansion: - NULL == 0 + 0 == 0 ConditionTests.cpp:: PASSED: REQUIRE( p == pNULL ) with expansion: - NULL == NULL + 0 == 0 ConditionTests.cpp:: PASSED: @@ -8864,7 +8864,7 @@ ToStringPair.cpp: ToStringPair.cpp:: PASSED: - REQUIRE( Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" ) + REQUIRE( ::Catch::Detail::stringify( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" ) with expansion: "{ { 42, "Arthur" }, { "Ford", 24 } }" == @@ -8880,7 +8880,7 @@ TrickyTests.cpp:: PASSED: REQUIRE( p == 0 ) with expansion: - NULL == 0 + 0 == 0 ------------------------------------------------------------------------------- random SECTION tests @@ -9078,7 +9078,7 @@ ToStringPair.cpp: ToStringPair.cpp:: PASSED: - REQUIRE( Catch::toString(value) == "{ 34, \"xyzzy\" }" ) + REQUIRE( ::Catch::Detail::stringify(value) == "{ 34, \"xyzzy\" }" ) with expansion: "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" @@ -9090,7 +9090,7 @@ ToStringPair.cpp: ToStringPair.cpp:: PASSED: - REQUIRE( Catch::toString( value ) == "{ 34, \"xyzzy\" }" ) + REQUIRE( ::Catch::Detail::stringify( value ) == "{ 34, \"xyzzy\" }" ) with expansion: "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" @@ -9102,7 +9102,7 @@ ToStringPair.cpp: ToStringPair.cpp:: PASSED: - REQUIRE( Catch::toString( pr ) == "{ { \"green\", 55 } }" ) + REQUIRE( ::Catch::Detail::stringify( pr ) == "{ { \"green\", 55 } }" ) with expansion: "{ { "green", 55 } }" == @@ -9119,6 +9119,48 @@ TrickyTests.cpp:: FAILED: with expansion: "first" == "second" +------------------------------------------------------------------------------- +stringify( has_maker ) +------------------------------------------------------------------------------- +ToStringWhich.cpp: +............................................................................... + +ToStringWhich.cpp:: +PASSED: + REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker" ) +with expansion: + "StringMaker" + == + "StringMaker" + +------------------------------------------------------------------------------- +stringify( has_maker_and_toString ) +------------------------------------------------------------------------------- +ToStringWhich.cpp: +............................................................................... + +ToStringWhich.cpp:: +PASSED: + REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker" ) +with expansion: + "StringMaker" + == + "StringMaker" + +------------------------------------------------------------------------------- +stringify( has_operator ) +------------------------------------------------------------------------------- +ToStringWhich.cpp: +............................................................................... + +ToStringWhich.cpp:: +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 ------------------------------------------------------------------------------- @@ -9167,48 +9209,6 @@ PASSED: with expansion: ""wide load"" == ""wide load"" -------------------------------------------------------------------------------- -toString( has_maker ) -------------------------------------------------------------------------------- -ToStringWhich.cpp: -............................................................................... - -ToStringWhich.cpp:: -PASSED: - REQUIRE( Catch::toString( item ) == "StringMaker" ) -with expansion: - "StringMaker" - == - "StringMaker" - -------------------------------------------------------------------------------- -toString( has_maker_and_toString ) -------------------------------------------------------------------------------- -ToStringWhich.cpp: -............................................................................... - -ToStringWhich.cpp:: -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: -............................................................................... - -ToStringWhich.cpp:: -PASSED: - REQUIRE( Catch::toString( item ) == "toString( has_toString )" ) -with expansion: - "toString( has_toString )" - == - "toString( has_toString )" - ------------------------------------------------------------------------------- toString( vectors ToStringWhich.cpp:: PASSED: - REQUIRE( Catch::toString( v ) == "{ StringMaker }" ) + REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker }" ) with expansion: "{ StringMaker }" == @@ -9231,13 +9231,13 @@ EnumToString.cpp: EnumToString.cpp:: PASSED: - CHECK( Catch::toString(e0) == "E2{0}" ) + CHECK( ::Catch::Detail::stringify(e0) == "E2{0}" ) with expansion: "E2{0}" == "E2{0}" EnumToString.cpp:: PASSED: - CHECK( Catch::toString(e1) == "E2{1}" ) + CHECK( ::Catch::Detail::stringify(e1) == "E2{1}" ) with expansion: "E2{1}" == "E2{1}" @@ -9249,13 +9249,13 @@ EnumToString.cpp: EnumToString.cpp:: PASSED: - CHECK( Catch::toString(e0) == "0" ) + CHECK( ::Catch::Detail::stringify(e0) == "0" ) with expansion: "0" == "0" EnumToString.cpp:: PASSED: - CHECK( Catch::toString(e1) == "1" ) + CHECK( ::Catch::Detail::stringify(e1) == "1" ) with expansion: "1" == "1" @@ -9267,19 +9267,19 @@ ToStringVector.cpp: ToStringVector.cpp:: PASSED: - REQUIRE( Catch::toString(vv) == "{ }" ) + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ) with expansion: "{ }" == "{ }" ToStringVector.cpp:: PASSED: - REQUIRE( Catch::toString(vv) == "{ 42 }" ) + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" ) with expansion: "{ 42 }" == "{ 42 }" ToStringVector.cpp:: PASSED: - REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ) + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" ) with expansion: "{ 42, 250 }" == "{ 42, 250 }" @@ -9291,19 +9291,19 @@ ToStringVector.cpp: ToStringVector.cpp:: PASSED: - REQUIRE( Catch::toString(vv) == "{ }" ) + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ) with expansion: "{ }" == "{ }" ToStringVector.cpp:: PASSED: - REQUIRE( Catch::toString(vv) == "{ \"hello\" }" ) + REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" ) with expansion: "{ "hello" }" == "{ "hello" }" ToStringVector.cpp:: PASSED: - REQUIRE( Catch::toString(vv) == "{ \"hello\", \"world\" }" ) + REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" ) with expansion: "{ "hello", "world" }" == diff --git a/projects/SelfTest/Baselines/console.swa4.approved.txt b/projects/SelfTest/Baselines/console.swa4.approved.txt index 6ce6850a..f35e745d 100644 --- a/projects/SelfTest/Baselines/console.swa4.approved.txt +++ b/projects/SelfTest/Baselines/console.swa4.approved.txt @@ -119,7 +119,7 @@ with expansion: MiscTests.cpp:: PASSED: - REQUIRE( (*__error()) == 1 ) + REQUIRE( (*__errno_location ()) == 1 ) with expansion: 1 == 1 diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt index fc9528f7..ce6f7d23 100644 --- a/projects/SelfTest/Baselines/junit.sw.approved.txt +++ b/projects/SelfTest/Baselines/junit.sw.approved.txt @@ -715,13 +715,13 @@ MessageTests.cpp: TrickyTests.cpp: + + + - - - diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt index 001ad64b..32d233a1 100644 --- a/projects/SelfTest/Baselines/xml.sw.approved.txt +++ b/projects/SelfTest/Baselines/xml.sw.approved.txt @@ -128,7 +128,7 @@ - (*__error()) == 1 + (*__errno_location ()) == 1 1 == 1 @@ -6214,7 +6214,7 @@ re>" (std::pair<int, int>( 1, 2 )) == aNicePair - std::pair( 1, 2 ) == std::pair( 1, 2 ) + { 1, 2 } == { 1, 2 } @@ -6225,7 +6225,7 @@ re>" p == 0 - NULL == 0 + 0 == 0 @@ -6233,7 +6233,7 @@ re>" p == pNULL - NULL == NULL + 0 == 0 @@ -9516,7 +9516,7 @@ spanner - Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" + ::Catch::Detail::stringify( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" "{ { 42, "Arthur" }, { "Ford", 24 } }" @@ -9532,7 +9532,7 @@ spanner p == 0 - NULL == 0 + 0 == 0 @@ -9743,7 +9743,7 @@ spanner - Catch::toString(value) == "{ 34, \"xyzzy\" }" + ::Catch::Detail::stringify(value) == "{ 34, \"xyzzy\" }" "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" @@ -9754,7 +9754,7 @@ spanner - Catch::toString( value ) == "{ 34, \"xyzzy\" }" + ::Catch::Detail::stringify( value ) == "{ 34, \"xyzzy\" }" "{ 34, "xyzzy" }" == "{ 34, "xyzzy" }" @@ -9765,7 +9765,7 @@ spanner - Catch::toString( pr ) == "{ { \"green\", 55 } }" + ::Catch::Detail::stringify( pr ) == "{ { \"green\", 55 } }" "{ { "green", 55 } }" @@ -9786,6 +9786,45 @@ spanner + + + + ::Catch::Detail::stringify( item ) == "StringMaker<has_maker>" + + + "StringMaker<has_maker>" +== +"StringMaker<has_maker>" + + + + + + + + ::Catch::Detail::stringify( item ) == "StringMaker<has_maker_and_operator>" + + + "StringMaker<has_maker_and_operator>" +== +"StringMaker<has_maker_and_operator>" + + + + + + + + ::Catch::Detail::stringify( item ) == "operator<<( has_operator )" + + + "operator<<( has_operator )" +== +"operator<<( has_operator )" + + + + @@ -9830,49 +9869,10 @@ spanner - - - - Catch::toString( item ) == "StringMaker<has_maker>" - - - "StringMaker<has_maker>" -== -"StringMaker<has_maker>" - - - - - - - - Catch::toString( item ) == "toString( has_maker_and_toString )" - - - "toString( has_maker_and_toString )" -== -"toString( has_maker_and_toString )" - - - - - - - - Catch::toString( item ) == "toString( has_toString )" - - - "toString( has_toString )" -== -"toString( has_toString )" - - - - - Catch::toString( v ) == "{ StringMaker<has_maker> }" + ::Catch::Detail::stringify( v ) == "{ StringMaker<has_maker> }" "{ StringMaker<has_maker> }" @@ -9885,7 +9885,7 @@ spanner - Catch::toString(e0) == "E2{0}" + ::Catch::Detail::stringify(e0) == "E2{0}" "E2{0}" == "E2{0}" @@ -9893,7 +9893,7 @@ spanner - Catch::toString(e1) == "E2{1}" + ::Catch::Detail::stringify(e1) == "E2{1}" "E2{1}" == "E2{1}" @@ -9904,7 +9904,7 @@ spanner - Catch::toString(e0) == "0" + ::Catch::Detail::stringify(e0) == "0" "0" == "0" @@ -9912,7 +9912,7 @@ spanner - Catch::toString(e1) == "1" + ::Catch::Detail::stringify(e1) == "1" "1" == "1" @@ -9923,7 +9923,7 @@ spanner - Catch::toString(vv) == "{ }" + ::Catch::Detail::stringify(vv) == "{ }" "{ }" == "{ }" @@ -9931,7 +9931,7 @@ spanner - Catch::toString(vv) == "{ 42 }" + ::Catch::Detail::stringify(vv) == "{ 42 }" "{ 42 }" == "{ 42 }" @@ -9939,7 +9939,7 @@ spanner - Catch::toString(vv) == "{ 42, 250 }" + ::Catch::Detail::stringify(vv) == "{ 42, 250 }" "{ 42, 250 }" == "{ 42, 250 }" @@ -9950,7 +9950,7 @@ spanner - Catch::toString(vv) == "{ }" + ::Catch::Detail::stringify(vv) == "{ }" "{ }" == "{ }" @@ -9958,7 +9958,7 @@ spanner - Catch::toString(vv) == "{ \"hello\" }" + ::Catch::Detail::stringify(vv) == "{ \"hello\" }" "{ "hello" }" == "{ "hello" }" @@ -9966,7 +9966,7 @@ spanner - Catch::toString(vv) == "{ \"hello\", \"world\" }" + ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" "{ "hello", "world" }" diff --git a/projects/SelfTest/EnumToString.cpp b/projects/SelfTest/EnumToString.cpp index 52a39812..eb80c747 100644 --- a/projects/SelfTest/EnumToString.cpp +++ b/projects/SelfTest/EnumToString.cpp @@ -1,32 +1,28 @@ #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 Enum1 { Enum1Value0, Enum1Value1 }; TEST_CASE( "toString(enum)", "[toString][enum]" ) { Enum1 e0 = Enum1Value0; - CHECK( Catch::toString(e0) == "0" ); + CHECK( ::Catch::Detail::stringify(e0) == "0" ); Enum1 e1 = Enum1Value1; - CHECK( Catch::toString(e1) == "1" ); + CHECK( ::Catch::Detail::stringify(e1) == "1" ); } // Enum with user-provided stream operator 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(v) << "}"; } TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) { Enum2 e0 = Enum2Value0; - CHECK( Catch::toString(e0) == "E2{0}" ); + CHECK( ::Catch::Detail::stringify(e0) == "E2{0}" ); Enum2 e1 = Enum2Value1; - CHECK( Catch::toString(e1) == "E2{1}" ); + CHECK( ::Catch::Detail::stringify(e1) == "E2{1}" ); } // 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][.]" ) { EnumClass1 e0 = EnumClass1::EnumClass1Value0; - CHECK( Catch::toString(e0) == "0" ); + CHECK( ::Catch::Detail::stringify(e0) == "0" ); EnumClass1 e1 = EnumClass1::EnumClass1Value1; - CHECK( Catch::toString(e1) == "1" ); + CHECK( ::Catch::Detail::stringify(e1) == "1" ); } // Enum class with user-provided stream operator 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( e2 ) ) { case static_cast( EnumClass2::EnumClass2Value0 ): 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][.]" ) { EnumClass2 e0 = EnumClass2::EnumClass2Value0; - CHECK( Catch::toString(e0) == "E2/V0" ); + CHECK( ::Catch::Detail::stringify(e0) == "E2/V0" ); EnumClass2 e1 = EnumClass2::EnumClass2Value1; - CHECK( Catch::toString(e1) == "E2/V1" ); + CHECK( ::Catch::Detail::stringify(e1) == "E2/V1" ); EnumClass2 e3 = static_cast(10); - CHECK( Catch::toString(e3) == "Unknown enum value 10" ); + CHECK( ::Catch::Detail::stringify(e3) == "Unknown enum value 10" ); } diff --git a/projects/SelfTest/ExceptionTests.cpp b/projects/SelfTest/ExceptionTests.cpp index c9754d4a..50822ded 100644 --- a/projects/SelfTest/ExceptionTests.cpp +++ b/projects/SelfTest/ExceptionTests.cpp @@ -144,7 +144,7 @@ CATCH_TRANSLATE_EXCEPTION( CustomStdException& 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]" ) diff --git a/projects/SelfTest/MessageTests.cpp b/projects/SelfTest/MessageTests.cpp index 1c944a3d..e0970886 100644 --- a/projects/SelfTest/MessageTests.cpp +++ b/projects/SelfTest/MessageTests.cpp @@ -134,5 +134,5 @@ TEST_CASE( "Pointers can be converted to strings", "[messages][.]" ) { int p; WARN( "actual address of p: " << &p ); - WARN( "toString(p): " << Catch::toString( &p ) ); + WARN( "toString(p): " << ::Catch::Detail::stringify( &p ) ); } diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp index aab0295f..efa5c76f 100644 --- a/projects/SelfTest/MiscTests.cpp +++ b/projects/SelfTest/MiscTests.cpp @@ -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]" ) { 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\"" ); } TEST_CASE( "toString on const wchar_t pointer returns the string contents", "[toString]" ) { const wchar_t * s = L"wide load"; - std::string result = Catch::toString( s ); + std::string result = ::Catch::Detail::stringify( s ); CHECK( result == "\"wide load\"" ); } TEST_CASE( "toString on wchar_t const pointer returns the string contents", "[toString]" ) { wchar_t * const s = const_cast( L"wide load" ); - std::string result = Catch::toString( s ); + std::string result = ::Catch::Detail::stringify( s ); CHECK( result == "\"wide load\"" ); } TEST_CASE( "toString on wchar_t returns the string contents", "[toString]" ) { wchar_t * s = const_cast( L"wide load" ); - std::string result = Catch::toString( s ); + std::string result = ::Catch::Detail::stringify( s ); CHECK( result == "\"wide load\"" ); } diff --git a/projects/SelfTest/ToStringPair.cpp b/projects/SelfTest/ToStringPair.cpp index 8f510700..38f30854 100644 --- a/projects/SelfTest/ToStringPair.cpp +++ b/projects/SelfTest/ToStringPair.cpp @@ -1,37 +1,19 @@ #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 - struct StringMaker > { - static std::string convert( const std::pair& pair ) { - std::ostringstream oss; - oss << "{ " - << toString( pair.first ) - << ", " - << toString( pair.second ) - << " }"; - return oss.str(); - } - }; -} - TEST_CASE( "std::pair -> toString", "[toString][pair]" ) { std::pair value( 34, "xyzzy" ); - REQUIRE( Catch::toString( value ) == "{ 34, \"xyzzy\" }" ); + REQUIRE( ::Catch::Detail::stringify( value ) == "{ 34, \"xyzzy\" }" ); } TEST_CASE( "std::pair -> toString", "[toString][pair]" ) { std::pair value( 34, "xyzzy" ); - REQUIRE( Catch::toString(value) == "{ 34, \"xyzzy\" }" ); + REQUIRE( ::Catch::Detail::stringify(value) == "{ 34, \"xyzzy\" }" ); } TEST_CASE( "std::vector > -> toString", "[toString][pair]" ) { std::vector > pr; 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... @@ -43,5 +25,5 @@ TEST_CASE( "pair > -> toString", "[t right_t right( "Ford", 24 ); std::pair pair( left, right ); - REQUIRE( Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" ); + REQUIRE( ::Catch::Detail::stringify( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" ); } diff --git a/projects/SelfTest/ToStringTuple.cpp b/projects/SelfTest/ToStringTuple.cpp index 9c855715..dcd1d803 100644 --- a/projects/SelfTest/ToStringTuple.cpp +++ b/projects/SelfTest/ToStringTuple.cpp @@ -5,42 +5,42 @@ TEST_CASE( "tuple<>", "[toString][tuple][c++11][.]" ) { typedef std::tuple<> type; - CHECK( "{ }" == Catch::toString(type{}) ); + CHECK( "{ }" == ::Catch::Detail::stringify(type{}) ); type value {}; - CHECK( "{ }" == Catch::toString(value) ); + CHECK( "{ }" == ::Catch::Detail::stringify(value) ); } TEST_CASE( "tuple", "[toString][tuple][c++11][.]" ) { typedef std::tuple type; - CHECK( "{ 0 }" == Catch::toString(type{0}) ); + CHECK( "{ 0 }" == ::Catch::Detail::stringify(type{0}) ); } TEST_CASE( "tuple", "[toString][tuple][c++11][.]" ) { typedef std::tuple type; - CHECK( "1.2f" == Catch::toString(float(1.2)) ); - CHECK( "{ 1.2f, 0 }" == Catch::toString(type{1.2f,0}) ); + CHECK( "1.2f" == ::Catch::Detail::stringify(float(1.2)) ); + CHECK( "{ 1.2f, 0 }" == ::Catch::Detail::stringify(type{1.2f,0}) ); } TEST_CASE( "tuple", "[toString][tuple][c++11][.]" ) { typedef std::tuple type; - CHECK( "{ \"hello\", \"world\" }" == Catch::toString(type{"hello","world"}) ); + CHECK( "{ \"hello\", \"world\" }" == ::Catch::Detail::stringify(type{"hello","world"}) ); } TEST_CASE( "tuple,tuple<>,float>", "[toString][tuple][c++11][.]" ) { typedef std::tuple,std::tuple<>,float> type; type value { std::tuple{42}, {}, 1.2f }; - CHECK( "{ { 42 }, { }, 1.2f }" == Catch::toString(value) ); + CHECK( "{ { 42 }, { }, 1.2f }" == ::Catch::Detail::stringify(value) ); } TEST_CASE( "tuple", "[toString][tuple][c++11][.]" ) { typedef std::tuple type; type value { nullptr, 42, "Catch me" }; - CHECK( "{ nullptr, 42, \"Catch me\" }" == Catch::toString(value) ); + CHECK( "{ nullptr, 42, \"Catch me\" }" == ::Catch::Detail::stringify(value) ); } diff --git a/projects/SelfTest/ToStringVector.cpp b/projects/SelfTest/ToStringVector.cpp index eafe886d..c70872c8 100644 --- a/projects/SelfTest/ToStringVector.cpp +++ b/projects/SelfTest/ToStringVector.cpp @@ -6,21 +6,21 @@ TEST_CASE( "vector -> toString", "[toString][vector]" ) { std::vector vv; - REQUIRE( Catch::toString(vv) == "{ }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); vv.push_back( 42 ); - REQUIRE( Catch::toString(vv) == "{ 42 }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" ); vv.push_back( 250 ); - REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" ); } TEST_CASE( "vector -> toString", "[toString][vector]" ) { std::vector vv; - REQUIRE( Catch::toString(vv) == "{ }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); vv.push_back( "hello" ); - REQUIRE( Catch::toString(vv) == "{ \"hello\" }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\" }" ); vv.push_back( "world" ); - REQUIRE( Catch::toString(vv) == "{ \"hello\", \"world\" }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ \"hello\", \"world\" }" ); } namespace { @@ -50,19 +50,19 @@ namespace { TEST_CASE( "vector -> toString", "[toString][vector,allocator][c++11][.]" ) { std::vector > vv; - REQUIRE( Catch::toString(vv) == "{ }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ }" ); vv.push_back( 42 ); - REQUIRE( Catch::toString(vv) == "{ 42 }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42 }" ); vv.push_back( 250 ); - REQUIRE( Catch::toString(vv) == "{ 42, 250 }" ); + REQUIRE( ::Catch::Detail::stringify(vv) == "{ 42, 250 }" ); } TEST_CASE( "vec> -> toString", "[toString][vector,allocator][c++11][.]" ) { using inner = std::vector>; using vector = std::vector; vector v; - REQUIRE( Catch::toString(v) == "{ }" ); + REQUIRE( ::Catch::Detail::stringify(v) == "{ }" ); v.push_back( inner { "hello" } ); v.push_back( inner { "world" } ); - REQUIRE( Catch::toString(v) == "{ { \"hello\" }, { \"world\" } }" ); + REQUIRE( ::Catch::Detail::stringify(v) == "{ { \"hello\" }, { \"world\" } }" ); } diff --git a/projects/SelfTest/ToStringWhich.cpp b/projects/SelfTest/ToStringWhich.cpp index a1d2a609..8bd1b6f9 100644 --- a/projects/SelfTest/ToStringWhich.cpp +++ b/projects/SelfTest/ToStringWhich.cpp @@ -5,67 +5,69 @@ */ -struct has_toString { }; +struct has_operator { }; 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 { - 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<> struct StringMaker { - static std::string convert( const has_maker& ) { + std::string operator()( const has_maker& ) { return "StringMaker"; } }; template<> - struct StringMaker { - static std::string convert( const has_maker_and_toString& ) { - return "StringMaker"; + struct StringMaker { + std::string operator()( const has_maker_and_operator& ) { + return "StringMaker"; } }; } -// Call the overload -TEST_CASE( "toString( has_toString )", "[toString]" ) { - has_toString item; - REQUIRE( Catch::toString( item ) == "toString( has_toString )" ); +// Call the operator +TEST_CASE( "stringify( has_operator )", "[toString]" ) { + has_operator item; + REQUIRE( ::Catch::Detail::stringify( item ) == "operator<<( has_operator )" ); } -// Call the overload -TEST_CASE( "toString( has_maker )", "toString]" ) { +// Call the stringmaker +TEST_CASE( "stringify( has_maker )", "[toString]" ) { has_maker item; - REQUIRE( Catch::toString( item ) == "StringMaker" ); + REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker" ); } -// Call the overload -TEST_CASE( "toString( has_maker_and_toString )", "[.][toString]" ) { - has_maker_and_toString item; - REQUIRE( Catch::toString( item ) == "toString( has_maker_and_toString )" ); +// Call the stringmaker +TEST_CASE( "stringify( has_maker_and_toString )", "[.][toString]" ) { + has_maker_and_operator item; + REQUIRE( ::Catch::Detail::stringify( item ) == "StringMaker" ); } // Vectors... // Don't run this in approval tests as it is sensitive to two phase lookup differences TEST_CASE( "toString( vectors v(1); - // This invokes template toString which actually gives us '{ ? }' - REQUIRE( Catch::toString( v ) == "{ {?} }" ); + std::vector v(1); + REQUIRE( ::Catch::Detail::stringify( v ) == "{ operator<<( has_operator ) }" ); } TEST_CASE( "toString( vectors v(1); - REQUIRE( Catch::toString( v ) == "{ StringMaker }" ); + REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker }" ); } // Don't run this in approval tests as it is sensitive to two phase lookup differences TEST_CASE( "toString( vectors v(1); - // Note: This invokes the template toString -> StringMaker - REQUIRE( Catch::toString( v ) == "{ StringMaker }" ); + std::vector v(1); + REQUIRE( ::Catch::Detail::stringify( v ) == "{ StringMaker }" ); } diff --git a/projects/SelfTest/TrickyTests.cpp b/projects/SelfTest/TrickyTests.cpp index 129fc2a2..e5616d0e 100644 --- a/projects/SelfTest/TrickyTests.cpp +++ b/projects/SelfTest/TrickyTests.cpp @@ -14,15 +14,11 @@ #include "catch.hpp" -namespace Catch -{ - template<> - std::string toString >( const std::pair& value ) - { +namespace Catch { + std::string toString( const std::pair& value ) { std::ostringstream oss; oss << "std::pair( " << value.first << ", " << value.second << " )"; return oss.str(); - } } @@ -340,7 +336,7 @@ struct Awkward TEST_CASE( "non streamable - with conv. op", "[Tricky]" ) { Awkward awkward; - std::string s = Catch::toString( awkward ); + std::string s = ::Catch::Detail::stringify( awkward ); REQUIRE( s == "7" ); }