mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
A bunch of Catch::toString tests
This commit is contained in:
parent
cd2a5aa688
commit
abf9ffc982
51
projects/SelfTest/ToStringPair.cpp
Normal file
51
projects/SelfTest/ToStringPair.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#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<typename T1, typename T2>
|
||||
struct StringMaker<std::pair<T1,T2> > {
|
||||
static std::string convert( const std::pair<T1,T2>& pair ) {
|
||||
std::ostringstream oss;
|
||||
oss << "{ "
|
||||
<< toString( pair.first )
|
||||
<< ", "
|
||||
<< toString( pair.second )
|
||||
<< " }";
|
||||
return oss.str();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
TEST_CASE( "std::pair<int,std::string> -> toString", "[toString][pair]" )
|
||||
{
|
||||
std::pair<int,std::string> value( 34, "xyzzy" );
|
||||
REQUIRE( Catch::toString( value ) == "{ 34, \"xyzzy\" }" );
|
||||
}
|
||||
|
||||
TEST_CASE( "std::pair<int,const std::string> -> toString", "[toString][pair]" )
|
||||
{
|
||||
std::pair<int,const std::string> value( 34, "xyzzy" );
|
||||
REQUIRE( Catch::toString(value) == "{ 34, \"xyzzy\" }" );
|
||||
}
|
||||
|
||||
TEST_CASE( "std::vector<std::pair<std::string,int> > -> toString", "[toString][pair]" )
|
||||
{
|
||||
std::vector<std::pair<std::string,int> > pr;
|
||||
pr.push_back( std::make_pair("green", 55 ) );
|
||||
REQUIRE( Catch::toString( pr ) == "{ { \"green\", 55 } }" );
|
||||
}
|
||||
|
||||
// This is pretty contrived - I figure if this works, anything will...
|
||||
TEST_CASE( "pair<pair<int,const char *,pair<std::string,int> > -> toString", "[toString][pair]" )
|
||||
{
|
||||
typedef std::pair<int,const char *> left_t;
|
||||
typedef std::pair<std::string,int> right_t;
|
||||
|
||||
left_t left( 42, "Arthur" );
|
||||
right_t right( "Ford", 24 );
|
||||
|
||||
std::pair<left_t,right_t> pair( left, right );
|
||||
REQUIRE( Catch::toString( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" );
|
||||
}
|
72
projects/SelfTest/ToStringVector.cpp
Normal file
72
projects/SelfTest/ToStringVector.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "catch.hpp"
|
||||
#include <vector>
|
||||
|
||||
|
||||
// vedctor
|
||||
TEST_CASE( "vector<int> -> toString", "[toString][vector]" )
|
||||
{
|
||||
std::vector<int> vv;
|
||||
REQUIRE( Catch::toString(vv) == "{ }" );
|
||||
vv.push_back( 42 );
|
||||
REQUIRE( Catch::toString(vv) == "{ 42 }" );
|
||||
vv.push_back( 512 );
|
||||
REQUIRE( Catch::toString(vv) == "{ 42, 512 }" );
|
||||
}
|
||||
|
||||
TEST_CASE( "vector<string> -> toString", "[toString][vector]" )
|
||||
{
|
||||
std::vector<std::string> vv;
|
||||
REQUIRE( Catch::toString(vv) == "{ }" );
|
||||
vv.push_back( "hello" );
|
||||
REQUIRE( Catch::toString(vv) == "{ \"hello\" }" );
|
||||
vv.push_back( "world" );
|
||||
REQUIRE( Catch::toString(vv) == "{ \"hello\", \"world\" }" );
|
||||
}
|
||||
|
||||
#if defined(CATCH_CPP11_OR_GREATER)
|
||||
/*
|
||||
Note: These tests *can* be made to work with C++ < 11, but the
|
||||
allocator is a lot more work...
|
||||
*/
|
||||
namespace {
|
||||
/* Minimal Allocator */
|
||||
template<typename T>
|
||||
struct minimal_allocator {
|
||||
typedef T value_type;
|
||||
typedef std::size_t size_type;
|
||||
T *allocate( size_type n )
|
||||
{
|
||||
return static_cast<T *>( ::operator new( n * sizeof(T) ) );
|
||||
}
|
||||
void deallocate( T *p, size_type /*n*/ )
|
||||
{
|
||||
::operator delete( static_cast<void *>(p) );
|
||||
}
|
||||
template<typename U>
|
||||
bool operator==( const minimal_allocator<U>& ) const { return true; }
|
||||
template<typename U>
|
||||
bool operator!=( const minimal_allocator<U>& ) const { return false; }
|
||||
};
|
||||
}
|
||||
|
||||
TEST_CASE( "vector<int,allocator> -> toString", "[toString][vector,allocator]" )
|
||||
{
|
||||
std::vector<int,minimal_allocator<int> > vv;
|
||||
REQUIRE( Catch::toString(vv) == "{ }" );
|
||||
vv.push_back( 42 );
|
||||
REQUIRE( Catch::toString(vv) == "{ 42 }" );
|
||||
vv.push_back( 512 );
|
||||
REQUIRE( Catch::toString(vv) == "{ 42, 512 }" );
|
||||
}
|
||||
|
||||
TEST_CASE( "vec<vec<string,alloc>> -> toString", "[toString][vector,allocator]" )
|
||||
{
|
||||
typedef std::vector<std::string,minimal_allocator<std::string> > inner;
|
||||
typedef std::vector<inner> vector;
|
||||
vector v;
|
||||
REQUIRE( Catch::toString(v) == "{ }" );
|
||||
v.push_back( inner { "hello" } );
|
||||
v.push_back( inner { "world" } );
|
||||
REQUIRE( Catch::toString(v) == "{ { \"hello\" }, { \"world\" } }" );
|
||||
}
|
||||
#endif
|
74
projects/SelfTest/ToStringWhich.cpp
Normal file
74
projects/SelfTest/ToStringWhich.cpp
Normal file
@ -0,0 +1,74 @@
|
||||
#include "catch.hpp"
|
||||
/*
|
||||
Demonstrate which version of toString/StringMaker is being used
|
||||
for various types
|
||||
*/
|
||||
|
||||
|
||||
struct has_toString { };
|
||||
struct has_maker {};
|
||||
struct has_maker_and_toString {};
|
||||
|
||||
namespace Catch {
|
||||
std::string toString( const has_toString& ) {
|
||||
return "toString( has_toString )";
|
||||
}
|
||||
std::string toString( const has_maker_and_toString& ) {
|
||||
return "toString( has_maker_and_toString )";
|
||||
}
|
||||
template<>
|
||||
struct StringMaker<has_maker> {
|
||||
static std::string convert( const has_maker& ) {
|
||||
return "StringMaker<has_maker>";
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct StringMaker<has_maker_and_toString> {
|
||||
static std::string convert( const has_maker_and_toString& ) {
|
||||
return "StringMaker<has_maker_and_toString>";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Call the overload
|
||||
TEST_CASE( "toString( has_toString )", "[toString]" )
|
||||
{
|
||||
has_toString item;
|
||||
REQUIRE( Catch::toString( item ) == "toString( has_toString )" );
|
||||
}
|
||||
|
||||
// Call the overload
|
||||
TEST_CASE( "toString( has_maker )", "[toString]" )
|
||||
{
|
||||
has_maker item;
|
||||
REQUIRE( Catch::toString( item ) == "StringMaker<has_maker>" );
|
||||
}
|
||||
|
||||
// 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 )" );
|
||||
}
|
||||
|
||||
// Vectors...
|
||||
TEST_CASE( "toString( vectors<has_toString )", "[toString]" )
|
||||
{
|
||||
std::vector<has_toString> v(1);
|
||||
// This invokes template<T> toString which actually gives us '{ ? }'
|
||||
REQUIRE( Catch::toString( v ) == "{ {?} }" );
|
||||
}
|
||||
|
||||
TEST_CASE( "toString( vectors<has_maker )", "[toString]" )
|
||||
{
|
||||
std::vector<has_maker> v(1);
|
||||
REQUIRE( Catch::toString( v ) == "{ StringMaker<has_maker> }" );
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE( "toString( vectors<has_maker_and_toString )", "[toString]" )
|
||||
{
|
||||
std::vector<has_maker_and_toString> v(1);
|
||||
// Note: This invokes the template<T> toString -> StringMaker
|
||||
REQUIRE( Catch::toString( v ) == "{ StringMaker<has_maker_and_toString> }" );
|
||||
}
|
@ -9,7 +9,10 @@ SOURCES = ApproxTests.cpp \
|
||||
TrickyTests.cpp \
|
||||
BDDTests.cpp \
|
||||
VariadicMacrosTests.cpp \
|
||||
EnumToString.cpp
|
||||
EnumToString.cpp \
|
||||
ToStringPair.cpp \
|
||||
ToStringVector.cpp \
|
||||
ToStringWhich.cpp
|
||||
|
||||
|
||||
OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES))
|
||||
|
Loading…
Reference in New Issue
Block a user