Skip to content

Commit

Permalink
Normalize C++ namespace in JUnit's reporter classname field
Browse files Browse the repository at this point in the history
Closes #2468
  • Loading branch information
horenmar committed Jul 17, 2022
1 parent 1bd2338 commit 7b2fafd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/catch2/reporters/catch_reporter_junit.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -170,6 +179,8 @@ namespace Catch {
if ( !m_config->name().empty() )
className = static_cast<std::string>(m_config->name()) + '.' + className;

normalizeNamespaceMarkers(className);

writeSection( className, "", rootSection, stats.testInfo->okToFail() );
}

Expand Down
11 changes: 11 additions & 0 deletions tests/CMakeLists.txt
Expand Up @@ -553,6 +553,17 @@ set_tests_properties("Colours::ColourModeCanBeExplicitlySetToAnsi"
PASS_REGULAR_EXPRESSION "\\[1\;32mAll tests passed"
)

add_test(NAME "Reporters::JUnit::NamespacesAreNormalized"
COMMAND
$<TARGET_FILE:SelfTest>
--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")
Expand Down
16 changes: 16 additions & 0 deletions tests/SelfTest/UsageTests/Class.tests.cpp
Expand Up @@ -114,4 +114,20 @@ namespace Inner
{
REQUIRE(Template_Fixture_2<TestType>{}.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();
}

0 comments on commit 7b2fafd

Please sign in to comment.