Split TestCaseInfo into a data only component and the test case function and behaviour.

Reporters only get to see the former
This commit is contained in:
Phil Nash
2012-11-25 11:19:55 +00:00
parent 06a671a349
commit 8baa06c63e
13 changed files with 298 additions and 285 deletions

View File

@@ -108,9 +108,9 @@ namespace Catch
virtual void StartGroup( const std::string& groupName ) = 0;
virtual void EndGroup( const std::string& groupName, const Totals& totals ) = 0;
virtual void StartTestCase( const TestCase& testInfo ) = 0;
virtual void StartTestCase( const TestCaseInfo& testInfo ) = 0;
// TestCaseResult
virtual void EndTestCase( const TestCase& testInfo, const Totals& totals, const std::string& stdOut, const std::string& stdErr ) = 0;
virtual void EndTestCase( const TestCaseInfo& testInfo, const Totals& totals, const std::string& stdOut, const std::string& stdErr ) = 0;
// SectionInfo
virtual void StartSection( const std::string& sectionName, const std::string& description ) = 0;

View File

@@ -45,9 +45,9 @@ namespace Catch {
if( matchesFilters( config.filters, *it ) ) {
matchedTests++;
// !TBD: consider listAs()
std::cout << "\t" << it->getName() << "\n";
std::cout << "\t" << it->getTestCaseInfo().name << "\n";
if( ( config.listSpec & List::TestNames ) != List::TestNames )
std::cout << "\t\t '" << it->getDescription() << "'\n";
std::cout << "\t\t '" << it->getTestCaseInfo().description << "'\n";
}
}
if( config.filters.empty() )

View File

