WIldcards at both ends

This commit is contained in:
Phil Nash 2012-08-24 08:23:50 +01:00
parent 56d5c42912
commit 4c97fc5619
6 changed files with 141 additions and 66 deletions

View File

@ -78,6 +78,29 @@ namespace Catch {
inline bool startsWith( const std::string& s, const std::string& prefix ) { inline bool startsWith( const std::string& s, const std::string& prefix ) {
return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix; return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix;
} }
inline bool endsWith( const std::string& s, const std::string& suffix ) {
return s.size() >= suffix.size() && s.substr( s.size()-suffix.size(), suffix.size() ) == suffix;
}
inline bool contains( const std::string& s, const std::string& infix ) {
return s.find( infix ) != std::string::npos;
}
struct pluralise {
pluralise( std::size_t count, const std::string& label )
: m_count( count ),
m_label( label )
{}
friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) {
os << pluraliser.m_count << " " << pluraliser.m_label;
if( pluraliser.m_count != 1 )
os << "s";
return os;
}
std::size_t m_count;
std::string m_label;
};
struct SourceLineInfo { struct SourceLineInfo {

View File

@ -35,20 +35,25 @@ namespace Catch {
if( config.listSpec & List::Tests ) { if( config.listSpec & List::Tests ) {
if( config.filters.empty() ) if( config.filters.empty() )
std::cout << "All available tests:\n"; std::cout << "All available test cases:\n";
else else
std::cout << "Matching tests:\n"; std::cout << "Matching test cases:\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();
std::size_t matchedTests = 0;
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
if( matchesFilters( config.filters, *it ) ) { if( matchesFilters( config.filters, *it ) ) {
matchedTests++;
// !TBD: consider listAs() // !TBD: consider listAs()
std::cout << "\t" << it->getName() << "\n"; std::cout << "\t" << it->getName() << "\n";
if( ( config.listSpec & List::TestNames ) != List::TestNames ) if( ( config.listSpec & List::TestNames ) != List::TestNames )
std::cout << "\t\t '" << it->getDescription() << "'\n"; std::cout << "\t\t '" << it->getDescription() << "'\n";
} }
} }
std::cout << std::endl; if( config.filters.empty() )
std::cout << pluralise( matchedTests, "test case" ) << std::endl;
else
std::cout << pluralise( matchedTests, "matching test case" ) << std::endl;
} }
if( ( config.listSpec & List::All ) == 0 ) { if( ( config.listSpec & List::All ) == 0 ) {

View File

@ -18,15 +18,26 @@ namespace Catch {
}; }; }; };
class TestCaseFilter { class TestCaseFilter {
enum WildcardPosition {
NoWildcard = 0,
WildcardAtStart = 1,
WildcardAtEnd = 2,
WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd
};
public: public:
TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat matchBehaviour = IfFilterMatches::IncludeTests ) TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat matchBehaviour = IfFilterMatches::IncludeTests )
: m_testSpec( testSpec ), : m_stringToMatch( testSpec ),
m_filterType( matchBehaviour ), m_filterType( matchBehaviour ),
m_isWildcarded( false ) m_wildcardPosition( NoWildcard )
{ {
if( m_testSpec[m_testSpec.size()-1] == '*' ) { if( m_stringToMatch[0] == '*' ) {
m_testSpec = m_testSpec.substr( 0, m_testSpec.size()-1 ); m_stringToMatch = m_stringToMatch.substr( 1 );
m_isWildcarded = true; m_wildcardPosition = (WildcardPosition)( m_wildcardPosition | WildcardAtStart );
}
if( m_stringToMatch[m_stringToMatch.size()-1] == '*' ) {
m_stringToMatch = m_stringToMatch.substr( 0, m_stringToMatch.size()-1 );
m_wildcardPosition = (WildcardPosition)( m_wildcardPosition | WildcardAtEnd );
} }
} }
@ -41,15 +52,23 @@ namespace Catch {
bool isMatch( const TestCaseInfo& testCase ) const { bool isMatch( const TestCaseInfo& testCase ) const {
const std::string& name = testCase.getName(); const std::string& name = testCase.getName();
if( !m_isWildcarded )
return m_testSpec == name; switch( m_wildcardPosition ) {
else case NoWildcard:
return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec; return m_stringToMatch == name;
case WildcardAtStart:
return endsWith( name, m_stringToMatch );
case WildcardAtEnd:
return startsWith( name, m_stringToMatch );
case WildcardAtBothEnds:
return contains( name, m_stringToMatch );
}
} }
std::string m_testSpec; std::string m_stringToMatch;
IfFilterMatches::DoWhat m_filterType; IfFilterMatches::DoWhat m_filterType;
bool m_isWildcarded; WildcardPosition m_wildcardPosition;
}; };
class TestCaseFilters { class TestCaseFilters {

View File

@ -15,23 +15,6 @@
namespace Catch { namespace Catch {
struct pluralise {
pluralise( std::size_t count, const std::string& label )
: m_count( count ),
m_label( label )
{}
friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) {
os << pluraliser.m_count << " " << pluraliser.m_label;
if( pluraliser.m_count != 1 )
os << "s";
return os;
}
std::size_t m_count;
std::string m_label;
};
class BasicReporter : public SharedImpl<IReporter> { class BasicReporter : public SharedImpl<IReporter> {
struct SpanInfo { struct SpanInfo {

View File

@ -251,7 +251,7 @@ TEST_CASE( "selftest/parser/2", "ConfigData" ) {
} }
} }
TEST_CASE( "selftest/test filter", "Groups of tests can be selected" ) { TEST_CASE( "selftest/test filter", "Individual filters" ) {
Catch::TestCaseFilter matchAny( "*" ); Catch::TestCaseFilter matchAny( "*" );
Catch::TestCaseFilter matchNone( "*", Catch::IfFilterMatches::ExcludeTests ); Catch::TestCaseFilter matchNone( "*", Catch::IfFilterMatches::ExcludeTests );
@ -268,7 +268,7 @@ TEST_CASE( "selftest/test filter", "Groups of tests can be selected" ) {
CHECK( matchNonHidden.shouldInclude( makeTestCase( "./any" ) ) == false ); CHECK( matchNonHidden.shouldInclude( makeTestCase( "./any" ) ) == false );
} }
TEST_CASE( "selftest/test filters", "Groups of tests can be selected" ) { TEST_CASE( "selftest/test filters", "Sets of filters" ) {
Catch::TestCaseFilter matchHidden( "./*" ); Catch::TestCaseFilter matchHidden( "./*" );
Catch::TestCaseFilter dontMatchA( "./a*", Catch::IfFilterMatches::ExcludeTests ); Catch::TestCaseFilter dontMatchA( "./a*", Catch::IfFilterMatches::ExcludeTests );
@ -282,3 +282,18 @@ TEST_CASE( "selftest/test filters", "Groups of tests can be selected" ) {
CHECK( filters.shouldInclude( makeTestCase( "./something" ) ) ); CHECK( filters.shouldInclude( makeTestCase( "./something" ) ) );
CHECK( filters.shouldInclude( makeTestCase( "./anything" ) ) == false ); CHECK( filters.shouldInclude( makeTestCase( "./anything" ) ) == false );
} }
TEST_CASE( "selftest/filter/prefix wildcard", "Individual filters with wildcards at the start" ) {
Catch::TestCaseFilter matchBadgers( "*badger" );
CHECK( matchBadgers.shouldInclude( makeTestCase( "big badger" ) ));
CHECK( matchBadgers.shouldInclude( makeTestCase( "little badgers" ) ) == false );
}
TEST_CASE( "selftest/filter/wildcard at both ends", "Individual filters with wildcards at both ends" ) {
Catch::TestCaseFilter matchBadgers( "*badger*" );
CHECK( matchBadgers.shouldInclude( makeTestCase( "big badger" ) ));
CHECK( matchBadgers.shouldInclude( makeTestCase( "little badgers" ) ) );
CHECK( matchBadgers.shouldInclude( makeTestCase( "badgers are big" ) ) );
CHECK( matchBadgers.shouldInclude( makeTestCase( "hedgehogs" ) ) == false );
}

View File

@ -1,5 +1,5 @@
/* /*
* Generated: 2012-08-23 20:06:52.538506 * Generated: 2012-08-24 08:23:10.017875
* ---------------------------------------------------------- * ----------------------------------------------------------
* This file has been merged from multiple headers. Please don't edit it directly * This file has been merged from multiple headers. Please don't edit it directly
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. * Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
@ -95,6 +95,29 @@ namespace Catch {
inline bool startsWith( const std::string& s, const std::string& prefix ) { inline bool startsWith( const std::string& s, const std::string& prefix ) {
return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix; return s.size() >= prefix.size() && s.substr( 0, prefix.size() ) == prefix;
} }
inline bool endsWith( const std::string& s, const std::string& suffix ) {
return s.size() >= suffix.size() && s.substr( s.size()-suffix.size(), suffix.size() ) == suffix;
}
inline bool contains( const std::string& s, const std::string& infix ) {
return s.find( infix ) != std::string::npos;
}
struct pluralise {
pluralise( std::size_t count, const std::string& label )
: m_count( count ),
m_label( label )
{}
friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) {
os << pluraliser.m_count << " " << pluraliser.m_label;
if( pluraliser.m_count != 1 )
os << "s";
return os;
}
std::size_t m_count;
std::string m_label;
};
struct SourceLineInfo { struct SourceLineInfo {
@ -2216,15 +2239,26 @@ namespace Catch {
}; }; }; };
class TestCaseFilter { class TestCaseFilter {
enum WildcardPosition {
NoWildcard = 0,
WildcardAtStart = 1,
WildcardAtEnd = 2,
WildcardAtBothEnds = WildcardAtStart | WildcardAtEnd
};
public: public:
TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat matchBehaviour = IfFilterMatches::IncludeTests ) TestCaseFilter( const std::string& testSpec, IfFilterMatches::DoWhat matchBehaviour = IfFilterMatches::IncludeTests )
: m_testSpec( testSpec ), : m_stringToMatch( testSpec ),
m_filterType( matchBehaviour ), m_filterType( matchBehaviour ),
m_isWildcarded( false ) m_wildcardPosition( NoWildcard )
{ {
if( m_testSpec[m_testSpec.size()-1] == '*' ) { if( m_stringToMatch[0] == '*' ) {
m_testSpec = m_testSpec.substr( 0, m_testSpec.size()-1 ); m_stringToMatch = m_stringToMatch.substr( 1 );
m_isWildcarded = true; m_wildcardPosition = (WildcardPosition)( m_wildcardPosition | WildcardAtStart );
}
if( m_stringToMatch[m_stringToMatch.size()-1] == '*' ) {
m_stringToMatch = m_stringToMatch.substr( 0, m_stringToMatch.size()-1 );
m_wildcardPosition = (WildcardPosition)( m_wildcardPosition | WildcardAtEnd );
} }
} }
@ -2239,15 +2273,23 @@ namespace Catch {
bool isMatch( const TestCaseInfo& testCase ) const { bool isMatch( const TestCaseInfo& testCase ) const {
const std::string& name = testCase.getName(); const std::string& name = testCase.getName();
if( !m_isWildcarded )
return m_testSpec == name; switch( m_wildcardPosition ) {
else case NoWildcard:
return name.size() >= m_testSpec.size() && name.substr( 0, m_testSpec.size() ) == m_testSpec; return m_stringToMatch == name;
case WildcardAtStart:
return endsWith( name, m_stringToMatch );
case WildcardAtEnd:
return startsWith( name, m_stringToMatch );
case WildcardAtBothEnds:
return contains( name, m_stringToMatch );
}
} }
std::string m_testSpec; std::string m_stringToMatch;
IfFilterMatches::DoWhat m_filterType; IfFilterMatches::DoWhat m_filterType;
bool m_isWildcarded; WildcardPosition m_wildcardPosition;
}; };
class TestCaseFilters { class TestCaseFilters {
@ -2643,20 +2685,25 @@ namespace Catch {
if( config.listSpec & List::Tests ) { if( config.listSpec & List::Tests ) {
if( config.filters.empty() ) if( config.filters.empty() )
std::cout << "All available tests:\n"; std::cout << "All available test cases:\n";
else else
std::cout << "Matching tests:\n"; std::cout << "Matching test cases:\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();
std::size_t matchedTests = 0;
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
if( matchesFilters( config.filters, *it ) ) { if( matchesFilters( config.filters, *it ) ) {
matchedTests++;
// !TBD: consider listAs() // !TBD: consider listAs()
std::cout << "\t" << it->getName() << "\n"; std::cout << "\t" << it->getName() << "\n";
if( ( config.listSpec & List::TestNames ) != List::TestNames ) if( ( config.listSpec & List::TestNames ) != List::TestNames )
std::cout << "\t\t '" << it->getDescription() << "'\n"; std::cout << "\t\t '" << it->getDescription() << "'\n";
} }
} }
std::cout << std::endl; if( config.filters.empty() )
std::cout << pluralise( matchedTests, "test case" ) << std::endl;
else
std::cout << pluralise( matchedTests, "matching test case" ) << std::endl;
} }
if( ( config.listSpec & List::All ) == 0 ) { if( ( config.listSpec & List::All ) == 0 ) {
@ -4230,23 +4277,6 @@ namespace Catch {
namespace Catch { namespace Catch {
struct pluralise {
pluralise( std::size_t count, const std::string& label )
: m_count( count ),
m_label( label )
{}
friend std::ostream& operator << ( std::ostream& os, const pluralise& pluraliser ) {
os << pluraliser.m_count << " " << pluraliser.m_label;
if( pluraliser.m_count != 1 )
os << "s";
return os;
}
std::size_t m_count;
std::string m_label;
};
class BasicReporter : public SharedImpl<IReporter> { class BasicReporter : public SharedImpl<IReporter> {
struct SpanInfo { struct SpanInfo {