8000 Merge branch 'tuple_to_string' of https://github.com/PureAbstract/Cat… · log4cplus/Catch@75a08bb · GitHub
[go: up one dir, main page]

Skip to content

Commit 75a08bb

Browse files
committed
Merge branch 'tuple_to_string' of https://github.com/PureAbstract/Catch into PureAbstract-tuple_to_string
2 parents d76e081 + f559a51 commit 75a08bb

File tree

3 files changed

+132
-27
lines changed

3 files changed

+132
-27
lines changed

include/internal/catch_tostring.h

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,46 @@
2121
#include "catch_objc_arc.hpp"
2222
#endif
2323

24+
#ifdef CATCH_CPP11_OR_GREATER
25+
#include <tuple>
26+
#include <type_traits>
27+
#endif
28+
2429
namespace Catch {
2530

2631
// Why we're here.
2732
template<typename T>
2833
std::string toString( T const& value );
2934

35+
// Built in overloads
36+
37+
std::string toString( std::string const& value );
38+
std::string toString( std::wstring const& value );
39+
std::string toString( const char* const value );
40+
std::string toString( char* const value );
41+
std::string toString( const wchar_t* const value );
42+
std::string toString( wchar_t* const value );
43+
std::string toString( int value );
44+
std::string toString( unsigned long value );
45+
std::string toString( unsigned int value );
46+
std::string toString( const double value );
47+
std::string toString( const float value );
48+
std::string toString( bool value );
49+
std::string toString( char value );
50+
std::string toString( signed char value );
51+
std::string toString( unsigned char value );
52+
53+
#ifdef CATCH_CONFIG_CPP11_NULLPTR
54+
std::string toString( std::nullptr_t );
55+
#endif
56+
57+
#ifdef __OBJC__
58+
std::string toString( NSString const * const& nsstring );
59+
std::string toString( NSString * CATCH_ARC_STRONG const& nsstring );
60+
std::string toString( NSObject* const& nsObject );
61+
#endif
62+
63+
3064
namespace Detail {
3165

3266
extern std::string unprintableString;
@@ -166,6 +200,50 @@ std::string toString( std::vector<T,Allocator> const& v ) {
166200
return Detail::rangeToString( v.begin(), v.end() );
167201
}
168202

203+
204+
#ifdef CATCH_CPP11_OR_GREATER
205+
/*
206+
toString for tuples
207+
*/
208+
namespace TupleDetail {
209+
template<
210+
typename Tuple,
211+
std::size_t N = 0,
212+
bool = (N < std::tuple_size<Tuple>::value)
213+
>
214+
struct ElementPrinter {
215+
static void print( const Tuple& tuple, std::ostream& os )
216+
{
217+
os << ( N ? ", " : " " )
218+
<< Catch::toString(std::get<N>(tuple));
219+
ElementPrinter<Tuple,N+1>::print(tuple,os);
220+
}
221+
};
222+
223+
template<
224+
typename Tuple,
225+
std::size_t N
226+
>
227+
struct ElementPrinter<Tuple,N,false> {
228+
static void print( const Tuple&, std::ostream& ) {}
229+
};
230+
231+
}
232+
233+
template<typename ...Types>
234+
struct StringMaker<std::tuple<Types...>> {
235+
236+
static std::string convert( const std::tuple<Types...>& tuple )
237+
{
238+
std::ostringstream os;
239+
os << '{';
240+
TupleDetail::ElementPrinter<std::tuple<Types...>>::print( tuple, os );
241+
os << " }";
242+
return os.str();
243+
}
244+
};
245+
#endif
246+
169247
namespace Detail {
170248
template<typename T>
171249
std::string makeString( T const& value ) {
@@ -185,33 +263,6 @@ std::string toString( T const& value ) {
185263
return StringMaker<T>::convert( value );
186264
}
187265

188-
// Built in overloads
189-
190-
std::string toString( std::string const& value );
191-
std::string toString( std::wstring const& value );
192-
std::string toString( const char* const value );
193-
std::string toString( char* const value );
194-
std::string toString( const wchar_t* const value );
195-
std::string toString( wchar_t* const value );
196-
std::string toString( int value );
197-
std::string toString( unsigned long value );
198-
std::string toString( unsigned int value );
199-
std::string toString( const double value );
200-
std::string toString( const float value );
201-
std::string toString( bool value );
202-
std::string toString( char value );
203-
std::string toString( signed char value );
204-
std::string toString( unsigned char value );
205-
206-
#ifdef CATCH_CONFIG_CPP11_NULLPTR
207-
std::string toString( std::nullptr_t );
208-
#endif
209-
210-
#ifdef __OBJC__
211-
std::string toString( NSString const * const& nsstring );
212-
std::string toString( NSString * CATCH_ARC_STRONG const& nsstring );
213-
std::string toString( NSObject* const& nsObject );
214-
#endif
215266

216267
namespace Detail {
217268
template<typename InputIterator>

projects/CMake/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ project(Catch)
66
get_filename_component(CATCH_DIR "${CMAKE_CURRENT_SOURCE_DIR}" PATH)
77
get_filename_component(CATCH_DIR "${CATCH_DIR}" PATH)
88
set(SELF_TEST_DIR ${CATCH_DIR}/projects/SelfTest)
9+
if(USE_CPP11)
10+
## We can't turn this on by default, since it breaks on travis
11+
message(STATUS "Enabling C++11")
12+
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
13+
endif()
914

1015
# define the sources of the self test
1116
set(SOURCES
@@ -25,6 +30,7 @@ set(SOURCES
2530
${SELF_TEST_DIR}/ToStringPair.cpp
2631
${SELF_TEST_DIR}/ToStringVector.cpp
2732
${SELF_TEST_DIR}/ToStringWhich.cpp
33+
${SELF_TEST_DIR}/ToStringTuple.cpp
2834
)
2935

3036
# configure the executable

projects/SelfTest/ToStringTuple.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "catch.hpp"
2+
3+
#ifdef CATCH_CPP11_OR_GREATER
4+
5+
TEST_CASE( "tuple<>", "[toString][tuple]" )
6+
{
7+
typedef std::tuple<> type;
8+
CHECK( "{ }" == Catch::toString(type{}) );
9+
type value {};
10+
CHECK( "{ }" == Catch::toString(value) );
11+
}
12+
13+
TEST_CASE( "tuple<int>", "[toString][tuple]" )
14+
{
15+
typedef std::tuple<int> type;
16+
CHECK( "{ 0 }" == Catch::toString(type{0}) );
17+
}
18+
19+
20+
TEST_CASE( "tuple<float,int>", "[toString][tuple]" )
21+
{
22+
typedef std::tuple<float,int> type;
23+
CHECK( "1.2f" == Catch::toString(float(1.2)) );
24+
CHECK( "{ 1.2f, 0 }" == Catch::toString(type{1.2,0}) );
25+
}
26+
27+
TEST_CASE( "tuple<string,string>", "[toString][tuple]" )
28+
{
29+
typedef std::tuple<std::string,std::string> type;
30+
CHECK( "{ \"hello\", \"world\" }" == Catch::toString(type{"hello","world"}) );
31+
}
32+
33+
TEST_CASE( "tuple<tuple<int>,tuple<>,float>", "[toString][tuple]" )
34+
{
35+
typedef std::tuple<std::tuple<int>,std::tuple<>,float> type;
36+
type value { std::tuple<int>{42}, {}, 1.2f };
37+
CHECK( "{ { 42 }, { }, 1.2f }" == Catch::toString(value) );
38+
}
39+
40+
TEST_CASE( "tuple<nullptr,int,const char *>", "[toString][tuple]" )
41+
{
42+
typedef std::tuple<std::nullptr_t,int,const char *> type;
43+
type value { nullptr, 42, "Catch me" };
44+
CHECK( "{ nullptr, 42, \"Catch me\" }" == Catch::toString(value) );
45+
}
46+
47+
#endif /* #ifdef CATCH_CPP11_OR_GREATER */
48+

0 commit comments

Comments
 (0)
0