EnumToString.tests.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_macros.hpp>
  7. #include <catch2/internal/catch_enum_values_registry.hpp>
  8. namespace {
  9. // Enum without user-provided stream operator
  10. enum Enum1 { Enum1Value0, Enum1Value1 };
  11. // Enum with user-provided stream operator
  12. enum Enum2 { Enum2Value0, Enum2Value1 };
  13. std::ostream& operator<<( std::ostream& os, Enum2 v ) {
  14. return os << "E2{" << static_cast<int>(v) << "}";
  15. }
  16. } // end anonymous namespace
  17. TEST_CASE( "toString(enum)", "[toString][enum]" ) {
  18. Enum1 e0 = Enum1Value0;
  19. CHECK( ::Catch::Detail::stringify(e0) == "0" );
  20. Enum1 e1 = Enum1Value1;
  21. CHECK( ::Catch::Detail::stringify(e1) == "1" );
  22. }
  23. TEST_CASE( "toString(enum w/operator<<)", "[toString][enum]" ) {
  24. Enum2 e0 = Enum2Value0;
  25. CHECK( ::Catch::Detail::stringify(e0) == "E2{0}" );
  26. Enum2 e1 = Enum2Value1;
  27. CHECK( ::Catch::Detail::stringify(e1) == "E2{1}" );
  28. }
  29. // Enum class without user-provided stream operator
  30. namespace {
  31. enum class EnumClass1 { EnumClass1Value0, EnumClass1Value1 };
  32. // Enum class with user-provided stream operator
  33. enum class EnumClass2 { EnumClass2Value0, EnumClass2Value1 };
  34. std::ostream& operator<<( std::ostream& os, EnumClass2 e2 ) {
  35. switch( static_cast<int>( e2 ) ) {
  36. case static_cast<int>( EnumClass2::EnumClass2Value0 ):
  37. return os << "E2/V0";
  38. case static_cast<int>( EnumClass2::EnumClass2Value1 ):
  39. return os << "E2/V1";
  40. default:
  41. return os << "Unknown enum value " << static_cast<int>( e2 );
  42. }
  43. }
  44. } // end anonymous namespace
  45. TEST_CASE( "toString(enum class)", "[toString][enum][enumClass]" ) {
  46. EnumClass1 e0 = EnumClass1::EnumClass1Value0;
  47. CHECK( ::Catch::Detail::stringify(e0) == "0" );
  48. EnumClass1 e1 = EnumClass1::EnumClass1Value1;
  49. CHECK( ::Catch::Detail::stringify(e1) == "1" );
  50. }
  51. TEST_CASE( "toString(enum class w/operator<<)", "[toString][enum][enumClass]" ) {
  52. EnumClass2 e0 = EnumClass2::EnumClass2Value0;
  53. CHECK( ::Catch::Detail::stringify(e0) == "E2/V0" );
  54. EnumClass2 e1 = EnumClass2::EnumClass2Value1;
  55. CHECK( ::Catch::Detail::stringify(e1) == "E2/V1" );
  56. auto e3 = static_cast<EnumClass2>(10);
  57. CHECK( ::Catch::Detail::stringify(e3) == "Unknown enum value 10" );
  58. }
  59. enum class EnumClass3 { Value1, Value2, Value3, Value4 };
  60. CATCH_REGISTER_ENUM( EnumClass3, EnumClass3::Value1, EnumClass3::Value2, EnumClass3::Value3 )
  61. TEST_CASE( "Enums can quickly have stringification enabled using REGISTER_ENUM" ) {
  62. using Catch::Detail::stringify;
  63. REQUIRE( stringify( EnumClass3::Value1 ) == "Value1" );
  64. REQUIRE( stringify( EnumClass3::Value2 ) == "Value2" );
  65. REQUIRE( stringify( EnumClass3::Value3 ) == "Value3" );
  66. REQUIRE( stringify( EnumClass3::Value4 ) == "{** unexpected enum value **}" );
  67. EnumClass3 ec3 = EnumClass3::Value2;
  68. REQUIRE( stringify( ec3 ) == "Value2" );
  69. }
  70. namespace Bikeshed {
  71. enum class Colours { Red, Green, Blue };
  72. }
  73. // Important!: This macro must appear at top level scope - not inside a namespace
  74. // You can fully qualify the names, or use a using if you prefer
  75. CATCH_REGISTER_ENUM( Bikeshed::Colours,
  76. Bikeshed::Colours::Red,
  77. Bikeshed::Colours::Green,
  78. Bikeshed::Colours::Blue )
  79. TEST_CASE( "Enums in namespaces can quickly have stringification enabled using REGISTER_ENUM" ) {
  80. using Catch::Detail::stringify;
  81. REQUIRE( stringify( Bikeshed::Colours::Red ) == "Red" );
  82. REQUIRE( stringify( Bikeshed::Colours::Blue ) == "Blue" );
  83. }