Use StringRef for className in TestCaseInfo

This commit is contained in:
Martin Hořeňovský 2021-09-27 16:10:07 +02:00
parent 21b99d6f58
commit f02c2678a1
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
14 changed files with 47 additions and 219 deletions

View File

@ -108,13 +108,13 @@ namespace Catch {
} }
Detail::unique_ptr<TestCaseInfo> Detail::unique_ptr<TestCaseInfo>
makeTestCaseInfo(std::string const& _className, makeTestCaseInfo(StringRef _className,
NameAndTags const& nameAndTags, NameAndTags const& nameAndTags,
SourceLineInfo const& _lineInfo ) { SourceLineInfo const& _lineInfo ) {
return Detail::make_unique<TestCaseInfo>(_className, nameAndTags, _lineInfo); return Detail::make_unique<TestCaseInfo>(_className, nameAndTags, _lineInfo);
} }
TestCaseInfo::TestCaseInfo(std::string const& _className, TestCaseInfo::TestCaseInfo(StringRef _className,
NameAndTags const& _nameAndTags, NameAndTags const& _nameAndTags,
SourceLineInfo const& _lineInfo): SourceLineInfo const& _lineInfo):
name( _nameAndTags.name.empty() ? makeDefaultName() : _nameAndTags.name ), name( _nameAndTags.name.empty() ? makeDefaultName() : _nameAndTags.name ),

View File