@@ -97,15 +97,17 @@ namespace Catch {
return totals;
}
Totals runTest( const TestCase& testInfo ) {
Totals runTest( const TestCase& testCase ) {
Totals prevTotals = m_totals;
std::string redirectedCout;
std::string redirectedCerr;
TestCaseInfo testInfo = testCase.getTestCaseInfo();
m_reporter->StartTestCase( testInfo );
m_runningTest = new RunningTest( &testInfo );
m_runningTest = new RunningTest( &testCase );
do {
do {
@@ -120,7 +122,7 @@ namespace Catch {
( m_config.data().warnings & ConfigData::WarnAbout::NoAssertions ) ) {
m_totals.assertions.failed++;
deltaTotals = m_totals.delta( prevTotals );
m_reporter->NoAssertionsInTestCase( m_runningTest->getTestCase().getName() );
m_reporter->NoAssertionsInTestCase( testInfo.name );
}
m_totals.testCases += deltaTotals.testCases;
@@ -228,7 +230,7 @@ namespace Catch {
virtual std::string getCurrentTestName() const {
return m_runningTest
? m_runningTest->getTestCase().getName()
? m_runningTest->getTestCase().getTestCaseInfo().name
: "";
}
@@ -262,7 +264,7 @@ namespace Catch {
void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ) {
try {
m_lastAssertionInfo = AssertionInfo( "TEST_CASE", m_runningTest->getTestCase().getLineInfo(), "", ResultDisposition::Normal );
m_lastAssertionInfo = AssertionInfo( "TEST_CASE", m_runningTest->getTestCase().getTestCaseInfo().lineInfo, "", ResultDisposition::Normal );
m_runningTest->reset();
if( m_reporter->shouldRedirectStdout() ) {
StreamRedirect coutRedir( std::cout, redirectedCout );

View File

@@ -16,27 +16,37 @@
namespace Catch {
struct ITestCase;
struct TestCaseInfo {
TestCaseInfo( const std::string& _name,
const std::string& _className,
const std::string& _description,
const std::set<std::string>& _tags,
bool _isHidden,
const SourceLineInfo& _lineInfo );
TestCaseInfo( const TestCaseInfo& other );
std::string name;
std::string className;
std::string description;
std::set<std::string> tags;
bool isHidden;
SourceLineInfo lineInfo;
};
class TestCase {
class TestCase : protected TestCaseInfo {
public:
TestCase();
TestCase( ITestCase* testCase,
const std::string& className,
const std::string& name,
const std::string& description,
const SourceLineInfo& lineInfo );
TestCase( const TestCase& other, const std::string& name );
TestCase( ITestCase* testCase, const TestCaseInfo& info );
TestCase( const TestCase& other );
TestCase withName( const std::string& _newName ) const;
void invoke() const;
const std::string& getClassName() const;
const std::string& getName() const;
const std::string& getDescription() const;
const SourceLineInfo& getLineInfo() const;
const TestCaseInfo& getTestCaseInfo() const;
bool isHidden() const;
bool hasTag( const std::string& tag ) const;
bool matchesTags( const std::string& tagPattern ) const;
@@ -48,14 +58,14 @@ namespace Catch {
TestCase& operator = ( const TestCase& other );
private:
Ptr<ITestCase> m_test;
std::string m_className;
std::string m_name;
std::string m_description;
std::set<std::string> m_tags;
SourceLineInfo m_lineInfo;
bool m_isHidden;
Ptr<ITestCase> test;
};
TestCase makeTestCase( ITestCase* testCase,
const std::string& className,
const std::string& name,
const std::string& description,
const SourceLineInfo& lineInfo );
}
#endif // TWOBLUECUBES_CATCH_TEST_CASE_INFO_H_INCLUDED

View File

@@ -14,101 +14,95 @@
namespace Catch {
TestCase::TestCase( ITestCase* testCase,
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 ),
m_isHidden( startsWith( name, "./" ) )
TestCase makeTestCase( ITestCase* _testCase,
const std::string& _className,
const std::string& _name,
const std::string& _descOrTags,
const SourceLineInfo& _lineInfo )
{
TagExtracter( m_tags ).parse( m_description );
if( hasTag( "hide" ) )
m_isHidden = true;
std::string desc = _descOrTags;
bool isHidden( startsWith( _name, "./" ) );
std::set<std::string> tags;
TagExtracter( tags ).parse( desc );
if( tags.find( "hide" ) != tags.end() )
isHidden = true;
TestCaseInfo info( _name, _className, desc, tags, isHidden, _lineInfo );
return TestCase( _testCase, info );
}
TestCase::TestCase()
: m_test( NULL ),
m_className(),
m_name(),
m_description(),
m_isHidden( false )
TestCaseInfo::TestCaseInfo( const std::string& _name,
const std::string& _className,
const std::string& _description,
const std::set<std::string>& _tags,
bool _isHidden,
const SourceLineInfo& _lineInfo )
: name( _name ),
className( _className ),
description( _description ),
tags( _tags ),
isHidden( _isHidden ),
lineInfo( _lineInfo )
{}
TestCase::TestCase( const TestCase& 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 ),
m_lineInfo( other.m_lineInfo ),
m_isHidden( other.m_isHidden )
TestCaseInfo::TestCaseInfo( const TestCaseInfo& other )
: name( other.name ),
className( other.className ),
description( other.description ),
tags( other.tags ),
isHidden( other.isHidden ),
lineInfo( other.lineInfo )
{}
TestCase::TestCase( ITestCase* testCase, const TestCaseInfo& info ) : TestCaseInfo( info ), test( testCase ) {}
TestCase::TestCase( const TestCase& 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 ),
m_lineInfo( other.m_lineInfo ),
m_isHidden( other.m_isHidden )
: TestCaseInfo( other ),
test( other.test )
{}
void TestCase::invoke() const {
m_test->invoke();
TestCase TestCase::withName( const std::string& _newName ) const {
TestCase other( *this );
other.name = _newName;
return other;
}
const std::string& TestCase::getClassName() const {
return m_className;
}
const std::string& TestCase::getName() const {
return m_name;
}
const std::string& TestCase::getDescription() const {
return m_description;
}
const SourceLineInfo& TestCase::getLineInfo() const {
return m_lineInfo;
void TestCase::invoke() const {
test->invoke();
}
bool TestCase::isHidden() const {
return m_isHidden;
return TestCaseInfo::isHidden;
}
bool TestCase::hasTag( const std::string& tag ) const {
return m_tags.find( tag ) != m_tags.end();
return tags.find( tag ) != tags.end();
}
bool TestCase::matchesTags( const std::string& tagPattern ) const {
TagExpression exp;
TagExpressionParser( exp ).parse( tagPattern );
return exp.matches( m_tags );
return exp.matches( tags );
}
const std::set<std::string>& TestCase::getTags() const {
return m_tags;
return tags;
}
void TestCase::swap( TestCase& 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 );
std::swap( m_lineInfo, other.m_lineInfo );
test.swap( other.test );
className.swap( other.className );
name.swap( other.name );
description.swap( other.description );
std::swap( lineInfo, other.lineInfo );
}
bool TestCase::operator == ( const TestCase& other ) const {
return m_test.get() == other.m_test.get() &&
m_name == other.m_name &&
m_className == other.m_className;
return test.get() == other.test.get() &&
name == other.name &&
className == other.className;
}
bool TestCase::operator < ( const TestCase& other ) const {
return m_name < other.m_name;
return name < other.name;
}
TestCase& TestCase::operator = ( const TestCase& other ) {
TestCase temp( other );
@@ -116,6 +110,11 @@ namespace Catch {
return *this;
}
const TestCaseInfo& TestCase::getTestCaseInfo() const
{
return *this;
}
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_TEST_CASE_INFO_HPP_INCLUDED

View File

@@ -25,24 +25,25 @@ namespace Catch {
TestRegistry() : m_unnamedCount( 0 ) {}
virtual ~TestRegistry();
virtual void registerTest( const TestCase& testInfo ) {
if( testInfo.getName() == "" ) {
virtual void registerTest( const TestCase& testCase ) {
std::string name = testCase.getTestCaseInfo().name;
if( name == "" ) {
std::ostringstream oss;
oss << testInfo.getName() << "unnamed/" << ++m_unnamedCount;
return registerTest( TestCase( testInfo, oss.str() ) );
oss << name << "unnamed/" << ++m_unnamedCount;
return registerTest( testCase.withName( oss.str() ) );
}
if( m_functions.find( testInfo ) == m_functions.end() ) {
m_functions.insert( testInfo );
m_functionsInOrder.push_back( testInfo );
if( !testInfo.isHidden() )
m_nonHiddenFunctions.push_back( testInfo );
if( m_functions.find( testCase ) == m_functions.end() ) {
m_functions.insert( testCase );
m_functionsInOrder.push_back( testCase );
if( !testCase.isHidden() )
m_nonHiddenFunctions.push_back( testCase );
}
else {
const TestCase& prev = *m_functions.find( testInfo );
std::cerr << "error: TEST_CASE( \"" << testInfo.getName() << "\" ) already defined.\n"
<< "\tFirst seen at " << SourceLineInfo( prev.getLineInfo() ) << "\n"
<< "\tRedefined at " << SourceLineInfo( testInfo.getLineInfo() ) << std::endl;
const TestCase& prev = *m_functions.find( testCase );
std::cerr << "error: TEST_CASE( \"" << name << "\" ) already defined.\n"
<< "\tFirst seen at " << SourceLineInfo( prev.getTestCaseInfo().lineInfo ) << "\n"
<< "\tRedefined at " << SourceLineInfo( testCase.getTestCaseInfo().lineInfo ) << std::endl;
exit(1);
}
}
@@ -138,7 +139,7 @@ namespace Catch {
const char* description,
const SourceLineInfo& lineInfo ) {
getMutableRegistryHub().registerTest( TestCase( testCase, extractClassName( classOrQualifiedMethodName ), name, description, lineInfo ) );
getMutableRegistryHub().registerTest( makeTestCase( testCase, extractClassName( classOrQualifiedMethodName ), name, description, lineInfo ) );
}
} // end namespace Catch

View File

@@ -75,7 +75,7 @@ namespace Catch {
#endif
bool isMatch( const TestCase& testCase ) const {
const std::string& name = testCase.getName();
const std::string& name = testCase.getTestCaseInfo().name;
switch( m_wildcardPosition ) {
case NoWildcard: