1 #include "catch.hpp"
2 
3 // === Pair ===
4 namespace Catch {
5     // Note: If we put this in the right place in catch_tostring, then
6     // we can make it an overload of Catch::toString
7     template<typename T1, typename T2>
8     struct StringMaker<std::pair<T1,T2> > {
convertCatch::StringMaker9         static std::string convert( const std::pair<T1,T2>& pair ) {
10             std::ostringstream oss;
11             oss << "{ "
12                 << toString( pair.first )
13                 << ", "
14                 << toString( pair.second )
15                 << " }";
16             return oss.str();
17         }
18     };
19 }
20 
21 TEST_CASE( "std::pair<int,std::string> -> toString", "[toString][pair]" ) {
22     std::pair<int,std::string> value( 34, "xyzzy" );
23     REQUIRE( Catch::toString( value ) == "{ 34, \"xyzzy\" }" );
24 }
25 
26 TEST_CASE( "std::pair<int,const std::string> -> toString", "[toString][pair]" ) {
27     std::pair<int,const std::string> value( 34, "xyzzy" );
28     REQUIRE( Catch::toString(value) == "{ 34, \"xyzzy\" }" );
29 }
30 
31 TEST_CASE( "std::vector<std::pair<std::string,int> > -> toString", "[toString][pair]" ) {
32     std::vector<std::pair<std::string,int> > pr;
33     pr.push_back( std::make_pair("green", 55 ) );
34     REQUIRE( Catch::toString( pr ) == "{ { \"green\", 55 } }" );
35 }
36 
37 // This is pretty contrived - I figure if this works, anything will...
38 TEST_CASE( "pair<pair<int,const char *,pair<std::string,int> > -> toString", "[toString][pair]" ) {
39     typedef std::pair<int,const char *> left_t;
40     typedef std::pair<std::string,int> right_t;
41 
42     left_t  left( 42, "Arthur" );
43     right_t right( "Ford", 24 );
44 
45     std::pair<left_t,right_t> pair( left, right );
46     REQUIRE( Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" );
47 }
48