@ -57,7 +57,7 @@ namespace Catch {
*/ */
struct TestCaseInfo : Detail::NonCopyable { struct TestCaseInfo : Detail::NonCopyable {
TestCaseInfo(std::string const& _className, TestCaseInfo(StringRef _className,
NameAndTags const& _tags, NameAndTags const& _tags,
SourceLineInfo const& _lineInfo); SourceLineInfo const& _lineInfo);
@ -77,7 +77,7 @@ namespace Catch {
std::string tagsAsString() const; std::string tagsAsString() const;
std::string name; std::string name;
std::string className; StringRef className;
private: private:
std::string backingTags, backingLCaseTags; std::string backingTags, backingLCaseTags;
// Internally we copy tags to the backing storage and then add // Internally we copy tags to the backing storage and then add
@ -109,7 +109,8 @@ namespace Catch {
TestCaseInfo const& getTestCaseInfo() const; TestCaseInfo const& getTestCaseInfo() const;
}; };
Detail::unique_ptr<TestCaseInfo> makeTestCaseInfo( std::string const& className, Detail::unique_ptr<TestCaseInfo>
makeTestCaseInfo( StringRef className,
NameAndTags const& nameAndTags, NameAndTags const& nameAndTags,
SourceLineInfo const& lineInfo ); SourceLineInfo const& lineInfo );
} }

View File

@ -13,21 +13,36 @@
#include <catch2/internal/catch_string_manip.hpp> #include <catch2/internal/catch_string_manip.hpp>
#include <catch2/internal/catch_move_and_forward.hpp> #include <catch2/internal/catch_move_and_forward.hpp>
#include <algorithm>
#include <iterator>
namespace Catch { namespace Catch {
namespace { namespace {
std::string extractClassName( StringRef classOrQualifiedMethodName ) { StringRef extractClassName( StringRef classOrMethodName ) {
std::string className( classOrQualifiedMethodName ); if ( !startsWith( classOrMethodName, '&' ) ) {
if ( startsWith( className, '&' ) ) { return classOrMethodName;
std::size_t lastColons = className.rfind( "::" );
std::size_t penultimateColons =
className.rfind( "::", lastColons - 1 );
if ( penultimateColons == std::string::npos )
penultimateColons = 1;
className = className.substr( penultimateColons,
lastColons - penultimateColons );
} }
return className;
// Remove the leading '&' to avoid having to special case it later
const auto methodName =
classOrMethodName.substr( 1, classOrMethodName.size() );
auto reverseStart = std::make_reverse_iterator( methodName.end() );
auto reverseEnd = std::make_reverse_iterator( methodName.begin() );
// We make a simplifying assumption that ":" is only present
// in the input as part of "::" from C++ typenames (this is
// relatively safe assumption because the input is generated
// as stringification of type through preprocessor).
auto lastColons = std::find( reverseStart, reverseEnd, ':' ) + 1;
auto secondLastColons =
std::find( lastColons + 1, reverseEnd, ':' );
auto const startIdx = reverseEnd - secondLastColons;
auto const classNameSize = secondLastColons - lastColons - 1;
return methodName.substr( startIdx, classNameSize );
} }
} // namespace } // namespace

View File

@ -145,13 +145,15 @@ namespace Catch {
assert( testCaseNode.children.size() == 1 ); assert( testCaseNode.children.size() == 1 );
SectionNode const& rootSection = *testCaseNode.children.front(); SectionNode const& rootSection = *testCaseNode.children.front();
std::string className = stats.testInfo->className; std::string className =
static_cast<std::string>( stats.testInfo->className );
if( className.empty() ) { if( className.empty() ) {
className = fileNameTag(stats.testInfo->tags); className = fileNameTag(stats.testInfo->tags);
if ( className.empty() ) if ( className.empty() ) {
className = "global"; className = "global";
} }
}
if ( !m_config->name().empty() ) if ( !m_config->name().empty() )
className = m_config->name() + "." + className; className = m_config->name() + "." + className;

View File

@ -207,7 +207,6 @@ Message from section two
:test-result: PASS String matchers :test-result: PASS String matchers
:test-result: PASS StringRef :test-result: PASS StringRef
:test-result: PASS StringRef at compilation time :test-result: PASS StringRef at compilation time
:test-result: PASS StringRef::compare
:test-result: PASS Stringifying char arrays with statically known sizes - char :test-result: PASS Stringifying char arrays with statically known sizes - char
:test-result: PASS Stringifying char arrays with statically known sizes - signed char :test-result: PASS Stringifying char arrays with statically known sizes - signed char
:test-result: PASS Stringifying char arrays with statically known sizes - unsigned char :test-result: PASS Stringifying char arrays with statically known sizes - unsigned char

View File

@ -1537,16 +1537,6 @@ String.tests.cpp:<line number>: passed: with 1 message: '!(sr1.empty())'
String.tests.cpp:<line number>: passed: with 1 message: 'sr1.size() == 3' String.tests.cpp:<line number>: passed: with 1 message: 'sr1.size() == 3'
String.tests.cpp:<line number>: passed: with 1 message: 'sr2.empty()' String.tests.cpp:<line number>: passed: with 1 message: 'sr2.empty()'
String.tests.cpp:<line number>: passed: with 1 message: 'sr2.size() == 0' String.tests.cpp:<line number>: passed: with 1 message: 'sr2.size() == 0'
String.tests.cpp:<line number>: passed: sr1.compare(sr2) < 0 for: -1 < 0
String.tests.cpp:<line number>: passed: sr2.compare(sr1) > 0 for: 1 > 0
String.tests.cpp:<line number>: passed: sr1.compare(sr3) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: sr3.compare(sr1) == 0 for: 0 == 0
String.tests.cpp:<line number>: passed: sr1.compare(sr2) < 0 for: -1 < 0
String.tests.cpp:<line number>: passed: sr2.compare(sr1) > 0 for: 1 > 0
String.tests.cpp:<line number>: passed: sr1.compare(sr3) > 0 for: 3 > 0
String.tests.cpp:<line number>: passed: sr2.compare(sr3) > 0 for: 3 > 0
String.tests.cpp:<line number>: passed: sr3.compare(sr1) < 0 for: -3 < 0
String.tests.cpp:<line number>: passed: sr3.compare(sr2) < 0 for: -3 < 0
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( no_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ToString.tests.cpp:<line number>: passed: ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""

View File

@ -1386,6 +1386,6 @@ due to unexpected exception with message:
Why would you throw a std::string? Why would you throw a std::string?
=============================================================================== ===============================================================================
test cases: 370 | 294 passed | 70 failed | 6 failed as expected test cases: 369 | 293 passed | 70 failed | 6 failed as expected
assertions: 2116 | 1964 passed | 129 failed | 23 failed as expected assertions: 2106 | 1954 passed | 129 failed | 23 failed as expected

View File

@ -10906,70 +10906,6 @@ String.tests.cpp:<line number>: PASSED:
with message: with message:
sr2.size() == 0 sr2.size() == 0
-------------------------------------------------------------------------------
StringRef::compare
Same length on both sides
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr1.compare(sr2) < 0 )
with expansion:
-1 < 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr2.compare(sr1) > 0 )
with expansion:
1 > 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr1.compare(sr3) == 0 )
with expansion:
0 == 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr3.compare(sr1) == 0 )
with expansion:
0 == 0
-------------------------------------------------------------------------------
StringRef::compare
Different lengths
-------------------------------------------------------------------------------
String.tests.cpp:<line number>
...............................................................................
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr1.compare(sr2) < 0 )
with expansion:
-1 < 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr2.compare(sr1) > 0 )
with expansion:
1 > 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr1.compare(sr3) > 0 )
with expansion:
3 > 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr2.compare(sr3) > 0 )
with expansion:
3 > 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr3.compare(sr1) < 0 )
with expansion:
-3 < 0
String.tests.cpp:<line number>: PASSED:
REQUIRE( sr3.compare(sr2) < 0 )
with expansion:
-3 < 0
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Stringifying char arrays with statically known sizes - char Stringifying char arrays with statically known sizes - char
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -17018,6 +16954,6 @@ Misc.tests.cpp:<line number>
Misc.tests.cpp:<line number>: PASSED: Misc.tests.cpp:<line number>: PASSED:
=============================================================================== ===============================================================================
test cases: 370 | 278 passed | 86 failed | 6 failed as expected test cases: 369 | 277 passed | 86 failed | 6 failed as expected
assertions: 2133 | 1964 passed | 146 failed | 23 failed as expected assertions: 2123 | 1954 passed | 146 failed | 23 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="17" failures="129" tests="2133" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="17" failures="129" tests="2123" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties> <properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/>
<property name="random-seed" value="1"/> <property name="random-seed" value="1"/>
@ -1196,8 +1196,6 @@ Matchers.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="StringRef/StringRef + StringRef" time="{duration}" status="run"/> <testcase classname="<exe-name>.global" name="StringRef/StringRef + StringRef" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="StringRef at compilation time/Simple constructors" time="{duration}" status="run"/> <testcase classname="<exe-name>.global" name="StringRef at compilation time/Simple constructors" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="StringRef at compilation time/UDL construction" time="{duration}" status="run"/> <testcase classname="<exe-name>.global" name="StringRef at compilation time/UDL construction" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="StringRef::compare/Same length on both sides" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="StringRef::compare/Different lengths" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - char" time="{duration}" status="run"/> <testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - char" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - signed char" time="{duration}" status="run"/> <testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - signed char" time="{duration}" status="run"/>
<testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - unsigned char" time="{duration}" status="run"/> <testcase classname="<exe-name>.global" name="Stringifying char arrays with statically known sizes - unsigned char" time="{duration}" status="run"/>

