Merge branch 'tuple_to_string' of https://github.com/PureAbstract/Catch into PureAbstract-tuple_to_string

This commit is contained in:
Phil Nash 2014-12-30 18:42:17 +00:00
commit 75a08bbc6c
3 changed files with 132 additions and 27 deletions

View File

@ -21,12 +21,46 @@
#include "catch_objc_arc.hpp"
#endif
#ifdef CATCH_CPP11_OR_GREATER
#include <tuple>
#include <type_traits>
#endif
namespace Catch {
// Why we're here.
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 );
#ifdef CATCH_CONFIG_CPP11_NULLPTR
std::string toString( std::nullptr_t );
#endif
#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 {
extern std::string unprintableString;
@ -166,6 +200,50 @@ std::string toString( std::vector<T,Allocator> const& v ) {
return Detail::rangeToString( v.begin(), v.end() );
}
#ifdef CATCH_CPP11_OR_GREATER
/*
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();
}
};
#endif
namespace Detail {
template<typename T>
std::string makeString( T const& value ) {
@ -185,33 +263,6 @@ std::string toString( T const& value ) {
return StringMaker<T>::convert( 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 );
#ifdef CATCH_CONFIG_CPP11_NULLPTR
std::string toString( std::nullptr_t );
#endif
#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 {
template<typename InputIterator>

View File

@ -6,6 +6,11 @@ project(Catch)
get_filename_component(CATCH_DIR "${CMAKE_CURRENT_SOURCE_DIR}" PATH)
get_filename_component(CATCH_DIR "${CATCH_DIR}" PATH)
set(SELF_TEST_DIR ${CATCH_DIR}/projects/SelfTest)
if(USE_CPP11)
## We can't turn this on by default, since it breaks on travis
message(STATUS "Enabling C++11")
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
endif()
# define the sources of the self test
set(SOURCES
@ -25,6 +30,7 @@ set(SOURCES
${SELF_TEST_DIR}/ToStringPair.cpp
${SELF_TEST_DIR}/ToStringVector.cpp
${SELF_TEST_DIR}/ToStringWhich.cpp
${SELF_TEST_DIR}/ToStringTuple.cpp
)
# configure the executable

View File

@ -0,0 +1,48 @@
#include "catch.hpp"
#ifdef CATCH_CPP11_OR_GREATER
TEST_CASE( "tuple<>", "[toString][tuple]" )
{
typedef std::tuple<> type;
CHECK( "{ }" == Catch::toString(type{}) );
type value {};
CHECK( "{ }" == Catch::toString(value) );
}
TEST_CASE( "tuple<int>", "[toString][tuple]" )
{
typedef std::tuple<int> type;
CHECK( "{ 0 }" == Catch::toString(type{0}) );
}
TEST_CASE( "tuple<float,int>", "[toString][tuple]" )
{
typedef std::tuple<float,int> type;
CHECK( "1.2f" == Catch::toString(float(1.2)) );
CHECK( "{ 1.2f, 0 }" == Catch::toString(type{1.2,0}) );
}
TEST_CASE( "tuple<string,string>", "[toString][tuple]" )
{
typedef std::tuple<std::string,std::string> type;
CHECK( "{ \"hello\", \"world\" }" == Catch::toString(type{"hello","world"}) );
}
TEST_CASE( "tuple<tuple<int>,tuple<>,float>", "[toString][tuple]" )
{
typedef std::tuple<std::tuple<int>,std::tuple<>,float> type;
type value { std::tuple<int>{42}, {}, 1.2f };
CHECK( "{ { 42 }, { }, 1.2f }" == Catch::toString(value) );
}
TEST_CASE( "tuple<nullptr,int,const char *>", "[toString][tuple]" )
{
typedef std::tuple<std::nullptr_t,int,const char *> type;
type value { nullptr, 42, "Catch me" };
CHECK( "{ nullptr, 42, \"Catch me\" }" == Catch::toString(value) );
}
#endif /* #ifdef CATCH_CPP11_OR_GREATER */