diff --git a/include/internal/catch_enum_values_registry.cpp b/include/internal/catch_enum_values_registry.cpp index b94a70e5..9204e047 100644 --- a/include/internal/catch_enum_values_registry.cpp +++ b/include/internal/catch_enum_values_registry.cpp @@ -14,7 +14,6 @@ namespace Catch { IMutableEnumValuesRegistry::~IMutableEnumValuesRegistry() {} - IEnumInfo::~IEnumInfo() {} namespace Detail { @@ -29,25 +28,17 @@ namespace Catch { return parsed; } - struct EnumInfo : IEnumInfo { - std::string m_name; - std::map m_values; - - ~EnumInfo(); - - std::string lookup( int value ) const override { - auto it = m_values.find( value ); - if( it == m_values.end() ) { - ReusableStringStream rss; - rss << "{** unexpected value for " << m_name << ": " << value << "**}"; - return rss.str(); - } - return it->second; - } - }; EnumInfo::~EnumInfo() {} - IEnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector const& values ) { + StringRef EnumInfo::lookup( int value ) const { + for( auto const& valueToName : m_values ) { + if( valueToName.first == value ) + return valueToName.second; + } + return "{** unexpected enum value **}"; + } + + EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector const& values ) { std::unique_ptr enumInfo( new EnumInfo ); enumInfo->m_name = enumName; @@ -55,7 +46,7 @@ namespace Catch { assert( valueNames.size() == values.size() ); std::size_t i = 0; for( auto value : values ) - enumInfo->m_values.insert({ value, valueNames[i++] }); + enumInfo->m_values.push_back({ value, valueNames[i++] }); EnumInfo* raw = enumInfo.get(); m_enumInfos.push_back( std::move( enumInfo ) ); @@ -63,6 +54,5 @@ namespace Catch { } } // Detail - } // Catch diff --git a/include/internal/catch_enum_values_registry.h b/include/internal/catch_enum_values_registry.h index 8103bb9d..b6943ec2 100644 --- a/include/internal/catch_enum_values_registry.h +++ b/include/internal/catch_enum_values_registry.h @@ -18,9 +18,9 @@ namespace Catch { class EnumValuesRegistry : public IMutableEnumValuesRegistry { - std::vector> m_enumInfos; + std::vector> m_enumInfos; - IEnumInfo const& registerEnum(StringRef enumName, StringRef allEnums, std::vector const& values) override; + EnumInfo const& registerEnum(StringRef enumName, StringRef allEnums, std::vector const& values) override; }; std::vector parseEnums( StringRef enums ); diff --git a/include/internal/catch_interfaces_enum_values_registry.h b/include/internal/catch_interfaces_enum_values_registry.h index b94ee69b..fea6847d 100644 --- a/include/internal/catch_interfaces_enum_values_registry.h +++ b/include/internal/catch_interfaces_enum_values_registry.h @@ -14,19 +14,24 @@ namespace Catch { - struct IEnumInfo { - virtual ~IEnumInfo(); + namespace Detail { + struct EnumInfo { + StringRef m_name; + std::vector> m_values; - virtual std::string lookup( int value ) const = 0; - }; + ~EnumInfo(); + + StringRef lookup( int value ) const; + }; + } // namespace Detail struct IMutableEnumValuesRegistry { virtual ~IMutableEnumValuesRegistry(); - virtual IEnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector const& values ) = 0; + virtual Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector const& values ) = 0; template - IEnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::initializer_list values ) { + Detail::EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::initializer_list values ) { std::vector intValues; intValues.reserve( values.size() ); for( auto enumValue : values ) diff --git a/projects/SelfTest/UsageTests/EnumToString.tests.cpp b/projects/SelfTest/UsageTests/EnumToString.tests.cpp index bdd36315..f3fd2f6d 100644 --- a/projects/SelfTest/UsageTests/EnumToString.tests.cpp +++ b/projects/SelfTest/UsageTests/EnumToString.tests.cpp @@ -75,7 +75,7 @@ TEST_CASE( "STRINGIFY_ENUM" ) { REQUIRE( stringify( EnumClass3::Value1 ) == "Value1" ); REQUIRE( stringify( EnumClass3::Value2 ) == "Value2" ); REQUIRE( stringify( EnumClass3::Value3 ) == "Value3" ); - REQUIRE( stringify( EnumClass3::Value4 ) == "{** unexpected value for EnumClass3: 3**}" ); + REQUIRE( stringify( EnumClass3::Value4 ) == "{** unexpected enum value **}" ); EnumClass3 ec3 = EnumClass3 ::Value2; REQUIRE( stringify( ec3 ) == "Value2" ); @@ -91,7 +91,7 @@ TEST_CASE( "EnumInfo" ) { CHECK( enumInfo.lookup(0) == "Value1" ); CHECK( enumInfo.lookup(1) == "Value2" ); - CHECK( enumInfo.lookup(3) == "{** unexpected value for EnumName: 3**}" ); + CHECK( enumInfo.lookup(3) == "{** unexpected enum value **}" ); } #include "internal/catch_enum_values_registry.h"