First cut of new filtering mechanism

This commit is contained in:
Phil Nash 2012-08-23 20:08:50 +01:00
parent b354da9ab9
commit 56d5c42912
14 changed files with 1535 additions and 1365 deletions

View File

@ -11,6 +11,7 @@
#include "internal/catch_commandline.hpp" #include "internal/catch_commandline.hpp"
#include "internal/catch_list.hpp" #include "internal/catch_list.hpp"
#include "internal/catch_runner_impl.hpp" #include "internal/catch_runner_impl.hpp"
#include "internal/catch_test_spec.h"
#include <fstream> #include <fstream>
#include <stdlib.h> #include <stdlib.h>
@ -18,87 +19,136 @@
namespace Catch { namespace Catch {
inline int resolveStream( std::ofstream& ofs, Config& configWrapper ) { class Runner2 { // This will become Runner when Runner becomes Context
const ConfigData& config = configWrapper.data();
if( !config.stream.empty() ) { public:
if( config.stream[0] == '%' ) Runner2( Config& configWrapper )
configWrapper.useStream( config.stream.substr( 1 ) ); : m_configWrapper( configWrapper ),
m_config( configWrapper.data() )
{
resolveStream();
makeReporter();
}
Totals runTests() {
std::vector<TestCaseFilters> filterGroups = m_config.filters;
if( filterGroups.empty() ) {
TestCaseFilters filterGroup( "" );
filterGroup.addFilter( TestCaseFilter( "./*", IfFilterMatches::ExcludeTests ) );
filterGroups.push_back( filterGroup );
}
Runner context( m_configWrapper, m_reporter ); // This Runner will be renamed Context
Totals totals;
std::vector<TestCaseFilters>::const_iterator it = filterGroups.begin();
std::vector<TestCaseFilters>::const_iterator itEnd = filterGroups.end();
for(; it != itEnd; ++it ) {
m_reporter->StartGroup( it->getName() );
runTestsForGroup( context, *it );
if( context.aborting() )
m_reporter->Aborted();
m_reporter->EndGroup( it->getName(), totals );
}
return totals;
}
Totals runTestsForGroup( Runner& context, const TestCaseFilters& filterGroup ) {
Totals totals;
std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin();
std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end();
int testsRunForGroup = 0;
for(; it != itEnd; ++it ) {
if( filterGroup.shouldInclude( *it ) ) {
testsRunForGroup++;
if( m_testsAlreadyRun.find( *it ) == m_testsAlreadyRun.end() ) {
if( context.aborting() )
break;
totals += context.runTest( *it );
m_testsAlreadyRun.insert( *it );
}
}
}
if( testsRunForGroup == 0 )
std::cerr << "\n[No test cases matched with: " << filterGroup.getName() << "]" << std::endl;
return totals;
}
private:
void resolveStream() {
if( !m_config.stream.empty() ) {
if( m_config.stream[0] == '%' )
m_configWrapper.useStream( m_config.stream.substr( 1 ) );
else else
configWrapper.setFilename( config.stream ); m_configWrapper.setFilename( m_config.stream );
} }
// Open output file, if specified // Open output file, if specified
if( !config.outputFilename.empty() ) { if( !m_config.outputFilename.empty() ) {
ofs.open( config.outputFilename.c_str() ); m_ofs.open( m_config.outputFilename.c_str() );
if( ofs.fail() ) { if( m_ofs.fail() ) {
std::cerr << "Unable to open file: '" << config.outputFilename << "'" << std::endl; std::ostringstream oss;
return (std::numeric_limits<int>::max)(); oss << "Unable to open file: '" << m_config.outputFilename << "'";
throw std::domain_error( oss.str() );
} }
configWrapper.setStreamBuf( ofs.rdbuf() ); m_configWrapper.setStreamBuf( m_ofs.rdbuf() );
} }
return 0;
} }
void makeReporter() {
inline Ptr<IReporter> makeReporter( Config& configWrapper ) { std::string reporterName = m_config.reporter.empty()
const ConfigData& config = configWrapper.data();
std::string reporterName = config.reporter.empty()
? "basic" ? "basic"
: config.reporter; : m_config.reporter;
ReporterConfig reporterConfig( config.name, configWrapper.stream(), config.includeWhichResults == Include::SuccessfulResults ); ReporterConfig reporterConfig( m_config.name, m_configWrapper.stream(), m_config.includeWhichResults == Include::SuccessfulResults );
Ptr<IReporter> reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig ); m_reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig );
if( !reporter ) if( !m_reporter ) {
std::cerr << "No reporter registered with name: '" << reporterName << "'" << std::endl; std::ostringstream oss;
return reporter; oss << "No reporter registered with name: '" << reporterName << "'";
throw std::domain_error( oss.str() );
} }
}
private:
Config& m_configWrapper;
const ConfigData& m_config;
std::ofstream m_ofs;
Ptr<IReporter> m_reporter;
std::set<TestCaseInfo> m_testsAlreadyRun;
};
inline int Main( Config& configWrapper ) { inline int Main( Config& configWrapper ) {
int result = 0;
std::ofstream ofs; try
int result = resolveStream( ofs, configWrapper ); {
if( result != 0 ) Runner2 runner( configWrapper );
return result;
Ptr<IReporter> reporter = makeReporter( configWrapper );
if( !reporter )
return (std::numeric_limits<int>::max)();
const ConfigData& config = configWrapper.data(); const ConfigData& config = configWrapper.data();
// Handle list request // Handle list request
if( config.listSpec != List::None ) if( config.listSpec != List::None ) {
return List( config ); List( config );
return 0;
}
// Scope here for the Runner so it can use the context before it is cleaned-up result = static_cast<int>( runner.runTests().assertions.failed );
{
Runner runner( configWrapper, reporter );
Totals totals;
// Run test specs specified on the command line - or default to all
if( config.testSpecs.empty() ) {
totals = runner.runAllNonHidden();
} }
else { catch( std::exception& ex ) {
std::vector<std::string>::const_iterator it = config.testSpecs.begin(); std::cerr << ex.what() << std::endl;
std::vector<std::string>::const_iterator itEnd = config.testSpecs.end(); result = (std::numeric_limits<int>::max)();
for(; it != itEnd; ++it ) {
Totals groupTotals = runner.runMatching( *it );
if( groupTotals.testCases.total() == 0 )
std::cerr << "\n[No test cases matched with: " << *it << "]" << std::endl;
totals += groupTotals;
}
}
result = static_cast<int>( totals.assertions.failed );
} }
Catch::cleanUp(); Catch::cleanUp();
return result; return result;
} }
inline void showUsage( std::ostream& os ) { inline void showUsage( std::ostream& os ) {
os << "\t-?, -h, --help\n" os << "\t-?, -h, --help\n"
<< "\t-l, --list <tests | reporters> [xml]\n" << "\t-l, --list [all | tests | reporters [xml]]\n"
<< "\t-t, --test <testspec> [<testspec>...]\n" << "\t-t, --test <testspec> [<testspec>...]\n"
<< "\t-r, --reporter <reporter name>\n" << "\t-r, --reporter <reporter name>\n"
<< "\t-o, --out <file name>|<%stream name>\n" << "\t-o, --out <file name>|<%stream name>\n"

View File

@ -9,6 +9,7 @@
#define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED #define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
#include "catch_config.hpp" #include "catch_config.hpp"
#include "catch_common.h"
namespace Catch { namespace Catch {
@ -95,9 +96,11 @@ namespace Catch {
if( cmd.argsCount() > 2 ) if( cmd.argsCount() > 2 )
cmd.raiseError( "Expected upto 2 arguments" ); cmd.raiseError( "Expected upto 2 arguments" );
List::What listSpec = List::All; config.listSpec = List::TestNames;
if( cmd.argsCount() >= 1 ) { if( cmd.argsCount() >= 1 ) {
if( cmd[0] == "tests" ) if( cmd[0] == "all" )
config.listSpec = List::All;
else if( cmd[0] == "tests" )
config.listSpec = List::Tests; config.listSpec = List::Tests;
else if( cmd[0] == "reporters" ) else if( cmd[0] == "reporters" )
config.listSpec = List::Reports; config.listSpec = List::Reports;
@ -106,9 +109,9 @@ namespace Catch {
} }
if( cmd.argsCount() >= 2 ) { if( cmd.argsCount() >= 2 ) {
if( cmd[1] == "xml" ) if( cmd[1] == "xml" )
config.listSpec = static_cast<List::What>( listSpec | List::AsXml ); config.listSpec = static_cast<List::What>( config.listSpec | List::AsXml );
else if( cmd[1] == "text" ) else if( cmd[1] == "text" )
config.listSpec = static_cast<List::What>( listSpec | List::AsText ); config.listSpec = static_cast<List::What>( config.listSpec | List::AsText );
else else
cmd.raiseError( "Expected [xml] or [text]" ); cmd.raiseError( "Expected [xml] or [text]" );
} }
@ -117,8 +120,20 @@ namespace Catch {
if( Command cmd = parser.find( "-t", "--test" ) ) { if( Command cmd = parser.find( "-t", "--test" ) ) {
if( cmd.argsCount() == 0 ) if( cmd.argsCount() == 0 )
cmd.raiseError( "Expected at least one argument" ); cmd.raiseError( "Expected at least one argument" );
for( std::size_t i = 0; i < cmd.argsCount(); ++i ) std::string groupName;
config.testSpecs.push_back( cmd[i] ); for( std::size_t i = 0; i < cmd.argsCount(); ++i ) {
if( i != 0 )
groupName += " ";
groupName += cmd[i];
}
TestCaseFilters filters( groupName );
for( std::size_t i = 0; i < cmd.argsCount(); ++i ) {
if( startsWith( cmd[i], "exclude:" ) )
filters.addFilter( TestCaseFilter( cmd[i].substr( 8 ), IfFilterMatches::ExcludeTests ) );
else
filters.addFilter( TestCaseFilter( cmd[i] ) );
}
config.filters.push_back( filters );
} }
if( Command cmd = parser.find( "-r", "--reporter" ) ) { if( Command cmd = parser.find( "-r", "--reporter" ) ) {

View File

@ -75,6 +75,10 @@ namespace Catch {
std::for_each( container.begin(), container.end(), function ); std::for_each( container.begin(), container.end(), function );
} }
inline bool startsWith( const std::string& s, const std::string& prefix ) {
return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix;
}
struct SourceLineInfo { struct SourceLineInfo {
SourceLineInfo() : line( 0 ){} SourceLineInfo() : line( 0 ){}

View File

@ -8,6 +8,7 @@
#ifndef TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED #ifndef TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED #define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED
#include "catch_test_spec.h"
#include "catch_interfaces_reporter.h" #include "catch_interfaces_reporter.h"
#include "catch_context.h" #include "catch_context.h"
@ -30,6 +31,8 @@ namespace Catch {
Tests = 2, Tests = 2,
All = 3, All = 3,
TestNames = 6,
WhatMask = 0xf, WhatMask = 0xf,
AsText = 0x10, AsText = 0x10,
@ -50,7 +53,7 @@ namespace Catch {
std::string reporter; std::string reporter;
std::string outputFilename; std::string outputFilename;
List::What listSpec; List::What listSpec;
std::vector<std::string> testSpecs; std::vector<TestCaseFilters> filters;
bool shouldDebugBreak; bool shouldDebugBreak;
std::string stream; std::string stream;
Include::WhichResults includeWhichResults; Include::WhichResults includeWhichResults;
@ -87,14 +90,6 @@ namespace Catch {
m_data.outputFilename = filename; m_data.outputFilename = filename;
} }
bool testsSpecified() const {
return !m_data.testSpecs.empty();
}
const std::vector<std::string>& getTestSpecs() const {
return m_data.testSpecs;
}
List::What getListSpec( void ) const { List::What getListSpec( void ) const {
return m_data.listSpec; return m_data.listSpec;
} }

View File

@ -24,6 +24,10 @@
#include "catch_resultinfo_builder.hpp" #include "catch_resultinfo_builder.hpp"
#include "catch_test_case_info.hpp" #include "catch_test_case_info.hpp"
#include "../reporters/catch_reporter_basic.hpp"
#include "../reporters/catch_reporter_xml.hpp"
#include "../reporters/catch_reporter_junit.hpp"
namespace Catch { namespace Catch {
NonCopyable::~NonCopyable() {} NonCopyable::~NonCopyable() {}
IShared::~IShared() {} IShared::~IShared() {}
@ -52,6 +56,10 @@ namespace Catch {
void Config::dummy() {} void Config::dummy() {}
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter )
} }
#ifdef __clang__ #ifdef __clang__

View File

@ -17,18 +17,6 @@ namespace Catch {
struct IRunner { struct IRunner {
virtual ~IRunner(); virtual ~IRunner();
/// Runs all tests, even if hidden
virtual Totals runAll() = 0;
/// Runs all tests unless 'hidden' by ./ prefix
virtual Totals runAllNonHidden() = 0;
/// Runs all test that match the spec string
virtual Totals runMatching( const std::string& rawTestSpec ) = 0;
/// Runs all the tests passed in
virtual Totals runTests( const std::string& groupName, const std::vector<TestCaseInfo>& testCases ) = 0;
}; };
} }

View File

@ -11,6 +11,9 @@
#include <vector> #include <vector>
namespace Catch { namespace Catch {
class TestCaseFilters;
struct ITestCase : IShared { struct ITestCase : IShared {
virtual void invoke () const = 0; virtual void invoke () const = 0;
protected: protected:
@ -22,10 +25,7 @@ namespace Catch {
struct ITestCaseRegistry { struct ITestCaseRegistry {
virtual ~ITestCaseRegistry(); virtual ~ITestCaseRegistry();
virtual const std::vector<TestCaseInfo>& getAllTests() const = 0; virtual const std::vector<TestCaseInfo>& getAllTests() const = 0;
virtual const std::vector<TestCaseInfo>& getAllNonHiddenTests() const = 0;
virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const = 0; virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const = 0;
virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const = 0;
}; };
} }

View File

@ -12,7 +12,15 @@
#include <limits> #include <limits>
namespace Catch { namespace Catch {
inline int List( const ConfigData& config ) { inline bool matchesFilters( const std::vector<TestCaseFilters>& filters, const TestCaseInfo& testCase ) {
std::vector<TestCaseFilters>::const_iterator it = filters.begin();
std::vector<TestCaseFilters>::const_iterator itEnd = filters.end();
for(; it != itEnd; ++it )
if( !it->shouldInclude( testCase ) )
return false;
return true;
}
inline void List( const ConfigData& config ) {
if( config.listSpec & List::Reports ) { if( config.listSpec & List::Reports ) {
std::cout << "Available reports:\n"; std::cout << "Available reports:\n";
@ -26,22 +34,28 @@ namespace Catch {
} }
if( config.listSpec & List::Tests ) { if( config.listSpec & List::Tests ) {
std::cout << "Available tests:\n"; if( config.filters.empty() )
std::cout << "All available tests:\n";
else
std::cout << "Matching tests:\n";
std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin(); std::vector<TestCaseInfo>::const_iterator it = getRegistryHub().getTestCaseRegistry().getAllTests().begin();
std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end(); std::vector<TestCaseInfo>::const_iterator itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end();
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
if( matchesFilters( config.filters, *it ) ) {
// !TBD: consider listAs() // !TBD: consider listAs()
std::cout << "\t" << it->getName() << "\n\t\t '" << it->getDescription() << "'\n"; std::cout << "\t" << it->getName() << "\n";
if( ( config.listSpec & List::TestNames ) != List::TestNames )
std::cout << "\t\t '" << it->getDescription() << "'\n";
}
} }
std::cout << std::endl; std::cout << std::endl;
} }
if( ( config.listSpec & List::All ) == 0 ) { if( ( config.listSpec & List::All ) == 0 ) {
std::cerr << "Unknown list type" << std::endl; std::ostringstream oss;
return (std::numeric_limits<int>::max)(); oss << "Unknown list type";
throw std::domain_error( oss.str() );
} }
return 0;
} }
} // end namespace Catch } // end namespace Catch

View File

@ -52,19 +52,14 @@ namespace Catch {
return m_p; return m_p;
} }
T& operator*(){ T& operator*() const {
return *m_p;
}
const T& operator*() const{
return *m_p; return *m_p;
} }
T* operator->(){ T* operator->() const {
return m_p;
}
const T* operator->() const{
return m_p; return m_p;
} }
bool operator !() const { bool operator !() const {
return m_p == NULL; return m_p == NULL;
} }

View File

@ -56,7 +56,7 @@ namespace Catch {
public: public:
explicit Runner( Config& config, const Ptr<IReporter>& reporter ) explicit Runner( const Config& config, const Ptr<IReporter>& reporter )
: m_context( getCurrentMutableContext() ), : m_context( getCurrentMutableContext() ),
m_runningTest( NULL ), m_runningTest( NULL ),
m_config( config ), m_config( config ),
@ -79,33 +79,21 @@ namespace Catch {
m_context.setConfig( m_prevConfig ); m_context.setConfig( m_prevConfig );
} }
virtual Totals runAll() { Totals runMatching( const std::string& testSpec ) {
return runTests( "", getRegistryHub().getTestCaseRegistry().getAllTests() );
}
virtual Totals runAllNonHidden() { std::vector<TestCaseInfo> matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( testSpec );
return runTests( "", getRegistryHub().getTestCaseRegistry().getAllNonHiddenTests() );
}
virtual Totals runMatching( const std::string& rawTestSpec ) {
const std::vector<TestCaseInfo>& matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( rawTestSpec );
return runTests( rawTestSpec, matchingTests );
}
virtual Totals runTests( const std::string& groupName, const std::vector<TestCaseInfo>& testCases ) {
Totals totals; Totals totals;
m_reporter->StartGroup( groupName );
for( std::size_t i=0; i < testCases.size(); ++i ) { m_reporter->StartGroup( testSpec );
if( aborting() ) {
m_reporter->Aborted(); std::vector<TestCaseInfo>::const_iterator it = matchingTests.begin();
break; std::vector<TestCaseInfo>::const_iterator itEnd = matchingTests.end();
} for(; it != itEnd; ++it )
totals += runTest( testCases[i] ); totals += runTest( *it );
} // !TBD use std::accumulate?
m_reporter->EndGroup( groupName, totals );
m_reporter->EndGroup( testSpec, totals );
return totals; return totals;
} }
@ -229,12 +217,14 @@ namespace Catch {
return &m_lastResult; return &m_lastResult;
} }
private: public:
// !TBD We need to do this another way!
bool aborting() const { bool aborting() const {
return m_totals.assertions.failed == static_cast<std::size_t>( m_config.getCutoff() ); return m_totals.assertions.failed == static_cast<std::size_t>( m_config.getCutoff() );
} }
private:
ResultAction::Value actOnCurrentResult() { ResultAction::Value actOnCurrentResult() {
testEnded( m_currentResult ); testEnded( m_currentResult );
m_lastResult = m_currentResult; m_lastResult = m_currentResult;

View File

@ -52,25 +52,33 @@ namespace Catch {
return m_nonHiddenFunctions; return m_nonHiddenFunctions;
} }
// !TBD deprecated
virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const { virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const {
TestSpec testSpec( rawTestSpec );
std::vector<TestCaseInfo> matchingTests; std::vector<TestCaseInfo> matchingTests;
getMatchingTestCases( rawTestSpec, matchingTests ); getMatchingTestCases( rawTestSpec, matchingTests );
return matchingTests; return matchingTests;
} }
// !TBD deprecated
virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const { virtual void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const {
TestSpec testSpec( rawTestSpec ); TestCaseFilter filter( rawTestSpec );
std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin(); std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin();
std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end(); std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end();
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
if( testSpec.matches( it->getName() ) ) { if( filter.shouldInclude( *it ) ) {
matchingTestsOut.push_back( *it ); matchingTestsOut.push_back( *it );
} }
} }
} }
virtual void getMatchingTestCases( const TestCaseFilters& filters, std::vector<TestCaseInfo>& matchingTestsOut ) const {
std::vector<TestCaseInfo>::const_iterator it = m_functionsInOrder.begin();
std::vector<TestCaseInfo>::const_iterator itEnd = m_functionsInOrder.end();
// !TBD: replace with algorithm
for(; it != itEnd; ++it )
if( filters.shouldInclude( *it ) )
matchingTestsOut.push_back( *it );
}
private: private:

View File

@ -12,29 +12,84 @@
namespace Catch { namespace Catch {
class TestSpec { struct IfFilterMatches{ enum DoWhat {
public: IncludeTests,
TestSpec( const std::string& rawSpec ) ExcludeTests
: m_rawSpec( rawSpec ), }; };
m_isWildcarded( false ) {
if( m_rawSpec[m_rawSpec.size()-1] == '*' ) { class TestCaseFilter {
m_rawSpec = m_rawSpec.substr( 0, m_rawSpec.size()-1 ); public:
TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat matchBehaviour = IfFilterMatches::IncludeTests )
: m_testSpec( testSpec ),
m_filterType( matchBehaviour ),
m_isWildcarded( false )
{
if( m_testSpec[m_testSpec.size()-1] == '*' ) {
m_testSpec = m_testSpec.substr( 0, m_testSpec.size()-1 );
m_isWildcarded = true; m_isWildcarded = true;
} }
} }
bool matches ( const std::string& testName ) const { IfFilterMatches::DoWhat getFilterType() const {
if( !m_isWildcarded ) return m_filterType;
return m_rawSpec == testName;
else
return testName.size() >= m_rawSpec.size() && testName.substr( 0, m_rawSpec.size() ) == m_rawSpec;
} }
bool shouldInclude( const TestCaseInfo& testCase ) const {
return isMatch( testCase ) == (m_filterType == IfFilterMatches::IncludeTests);
}
private: private:
std::string m_rawSpec;
bool isMatch( const TestCaseInfo& testCase ) const {
const std::string& name = testCase.getName();
if( !m_isWildcarded )
return m_testSpec == name;
else
return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec;
}
std::string m_testSpec;
IfFilterMatches::DoWhat m_filterType;
bool m_isWildcarded; bool m_isWildcarded;
}; };
class TestCaseFilters {
public:
TestCaseFilters( const std::string& name ) : m_name( name ) {}
std::string getName() const {
return m_name;
}
void addFilter( const TestCaseFilter& filter ) {
if( filter.getFilterType() == IfFilterMatches::ExcludeTests )
m_exclusionFilters.push_back( filter );
else
m_inclusionFilters.push_back( filter );
}
bool shouldInclude( const TestCaseInfo& testCase ) const {
if( !m_inclusionFilters.empty() ) {
std::vector<TestCaseFilter>::const_iterator it = m_inclusionFilters.begin();
std::vector<TestCaseFilter>::const_iterator itEnd = m_inclusionFilters.end();
for(; it != itEnd; ++it )
if( it->shouldInclude( testCase ) )
break;
if( it == itEnd )
return false;
}
std::vector<TestCaseFilter>::const_iterator it = m_exclusionFilters.begin();
std::vector<TestCaseFilter>::const_iterator itEnd = m_exclusionFilters.end();
for(; it != itEnd; ++it )
if( !it->shouldInclude( testCase ) )
return false;
return true;
}
private:
std::vector<TestCaseFilter> m_inclusionFilters;
std::vector<TestCaseFilter> m_exclusionFilters;
std::string m_name;
};
} }
#endif // TWOBLUECUBES_CATCH_TESTSPEC_H_INCLUDED #endif // TWOBLUECUBES_CATCH_TESTSPEC_H_INCLUDED

View File

@ -63,6 +63,7 @@ TEST_CASE( "meta/Misc/Sections", "looped tests" ) {
#endif #endif
#include "../../include/internal/catch_commandline.hpp" #include "../../include/internal/catch_commandline.hpp"
#include "../../include/internal/catch_test_spec.h"
#include "../../include/reporters/catch_reporter_basic.hpp" #include "../../include/reporters/catch_reporter_basic.hpp"
#include "../../include/reporters/catch_reporter_xml.hpp" #include "../../include/reporters/catch_reporter_xml.hpp"
#include "../../include/reporters/catch_reporter_junit.hpp" #include "../../include/reporters/catch_reporter_junit.hpp"
@ -84,6 +85,8 @@ std::string parseIntoConfigAndReturnError( const char * (&argv)[size], Catch::Co
return ""; return "";
} }
inline Catch::TestCaseInfo makeTestCase( const char* name ){ return Catch::TestCaseInfo( NULL, name, "", CATCH_INTERNAL_LINEINFO ); }
TEST_CASE( "selftest/parser/2", "ConfigData" ) { TEST_CASE( "selftest/parser/2", "ConfigData" ) {
Catch::ConfigData config; Catch::ConfigData config;
@ -92,7 +95,6 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) {
const char* argv[] = { "test" }; const char* argv[] = { "test" };
CHECK_NOTHROW( parseIntoConfig( argv, config ) ); CHECK_NOTHROW( parseIntoConfig( argv, config ) );
CHECK( config.testSpecs.empty() );
CHECK( config.shouldDebugBreak == false ); CHECK( config.shouldDebugBreak == false );
CHECK( config.cutoff == -1 ); CHECK( config.cutoff == -1 );
CHECK( config.allowThrows == true ); CHECK( config.allowThrows == true );
@ -104,25 +106,45 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) {
const char* argv[] = { "test", "-t", "test1" }; const char* argv[] = { "test", "-t", "test1" };
CHECK_NOTHROW( parseIntoConfig( argv, config ) ); CHECK_NOTHROW( parseIntoConfig( argv, config ) );
REQUIRE( config.testSpecs.size() == 1 ); REQUIRE( config.filters.size() == 1 );
REQUIRE( config.testSpecs[0] == "test1" ); REQUIRE( config.filters[0].shouldInclude( makeTestCase( "notIncluded" ) ) == false );
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) );
}
SECTION( "-t/exclude:1", "Specify one test case exclusion using -t exclude:" ) {
const char* argv[] = { "test", "-t", "exclude:test1" };
CHECK_NOTHROW( parseIntoConfig( argv, config ) );
REQUIRE( config.filters.size() == 1 );
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) == false );
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "alwaysIncluded" ) ) );
} }
SECTION( "--test/1", "Specify one test case using --test" ) { SECTION( "--test/1", "Specify one test case using --test" ) {
const char* argv[] = { "test", "--test", "test1" }; const char* argv[] = { "test", "--test", "test1" };
CHECK_NOTHROW( parseIntoConfig( argv, config ) ); CHECK_NOTHROW( parseIntoConfig( argv, config ) );
REQUIRE( config.testSpecs.size() == 1 ); REQUIRE( config.filters.size() == 1 );
REQUIRE( config.testSpecs[0] == "test1" ); REQUIRE( config.filters[0].shouldInclude( makeTestCase( "notIncluded" ) ) == false );
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) );
}
SECTION( "--test/exclude:1", "Specify one test case exclusion using --test exclude:" ) {
const char* argv[] = { "test", "--test", "exclude:test1" };
CHECK_NOTHROW( parseIntoConfig( argv, config ) );
REQUIRE( config.filters.size() == 1 );
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) == false );
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "alwaysIncluded" ) ) );
} }
SECTION( "-t/2", "Specify two test cases using -t" ) { SECTION( "-t/2", "Specify two test cases using -t" ) {
const char* argv[] = { "test", "-t", "test1", "test2" }; const char* argv[] = { "test", "-t", "test1", "test2" };
CHECK_NOTHROW( parseIntoConfig( argv, config ) ); CHECK_NOTHROW( parseIntoConfig( argv, config ) );
REQUIRE( config.testSpecs.size() == 2 ); REQUIRE( config.filters.size() == 1 );
REQUIRE( config.testSpecs[0] == "test1" ); REQUIRE( config.filters[0].shouldInclude( makeTestCase( "notIncluded" ) ) == false );
REQUIRE( config.testSpecs[1] == "test2" ); REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test1" ) ) );
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "test2" ) ) );
} }
SECTION( "-t/0", "When no test names are supplied it is an error" ) { SECTION( "-t/0", "When no test names are supplied it is an error" ) {
@ -229,91 +251,6 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) {
} }
} }
class TestSpec {
public:
TestSpec( const std::string& rawSpec )
: m_rawSpec( rawSpec ),
m_isWildcarded( false ) {
if( m_rawSpec[m_rawSpec.size()-1] == '*' ) {
m_rawSpec = m_rawSpec.substr( 0, m_rawSpec.size()-1 );
m_isWildcarded = true;
}
}
bool matches ( const std::string& testName ) const {
if( !m_isWildcarded )
return m_rawSpec == testName;
else
return testName.size() >= m_rawSpec.size() && testName.substr( 0, m_rawSpec.size() ) == m_rawSpec;
}
private:
std::string m_rawSpec;
bool m_isWildcarded;
};
namespace Catch{ //
struct IfFilterMatches{ enum DoWhat {
IncludeTests,
ExcludeTests
}; };
class TestCaseFilter {
public:
TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat filterType = IfFilterMatches::IncludeTests )
: m_testSpec( testSpec ),
m_filterType( filterType ),
m_isWildcarded( false )
{
if( m_testSpec[m_testSpec.size()-1] == '*' ) {
m_testSpec = m_testSpec.substr( 0, m_testSpec.size()-1 );
m_isWildcarded = true;
}
}
bool shouldInclude( const TestCaseInfo& testCase ) const {
return isMatch( testCase ) == (m_filterType == IfFilterMatches::IncludeTests);
}
private:
bool isMatch( const TestCaseInfo& testCase ) const {
const std::string& name = testCase.getName();
if( !m_isWildcarded )
return m_testSpec == name;
else
return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec;
}
std::string m_testSpec;
IfFilterMatches::DoWhat m_filterType;
bool m_isWildcarded;
};
class TestCaseFilters {
public:
void addFilter( const TestCaseFilter& filter ) {
m_filters.push_back( filter );
}
bool shouldInclude( const TestCaseInfo& testCase ) const {
std::vector<TestCaseFilter>::const_iterator it = m_filters.begin();
std::vector<TestCaseFilter>::const_iterator itEnd = m_filters.end();
for(; it != itEnd; ++it )
if( !it->shouldInclude( testCase ) )
return false;
return true;
}
private:
std::vector<TestCaseFilter> m_filters;
};
}
inline Catch::TestCaseInfo makeTestCase( const char* name ){ return Catch::TestCaseInfo( NULL, name, "", CATCH_INTERNAL_LINEINFO ); }
TEST_CASE( "selftest/test filter", "Groups of tests can be selected" ) { TEST_CASE( "selftest/test filter", "Groups of tests can be selected" ) {
Catch::TestCaseFilter matchAny( "*" ); Catch::TestCaseFilter matchAny( "*" );
@ -335,7 +272,7 @@ TEST_CASE( "selftest/test filters", "Groups of tests can be selected" ) {
Catch::TestCaseFilter matchHidden( "./*" ); Catch::TestCaseFilter matchHidden( "./*" );
Catch::TestCaseFilter dontMatchA( "./a*", Catch::IfFilterMatches::ExcludeTests ); Catch::TestCaseFilter dontMatchA( "./a*", Catch::IfFilterMatches::ExcludeTests );
Catch::TestCaseFilters filters; Catch::TestCaseFilters filters( "" );
filters.addFilter( matchHidden ); filters.addFilter( matchHidden );
filters.addFilter( dontMatchA ); filters.addFilter( dontMatchA );

File diff suppressed because it is too large Load Diff