catch_test_spec.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright Catch2 Authors
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // https://www.boost.org/LICENSE_1_0.txt)
  5. // SPDX-License-Identifier: BSL-1.0
  6. #include <catch2/catch_test_spec.hpp>
  7. #include <catch2/internal/catch_string_manip.hpp>
  8. #include <catch2/catch_test_case_info.hpp>
  9. #include <algorithm>
  10. #include <string>
  11. #include <vector>
  12. namespace Catch {
  13. TestSpec::Pattern::Pattern( std::string const& name )
  14. : m_name( name )
  15. {}
  16. TestSpec::Pattern::~Pattern() = default;
  17. std::string const& TestSpec::Pattern::name() const {
  18. return m_name;
  19. }
  20. TestSpec::NamePattern::NamePattern( std::string const& name, std::string const& filterString )
  21. : Pattern( filterString )
  22. , m_wildcardPattern( toLower( name ), CaseSensitive::No )
  23. {}
  24. bool TestSpec::NamePattern::matches( TestCaseInfo const& testCase ) const {
  25. return m_wildcardPattern.matches( testCase.name );
  26. }
  27. TestSpec::TagPattern::TagPattern( std::string const& tag, std::string const& filterString )
  28. : Pattern( filterString )
  29. , m_tag( tag )
  30. {}
  31. bool TestSpec::TagPattern::matches( TestCaseInfo const& testCase ) const {
  32. return std::find( begin( testCase.tags ),
  33. end( testCase.tags ),
  34. Tag( m_tag ) ) != end( testCase.tags );
  35. }
  36. bool TestSpec::Filter::matches( TestCaseInfo const& testCase ) const {
  37. bool should_use = !testCase.isHidden();
  38. for (auto const& pattern : m_required) {
  39. should_use = true;
  40. if (!pattern->matches(testCase)) {
  41. return false;
  42. }
  43. }
  44. for (auto const& pattern : m_forbidden) {
  45. if (pattern->matches(testCase)) {
  46. return false;
  47. }
  48. }
  49. return should_use;
  50. }
  51. std::string TestSpec::Filter::name() const {
  52. std::string name;
  53. for (auto const& p : m_required) {
  54. name += p->name();
  55. }
  56. for (auto const& p : m_forbidden) {
  57. name += p->name();
  58. }
  59. return name;
  60. }
  61. bool TestSpec::hasFilters() const {
  62. return !m_filters.empty();
  63. }
  64. bool TestSpec::matches( TestCaseInfo const& testCase ) const {
  65. return std::any_of( m_filters.begin(), m_filters.end(), [&]( Filter const& f ){ return f.matches( testCase ); } );
  66. }
  67. TestSpec::Matches TestSpec::matchesByFilter( std::vector<TestCaseHandle> const& testCases, IConfig const& config ) const
  68. {
  69. Matches matches( m_filters.size() );
  70. std::transform( m_filters.begin(), m_filters.end(), matches.begin(), [&]( Filter const& filter ){
  71. std::vector<TestCaseHandle const*> currentMatches;
  72. for( auto const& test : testCases )
  73. if( isThrowSafe( test, config ) && filter.matches( test.getTestCaseInfo() ) )
  74. currentMatches.emplace_back( &test );
  75. return FilterMatch{ filter.name(), currentMatches };
  76. } );
  77. return matches;
  78. }
  79. const TestSpec::vectorStrings& TestSpec::getInvalidSpecs() const {
  80. return m_invalidSpecs;
  81. }
  82. }