catch_test_spec.hpp
1 2 // Copyright Catch2 Authors 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE.txt or copy at 5 // https://www.boost.org/LICENSE_1_0.txt) 6 7 // SPDX-License-Identifier: BSL-1.0 8 #ifndef CATCH_TEST_SPEC_HPP_INCLUDED 9 #define CATCH_TEST_SPEC_HPP_INCLUDED 10 11 #ifdef __clang__ 12 #pragma clang diagnostic push 13 #pragma clang diagnostic ignored "-Wpadded" 14 #endif 15 16 #include <catch2/internal/catch_unique_ptr.hpp> 17 #include <catch2/internal/catch_wildcard_pattern.hpp> 18 19 #include <iosfwd> 20 #include <string> 21 #include <vector> 22 23 namespace Catch { 24 25 class IConfig; 26 struct TestCaseInfo; 27 class TestCaseHandle; 28 29 class TestSpec { 30 31 class Pattern { 32 public: 33 explicit Pattern( std::string const& name ); 34 virtual ~Pattern(); 35 virtual bool matches( TestCaseInfo const& testCase ) const = 0; 36 std::string const& name() const; 37 private: 38 virtual void serializeTo( std::ostream& out ) const = 0; 39 // Writes string that would be reparsed into the pattern 40 friend std::ostream& operator<<(std::ostream& out, 41 Pattern const& pattern) { 42 pattern.serializeTo( out ); 43 return out; 44 } 45 46 std::string const m_name; 47 }; 48 49 class NamePattern : public Pattern { 50 public: 51 explicit NamePattern( std::string const& name, std::string const& filterString ); 52 bool matches( TestCaseInfo const& testCase ) const override; 53 private: 54 void serializeTo( std::ostream& out ) const override; 55 56 WildcardPattern m_wildcardPattern; 57 }; 58 59 class TagPattern : public Pattern { 60 public: 61 explicit TagPattern( std::string const& tag, std::string const& filterString ); 62 bool matches( TestCaseInfo const& testCase ) const override; 63 private: 64 void serializeTo( std::ostream& out ) const override; 65 66 std::string m_tag; 67 }; 68 69 struct Filter { 70 std::vector<Detail::unique_ptr<Pattern>> m_required; 71 std::vector<Detail::unique_ptr<Pattern>> m_forbidden; 72 73 //! Serializes this filter into a string that would be parsed into 74 //! an equivalent filter 75 void serializeTo( std::ostream& out ) const; 76 friend std::ostream& operator<<(std::ostream& out, Filter const& f) { 77 f.serializeTo( out ); 78 return out; 79 } 80 81 bool matches( TestCaseInfo const& testCase ) const; 82 }; 83 84 static std::string extractFilterName( Filter const& filter ); 85 86 public: 87 struct FilterMatch { 88 std::string name; 89 std::vector<TestCaseHandle const*> tests; 90 }; 91 using Matches = std::vector<FilterMatch>; 92 using vectorStrings = std::vector<std::string>; 93 94 bool hasFilters() const; 95 bool matches( TestCaseInfo const& testCase ) const; 96 Matches matchesByFilter( std::vector<TestCaseHandle> const& testCases, IConfig const& config ) const; 97 const vectorStrings & getInvalidSpecs() const; 98 99 private: 100 std::vector<Filter> m_filters; 101 std::vector<std::string> m_invalidSpecs; 102 103 friend class TestSpecParser; 104 //! Serializes this test spec into a string that would be parsed into 105 //! equivalent test spec 106 void serializeTo( std::ostream& out ) const; 107 friend std::ostream& operator<<(std::ostream& out, 108 TestSpec const& spec) { 109 spec.serializeTo( out ); 110 return out; 111 } 112 }; 113 } 114 115 #ifdef __clang__ 116 #pragma clang diagnostic pop 117 #endif 118 119 #endif // CATCH_TEST_SPEC_HPP_INCLUDED