View File

@ -204,8 +204,6 @@
<testCase name="StringRef/StringRef + StringRef" duration="{duration}"/> <testCase name="StringRef/StringRef + StringRef" duration="{duration}"/>
<testCase name="StringRef at compilation time/Simple constructors" duration="{duration}"/> <testCase name="StringRef at compilation time/Simple constructors" duration="{duration}"/>
<testCase name="StringRef at compilation time/UDL construction" duration="{duration}"/> <testCase name="StringRef at compilation time/UDL construction" duration="{duration}"/>
<testCase name="StringRef::compare/Same length on both sides" duration="{duration}"/>
<testCase name="StringRef::compare/Different lengths" duration="{duration}"/>
</file> </file>
<file path="tests/<exe-name>/IntrospectiveTests/StringManip.tests.cpp"> <file path="tests/<exe-name>/IntrospectiveTests/StringManip.tests.cpp">
<testCase name="Trim strings" duration="{duration}"/> <testCase name="Trim strings" duration="{duration}"/>

View File

@ -2753,26 +2753,6 @@ ok {test-number} - with 1 message: 'sr1.size() == 3'
ok {test-number} - with 1 message: 'sr2.empty()' ok {test-number} - with 1 message: 'sr2.empty()'
# StringRef at compilation time # StringRef at compilation time
ok {test-number} - with 1 message: 'sr2.size() == 0' ok {test-number} - with 1 message: 'sr2.size() == 0'
# StringRef::compare
ok {test-number} - sr1.compare(sr2) < 0 for: -1 < 0
# StringRef::compare
ok {test-number} - sr2.compare(sr1) > 0 for: 1 > 0
# StringRef::compare
ok {test-number} - sr1.compare(sr3) == 0 for: 0 == 0
# StringRef::compare
ok {test-number} - sr3.compare(sr1) == 0 for: 0 == 0
# StringRef::compare
ok {test-number} - sr1.compare(sr2) < 0 for: -1 < 0
# StringRef::compare
ok {test-number} - sr2.compare(sr1) > 0 for: 1 > 0
# StringRef::compare
ok {test-number} - sr1.compare(sr3) > 0 for: 3 > 0
# StringRef::compare
ok {test-number} - sr2.compare(sr3) > 0 for: 3 > 0
# StringRef::compare
ok {test-number} - sr3.compare(sr1) < 0 for: -3 < 0
# StringRef::compare
ok {test-number} - sr3.compare(sr2) < 0 for: -3 < 0
# Stringifying char arrays with statically known sizes - char # Stringifying char arrays with statically known sizes - char
ok {test-number} - ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc"" ok {test-number} - ::Catch::Detail::stringify( with_null_terminator ) == R"("abc")"s for: ""abc"" == ""abc""
# Stringifying char arrays with statically known sizes - char # Stringifying char arrays with statically known sizes - char
@ -4268,5 +4248,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0
ok {test-number} - ok {test-number} -
# xmlentitycheck # xmlentitycheck
ok {test-number} - ok {test-number} -
1..2133 1..2123

