Use StringRefs through the enum registration

This commit is contained in:
Martin Hořeňovský 2019-09-08 14:07:18 +02:00
parent fbbaadb704
commit 923db16322
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
7 changed files with 60 additions and 50 deletions

View File

@ -18,13 +18,25 @@ namespace Catch {
namespace Detail {
std::vector<std::string> parseEnums( StringRef enums ) {
namespace {
// Extracts the actual name part of an enum instance
// In other words, it returns the Blue part of Bikeshed::Colour::Blue
StringRef extractInstanceName(StringRef enumInstance) {
// Find last occurence of ":"
size_t name_start = enumInstance.size();
while (name_start > 0 && enumInstance[name_start - 1] != ':') {
--name_start;
}
return enumInstance.substr(name_start, enumInstance.size() - name_start);
}
}
std::vector<StringRef> parseEnums( StringRef enums ) {
auto enumValues = splitStringRef( enums, ',' );
std::vector<std::string> parsed;
std::vector<StringRef> parsed;
parsed.reserve( enumValues.size() );
for( auto const& enumValue : enumValues ) {
auto identifiers = splitStringRef( enumValue, ':' );
parsed.push_back( Catch::trim( identifiers.back() ) );
parsed.push_back(trim(extractInstanceName(enumValue)));
}
return parsed;
}
@ -54,10 +66,8 @@ namespace Catch {
}
EnumInfo const& EnumValuesRegistry::registerEnum( StringRef enumName, StringRef allValueNames, std::vector<int> const& values ) {
auto enumInfo = makeEnumInfo( enumName, allValueNames, values );
EnumInfo* raw = enumInfo.get();
m_enumInfos.push_back( std::move( enumInfo ) );
return *raw;
m_enumInfos.push_back(makeEnumInfo(enumName, allValueNames, values));
return *m_enumInfos.back();
}
} // Detail

View File

@ -26,7 +26,7 @@ namespace Catch {
EnumInfo const& registerEnum( StringRef enumName, StringRef allEnums, std::vector<int> const& values) override;
};
std::vector<std::string> parseEnums( StringRef enums );
std::vector<StringRef> parseEnums( StringRef enums );
} // Detail

View File

@ -17,7 +17,7 @@ namespace Catch {
namespace Detail {
struct EnumInfo {
StringRef m_name;
std::vector<std::pair<int, std::string>> m_values;
std::vector<std::pair<int, StringRef>> m_values;
~EnumInfo();

View File

@ -1505,13 +1505,13 @@ Tricky.tests.cpp:<line number>: passed: ptr.get() == 0 for: 0 == 0
ToStringPair.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( pair ) == "{ { 42, \"Arthur\" }, { \"Ford\", 24 } }" for: "{ { 42, "Arthur" }, { "Ford", 24 } }"
==
"{ { 42, "Arthur" }, { "Ford", 24 } }"
ToString.tests.cpp:<line number>: passed: parseEnums( "" ), Equals( std::vector<std::string>{} ) for: { } Equals: { }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1" ), Equals(std::vector<std::string>{"Value1"} ) for: { "Value1" } Equals: { "Value1" }
ToString.tests.cpp:<line number>: passed: parseEnums( "Value1" ), Equals( std::vector<std::string>{"Value1"} ) for: { "Value1" } Equals: { "Value1" }
ToString.tests.cpp:<line number>: passed: parseEnums( "EnumName::Value1" ), Equals(std::vector<std::string>{"Value1"} ) for: { "Value1" } Equals: { "Value1" }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ), Equals( std::vector<std::string>{"Value1", "Value2"} ) for: { "Value1", "Value2" } Equals: { "Value1", "Value2" }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ), Equals( std::vector<std::string>{"Value1", "Value2", "Value3"} ) for: { "Value1", "Value2", "Value3" } Equals: { "Value1", "Value2", "Value3" }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ), Equals( std::vector<std::string>{"Value1", "Value2", "Value3"} ) for: { "Value1", "Value2", "Value3" } Equals: { "Value1", "Value2", "Value3" }
ToString.tests.cpp:<line number>: passed: parseEnums( "" ), Equals( std::vector<Catch::StringRef>{} ) for: { } Equals: { }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1" ), Equals(std::vector<Catch::StringRef>{"Value1"} ) for: { Value1 } Equals: { Value1 }
ToString.tests.cpp:<line number>: passed: parseEnums( "Value1" ), Equals( std::vector<Catch::StringRef>{"Value1"} ) for: { Value1 } Equals: { Value1 }
ToString.tests.cpp:<line number>: passed: parseEnums( "EnumName::Value1" ), Equals(std::vector<Catch::StringRef>{"Value1"} ) for: { Value1 } Equals: { Value1 }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ), Equals( std::vector<Catch::StringRef>{"Value1", "Value2"} ) for: { Value1, Value2 } Equals: { Value1, Value2 }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ), Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) for: { Value1, Value2, Value3 } Equals: { Value1, Value2, Value3 }
ToString.tests.cpp:<line number>: passed: parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ), Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) for: { Value1, Value2, Value3 } Equals: { Value1, Value2, Value3 }
Tricky.tests.cpp:<line number>: passed: p == 0 for: 0 == 0
Message.tests.cpp:<line number>: passed: true with 1 message: 'this MAY be seen IF info is printed for passing assertions'
Message.tests.cpp:<line number>: failed: false with 2 messages: 'this SHOULD be seen' and 'this SHOULD also be seen'

