From 000e746415912ecaa6e87a59d4c4a7b7e1660a05 Mon Sep 17 00:00:00 2001 From: Andy Sawyer Date: Mon, 1 Sep 2014 17:19:59 +0100 Subject: [PATCH 1/3] SelfTest: refresh makefile --- projects/SelfTest/makefile | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/projects/SelfTest/makefile b/projects/SelfTest/makefile index e5dec0dc..458b3dea 100644 --- a/projects/SelfTest/makefile +++ b/projects/SelfTest/makefile @@ -1,15 +1,16 @@ -SOURCES = ApproxTests.cpp \ - ClassTests.cpp \ - ConditionTests.cpp \ - ExceptionTests.cpp \ - GeneratorTests.cpp \ - MessageTests.cpp \ - MiscTests.cpp \ - TestMain.cpp \ - TrickyTests.cpp \ - BDDTests.cpp \ - VariadicMacrosTests.cpp \ - catch_self_test.cpp +SOURCES = ApproxTests.cpp \ + ClassTests.cpp \ + ConditionTests.cpp \ + ExceptionTests.cpp \ + GeneratorTests.cpp \ + MessageTests.cpp \ + MiscTests.cpp \ + TestMain.cpp \ + TrickyTests.cpp \ + BDDTests.cpp \ + VariadicMacrosTests.cpp + + OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES)) CXX = g++ CXXFLAGS = -I../../include @@ -17,5 +18,9 @@ CXXFLAGS = -I../../include CatchSelfTest: $(OBJECTS) $(CXX) -o $@ $^ +test: CatchSelfTest + ./CatchSelfTest + clean: - rm -f $(OBJECTS) + rm -f $(OBJECTS) CatchSelfTest + From 142f8f4b1e7f75aadae7df54ffd5f0a384bd70c0 Mon Sep 17 00:00:00 2001 From: Andy Sawyer Date: Mon, 1 Sep 2014 17:24:10 +0100 Subject: [PATCH 2/3] SelfTest: Build as C++11, add EnumToString tests Note: EnumToString tests currently fail - haven't made the changes to catch_tostring yet. --- projects/SelfTest/EnumToString.cpp | 71 ++++++++++++++++++++++++++++++ projects/SelfTest/makefile | 5 ++- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 projects/SelfTest/EnumToString.cpp diff --git a/projects/SelfTest/EnumToString.cpp b/projects/SelfTest/EnumToString.cpp new file mode 100644 index 00000000..53738028 --- /dev/null +++ b/projects/SelfTest/EnumToString.cpp @@ -0,0 +1,71 @@ +#include "catch.hpp" + +/* + TODO: maybe ought to check that user-provided specialisations of + Catch::toString also do the right thing +*/ + +// Enum without user-provided stream operator +enum Enum1 { Enum1Value0, Enum1Value1 }; + +TEST_CASE( "toString(enum)", "[toString][enum]" ) +{ + Enum1 e0 = Enum1Value0; + CHECK( Catch::toString(e0) == "0" ); + Enum1 e1 = Enum1Value1; + CHECK( Catch::toString(e1) == "1" ); +} + +// Enum with user-provided stream operator +enum Enum2 { Enum2Value0, Enum2Value1 }; + +std::ostream& operator<<( std::ostream& os, Enum2 v ) +{ + return os << "E2{" << static_cast(v) << "}"; +} + +TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) +{ + Enum2 e0 = Enum2Value0; + CHECK( Catch::toString(e0) == "E2{0}" ); + Enum2 e1 = Enum2Value1; + CHECK( Catch::toString(e1) == "E2{1}" ); +} + +#if defined(CATCH_CPP11_OR_GREATER) +// Enum class without user-provided stream operator +enum class EnumClass1 { EnumClass1Value0, EnumClass1Value1 }; + +TEST_CASE( "toString(enum class)", "[toString][enum][enumClass]" ) +{ + EnumClass1 e0 = EnumClass1::EnumClass1Value0; + CHECK( Catch::toString(e0) == "0" ); + EnumClass1 e1 = EnumClass1::EnumClass1Value1; + CHECK( Catch::toString(e1) == "1" ); +} + +// Enum class with user-provided stream operator +enum class EnumClass2 : short { EnumClass2Value0, EnumClass2Value1 }; + +std::ostream& operator<<( std::ostream& os, EnumClass2 e2 ) +{ + switch( e2 ) + { + case EnumClass2::EnumClass2Value0: + return os << "E2/V0"; + case EnumClass2::EnumClass2Value1: + return os << "E2/V1"; + } + return os << "unexpected..."; +} + +TEST_CASE( "toString(enum class w/operator<<)", "[toString][enum][enumClass]" ) +{ + EnumClass2 e0 = EnumClass2::EnumClass2Value0; + CHECK( Catch::toString(e0) == "E2/V0" ); + EnumClass2 e1 = EnumClass2::EnumClass2Value1; + CHECK( Catch::toString(e1) == "E2/V1" ); +} + +#endif + diff --git a/projects/SelfTest/makefile b/projects/SelfTest/makefile index 458b3dea..e9f64855 100644 --- a/projects/SelfTest/makefile +++ b/projects/SelfTest/makefile @@ -8,12 +8,13 @@ SOURCES = ApproxTests.cpp \ TestMain.cpp \ TrickyTests.cpp \ BDDTests.cpp \ - VariadicMacrosTests.cpp + VariadicMacrosTests.cpp \ + EnumToString.cpp OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES)) CXX = g++ -CXXFLAGS = -I../../include +CXXFLAGS = -I../../include -std=c++11 CatchSelfTest: $(OBJECTS) $(CXX) -o $@ $^ From cd2a5aa688be79226f646aeb94f7bd27c39f033f Mon Sep 17 00:00:00 2001 From: Andy Sawyer Date: Mon, 1 Sep 2014 17:35:01 +0100 Subject: [PATCH 3/3] catch_tostring: tostring for enum class types --- include/internal/catch_tostring.h | 36 ++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h index e8e38296..dbb31423 100644 --- a/include/internal/catch_tostring.h +++ b/include/internal/catch_tostring.h @@ -22,6 +22,11 @@ #endif namespace Catch { + +// Why we're here. +template +std::string toString( T const& value ); + namespace Detail { // SFINAE is currently disabled by default for all compilers. @@ -64,10 +69,38 @@ namespace Detail { #endif +#if defined(CATCH_CPP11_OR_GREATER) + template::value + > + struct EnumStringMaker + { + static std::string convert( T const& ) { return "{?}"; } + }; + + template + struct EnumStringMaker + { + static std::string convert( T const& v ) + { + return ::Catch::toString( + static_cast::type>(v) + ); + } + }; +#endif template struct StringMakerBase { +#if defined(CATCH_CPP11_OR_GREATER) + template + static std::string convert( T const& v ) + { + return EnumStringMaker::convert( v ); + } +#else template static std::string convert( T const& ) { return "{?}"; } +#endif }; template<> @@ -89,9 +122,6 @@ namespace Detail { } // end namespace Detail -template -std::string toString( T const& value ); - template struct StringMaker : Detail::StringMakerBase::value> {};