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.
This commit is contained in:
Phil Nash
2012-11-04 21:11:59 +00:00
parent 81cb69ef18
commit 78fba28c4b
7 changed files with 64 additions and 31 deletions

View File

@@ -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<ITestCase> m_test;
std::string m_className;
std::string m_name;
std::string m_description;
std::set<std::string> m_tags;

View File

@@ -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 {

View File

@@ -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

View File

@@ -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<typename C>
AutoReg( void (C::*method)(),
AutoReg( void (C::*method)(),
const char* className,
const char* name,
const char* description,
const SourceLineInfo& lineInfo ) {
registerTestCase( new MethodTestCase<C>( method ), name, description, lineInfo );
registerTestCase( new MethodTestCase<C>( 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()

View File

@@ -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 ) {