View File

@ -11304,7 +11304,7 @@ ToString.tests.cpp:<line number>
...............................................................................
ToString.tests.cpp:<line number>: PASSED:
CHECK_THAT( parseEnums( "" ), Equals( std::vector<std::string>{} ) )
CHECK_THAT( parseEnums( "" ), Equals( std::vector<Catch::StringRef>{} ) )
with expansion:
{ } Equals: { }
@ -11316,19 +11316,19 @@ ToString.tests.cpp:<line number>
...............................................................................
ToString.tests.cpp:<line number>: PASSED:
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1" ), Equals(std::vector<std::string>{"Value1"} ) )
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1" ), Equals(std::vector<Catch::StringRef>{"Value1"} ) )
with expansion:
{ "Value1" } Equals: { "Value1" }
{ Value1 } Equals: { Value1 }
ToString.tests.cpp:<line number>: PASSED:
CHECK_THAT( parseEnums( "Value1" ), Equals( std::vector<std::string>{"Value1"} ) )
CHECK_THAT( parseEnums( "Value1" ), Equals( std::vector<Catch::StringRef>{"Value1"} ) )
with expansion:
{ "Value1" } Equals: { "Value1" }
{ Value1 } Equals: { Value1 }
ToString.tests.cpp:<line number>: PASSED:
CHECK_THAT( parseEnums( "EnumName::Value1" ), Equals(std::vector<std::string>{"Value1"} ) )
CHECK_THAT( parseEnums( "EnumName::Value1" ), Equals(std::vector<Catch::StringRef>{"Value1"} ) )
with expansion:
{ "Value1" } Equals: { "Value1" }
{ Value1 } Equals: { Value1 }
-------------------------------------------------------------------------------
parseEnums
@ -11338,19 +11338,19 @@ ToString.tests.cpp:<line number>
...............................................................................
ToString.tests.cpp:<line number>: PASSED:
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ), Equals( std::vector<std::string>{"Value1", "Value2"} ) )
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ), Equals( std::vector<Catch::StringRef>{"Value1", "Value2"} ) )
with expansion:
{ "Value1", "Value2" } Equals: { "Value1", "Value2" }
{ Value1, Value2 } Equals: { Value1, Value2 }
ToString.tests.cpp:<line number>: PASSED:
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ), Equals( std::vector<std::string>{"Value1", "Value2", "Value3"} ) )
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ), Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) )
with expansion:
{ "Value1", "Value2", "Value3" } Equals: { "Value1", "Value2", "Value3" }
{ Value1, Value2, Value3 } Equals: { Value1, Value2, Value3 }
ToString.tests.cpp:<line number>: PASSED:
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ), Equals( std::vector<std::string>{"Value1", "Value2", "Value3"} ) )
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ), Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) )
with expansion:
{ "Value1", "Value2", "Value3" } Equals: { "Value1", "Value2", "Value3" }
{ Value1, Value2, Value3 } Equals: { Value1, Value2, Value3 }
-------------------------------------------------------------------------------
pointer to class

View File

