ToString.tests.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "catch.hpp"
  2. #include "internal/catch_enum_values_registry.h"
  3. enum class EnumClass3 { Value1, Value2, Value3, Value4 };
  4. struct UsesSentinel {
  5. using const_iterator = int const*;
  6. using const_sentinel = std::nullptr_t;
  7. const_iterator begin() const { return nullptr; }
  8. const_iterator end() const { return nullptr; }
  9. };
  10. TEST_CASE( "parseEnums", "[Strings][enums]" ) {
  11. using namespace Catch::Matchers;
  12. using Catch::Detail::parseEnums;
  13. SECTION( "No enums" )
  14. CHECK_THAT( parseEnums( "" ), Equals( std::vector<Catch::StringRef>{} ) );
  15. SECTION( "One enum value" ) {
  16. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1" ),
  17. Equals(std::vector<Catch::StringRef>{"Value1"} ) );
  18. CHECK_THAT( parseEnums( "Value1" ),
  19. Equals( std::vector<Catch::StringRef>{"Value1"} ) );
  20. CHECK_THAT( parseEnums( "EnumName::Value1" ),
  21. Equals(std::vector<Catch::StringRef>{"Value1"} ) );
  22. }
  23. SECTION( "Multiple enum values" ) {
  24. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ),
  25. Equals( std::vector<Catch::StringRef>{"Value1", "Value2"} ) );
  26. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ),
  27. Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
  28. CHECK_THAT( parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ),
  29. Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
  30. }
  31. }
  32. TEST_CASE( "Directly creating an EnumInfo" ) {
  33. using namespace Catch::Detail;
  34. std::unique_ptr<EnumInfo> enumInfo = makeEnumInfo( "EnumName", "EnumName::Value1, EnumName::Value2", {0, 1} );
  35. CHECK( enumInfo->lookup(0) == "Value1" );
  36. CHECK( enumInfo->lookup(1) == "Value2" );
  37. CHECK( enumInfo->lookup(3) == "{** unexpected enum value **}" );
  38. }
  39. TEST_CASE("Range type with sentinel") {
  40. CHECK( Catch::Detail::stringify(UsesSentinel{}) == "{ }" );
  41. }