Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorPhil Nash2012-08-23 20:08:50 +0100
committerPhil Nash2012-08-23 20:08:50 +0100
commit56d5c429121cfa6770961d22a9ef8191a30867ce (patch)
treea7dd1be656dcc61825506a2e92f0aa85695c90e2 /include
parentb354da9ab9f4e5b5699e14672b62b65222c2dd81 (diff)
downloadcatch3-56d5c429121cfa6770961d22a9ef8191a30867ce.tar.gz
First cut of new filtering mechanism
Diffstat (limited to 'include')
-rw-r--r--include/catch_runner.hpp174
-rw-r--r--include/internal/catch_commandline.hpp29
-rw-r--r--include/internal/catch_common.h4
-rw-r--r--include/internal/catch_config.hpp15
-rw-r--r--include/internal/catch_impl.hpp8
-rw-r--r--include/internal/catch_interfaces_runner.h12
-rw-r--r--include/internal/catch_interfaces_testcase.h6
-rw-r--r--include/internal/catch_list.hpp30
-rw-r--r--include/internal/catch_ptr.hpp13
-rw-r--r--include/internal/catch_runner_impl.hpp42
-rw-r--r--include/internal/catch_test_case_registry_impl.hpp16
-rw-r--r--include/internal/catch_test_spec.h79
12 files changed, 275 insertions, 153 deletions
diff --git a/include/catch_runner.hpp b/include/catch_runner.hpp
index 6a4dca07..5bda2c9d 100644
--- a/include/catch_runner.hpp
+++ b/include/catch_runner.hpp
@@ -11,6 +11,7 @@
#include "internal/catch_commandline.hpp"
#include "internal/catch_list.hpp"
#include "internal/catch_runner_impl.hpp"
+#include "internal/catch_test_spec.h"
#include <fstream>
#include <stdlib.h>
@@ -18,87 +19,136 @@
namespace Catch {
- inline int resolveStream( std::ofstream& ofs, Config& configWrapper ) {
- const ConfigData& config = configWrapper.data();
-
- if( !config.stream.empty() ) {
- if( config.stream[0] == '%' )
- configWrapper.useStream( config.stream.substr( 1 ) );
- else
- configWrapper.setFilename( config.stream );
+ class Runner2 { // This will become Runner when Runner becomes Context
+
+ public:
+ Runner2( Config& configWrapper )
+ : m_configWrapper( configWrapper ),
+ m_config( configWrapper.data() )
+ {
+ resolveStream();
+ makeReporter();
}
- // Open output file, if specified
- if( !config.outputFilename.empty() ) {
- ofs.open( config.outputFilename.c_str() );
- if( ofs.fail() ) {
- std::cerr << "Unable to open file: '" << config.outputFilename << "'" << std::endl;
- return (std::numeric_limits<int>::max)();
+
+ Totals runTests() {
+
+ std::vector<TestCaseFilters> filterGroups = m_config.filters;
+ if( filterGroups.empty() ) {
+ TestCaseFilters filterGroup( "" );
+ filterGroup.addFilter( TestCaseFilter( "./*", IfFilterMatches::ExcludeTests ) );
+ filterGroups.push_back( filterGroup );
}
- configWrapper.setStreamBuf( ofs.rdbuf() );
- }
- return 0;
- }
-
- inline Ptr<IReporter> makeReporter( Config& configWrapper ) {
- const ConfigData& config = configWrapper.data();
-
- std::string reporterName = config.reporter.empty()
- ? "basic"
- : config.reporter;
- ReporterConfig reporterConfig( config.name, configWrapper.stream(), config.includeWhichResults == Include::SuccessfulResults );
+ Runner context( m_configWrapper, m_reporter ); // This Runner will be renamed Context
+ Totals totals;
- Ptr<IReporter> reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig );
- if( !reporter )
- std::cerr << "No reporter registered with name: '" << reporterName << "'" << std::endl;
- return reporter;
- }
+ 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;
+ }
- inline int Main( Config& configWrapper ) {
+ 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
+ m_configWrapper.setFilename( m_config.stream );
+ }
+ // Open output file, if specified
+ if( !m_config.outputFilename.empty() ) {
+ m_ofs.open( m_config.outputFilename.c_str() );
+ if( m_ofs.fail() ) {
+ std::ostringstream oss;
+ oss << "Unable to open file: '" << m_config.outputFilename << "'";
+ throw std::domain_error( oss.str() );
+ }
+ m_configWrapper.setStreamBuf( m_ofs.rdbuf() );
+ }
+ }
+ void makeReporter() {
+ std::string reporterName = m_config.reporter.empty()
+ ? "basic"
+ : m_config.reporter;
- std::ofstream ofs;
- int result = resolveStream( ofs, configWrapper );
- if( result != 0 )
- return result;
+ ReporterConfig reporterConfig( m_config.name, m_configWrapper.stream(), m_config.includeWhichResults == Include::SuccessfulResults );
- Ptr<IReporter> reporter = makeReporter( configWrapper );
- if( !reporter )
- return (std::numeric_limits<int>::max)();
+ m_reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig );
+ if( !m_reporter ) {
+ std::ostringstream oss;
+ oss << "No reporter registered with name: '" << reporterName << "'";
+ throw std::domain_error( oss.str() );
+ }
+ }
- const ConfigData& config = configWrapper.data();
+ private:
+ Config& m_configWrapper;
+ const ConfigData& m_config;
+ std::ofstream m_ofs;
+ Ptr<IReporter> m_reporter;
+ std::set<TestCaseInfo> m_testsAlreadyRun;
+ };
- // Handle list request
- if( config.listSpec != List::None )
- return List( config );
-
- // Scope here for the Runner so it can use the context before it is cleaned-up
+ inline int Main( Config& configWrapper ) {
+ int result = 0;
+ try
{
- Runner runner( configWrapper, reporter );
+ Runner2 runner( configWrapper );
- Totals totals;
- // Run test specs specified on the command line - or default to all
- if( config.testSpecs.empty() ) {
- totals = runner.runAllNonHidden();
- }
- else {
- std::vector<std::string>::const_iterator it = config.testSpecs.begin();
- std::vector<std::string>::const_iterator itEnd = config.testSpecs.end();
- 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;
- }
+ const ConfigData& config = configWrapper.data();
+
+ // Handle list request
+ if( config.listSpec != List::None ) {
+ List( config );
+ return 0;
}
- result = static_cast<int>( totals.assertions.failed );
+
+ result = static_cast<int>( runner.runTests().assertions.failed );
+
}
+ catch( std::exception& ex ) {
+ std::cerr << ex.what() << std::endl;
+ result = (std::numeric_limits<int>::max)();
+ }
+
Catch::cleanUp();
return result;
}
inline void showUsage( std::ostream& os ) {
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-r, --reporter <reporter name>\n"
<< "\t-o, --out <file name>|<%stream name>\n"
diff --git a/include/internal/catch_commandline.hpp b/include/internal/catch_commandline.hpp
index d1b39a82..3b6c9f1f 100644
--- a/include/internal/catch_commandline.hpp
+++ b/include/internal/catch_commandline.hpp
@@ -9,6 +9,7 @@
#define TWOBLUECUBES_CATCH_COMMANDLINE_HPP_INCLUDED
#include "catch_config.hpp"
+#include "catch_common.h"
namespace Catch {
@@ -95,9 +96,11 @@ namespace Catch {
if( cmd.argsCount() > 2 )
cmd.raiseError( "Expected upto 2 arguments" );
- List::What listSpec = List::All;
+ config.listSpec = List::TestNames;
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;
else if( cmd[0] == "reporters" )
config.listSpec = List::Reports;
@@ -106,9 +109,9 @@ namespace Catch {
}
if( cmd.argsCount() >= 2 ) {
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" )
- config.listSpec = static_cast<List::What>( listSpec | List::AsText );
+ config.listSpec = static_cast<List::What>( config.listSpec | List::AsText );
else
cmd.raiseError( "Expected [xml] or [text]" );
}
@@ -117,10 +120,22 @@ namespace Catch {
if( Command cmd = parser.find( "-t", "--test" ) ) {
if( cmd.argsCount() == 0 )
cmd.raiseError( "Expected at least one argument" );
- for( std::size_t i = 0; i < cmd.argsCount(); ++i )
- config.testSpecs.push_back( cmd[i] );
+ std::string groupName;
+ 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( cmd.argsCount() != 1 )
cmd.raiseError( "Expected one argument" );
diff --git a/include/internal/catch_common.h b/include/internal/catch_common.h
index ee4a565b..4a3567e5 100644
--- a/include/internal/catch_common.h
+++ b/include/internal/catch_common.h
@@ -74,6 +74,10 @@ namespace Catch {
inline void forEach( const ContainerT& container, Function 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 {
diff --git a/include/internal/catch_config.hpp b/include/internal/catch_config.hpp
index fa488095..f859f004 100644
--- a/include/internal/catch_config.hpp
+++ b/include/internal/catch_config.hpp
@@ -8,6 +8,7 @@
#ifndef TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_RUNNERCONFIG_HPP_INCLUDED
+#include "catch_test_spec.h"
#include "catch_interfaces_reporter.h"
#include "catch_context.h"
@@ -29,7 +30,9 @@ namespace Catch {
Reports = 1,
Tests = 2,
All = 3,
-
+
+ TestNames = 6,
+
WhatMask = 0xf,
AsText = 0x10,
@@ -50,7 +53,7 @@ namespace Catch {
std::string reporter;
std::string outputFilename;
List::What listSpec;
- std::vector<std::string> testSpecs;
+ std::vector<TestCaseFilters> filters;
bool shouldDebugBreak;
std::string stream;
Include::WhichResults includeWhichResults;
@@ -87,14 +90,6 @@ namespace Catch {
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 {
return m_data.listSpec;
}
diff --git a/include/internal/catch_impl.hpp b/include/internal/catch_impl.hpp
index 73aa5e49..9815f271 100644
--- a/include/internal/catch_impl.hpp
+++ b/include/internal/catch_impl.hpp
@@ -24,6 +24,10 @@
#include "catch_resultinfo_builder.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 {
NonCopyable::~NonCopyable() {}
IShared::~IShared() {}
@@ -52,6 +56,10 @@ namespace Catch {
void Config::dummy() {}
+ INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
+ INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
+ INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter )
+
}
#ifdef __clang__
diff --git a/include/internal/catch_interfaces_runner.h b/include/internal/catch_interfaces_runner.h
index f3825626..874907b6 100644
--- a/include/internal/catch_interfaces_runner.h
+++ b/include/internal/catch_interfaces_runner.h
@@ -17,18 +17,6 @@ namespace Catch {
struct 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;
};
}
diff --git a/include/internal/catch_interfaces_testcase.h b/include/internal/catch_interfaces_testcase.h
index 992b71d3..7d5192aa 100644
--- a/include/internal/catch_interfaces_testcase.h
+++ b/include/internal/catch_interfaces_testcase.h
@@ -11,6 +11,9 @@
#include <vector>
namespace Catch {
+
+ class TestCaseFilters;
+
struct ITestCase : IShared {
virtual void invoke () const = 0;
protected:
@@ -22,10 +25,7 @@ namespace Catch {
struct ITestCaseRegistry {
virtual ~ITestCaseRegistry();
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 void getMatchingTestCases( const std::string& rawTestSpec, std::vector<TestCaseInfo>& matchingTestsOut ) const = 0;
};
}
diff --git a/include/internal/catch_list.hpp b/include/internal/catch_list.hpp
index 747939bc..d9c1200a 100644
--- a/include/internal/catch_list.hpp
+++ b/include/internal/catch_list.hpp
@@ -12,7 +12,15 @@
#include <limits>
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 ) {
std::cout << "Available reports:\n";
@@ -26,22 +34,28 @@ namespace Catch {
}
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 itEnd = getRegistryHub().getTestCaseRegistry().getAllTests().end();
for(; it != itEnd; ++it ) {
- // !TBD: consider listAs()
- std::cout << "\t" << it->getName() << "\n\t\t '" << it->getDescription() << "'\n";
+ if( matchesFilters( config.filters, *it ) ) {
+ // !TBD: consider listAs()
+ std::cout << "\t" << it->getName() << "\n";
+ if( ( config.listSpec & List::TestNames ) != List::TestNames )
+ std::cout << "\t\t '" << it->getDescription() << "'\n";
+ }
}
std::cout << std::endl;
}
if( ( config.listSpec & List::All ) == 0 ) {
- std::cerr << "Unknown list type" << std::endl;
- return (std::numeric_limits<int>::max)();
+ std::ostringstream oss;
+ oss << "Unknown list type";
+ throw std::domain_error( oss.str() );
}
-
- return 0;
}
} // end namespace Catch
diff --git a/include/internal/catch_ptr.hpp b/include/internal/catch_ptr.hpp
index 3976b7e9..2b8caf6f 100644
--- a/include/internal/catch_ptr.hpp
+++ b/include/internal/catch_ptr.hpp
@@ -52,19 +52,14 @@ namespace Catch {
return m_p;
}
- T& operator*(){
+ T& operator*() const {
return *m_p;
}
- const T& operator*() const{
- return *m_p;
- }
-
- T* operator->(){
- return m_p;
- }
- const T* operator->() const{
+
+ T* operator->() const {
return m_p;
}
+
bool operator !() const {
return m_p == NULL;
}
diff --git a/include/internal/catch_runner_impl.hpp b/include/internal/catch_runner_impl.hpp
index 7a22b3b9..fc438b00 100644
--- a/include/internal/catch_runner_impl.hpp
+++ b/include/internal/catch_runner_impl.hpp
@@ -56,7 +56,7 @@ namespace Catch {
public:
- explicit Runner( Config& config, const Ptr<IReporter>& reporter )
+ explicit Runner( const Config& config, const Ptr<IReporter>& reporter )
: m_context( getCurrentMutableContext() ),
m_runningTest( NULL ),
m_config( config ),
@@ -78,34 +78,22 @@ namespace Catch {
m_context.setResultCapture( m_prevResultCapture );
m_context.setConfig( m_prevConfig );
}
-
- virtual Totals runAll() {
- return runTests( "", getRegistryHub().getTestCaseRegistry().getAllTests() );
- }
- virtual Totals runAllNonHidden() {
- return runTests( "", getRegistryHub().getTestCaseRegistry().getAllNonHiddenTests() );
- }
+ Totals runMatching( const std::string& testSpec ) {
- virtual Totals runMatching( const std::string& rawTestSpec ) {
+ std::vector<TestCaseInfo> matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( testSpec );
- const std::vector<TestCaseInfo>& matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( rawTestSpec );
- return runTests( rawTestSpec, matchingTests );
- }
+ Totals totals;
- virtual Totals runTests( const std::string& groupName, const std::vector<TestCaseInfo>& testCases ) {
+ m_reporter->StartGroup( testSpec );
- Totals totals;
- m_reporter->StartGroup( groupName );
+ std::vector<TestCaseInfo>::const_iterator it = matchingTests.begin();
+ std::vector<TestCaseInfo>::const_iterator itEnd = matchingTests.end();
+ for(; it != itEnd; ++it )
+ totals += runTest( *it );
+ // !TBD use std::accumulate?
- for( std::size_t i=0; i < testCases.size(); ++i ) {
- if( aborting() ) {
- m_reporter->Aborted();
- break;
- }
- totals += runTest( testCases[i] );
- }
- m_reporter->EndGroup( groupName, totals );
+ m_reporter->EndGroup( testSpec, totals );
return totals;
}
@@ -228,13 +216,15 @@ namespace Catch {
virtual const ResultInfo* getLastResult() const {
return &m_lastResult;
}
-
- private:
+ public:
+ // !TBD We need to do this another way!
bool aborting() const {
return m_totals.assertions.failed == static_cast<std::size_t>( m_config.getCutoff() );
}
-
+
+ private:
+
ResultAction::Value actOnCurrentResult() {
testEnded( m_currentResult );
m_lastResult = m_currentResult;
diff --git a/include/internal/catch_test_case_registry_impl.hpp b/include/internal/catch_test_case_registry_impl.hpp
index a17e6033..ade5878d 100644
--- a/include/internal/catch_test_case_registry_impl.hpp
+++ b/include/internal/catch_test_case_registry_impl.hpp
@@ -52,25 +52,33 @@ namespace Catch {
return m_nonHiddenFunctions;
}
+ // !TBD deprecated
virtual std::vector<TestCaseInfo> getMatchingTestCases( const std::string& rawTestSpec ) const {
- TestSpec testSpec( rawTestSpec );
-
std::vector<TestCaseInfo> matchingTests;
getMatchingTestCases( rawTestSpec, matchingTests );
return matchingTests;
}
+ // !TBD deprecated
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 itEnd = m_functionsInOrder.end();
for(; it != itEnd; ++it ) {
- if( testSpec.matches( it->getName() ) ) {
+ if( filter.shouldInclude( *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:
diff --git a/include/internal/catch_test_spec.h b/include/internal/catch_test_spec.h
index 90013248..909db165 100644
--- a/include/internal/catch_test_spec.h
+++ b/include/internal/catch_test_spec.h
@@ -11,30 +11,85 @@
#include <string>
namespace Catch {
+
+ struct IfFilterMatches{ enum DoWhat {
+ IncludeTests,
+ ExcludeTests
+ }; };
- class TestSpec {
+ class TestCaseFilter {
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 );
+ 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;
}
}
- bool matches ( const std::string& testName ) const {
+ IfFilterMatches::DoWhat getFilterType() const {
+ return m_filterType;
+ }
+
+ 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_rawSpec == testName;
+ return m_testSpec == name;
else
- return testName.size() >= m_rawSpec.size() && testName.substr( 0, m_rawSpec.size() ) == m_rawSpec;
+ return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec;
}
- private:
- std::string m_rawSpec;
+ std::string m_testSpec;
+ IfFilterMatches::DoWhat m_filterType;
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