catch_test_spec.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "catch_test_spec.h"
  8. #include "catch_string_manip.h"
  9. #include "catch_interfaces_config.h"
  10. #include <algorithm>
  11. #include <string>
  12. #include <vector>
  13. #include <memory>
  14. namespace Catch {
  15. TestSpec::Pattern::Pattern( std::string const& name )
  16. : m_name( name )
  17. {}
  18. TestSpec::Pattern::~Pattern() = default;
  19. std::string const& TestSpec::Pattern::name() const {
  20. return m_name;
  21. }
  22. TestSpec::NamePattern::NamePattern( std::string const& name, std::string const& filterString )
  23. : Pattern( filterString )
  24. , m_wildcardPattern( toLower( name ), CaseSensitive::No )
  25. {}
  26. bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const {
  27. return m_wildcardPattern.matches( testCase.name );
  28. }
  29. TestSpec::TagPattern::TagPattern( std::string const& tag, std::string const& filterString )
  30. : Pattern( filterString )
  31. , m_tag( toLower( tag ) )
  32. {}
  33. bool TestSpec::TagPattern::matches( TestCaseInfo const& testCase ) const {
  34. return std::find(begin(testCase.lcaseTags),
  35. end(testCase.lcaseTags),
  36. m_tag) != end(testCase.lcaseTags);
  37. }
  38. TestSpec::ExcludedPattern::ExcludedPattern( PatternPtr const& underlyingPattern )
  39. : Pattern( underlyingPattern->name() )
  40. , m_underlyingPattern( underlyingPattern )
  41. {}
  42. bool TestSpec::ExcludedPattern::matches( TestCaseInfo const& testCase ) const {
  43. return !m_underlyingPattern->matches( testCase );
  44. }
  45. bool TestSpec::Filter::matches( TestCaseInfo const& testCase ) const {
  46. return std::all_of( m_patterns.begin(), m_patterns.end(), [&]( PatternPtr const& p ){ return p->matches( testCase ); } );
  47. }
  48. std::string TestSpec::Filter::name() const {
  49. std::string name;
  50. for( auto const& p : m_patterns )
  51. name += p->name();
  52. return name;
  53. }
  54. bool TestSpec::hasFilters() const {
  55. return !m_filters.empty();
  56. }
  57. bool TestSpec::matches( TestCaseInfo const& testCase ) const {
  58. return std::any_of( m_filters.begin(), m_filters.end(), [&]( Filter const& f ){ return f.matches( testCase ); } );
  59. }
  60. TestSpec::Matches TestSpec::matchesByFilter( std::vector<TestCase> const& testCases, IConfig const& config ) const
  61. {
  62. Matches matches( m_filters.size() );
  63. std::transform( m_filters.begin(), m_filters.end(), matches.begin(), [&]( Filter const& filter ){
  64. std::vector<TestCase const*> currentMatches;
  65. for( auto const& test : testCases )
  66. if( isThrowSafe( test, config ) && filter.matches( test ) )
  67. currentMatches.emplace_back( &test );
  68. return FilterMatch{ filter.name(), currentMatches };
  69. } );
  70. return matches;
  71. }
  72. const TestSpec::vectorStrings& TestSpec::getInvalidArgs() const{
  73. return (m_invalidArgs);
  74. }
  75. }