Ignore leading/trailing whitespace in test/section specs

The leading/trailing whitespace is problematic because of e.g.
`WHEN` macro having preceeding whitespace for alignment, and it is
generally messy.

Credits to Phil who did lot of the original work.

Closes #1708
This commit is contained in:
Martin Hořeňovský 2019-09-09 11:30:45 +02:00
parent a156440b19
commit af8b2538a6
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
12 changed files with 219 additions and 27 deletions

View File

@ -15,12 +15,24 @@ namespace Catch {
: m_data( data ), : m_data( data ),
m_stream( openStream() ) m_stream( openStream() )
{ {
// We need to trim filter specs to avoid trouble with superfluous
// whitespace (esp. important for bdd macros, as those are manually
// aligned with whitespace).
for (auto& elem : m_data.testsOrTags) {
elem = trim(elem);
}
for (auto& elem : m_data.sectionsToRun) {
elem = trim(elem);
}
TestSpecParser parser(ITagAliasRegistry::get()); TestSpecParser parser(ITagAliasRegistry::get());
if (!data.testsOrTags.empty()) { if (!m_data.testsOrTags.empty()) {
m_hasTestFilters = true; m_hasTestFilters = true;
for( auto const& testOrTags : data.testsOrTags ) for (auto const& testOrTags : m_data.testsOrTags) {
parser.parse(testOrTags); parser.parse(testOrTags);
} }
}
m_testSpec = parser.testSpec(); m_testSpec = parser.testSpec();
} }

View File

@ -8,6 +8,7 @@
#include "catch_test_case_tracker.h" #include "catch_test_case_tracker.h"
#include "catch_enforce.h" #include "catch_enforce.h"
#include "catch_string_manip.h"
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
@ -174,7 +175,8 @@ namespace TestCaseTracking {
} }
SectionTracker::SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ) SectionTracker::SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent )
: TrackerBase( nameAndLocation, ctx, parent ) : TrackerBase( nameAndLocation, ctx, parent ),
m_trimmed_name(trim(nameAndLocation.name))
{ {
if( parent ) { if( parent ) {
while( !parent->isSectionTracker() ) while( !parent->isSectionTracker() )
@ -188,12 +190,11 @@ namespace TestCaseTracking {
bool SectionTracker::isComplete() const { bool SectionTracker::isComplete() const {
bool complete = true; bool complete = true;
if ((m_filters.empty() || m_filters[0] == "") || if ((m_filters.empty() || m_filters[0] == "")
std::find(m_filters.begin(), m_filters.end(), || std::find(m_filters.begin(), m_filters.end(), m_trimmed_name) != m_filters.end()) {
m_nameAndLocation.name) != m_filters.end())
complete = TrackerBase::isComplete(); complete = TrackerBase::isComplete();
}
return complete; return complete;
} }
bool SectionTracker::isSectionTracker() const { return true; } bool SectionTracker::isSectionTracker() const { return true; }
@ -217,12 +218,13 @@ namespace TestCaseTracking {
} }
void SectionTracker::tryOpen() { void SectionTracker::tryOpen() {
if( !isComplete() && (m_filters.empty() || m_filters[0].empty() || m_filters[0] == m_nameAndLocation.name ) ) if( !isComplete() )
open(); open();
} }
void SectionTracker::addInitialFilters( std::vector<std::string> const& filters ) { void SectionTracker::addInitialFilters( std::vector<std::string> const& filters ) {
if( !filters.empty() ) { if( !filters.empty() ) {
m_filters.reserve( m_filters.size() + filters.size() + 2 );
m_filters.push_back(""); // Root - should never be consulted m_filters.push_back(""); // Root - should never be consulted
m_filters.push_back(""); // Test Case - not a section filter m_filters.push_back(""); // Test Case - not a section filter
m_filters.insert( m_filters.end(), filters.begin(), filters.end() ); m_filters.insert( m_filters.end(), filters.begin(), filters.end() );
@ -230,7 +232,7 @@ namespace TestCaseTracking {
} }
void SectionTracker::addNextFilters( std::vector<std::string> const& filters ) { void SectionTracker::addNextFilters( std::vector<std::string> const& filters ) {
if( filters.size() > 1 ) if( filters.size() > 1 )
m_filters.insert( m_filters.end(), ++filters.begin(), filters.end() ); m_filters.insert( m_filters.end(), filters.begin()+1, filters.end() );
} }
} // namespace TestCaseTracking } // namespace TestCaseTracking

View File

@ -133,6 +133,7 @@ namespace TestCaseTracking {
class SectionTracker : public TrackerBase { class SectionTracker : public TrackerBase {
std::vector<std::string> m_filters; std::vector<std::string> m_filters;
std::string m_trimmed_name;
public: public:
SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent ); SectionTracker( NameAndLocation const& nameAndLocation, TrackerContext& ctx, ITracker* parent );

View File

@ -33,7 +33,7 @@ namespace Catch {
{} {}
bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const { bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const {
return m_wildcardPattern.matches( toLower( testCase.name ) ); return m_wildcardPattern.matches( testCase.name );
} }

View File

@ -9,14 +9,12 @@
#include "catch_enforce.h" #include "catch_enforce.h"
#include "catch_string_manip.h" #include "catch_string_manip.h"
#include <sstream>
namespace Catch { namespace Catch {
WildcardPattern::WildcardPattern( std::string const& pattern, WildcardPattern::WildcardPattern( std::string const& pattern,
CaseSensitive::Choice caseSensitivity ) CaseSensitive::Choice caseSensitivity )
: m_caseSensitivity( caseSensitivity ), : m_caseSensitivity( caseSensitivity ),
m_pattern( adjustCase( pattern ) ) m_pattern( normaliseString( pattern ) )
{ {
if( startsWith( m_pattern, '*' ) ) { if( startsWith( m_pattern, '*' ) ) {
m_pattern = m_pattern.substr( 1 ); m_pattern = m_pattern.substr( 1 );
@ -31,19 +29,19 @@ namespace Catch {
bool WildcardPattern::matches( std::string const& str ) const { bool WildcardPattern::matches( std::string const& str ) const {
switch( m_wildcard ) { switch( m_wildcard ) {
case NoWildcard: case NoWildcard:
return m_pattern == adjustCase( str ); return m_pattern == normaliseString( str );
case WildcardAtStart: case WildcardAtStart:
return endsWith( adjustCase( str ), m_pattern ); return endsWith( normaliseString( str ), m_pattern );
case WildcardAtEnd: case WildcardAtEnd:
return startsWith( adjustCase( str ), m_pattern ); return startsWith( normaliseString( str ), m_pattern );
case WildcardAtBothEnds: case WildcardAtBothEnds:
return contains( adjustCase( str ), m_pattern ); return contains( normaliseString( str ), m_pattern );
default: default:
CATCH_INTERNAL_ERROR( "Unknown enum" ); CATCH_INTERNAL_ERROR( "Unknown enum" );
} }
} }
std::string WildcardPattern::adjustCase( std::string const& str ) const { std::string WildcardPattern::normaliseString( std::string const& str ) const {
return m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str; return trim( m_caseSensitivity == CaseSensitive::No ? toLower( str ) : str );
} }
} }

View File

@ -28,7 +28,7 @@ namespace Catch
virtual bool matches( std::string const& str ) const; virtual bool matches( std::string const& str ) const;
private: private:
std::string adjustCase( std::string const& str ) const; std::string normaliseString( std::string const& str ) const;
CaseSensitive::Choice m_caseSensitivity; CaseSensitive::Choice m_caseSensitivity;
WildcardPosition m_wildcard = NoWildcard; WildcardPosition m_wildcard = NoWildcard;
std::string m_pattern; std::string m_pattern;

View File

@ -866,6 +866,16 @@ CmdLine.tests.cpp:<line number>: passed: spec.matches( tcA ) == false for: false
CmdLine.tests.cpp:<line number>: passed: spec.matches( tcB ) == false for: false == false CmdLine.tests.cpp:<line number>: passed: spec.matches( tcB ) == false for: false == false
CmdLine.tests.cpp:<line number>: passed: spec.matches( tcC ) == false for: false == false CmdLine.tests.cpp:<line number>: passed: spec.matches( tcC ) == false for: false == false
CmdLine.tests.cpp:<line number>: passed: spec.matches( tcD ) == true for: true == true CmdLine.tests.cpp:<line number>: passed: spec.matches( tcD ) == true for: true == true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( " aardvark " ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( " aardvark" ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( " aardvark " ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( "aardvark " ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( "aardvark" ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( " aardvark " ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( " aardvark" ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( " aardvark " ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( "aardvark " ) ) for: true
CmdLine.tests.cpp:<line number>: passed: spec.matches( fakeTestCase( "aardvark" ) ) for: true
Condition.tests.cpp:<line number>: passed: p == 0 for: 0 == 0 Condition.tests.cpp:<line number>: passed: p == 0 for: 0 == 0
Condition.tests.cpp:<line number>: passed: p == pNULL for: 0 == 0 Condition.tests.cpp:<line number>: passed: p == pNULL for: 0 == 0
Condition.tests.cpp:<line number>: passed: p != 0 for: 0x<hex digits> != 0 Condition.tests.cpp:<line number>: passed: p != 0 for: 0x<hex digits> != 0

View File

@ -1381,5 +1381,5 @@ due to unexpected exception with message:
=============================================================================== ===============================================================================
test cases: 300 | 226 passed | 70 failed | 4 failed as expected test cases: 300 | 226 passed | 70 failed | 4 failed as expected
assertions: 1558 | 1406 passed | 131 failed | 21 failed as expected assertions: 1568 | 1416 passed | 131 failed | 21 failed as expected

View File

@ -6366,6 +6366,70 @@ CmdLine.tests.cpp:<line number>: PASSED:
with expansion: with expansion:
true == true true == true
-------------------------------------------------------------------------------
Parse test names and tags
Leading and trailing spaces in test spec
-------------------------------------------------------------------------------
CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( " aardvark" ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( "aardvark " ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( "aardvark" ) ) )
with expansion:
true
-------------------------------------------------------------------------------
Parse test names and tags
Leading and trailing spaces in test name
-------------------------------------------------------------------------------
CmdLine.tests.cpp:<line number>
...............................................................................
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( " aardvark" ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( "aardvark " ) ) )
with expansion:
true
CmdLine.tests.cpp:<line number>: PASSED:
CHECK( spec.matches( fakeTestCase( "aardvark" ) ) )
with expansion:
true
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Pointers can be compared to null Pointers can be compared to null
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -12457,5 +12521,5 @@ Misc.tests.cpp:<line number>: PASSED:
=============================================================================== ===============================================================================
test cases: 300 | 210 passed | 86 failed | 4 failed as expected test cases: 300 | 210 passed | 86 failed | 4 failed as expected
assertions: 1575 | 1406 passed | 148 failed | 21 failed as expected assertions: 1585 | 1416 passed | 148 failed | 21 failed as expected

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuitesloose text artifact <testsuitesloose text artifact
> >
<testsuite name="<exe-name>" errors="17" failures="132" tests="1576" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="17" failures="132" tests="1586" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<properties> <properties>
<property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/> <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/>
<property name="random-seed" value="1"/> <property name="random-seed" value="1"/>
@ -620,6 +620,8 @@ Message.tests.cpp:<line number>
<testcase classname="<exe-name>.global" name="Parse test names and tags/empty tag" time="{duration}"/> <testcase classname="<exe-name>.global" name="Parse test names and tags/empty tag" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Parse test names and tags/empty quoted name" time="{duration}"/> <testcase classname="<exe-name>.global" name="Parse test names and tags/empty quoted name" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Parse test names and tags/quoted string followed by tag exclusion" time="{duration}"/> <testcase classname="<exe-name>.global" name="Parse test names and tags/quoted string followed by tag exclusion" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Parse test names and tags/Leading and trailing spaces in test spec" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Parse test names and tags/Leading and trailing spaces in test name" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Pointers can be compared to null" time="{duration}"/> <testcase classname="<exe-name>.global" name="Pointers can be compared to null" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Precision of floating point stringification can be set/Floats" time="{duration}"/> <testcase classname="<exe-name>.global" name="Precision of floating point stringification can be set/Floats" time="{duration}"/>
<testcase classname="<exe-name>.global" name="Precision of floating point stringification can be set/Double" time="{duration}"/> <testcase classname="<exe-name>.global" name="Precision of floating point stringification can be set/Double" time="{duration}"/>

View File

@ -7901,6 +7901,92 @@ Nor would this
</Expression> </Expression>
<OverallResults successes="5" failures="0" expectedFailures="0"/> <OverallResults successes="5" failures="0" expectedFailures="0"/>
</Section> </Section>
<Section name="Leading and trailing spaces in test spec" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( " aardvark " ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( " aardvark" ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( " aardvark " ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( "aardvark " ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( "aardvark" ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<OverallResults successes="5" failures="0" expectedFailures="0"/>
</Section>
<Section name="Leading and trailing spaces in test name" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( " aardvark " ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( " aardvark" ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( " aardvark " ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( "aardvark " ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/IntrospectiveTests/CmdLine.tests.cpp" >
<Original>
spec.matches( fakeTestCase( "aardvark" ) )
</Original>
<Expanded>
true
</Expanded>
</Expression>
<OverallResults successes="5" failures="0" expectedFailures="0"/>
</Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="Pointers can be compared to null" filename="projects/<exe-name>/UsageTests/Condition.tests.cpp" > <TestCase name="Pointers can be compared to null" filename="projects/<exe-name>/UsageTests/Condition.tests.cpp" >
@ -14826,7 +14912,7 @@ loose text artifact
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="1406" failures="149" expectedFailures="21"/> <OverallResults successes="1416" failures="149" expectedFailures="21"/>
</Group> </Group>
<OverallResults successes="1406" failures="148" expectedFailures="21"/> <OverallResults successes="1416" failures="148" expectedFailures="21"/>
</Catch> </Catch>

View File

@ -262,8 +262,25 @@ TEST_CASE( "Parse test names and tags" ) {
CHECK( spec.matches( tcC ) == false ); CHECK( spec.matches( tcC ) == false );
CHECK( spec.matches( tcD ) == true ); CHECK( spec.matches( tcD ) == true );
} }
SECTION( "Leading and trailing spaces in test spec" ) {
TestSpec spec = parseTestSpec( "\" aardvark \"" );
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) );
CHECK( spec.matches( fakeTestCase( " aardvark" ) ) );
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) );
CHECK( spec.matches( fakeTestCase( "aardvark " ) ) );
CHECK( spec.matches( fakeTestCase( "aardvark" ) ) );
} }
SECTION( "Leading and trailing spaces in test name" ) {
TestSpec spec = parseTestSpec( "aardvark" );
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) );
CHECK( spec.matches( fakeTestCase( " aardvark" ) ) );
CHECK( spec.matches( fakeTestCase( " aardvark " ) ) );
CHECK( spec.matches( fakeTestCase( "aardvark " ) ) );
CHECK( spec.matches( fakeTestCase( "aardvark" ) ) );
}
}
TEST_CASE( "Process can be configured on command line", "[config][command-line]" ) { TEST_CASE( "Process can be configured on command line", "[config][command-line]" ) {