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
14 changed files with 47 additions and 219 deletions

View File

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

View File

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

View File

@@ -13,21 +13,36 @@
#include <catch2/internal/catch_string_manip.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <algorithm>
#include <iterator>
namespace Catch {
namespace {
std::string extractClassName( StringRef classOrQualifiedMethodName ) {
std::string className( classOrQualifiedMethodName );
if ( startsWith( className, '&' ) ) {
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 );
StringRef extractClassName( StringRef classOrMethodName ) {
if ( !startsWith( classOrMethodName, '&' ) ) {
return classOrMethodName;
}
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

View File

@@ -145,12 +145,14 @@ namespace Catch {
assert( testCaseNode.children.size() == 1 );
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() ) {
className = fileNameTag(stats.testInfo->tags);
if ( className.empty() )
if ( className.empty() ) {
className = "global";
}
}
if ( !m_config->name().empty() )