View File

@ -514,8 +514,6 @@ Matchers.tests.cpp:<line number>|nexpression failed|n CHECK_THAT( testStringFor
##teamcity[testFinished name='StringRef' duration="{duration}"] ##teamcity[testFinished name='StringRef' duration="{duration}"]
##teamcity[testStarted name='StringRef at compilation time'] ##teamcity[testStarted name='StringRef at compilation time']
##teamcity[testFinished name='StringRef at compilation time' duration="{duration}"] ##teamcity[testFinished name='StringRef at compilation time' duration="{duration}"]
##teamcity[testStarted name='StringRef::compare']
##teamcity[testFinished name='StringRef::compare' duration="{duration}"]
##teamcity[testStarted name='Stringifying char arrays with statically known sizes - char'] ##teamcity[testStarted name='Stringifying char arrays with statically known sizes - char']
##teamcity[testFinished name='Stringifying char arrays with statically known sizes - char' duration="{duration}"] ##teamcity[testFinished name='Stringifying char arrays with statically known sizes - char' duration="{duration}"]
##teamcity[testStarted name='Stringifying char arrays with statically known sizes - signed char'] ##teamcity[testStarted name='Stringifying char arrays with statically known sizes - signed char']

View File

@ -12945,95 +12945,6 @@ Message from section two
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="StringRef::compare" tags="[StringRef][Strings]" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Section name="Same length on both sides" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr1.compare(sr2) &lt; 0
</Original>
<Expanded>
-1 &lt; 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr2.compare(sr1) > 0
</Original>
<Expanded>
1 > 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr1.compare(sr3) == 0
</Original>
<Expanded>
0 == 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr3.compare(sr1) == 0
</Original>
<Expanded>
0 == 0
</Expanded>
</Expression>
<OverallResults successes="4" failures="0" expectedFailures="0"/>
</Section>
<Section name="Different lengths" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr1.compare(sr2) &lt; 0
</Original>
<Expanded>
-1 &lt; 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr2.compare(sr1) > 0
</Original>
<Expanded>
1 > 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr1.compare(sr3) > 0
</Original>
<Expanded>
3 > 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr2.compare(sr3) > 0
</Original>
<Expanded>
3 > 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr3.compare(sr1) &lt; 0
</Original>
<Expanded>
-3 &lt; 0
</Expanded>
</Expression>
<Expression success="true" type="REQUIRE" filename="tests/<exe-name>/IntrospectiveTests/String.tests.cpp" >
<Original>
sr3.compare(sr2) &lt; 0
</Original>
<Expanded>
-3 &lt; 0
</Expanded>
</Expression>
<OverallResults successes="6" failures="0" expectedFailures="0"/>
</Section>
<OverallResult success="true"/>
</TestCase>
<TestCase name="Stringifying char arrays with statically known sizes - char" tags="[toString]" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" > <TestCase name="Stringifying char arrays with statically known sizes - char" tags="[toString]" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" > <Expression success="true" type="CHECK" filename="tests/<exe-name>/IntrospectiveTests/ToString.tests.cpp" >
<Original> <Original>
@ -20042,6 +19953,6 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="1964" failures="146" expectedFailures="23"/> <OverallResults successes="1954" failures="146" expectedFailures="23"/>
<OverallResultsCases successes="278" failures="86" expectedFailures="6"/> <OverallResultsCases successes="277" failures="86" expectedFailures="6"/>
</Catch2TestRun> </Catch2TestRun>

View File

@ -166,7 +166,7 @@ TEST_CASE("StringRef at compilation time", "[Strings][StringRef][constexpr]") {
} }
} }
TEST_CASE("StringRef::compare", "[Strings][StringRef]") { TEST_CASE("StringRef::compare", "[Strings][StringRef][approvals]") {
using Catch::StringRef; using Catch::StringRef;
SECTION("Same length on both sides") { SECTION("Same length on both sides") {