Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/projects/SelfTest/EnumToString.cpp
diff options
context:
space:
mode:
authorPhil Nash2014-09-03 08:40:07 +0100
committerPhil Nash2014-09-03 08:40:07 +0100
commitdd22431fea6d92a9f518eb0227c1cf450fb1b22b (patch)
tree34bccc5ea6774cd0a2209a8cc75090024a8f6887 /projects/SelfTest/EnumToString.cpp
parent3eefa7f53709b5a6478a8bedf52939274ba0b182 (diff)
parent9721296694a332db440c63c45db5d83a41eac47c (diff)
downloadcatch3-dd22431fea6d92a9f518eb0227c1cf450fb1b22b.tar.gz
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
Diffstat (limited to 'projects/SelfTest/EnumToString.cpp')
-rw-r--r--projects/SelfTest/EnumToString.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/projects/SelfTest/EnumToString.cpp b/projects/SelfTest/EnumToString.cpp
new file mode 100644
index 00000000..40c11ca9
--- /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 };
+
+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
+