1 /* 2 * Created by Martin on 19/07/2017. 3 * 4 * Distributed under the Boost Software License, Version 1.0. (See accompanying 5 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 */ 7 8 #include "catch_test_spec.h" 9 #include "catch_string_manip.h" 10 #include "catch_interfaces_config.h" 11 12 #include <algorithm> 13 #include <string> 14 #include <vector> 15 #include <memory> 16 17 namespace Catch { 18 Pattern(std::string const & name)19 TestSpec::Pattern::Pattern( std::string const& name ) 20 : m_name( name ) 21 {} 22 23 TestSpec::Pattern::~Pattern() = default; 24 name() const25 std::string const& TestSpec::Pattern::name() const { 26 return m_name; 27 } 28 29 NamePattern(std::string const & name,std::string const & filterString)30 TestSpec::NamePattern::NamePattern( std::string const& name, std::string const& filterString ) 31 : Pattern( filterString ) 32 , m_wildcardPattern( toLower( name ), CaseSensitive::No ) 33 {} 34 matches(TestCaseInfo const & testCase) const35 bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const { 36 return m_wildcardPattern.matches( testCase.name ); 37 } 38 39 TagPattern(std::string const & tag,std::string const & filterString)40 TestSpec::TagPattern::TagPattern( std::string const& tag, std::string const& filterString ) 41 : Pattern( filterString ) 42 , m_tag( toLower( tag ) ) 43 {} 44 matches(TestCaseInfo const & testCase) const45 bool TestSpec::TagPattern::matches( TestCaseInfo const& testCase ) const { 46 return std::find(begin(testCase.lcaseTags), 47 end(testCase.lcaseTags), 48 m_tag) != end(testCase.lcaseTags); 49 } 50 51 ExcludedPattern(PatternPtr const & underlyingPattern)52 TestSpec::ExcludedPattern::ExcludedPattern( PatternPtr const& underlyingPattern ) 53 : Pattern( underlyingPattern->name() ) 54 , m_underlyingPattern( underlyingPattern ) 55 {} 56 matches(TestCaseInfo const & testCase) const57 bool TestSpec::ExcludedPattern::matches( TestCaseInfo const& testCase ) const { 58 return !m_underlyingPattern->matches( testCase ); 59 } 60 61 matches(TestCaseInfo const & testCase) const62 bool TestSpec::Filter::matches( TestCaseInfo const& testCase ) const { 63 return std::all_of( m_patterns.begin(), m_patterns.end(), [&]( PatternPtr const& p ){ return p->matches( testCase ); } ); 64 } 65 name() const66 std::string TestSpec::Filter::name() const { 67 std::string name; 68 for( auto const& p : m_patterns ) 69 name += p->name(); 70 return name; 71 } 72 73 hasFilters() const74 bool TestSpec::hasFilters() const { 75 return !m_filters.empty(); 76 } 77 matches(TestCaseInfo const & testCase) const78 bool TestSpec::matches( TestCaseInfo const& testCase ) const { 79 return std::any_of( m_filters.begin(), m_filters.end(), [&]( Filter const& f ){ return f.matches( testCase ); } ); 80 } 81 matchesByFilter(std::vector<TestCase> const & testCases,IConfig const & config) const82 TestSpec::Matches TestSpec::matchesByFilter( std::vector<TestCase> const& testCases, IConfig const& config ) const 83 { 84 Matches matches( m_filters.size() ); 85 std::transform( m_filters.begin(), m_filters.end(), matches.begin(), [&]( Filter const& filter ){ 86 std::vector<TestCase const*> currentMatches; 87 for( auto const& test : testCases ) 88 if( isThrowSafe( test, config ) && filter.matches( test ) ) 89 currentMatches.emplace_back( &test ); 90 return FilterMatch{ filter.name(), currentMatches }; 91 } ); 92 return matches; 93 } 94 getInvalidArgs() const95 const TestSpec::vectorStrings& TestSpec::getInvalidArgs() const{ 96 return (m_invalidArgs); 97 } 98 99 } 100