Skip to content

Commit

Permalink
Add support to pretty print c-arrays
Browse files Browse the repository at this point in the history
Signed-off-by: mat tso <mat-tso@topmail.ie>
  • Loading branch information
mat tso committed Oct 11, 2016
1 parent 3ec737c commit b5a0503
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/internal/catch_tostring.h
Expand Up @@ -196,10 +196,16 @@ template<class T>
template <class C>
typename C::const_iterator begin( const C& c ) { return c.begin(); }

template <class C, size_t N>
const C* begin( C (&array)[N] ) { return array; }

/// C++11 std::cend
template <class C>
typename C::const_iterator end( const C& c ) { return c.end(); }

template <class C, size_t N>
const C* end( C (&array)[N] ) { return array + N; }

#else
// Importing c++11 std instead of prefixing call by std:: to allow for user
// custom overload find through ADL
Expand Down Expand Up @@ -237,6 +243,12 @@ struct IsContainer {
static const bool value = false; // By default a type is not a container
};

// C array are containers altough not having the standard api.
template <class C, size_t N>
struct IsContainer<C[N], false> {
static const bool value = true;
};

// Pre `decltype` trick to test the type of an expression
template<class T, class U>
struct IsSame : FalseType {};
Expand Down
24 changes: 24 additions & 0 deletions projects/SelfTest/ToStringContainers.cpp
Expand Up @@ -97,6 +97,30 @@ TEST_CASE( "list<string,allocator> -> toString", "[toString][containers][list][a
SequenceTest<std::list, MinimalAllocator>::strings();
}

// C array
TEST_CASE( "int [N] -> toString", "[toString][containers][c-array]" ) {
// Arrays of size 0 can not exist in c++
int oneValue[] = { 42 };
REQUIRE( Catch::toString(oneValue) == "{ 42 }" );
int twoValues[] = { 42, 250 };
REQUIRE( Catch::toString(twoValues) == "{ 42, 250 }" );
}

TEST_CASE( "string [N] -> toString", "[toString][containers][c-array]" ) {
std::string oneValue[] = { "hello" };
REQUIRE( Catch::toString(oneValue) == "{ \"hello\" }" );
std::string twoValues[] = { "hello", "world" };
REQUIRE( Catch::toString(twoValues) == "{ \"hello\", \"world\" }" );
}

TEST_CASE( "char [N] -> toString", "[toString][c-string]" ) {
// Do not consider char[] as containers but rather as c-string
char emptyCString[] = "";
REQUIRE( Catch::toString(emptyCString) == "\"\"" );
char cstring[] = "hello";
REQUIRE( Catch::toString(cstring) == "\"hello\"" );
}

#if defined(CATCH_CPP11_OR_GREATER)

#ifdef __clang__
Expand Down

0 comments on commit b5a0503

Please sign in to comment.