mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 11:43:29 +01:00
First cut of new filtering mechanism
This commit is contained in:
parent
b354da9ab9
commit
56d5c42912
@ -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();
|
||||
class Runner2 { // This will become Runner when Runner becomes Context
|
||||
|
||||
if( !config.stream.empty() ) {
|
||||
if( config.stream[0] == '%' )
|
||||
configWrapper.useStream( config.stream.substr( 1 ) );
|
||||
else
|
||||
configWrapper.setFilename( config.stream );
|
||||
}
|
||||
// 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)();
|
||||
}
|
||||
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 );
|
||||
|
||||
Ptr<IReporter> reporter = getRegistryHub().getReporterRegistry().create( reporterName, reporterConfig );
|
||||
if( !reporter )
|
||||
std::cerr << "No reporter registered with name: '" << reporterName << "'" << std::endl;
|
||||
return reporter;
|
||||
}
|
||||
|
||||
inline int Main( Config& configWrapper ) {
|
||||
|
||||
std::ofstream ofs;
|
||||
int result = resolveStream( ofs, configWrapper );
|
||||
if( result != 0 )
|
||||
return result;
|
||||
|
||||
Ptr<IReporter> reporter = makeReporter( configWrapper );
|
||||
if( !reporter )
|
||||
return (std::numeric_limits<int>::max)();
|
||||
|
||||
const ConfigData& config = configWrapper.data();
|
||||
|
||||
// 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
|
||||
public:
|
||||
Runner2( Config& configWrapper )
|
||||
: m_configWrapper( configWrapper ),
|
||||
m_config( configWrapper.data() )
|
||||
{
|
||||
Runner runner( configWrapper, reporter );
|
||||
resolveStream();
|
||||
makeReporter();
|
||||
}
|
||||
|
||||
Totals totals;
|
||||
// Run test specs specified on the command line - or default to all
|
||||
if( config.testSpecs.empty() ) {
|
||||
totals = runner.runAllNonHidden();
|
||||
Totals runTests() {
|
||||
|
||||
std::vector<TestCaseFilters> filterGroups = m_config.filters;
|
||||
if( filterGroups.empty() ) {
|
||||
TestCaseFilters filterGroup( "" );
|
||||
filterGroup.addFilter( TestCaseFilter( "./*", IfFilterMatches::ExcludeTests ) );
|
||||
filterGroups.push_back( filterGroup );
|
||||
}
|
||||
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;
|
||||
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
result = static_cast<int>( totals.assertions.failed );
|
||||
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;
|
||||
|
||||
ReporterConfig reporterConfig( m_config.name, m_configWrapper.stream(), m_config.includeWhichResults == Include::SuccessfulResults );
|
||||
|
||||
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() );
|
||||
}
|
||||
}
|
||||
|
||||
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 ) {
|
||||
int result = 0;
|
||||
try
|
||||
{
|
||||
Runner2 runner( configWrapper );
|
||||
|
||||
const ConfigData& config = configWrapper.data();
|
||||
|
||||
// Handle list request
|
||||
if( config.listSpec != List::None ) {
|
||||
List( config );
|
||||
return 0;
|
||||
}
|
||||
|
||||
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"
|
||||
|
@ -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,8 +120,20 @@ 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" ) ) {
|
||||
|
@ -75,6 +75,10 @@ namespace Catch {
|
||||
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 {
|
||||
|
||||
SourceLineInfo() : line( 0 ){}
|
||||
|
@ -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"
|
||||
|
||||
@ -30,6 +31,8 @@ namespace Catch {
|
||||
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;
|
||||
}
|
||||
|
@ -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__
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -52,19 +52,14 @@ namespace Catch {
|
||||
return m_p;
|
||||
}
|
||||
|
||||
T& operator*(){
|
||||
return *m_p;
|
||||
}
|
||||
const T& operator*() 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;
|
||||
}
|
||||
|
@ -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 ),
|
||||
@ -79,33 +79,21 @@ namespace Catch {
|
||||
m_context.setConfig( m_prevConfig );
|
||||
}
|
||||
|
||||
virtual Totals runAll() {
|
||||
return runTests( "", getRegistryHub().getTestCaseRegistry().getAllTests() );
|
||||
}
|
||||
Totals runMatching( const std::string& testSpec ) {
|
||||
|
||||
virtual Totals runAllNonHidden() {
|
||||
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 ) {
|
||||
std::vector<TestCaseInfo> matchingTests = getRegistryHub().getTestCaseRegistry().getMatchingTestCases( testSpec );
|
||||
|
||||
Totals totals;
|
||||
m_reporter->StartGroup( groupName );
|
||||
|
||||
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->StartGroup( testSpec );
|
||||
|
||||
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?
|
||||
|
||||
m_reporter->EndGroup( testSpec, totals );
|
||||
return totals;
|
||||
}
|
||||
|
||||
@ -229,12 +217,14 @@ namespace Catch {
|
||||
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;
|
||||
|
@ -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:
|
||||
|
||||
|
@ -12,29 +12,84 @@
|
||||
|
||||
namespace Catch {
|
||||
|
||||
class TestSpec {
|
||||
public:
|
||||
TestSpec( const std::string& rawSpec )
|
||||
: m_rawSpec( rawSpec ),
|
||||
m_isWildcarded( false ) {
|
||||
struct IfFilterMatches{ enum DoWhat {
|
||||
IncludeTests,
|
||||
ExcludeTests
|
||||
}; };
|
||||
|
||||
if( m_rawSpec[m_rawSpec.size()-1] == '*' ) {
|
||||
m_rawSpec = m_rawSpec.substr( 0, m_rawSpec.size()-1 );
|
||||
class TestCaseFilter {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
IfFilterMatches::DoWhat getFilterType() const {
|
||||
return m_filterType;
|
||||
}
|
||||
|
||||
bool shouldInclude( const TestCaseInfo& testCase ) const {
|
||||
return isMatch( testCase ) == (m_filterType == IfFilterMatches::IncludeTests);
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
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
|
||||
|
@ -63,6 +63,7 @@ TEST_CASE( "meta/Misc/Sections", "looped tests" ) {
|
||||
#endif
|
||||
|
||||
#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_xml.hpp"
|
||||
#include "../../include/reporters/catch_reporter_junit.hpp"
|
||||
@ -84,6 +85,8 @@ 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 ); }
|
||||
|
||||
TEST_CASE( "selftest/parser/2", "ConfigData" ) {
|
||||
|
||||
Catch::ConfigData config;
|
||||
@ -92,7 +95,6 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) {
|
||||
const char* argv[] = { "test" };
|
||||
CHECK_NOTHROW( parseIntoConfig( argv, config ) );
|
||||
|
||||
CHECK( config.testSpecs.empty() );
|
||||
CHECK( config.shouldDebugBreak == false );
|
||||
CHECK( config.cutoff == -1 );
|
||||
CHECK( config.allowThrows == true );
|
||||
@ -104,25 +106,45 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) {
|
||||
const char* argv[] = { "test", "-t", "test1" };
|
||||
CHECK_NOTHROW( parseIntoConfig( argv, config ) );
|
||||
|
||||
REQUIRE( config.testSpecs.size() == 1 );
|
||||
REQUIRE( config.testSpecs[0] == "test1" );
|
||||
REQUIRE( config.filters.size() == 1 );
|
||||
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" ) {
|
||||
const char* argv[] = { "test", "--test", "test1" };
|
||||
CHECK_NOTHROW( parseIntoConfig( argv, config ) );
|
||||
|
||||
REQUIRE( config.testSpecs.size() == 1 );
|
||||
REQUIRE( config.testSpecs[0] == "test1" );
|
||||
REQUIRE( config.filters.size() == 1 );
|
||||
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" ) {
|
||||
const char* argv[] = { "test", "-t", "test1", "test2" };
|
||||
CHECK_NOTHROW( parseIntoConfig( argv, config ) );
|
||||
|
||||
REQUIRE( config.testSpecs.size() == 2 );
|
||||
REQUIRE( config.testSpecs[0] == "test1" );
|
||||
REQUIRE( config.testSpecs[1] == "test2" );
|
||||
REQUIRE( config.filters.size() == 1 );
|
||||
REQUIRE( config.filters[0].shouldInclude( makeTestCase( "notIncluded" ) ) == false );
|
||||
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" ) {
|
||||
@ -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" ) {
|
||||
|
||||
Catch::TestCaseFilter matchAny( "*" );
|
||||
@ -335,7 +272,7 @@ TEST_CASE( "selftest/test filters", "Groups of tests can be selected" ) {
|
||||
|
||||
Catch::TestCaseFilter matchHidden( "./*" );
|
||||
Catch::TestCaseFilter dontMatchA( "./a*", Catch::IfFilterMatches::ExcludeTests );
|
||||
Catch::TestCaseFilters filters;
|
||||
Catch::TestCaseFilters filters( "" );
|
||||
filters.addFilter( matchHidden );
|
||||
filters.addFilter( dontMatchA );
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user