Skip to content

Commit

Permalink
SelfTest: make toString test for vector<T> work for gcc 6.2 and MSVC12
Browse files Browse the repository at this point in the history
The StringMaker and toString functions for a type that's used
as the element_type of a std::vector need to be defined before
catch.hpp is included.

Signed-off-by: Stefan Naewe <[email protected]>
  • Loading branch information
snaewe committed Sep 16, 2016
1 parent 35f5105 commit 0205806
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions projects/SelfTest/ToStringWhich.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "catch.hpp"
/*
Demonstrate which version of toString/StringMaker is being used
for various types
*/

#include <string>

struct has_toString { };
struct has_maker {};
struct has_maker_and_toString {};

namespace Catch {
template<typename T>
struct StringMaker;
inline std::string toString( const has_toString& ) {
return "toString( has_toString )";
}
Expand All @@ -30,6 +32,8 @@ namespace Catch {
};
}

#include "catch.hpp"

// Call the overload
TEST_CASE( "toString( has_toString )", "[toString]" ) {
has_toString item;
Expand All @@ -52,7 +56,7 @@ TEST_CASE( "toString( has_maker_and_toString )", "[toString]" ) {
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 ) == "{ {?} }" );
REQUIRE( Catch::toString(v) == "{ toString( has_toString ) }" );
}

TEST_CASE( "toString( vectors<has_maker )", "[toString]" ) {
Expand All @@ -64,5 +68,5 @@ TEST_CASE( "toString( vectors<has_maker )", "[toString]" ) {
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> }" );
REQUIRE( Catch::toString( v ) == "{ toString( has_maker_and_toString ) }" );
}

0 comments on commit 0205806

Please sign in to comment.