From 78fba28c4b9f48ec25c9d234a29c36e4427ca6e0 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Sun, 4 Nov 2012 21:11:59 +0000 Subject: [PATCH] Added className to TestCaseInfo className is passed through from class based test methods and held in the TestCaseInfo. For free-function based test cases it is set to "global". The JUnit reporter uses the className value to populate he class attribute. --- include/internal/catch_test_case_info.h | 8 ++++-- include/internal/catch_test_case_info.hpp | 21 +++++++++++----- .../catch_test_case_registry_impl.hpp | 25 +++++++++++++++---- include/internal/catch_test_registry.hpp | 14 ++++++----- include/reporters/catch_reporter_junit.hpp | 7 ++++-- projects/SelfTest/ClassTests.cpp | 8 +++--- projects/SelfTest/TestMain.cpp | 12 ++++----- 7 files changed, 64 insertions(+), 31 deletions(-) diff --git a/include/internal/catch_test_case_info.h b/include/internal/catch_test_case_info.h index eee460a7..1fce27fb 100644 --- a/include/internal/catch_test_case_info.h +++ b/include/internal/catch_test_case_info.h @@ -22,8 +22,9 @@ namespace Catch { TestCaseInfo(); TestCaseInfo( ITestCase* testCase, - const char* name, - const char* description, + const std::string& className, + const std::string& name, + const std::string& description, const SourceLineInfo& lineInfo ); @@ -31,6 +32,8 @@ namespace Catch { TestCaseInfo( const TestCaseInfo& other ); void invoke() const; + + const std::string& getClassName() const; const std::string& getName() const; const std::string& getDescription() const; const SourceLineInfo& getLineInfo() const; @@ -46,6 +49,7 @@ namespace Catch { private: Ptr m_test; + std::string m_className; std::string m_name; std::string m_description; std::set m_tags; diff --git a/include/internal/catch_test_case_info.hpp b/include/internal/catch_test_case_info.hpp index 38adac91..bb50759a 100644 --- a/include/internal/catch_test_case_info.hpp +++ b/include/internal/catch_test_case_info.hpp @@ -16,10 +16,12 @@ namespace Catch { TestCaseInfo::TestCaseInfo( ITestCase* testCase, - const char* name, - const char* description, + const std::string& className, + const std::string& name, + const std::string& description, const SourceLineInfo& lineInfo ) : m_test( testCase ), + m_className( className ), m_name( name ), m_description( description ), m_lineInfo( lineInfo ), @@ -32,6 +34,7 @@ namespace Catch { TestCaseInfo::TestCaseInfo() : m_test( NULL ), + m_className(), m_name(), m_description(), m_isHidden( false ) @@ -39,6 +42,7 @@ namespace Catch { TestCaseInfo::TestCaseInfo( const TestCaseInfo& other, const std::string& name ) : m_test( other.m_test ), + m_className( other.m_className ), m_name( name ), m_description( other.m_description ), m_tags( other.m_tags ), @@ -48,6 +52,7 @@ namespace Catch { TestCaseInfo::TestCaseInfo( const TestCaseInfo& other ) : m_test( other.m_test ), + m_className( other.m_className ), m_name( other.m_name ), m_description( other.m_description ), m_tags( other.m_tags ), @@ -59,14 +64,15 @@ namespace Catch { m_test->invoke(); } + const std::string& TestCaseInfo::getClassName() const { + return m_className; + } const std::string& TestCaseInfo::getName() const { return m_name; } - const std::string& TestCaseInfo::getDescription() const { return m_description; } - const SourceLineInfo& TestCaseInfo::getLineInfo() const { return m_lineInfo; } @@ -89,13 +95,16 @@ namespace Catch { void TestCaseInfo::swap( TestCaseInfo& other ) { m_test.swap( other.m_test ); + m_className.swap( other.m_className ); m_name.swap( other.m_name ); m_description.swap( other.m_description ); - m_lineInfo.swap( other.m_lineInfo ); + std::swap( m_lineInfo, other.m_lineInfo ); } bool TestCaseInfo::operator == ( const TestCaseInfo& other ) const { - return m_test.get() == other.m_test.get() && m_name == other.m_name; + return m_test.get() == other.m_test.get() && + m_name == other.m_name && + m_className == other.m_className; } bool TestCaseInfo::operator < ( const TestCaseInfo& other ) const { diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp index 96c5783f..adaefc35 100644 --- a/include/internal/catch_test_case_registry_impl.hpp +++ b/include/internal/catch_test_case_registry_impl.hpp @@ -107,23 +107,38 @@ namespace Catch { TestFunction m_fun; }; - + + inline std::string extractClassName( const std::string& classOrQualifiedMethodName ) { + std::string className = classOrQualifiedMethodName; + if( className[0] == '&' ) + { + 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; + } + /////////////////////////////////////////////////////////////////////////// AutoReg::AutoReg( TestFunction function, const char* name, const char* description, const SourceLineInfo& lineInfo ) { - registerTestCase( new FreeFunctionTestCase( function ), name, description, lineInfo ); + registerTestCase( new FreeFunctionTestCase( function ), "global", name, description, lineInfo ); } AutoReg::~AutoReg() {} - void AutoReg::registerTestCase( ITestCase* testCase, - const char* name, + void AutoReg::registerTestCase( ITestCase* testCase, + const char* classOrQualifiedMethodName, + const char* name, const char* description, const SourceLineInfo& lineInfo ) { - getMutableRegistryHub().registerTest( TestCaseInfo( testCase, name, description, lineInfo ) ); + + getMutableRegistryHub().registerTest( TestCaseInfo( testCase, extractClassName( classOrQualifiedMethodName ), name, description, lineInfo ) ); } } // end namespace Catch diff --git a/include/internal/catch_test_registry.hpp b/include/internal/catch_test_registry.hpp index 6a42fdbe..0530e4f2 100644 --- a/include/internal/catch_test_registry.hpp +++ b/include/internal/catch_test_registry.hpp @@ -34,21 +34,23 @@ typedef void(*TestFunction)(); struct AutoReg { - AutoReg( TestFunction function, + AutoReg( TestFunction function, const char* name, const char* description, const SourceLineInfo& lineInfo ); template - AutoReg( void (C::*method)(), + AutoReg( void (C::*method)(), + const char* className, const char* name, const char* description, const SourceLineInfo& lineInfo ) { - registerTestCase( new MethodTestCase( method ), name, description, lineInfo ); + registerTestCase( new MethodTestCase( method ), className, name, description, lineInfo ); } void registerTestCase( ITestCase* testCase, - const char* name, + const char* className, + const char* name, const char* description, const SourceLineInfo& lineInfo ); @@ -75,7 +77,7 @@ private: /////////////////////////////////////////////////////////////////////////////// #define INTERNAL_CATCH_METHOD_AS_TEST_CASE( QualifiedMethod, Name, Desc ) \ - namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, Name, Desc, CATCH_INTERNAL_LINEINFO ); } + namespace{ Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar )( &QualifiedMethod, "&" #QualifiedMethod, Name, Desc, CATCH_INTERNAL_LINEINFO ); } /////////////////////////////////////////////////////////////////////////////// #define TEST_CASE_METHOD( ClassName, TestName, Desc )\ @@ -83,7 +85,7 @@ private: struct INTERNAL_CATCH_UNIQUE_NAME( TestCaseMethod_catch_internal_ ) : ClassName{ \ void test(); \ }; \ - Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseMethod_catch_internal_ )::test, TestName, Desc, CATCH_INTERNAL_LINEINFO ); \ + Catch::AutoReg INTERNAL_CATCH_UNIQUE_NAME( autoRegistrar ) ( &INTERNAL_CATCH_UNIQUE_NAME( TestCaseMethod_catch_internal_ )::test, #ClassName, TestName, Desc, CATCH_INTERNAL_LINEINFO ); \ } \ void INTERNAL_CATCH_UNIQUE_NAME( TestCaseMethod_catch_internal_ )::test() diff --git a/include/reporters/catch_reporter_junit.hpp b/include/reporters/catch_reporter_junit.hpp index 99e6ed04..93c48360 100644 --- a/include/reporters/catch_reporter_junit.hpp +++ b/include/reporters/catch_reporter_junit.hpp @@ -26,7 +26,10 @@ namespace Catch { struct TestCaseStats { - TestCaseStats( const std::string& name = std::string() ) :m_name( name ){} + TestCaseStats( const std::string& className, const std::string& name ) + : m_className( className ), + m_name( name ) + {} double m_timeInSeconds; std::string m_status; @@ -94,7 +97,7 @@ namespace Catch { virtual void EndSection( const std::string&, const Counts& ) {} virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) { - m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getName() ) ); + m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getClassName(), testInfo.getName() ) ); } virtual void Result( const Catch::AssertionResult& assertionResult ) { diff --git a/projects/SelfTest/ClassTests.cpp b/projects/SelfTest/ClassTests.cpp index 76c06dd7..573f5aaa 100644 --- a/projects/SelfTest/ClassTests.cpp +++ b/projects/SelfTest/ClassTests.cpp @@ -31,8 +31,8 @@ namespace } -METHOD_AS_TEST_CASE( TestClass::succeedingCase, "./succeeding/TestClass/succeedingCase", "A method based test run that succeeds" ) -METHOD_AS_TEST_CASE( TestClass::failingCase, "./failing/TestClass/failingCase", "A method based test run that fails" ) +METHOD_AS_TEST_CASE( TestClass::succeedingCase, "./succeeding/TestClass/succeedingCase", "A method based test run that succeeds [class]" ) +METHOD_AS_TEST_CASE( TestClass::failingCase, "./failing/TestClass/failingCase", "A method based test run that fails [class]" ) struct Fixture @@ -42,7 +42,7 @@ struct Fixture int m_a; }; -TEST_CASE_METHOD( Fixture, "./succeeding/Fixture/succeedingCase", "A method based test run that succeeds" ) +TEST_CASE_METHOD( Fixture, "./succeeding/Fixture/succeedingCase", "A method based test run that succeeds [class]" ) { REQUIRE( m_a == 1 ); } @@ -50,7 +50,7 @@ TEST_CASE_METHOD( Fixture, "./succeeding/Fixture/succeedingCase", "A method base // We should be able to write our tests within a different namespace namespace Inner { - TEST_CASE_METHOD( Fixture, "./failing/Fixture/failingCase", "A method based test run that fails" ) + TEST_CASE_METHOD( Fixture, "./failing/Fixture/failingCase", "A method based test run that fails [class]" ) { REQUIRE( m_a == 2 ); } diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp index 6e38d44c..12287799 100644 --- a/projects/SelfTest/TestMain.cpp +++ b/projects/SelfTest/TestMain.cpp @@ -86,7 +86,7 @@ std::string parseIntoConfigAndReturnError( const char * (&argv)[size], Catch::Co return ""; } -inline Catch::TestCaseInfo makeTestCase( const char* name ){ return Catch::TestCaseInfo( NULL, name, "", CATCH_INTERNAL_LINEINFO ); } +inline Catch::TestCaseInfo makeTestCase( const char* name ){ return Catch::TestCaseInfo( NULL, "", name, "", CATCH_INTERNAL_LINEINFO ); } TEST_CASE( "selftest/parser/2", "ConfigData" ) { @@ -364,7 +364,7 @@ TEST_CASE( "selftest/tags", "" ) { std::string p5 = "[one][two]~[hide],[three]"; SECTION( "one tag", "" ) { - Catch::TestCaseInfo oneTag( NULL, "test", "[one]", CATCH_INTERNAL_LINEINFO ); + Catch::TestCaseInfo oneTag( NULL, "", "test", "[one]", CATCH_INTERNAL_LINEINFO ); CHECK( oneTag.getDescription() == "" ); CHECK( oneTag.hasTag( "one" ) ); @@ -378,7 +378,7 @@ TEST_CASE( "selftest/tags", "" ) { } SECTION( "two tags", "" ) { - Catch::TestCaseInfo twoTags( NULL, "test", "[one][two]", CATCH_INTERNAL_LINEINFO ); + Catch::TestCaseInfo twoTags( NULL, "", "test", "[one][two]", CATCH_INTERNAL_LINEINFO ); CHECK( twoTags.getDescription() == "" ); CHECK( twoTags.hasTag( "one" ) ); @@ -395,7 +395,7 @@ TEST_CASE( "selftest/tags", "" ) { SECTION( "one tag with characters either side", "" ) { - Catch::TestCaseInfo oneTagWithExtras( NULL, "test", "12[one]34", CATCH_INTERNAL_LINEINFO ); + Catch::TestCaseInfo oneTagWithExtras( NULL, "", "test", "12[one]34", CATCH_INTERNAL_LINEINFO ); CHECK( oneTagWithExtras.getDescription() == "1234" ); CHECK( oneTagWithExtras.hasTag( "one" ) ); CHECK( oneTagWithExtras.hasTag( "two" ) == false ); @@ -404,7 +404,7 @@ TEST_CASE( "selftest/tags", "" ) { SECTION( "start of a tag, but not closed", "" ) { - Catch::TestCaseInfo oneTagOpen( NULL, "test", "[one", CATCH_INTERNAL_LINEINFO ); + Catch::TestCaseInfo oneTagOpen( NULL, "", "test", "[one", CATCH_INTERNAL_LINEINFO ); CHECK( oneTagOpen.getDescription() == "[one" ); CHECK( oneTagOpen.hasTag( "one" ) == false ); @@ -412,7 +412,7 @@ TEST_CASE( "selftest/tags", "" ) { } SECTION( "hidden", "" ) { - Catch::TestCaseInfo oneTag( NULL, "test", "[hide]", CATCH_INTERNAL_LINEINFO ); + Catch::TestCaseInfo oneTag( NULL, "", "test", "[hide]", CATCH_INTERNAL_LINEINFO ); CHECK( oneTag.getDescription() == "" ); CHECK( oneTag.hasTag( "hide" ) );