From 7b2fafd6d92d53328249c8abff9a78784b24963d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ho=C5=99e=C5=88ovsk=C3=BD?= Date: Sun, 17 Jul 2022 14:00:04 +0200 Subject: [PATCH] Normalize C++ namespace in JUnit's reporter classname field Closes #2468 --- src/catch2/reporters/catch_reporter_junit.cpp | 11 +++++++++++ tests/CMakeLists.txt | 11 +++++++++++ tests/SelfTest/UsageTests/Class.tests.cpp | 16 ++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/src/catch2/reporters/catch_reporter_junit.cpp b/src/catch2/reporters/catch_reporter_junit.cpp index 37546cd712..c24d1efa25 100644 --- a/src/catch2/reporters/catch_reporter_junit.cpp +++ b/src/catch2/reporters/catch_reporter_junit.cpp @@ -67,6 +67,15 @@ namespace Catch { return rss.str(); } + static void normalizeNamespaceMarkers(std::string& str) { + std::size_t pos = str.find( "::" ); + while ( pos != str.npos ) { + str.replace( pos, 2, "." ); + pos += 1; + pos = str.find( "::", pos ); + } + } + } // anonymous namespace JunitReporter::JunitReporter( ReporterConfig&& _config ) @@ -170,6 +179,8 @@ namespace Catch { if ( !m_config->name().empty() ) className = static_cast(m_config->name()) + '.' + className; + normalizeNamespaceMarkers(className); + writeSection( className, "", rootSection, stats.testInfo->okToFail() ); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d029e05d1b..fbe3b9e8c0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -553,6 +553,17 @@ set_tests_properties("Colours::ColourModeCanBeExplicitlySetToAnsi" PASS_REGULAR_EXPRESSION "\\[1\;32mAll tests passed" ) +add_test(NAME "Reporters::JUnit::NamespacesAreNormalized" + COMMAND + $ + --reporter junit + "A TEST_CASE_METHOD testing junit classname normalization" +) +set_tests_properties("Reporters::JUnit::NamespacesAreNormalized" + PROPERTIES + PASS_REGULAR_EXPRESSION "testcase classname=\"SelfTest\\.A\\.B\\.TestClass\"" +) + if (CATCH_ENABLE_CONFIGURE_TESTS) foreach(testName "DefaultReporter" "Disable" "DisableStringification" "ExperimentalRedirect") diff --git a/tests/SelfTest/UsageTests/Class.tests.cpp b/tests/SelfTest/UsageTests/Class.tests.cpp index 2bdc8d81e0..6b2ba4cfe6 100644 --- a/tests/SelfTest/UsageTests/Class.tests.cpp +++ b/tests/SelfTest/UsageTests/Class.tests.cpp @@ -114,4 +114,20 @@ namespace Inner { REQUIRE(Template_Fixture_2{}.m_a.size() < 2); } +} // namespace + + +// We want a class in nested namespace so we can test JUnit's classname normalization. +namespace { + namespace A { + namespace B { + class TestClass {}; + } + } +} // namespace + +TEST_CASE_METHOD( A::B::TestClass, + "A TEST_CASE_METHOD testing junit classname normalization", + "[class][approvals]" ) { + SUCCEED(); }