1 #include "catch.hpp" 2 /* 3 Demonstrate which version of toString/StringMaker is being used 4 for various types 5 */ 6 7 8 struct has_toString { }; 9 struct has_maker {}; 10 struct has_maker_and_toString {}; 11 12 namespace Catch { toString(const has_toString &)13 inline std::string toString( const has_toString& ) { 14 return "toString( has_toString )"; 15 } toString(const has_maker_and_toString &)16 inline std::string toString( const has_maker_and_toString& ) { 17 return "toString( has_maker_and_toString )"; 18 } 19 template<> 20 struct StringMaker<has_maker> { convertCatch::StringMaker21 static std::string convert( const has_maker& ) { 22 return "StringMaker<has_maker>"; 23 } 24 }; 25 template<> 26 struct StringMaker<has_maker_and_toString> { convertCatch::StringMaker27 static std::string convert( const has_maker_and_toString& ) { 28 return "StringMaker<has_maker_and_toString>"; 29 } 30 }; 31 } 32 33 // Call the overload 34 TEST_CASE( "toString( has_toString )", "[toString]" ) { 35 has_toString item; 36 REQUIRE( Catch::toString( item ) == "toString( has_toString )" ); 37 } 38 39 // Call the overload 40 TEST_CASE( "toString( has_maker )", "toString]" ) { 41 has_maker item; 42 REQUIRE( Catch::toString( item ) == "StringMaker<has_maker>" ); 43 } 44 45 // Call the overload 46 TEST_CASE( "toString( has_maker_and_toString )", "[.][toString]" ) { 47 has_maker_and_toString item; 48 REQUIRE( Catch::toString( item ) == "toString( has_maker_and_toString )" ); 49 } 50 51 // Vectors... 52 53 // Don't run this in approval tests as it is sensitive to two phase lookup differences 54 TEST_CASE( "toString( vectors<has_toString )", "[.][toString][!nonportable]" ) { 55 std::vector<has_toString> v(1); 56 // This invokes template<T> toString which actually gives us '{ ? }' 57 REQUIRE( Catch::toString( v ) == "{ {?} }" ); 58 } 59 60 TEST_CASE( "toString( vectors<has_maker )", "[toString]" ) { 61 std::vector<has_maker> v(1); 62 REQUIRE( Catch::toString( v ) == "{ StringMaker<has_maker> }" ); 63 } 64 65 66 // Don't run this in approval tests as it is sensitive to two phase lookup differences 67 TEST_CASE( "toString( vectors<has_maker_and_toString )", "[.][toString][!nonportable]" ) { 68 std::vector<has_maker_and_toString> v(1); 69 // Note: This invokes the template<T> toString -> StringMaker 70 REQUIRE( Catch::toString( v ) == "{ StringMaker<has_maker_and_toString> }" ); 71 } 72