Merge branch 'develop' of github.com:philsquared/Catch into develop

# By Andy Sawyer
# Via Andy Sawyer (1) and Phil Nash (1)
* 'develop' of github.com:philsquared/Catch:
  catch_tostring: tostring for enum class types
  SelfTest: Build as C++11, add EnumToString tests
  SelfTest: refresh makefile
This commit is contained in:
Phil Nash 2014-09-03 08:40:07 +01:00
commit dd22431fea
4 changed files with 128 additions and 17 deletions

View File

@ -22,6 +22,11 @@
#endif #endif
namespace Catch { namespace Catch {
// Why we're here.
template<typename T>
std::string toString( T const& value );
namespace Detail { namespace Detail {
// SFINAE is currently disabled by default for all compilers. // SFINAE is currently disabled by default for all compilers.
@ -64,10 +69,38 @@ namespace Detail {
#endif #endif
#if defined(CATCH_CPP11_OR_GREATER)
template<typename T,
bool IsEmum = std::is_enum<T>::value
>
struct EnumStringMaker
{
static std::string convert( T const& ) { return "{?}"; }
};
template<typename T>
struct EnumStringMaker<T,true>
{
static std::string convert( T const& v )
{
return ::Catch::toString(
static_cast<typename std::underlying_type<T>::type>(v)
);
}
};
#endif
template<bool C> template<bool C>
struct StringMakerBase { struct StringMakerBase {
#if defined(CATCH_CPP11_OR_GREATER)
template<typename T>
static std::string convert( T const& v )
{
return EnumStringMaker<T>::convert( v );
}
#else
template<typename T> template<typename T>
static std::string convert( T const& ) { return "{?}"; } static std::string convert( T const& ) { return "{?}"; }
#endif
}; };
template<> template<>
@ -89,9 +122,6 @@ namespace Detail {
} // end namespace Detail } // end namespace Detail
template<typename T>
std::string toString( T const& value );
template<typename T> template<typename T>
struct StringMaker : struct StringMaker :
Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {}; Detail::StringMakerBase<Detail::IsStreamInsertable<T>::value> {};

View File

@ -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 };
inline std::ostream& operator<<( std::ostream& os, Enum2 v )
{
return os << "E2{" << static_cast<int>(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 };
inline 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

View File

@ -9,13 +9,19 @@ SOURCES = ApproxTests.cpp \
TrickyTests.cpp \ TrickyTests.cpp \
BDDTests.cpp \ BDDTests.cpp \
VariadicMacrosTests.cpp \ VariadicMacrosTests.cpp \
catch_self_test.cpp EnumToString.cpp
OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES)) OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES))
CXX = g++ CXX = g++
CXXFLAGS = -I../../include CXXFLAGS = -I../../include -std=c++11
CatchSelfTest: $(OBJECTS) CatchSelfTest: $(OBJECTS)
$(CXX) -o $@ $^ $(CXX) -o $@ $^
test: CatchSelfTest
./CatchSelfTest
clean: clean:
rm -f $(OBJECTS) rm -f $(OBJECTS) CatchSelfTest

View File

@ -7,6 +7,7 @@
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
263F7A4719B6FCBF009474C2 /* EnumToString.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 263F7A4619B6FCBF009474C2 /* EnumToString.cpp */; };
2656C2211925E7330040DB02 /* catch_test_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2656C2201925E7330040DB02 /* catch_test_spec.cpp */; }; 2656C2211925E7330040DB02 /* catch_test_spec.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2656C2201925E7330040DB02 /* catch_test_spec.cpp */; };
266B06B816F3A60A004ED264 /* VariadicMacrosTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266B06B616F3A60A004ED264 /* VariadicMacrosTests.cpp */; }; 266B06B816F3A60A004ED264 /* VariadicMacrosTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266B06B616F3A60A004ED264 /* VariadicMacrosTests.cpp */; };
266ECD74170F3C620030D735 /* BDDTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266ECD73170F3C620030D735 /* BDDTests.cpp */; }; 266ECD74170F3C620030D735 /* BDDTests.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 266ECD73170F3C620030D735 /* BDDTests.cpp */; };
@ -67,6 +68,7 @@
262E7399184673A800CAC268 /* catch_reporter_bases.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_bases.hpp; sourceTree = "<group>"; }; 262E7399184673A800CAC268 /* catch_reporter_bases.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_reporter_bases.hpp; sourceTree = "<group>"; };
262E739A1846759000CAC268 /* catch_common.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_common.hpp; sourceTree = "<group>"; }; 262E739A1846759000CAC268 /* catch_common.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_common.hpp; sourceTree = "<group>"; };
263F7A4519A66608009474C2 /* catch_fatal_condition.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_fatal_condition.hpp; sourceTree = "<group>"; }; 263F7A4519A66608009474C2 /* catch_fatal_condition.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = catch_fatal_condition.hpp; sourceTree = "<group>"; };
263F7A4619B6FCBF009474C2 /* EnumToString.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = EnumToString.cpp; path = ../../../SelfTest/EnumToString.cpp; sourceTree = "<group>"; };
263FD06017AF8DF200988A20 /* catch_timer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_timer.hpp; sourceTree = "<group>"; }; 263FD06017AF8DF200988A20 /* catch_timer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_timer.hpp; sourceTree = "<group>"; };
263FD06117AF8DF200988A20 /* catch_timer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_timer.h; sourceTree = "<group>"; }; 263FD06117AF8DF200988A20 /* catch_timer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = catch_timer.h; sourceTree = "<group>"; };
2656C21F1925E5100040DB02 /* catch_test_spec_parser.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_test_spec_parser.hpp; sourceTree = "<group>"; }; 2656C21F1925E5100040DB02 /* catch_test_spec_parser.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = catch_test_spec_parser.hpp; sourceTree = "<group>"; };
@ -247,6 +249,7 @@
4A6D0C40149B3DAB00DB3EAA /* Tests */ = { 4A6D0C40149B3DAB00DB3EAA /* Tests */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
263F7A4619B6FCBF009474C2 /* EnumToString.cpp */,
266ECD73170F3C620030D735 /* BDDTests.cpp */, 266ECD73170F3C620030D735 /* BDDTests.cpp */,
4A6D0C36149B3D9E00DB3EAA /* TrickyTests.cpp */, 4A6D0C36149B3D9E00DB3EAA /* TrickyTests.cpp */,
4A6D0C2D149B3D9E00DB3EAA /* ApproxTests.cpp */, 4A6D0C2D149B3D9E00DB3EAA /* ApproxTests.cpp */,
@ -518,6 +521,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
263F7A4719B6FCBF009474C2 /* EnumToString.cpp in Sources */,
4A6D0C37149B3D9E00DB3EAA /* ApproxTests.cpp in Sources */, 4A6D0C37149B3D9E00DB3EAA /* ApproxTests.cpp in Sources */,
4A6D0C38149B3D9E00DB3EAA /* ClassTests.cpp in Sources */, 4A6D0C38149B3D9E00DB3EAA /* ClassTests.cpp in Sources */,
4A6D0C39149B3D9E00DB3EAA /* ConditionTests.cpp in Sources */, 4A6D0C39149B3D9E00DB3EAA /* ConditionTests.cpp in Sources */,