Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2021-09-27 16:10:07 +0200
committerMartin Hořeňovský2021-09-27 19:07:45 +0200
commitf02c2678a1a891f9577712fe160c1f6f3baef3a8 (patch)
treef83a473695dedf054dd241f65997274823b3966b /src
parent21b99d6f589090983136bf764514261dc863ed9a (diff)
downloadcatch3-f02c2678a1a891f9577712fe160c1f6f3baef3a8.tar.gz
Use StringRef for className in TestCaseInfo
Diffstat (limited to 'src')
-rw-r--r--src/catch2/catch_test_case_info.cpp4
-rw-r--r--src/catch2/catch_test_case_info.hpp11
-rw-r--r--src/catch2/internal/catch_test_registry.cpp37
-rw-r--r--src/catch2/reporters/catch_reporter_junit.cpp6
4 files changed, 38 insertions, 20 deletions
diff --git a/src/catch2/catch_test_case_info.cpp b/src/catch2/catch_test_case_info.cpp
index 7dab6ae4..7dd88c2d 100644
--- a/src/catch2/catch_test_case_info.cpp
+++ b/src/catch2/catch_test_case_info.cpp
@@ -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 ),
diff --git a/src/catch2/catch_test_case_info.hpp b/src/catch2/catch_test_case_info.hpp
index 8048bd3a..cae3b45a 100644
--- a/src/catch2/catch_test_case_info.hpp
+++ b/src/catch2/catch_test_case_info.hpp
@@ -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__
diff --git a/src/catch2/internal/catch_test_registry.cpp b/src/catch2/internal/catch_test_registry.cpp
index d33ba27a..c9e9fe4f 100644
--- a/src/catch2/internal/catch_test_registry.cpp
+++ b/src/catch2/internal/catch_test_registry.cpp
@@ -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
diff --git a/src/catch2/reporters/catch_reporter_junit.cpp b/src/catch2/reporters/catch_reporter_junit.cpp
index f7420b7a..442795ea 100644
--- a/src/catch2/reporters/catch_reporter_junit.cpp
+++ b/src/catch2/reporters/catch_reporter_junit.cpp
@@ -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() )