@ -13552,7 +13552,7 @@ loose text artifact
<Section name="No enums" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original>
parseEnums( "" ), Equals( std::vector&lt;std::string>{} )
parseEnums( "" ), Equals( std::vector&lt;Catch::StringRef>{} )
</Original>
<Expanded>
{ } Equals: { }
@ -13563,26 +13563,26 @@ loose text artifact
<Section name="One enum value" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original>
parseEnums( "ClassName::EnumName::Value1" ), Equals(std::vector&lt;std::string>{"Value1"} )
parseEnums( "ClassName::EnumName::Value1" ), Equals(std::vector&lt;Catch::StringRef>{"Value1"} )
</Original>
<Expanded>
{ "Value1" } Equals: { "Value1" }
{ Value1 } Equals: { Value1 }
</Expanded>
</Expression>
<Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original>
parseEnums( "Value1" ), Equals( std::vector&lt;std::string>{"Value1"} )
parseEnums( "Value1" ), Equals( std::vector&lt;Catch::StringRef>{"Value1"} )
</Original>
<Expanded>
{ "Value1" } Equals: { "Value1" }
{ Value1 } Equals: { Value1 }
</Expanded>
</Expression>
<Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original>
parseEnums( "EnumName::Value1" ), Equals(std::vector&lt;std::string>{"Value1"} )
parseEnums( "EnumName::Value1" ), Equals(std::vector&lt;Catch::StringRef>{"Value1"} )
</Original>
<Expanded>
{ "Value1" } Equals: { "Value1" }
{ Value1 } Equals: { Value1 }
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>
@ -13590,26 +13590,26 @@ loose text artifact
<Section name="Multiple enum values" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original>
parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ), Equals( std::vector&lt;std::string>{"Value1", "Value2"} )
parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ), Equals( std::vector&lt;Catch::StringRef>{"Value1", "Value2"} )
</Original>
<Expanded>
{ "Value1", "Value2" } Equals: { "Value1", "Value2" }
{ Value1, Value2 } Equals: { Value1, Value2 }
</Expanded>
</Expression>
<Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original>
parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ), Equals( std::vector&lt;std::string>{"Value1", "Value2", "Value3"} )
parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ), Equals( std::vector&lt;Catch::StringRef>{"Value1", "Value2", "Value3"} )
</Original>
<Expanded>
{ "Value1", "Value2", "Value3" } Equals: { "Value1", "Value2", "Value3" }
{ Value1, Value2, Value3 } Equals: { Value1, Value2, Value3 }
</Expanded>
</Expression>
<Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original>
parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ), Equals( std::vector&lt;std::string>{"Value1", "Value2", "Value3"} )
parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ), Equals( std::vector&lt;Catch::StringRef>{"Value1", "Value2", "Value3"} )
</Original>
<Expanded>
{ "Value1", "Value2", "Value3" } Equals: { "Value1", "Value2", "Value3" }
{ Value1, Value2, Value3 } Equals: { Value1, Value2, Value3 }
</Expanded>
</Expression>
<OverallResults successes="3" failures="0" expectedFailures="0"/>

View File

@ -10,24 +10,24 @@ TEST_CASE( "parseEnums", "[Strings][enums]" ) {
using Catch::Detail::parseEnums;
SECTION( "No enums" )
CHECK_THAT( parseEnums( "" ), Equals( std::vector<std::string>{} ) );
CHECK_THAT( parseEnums( "" ), Equals( std::vector<Catch::StringRef>{} ) );
SECTION( "One enum value" ) {
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1" ),
Equals(std::vector<std::string>{"Value1"} ) );
Equals(std::vector<Catch::StringRef>{"Value1"} ) );
CHECK_THAT( parseEnums( "Value1" ),
Equals( std::vector<std::string>{"Value1"} ) );
Equals( std::vector<Catch::StringRef>{"Value1"} ) );
CHECK_THAT( parseEnums( "EnumName::Value1" ),
Equals(std::vector<std::string>{"Value1"} ) );
Equals(std::vector<Catch::StringRef>{"Value1"} ) );
}
SECTION( "Multiple enum values" ) {
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2" ),
Equals( std::vector<std::string>{"Value1", "Value2"} ) );
Equals( std::vector<Catch::StringRef>{"Value1", "Value2"} ) );
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1, ClassName::EnumName::Value2, ClassName::EnumName::Value3" ),
Equals( std::vector<std::string>{"Value1", "Value2", "Value3"} ) );
Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
CHECK_THAT( parseEnums( "ClassName::EnumName::Value1,ClassName::EnumName::Value2 , ClassName::EnumName::Value3" ),
Equals( std::vector<std::string>{"Value1", "Value2", "Value3"} ) );
Equals( std::vector<Catch::StringRef>{"Value1", "Value2", "Value3"} ) );
}
}