xref: /freebsd-src/contrib/kyua/engine/filters.hpp (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
1*b0d29bc4SBrooks Davis // Copyright 2011 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis //   notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis //   documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis //   may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis //   without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis 
29*b0d29bc4SBrooks Davis /// \file engine/filters.hpp
30*b0d29bc4SBrooks Davis /// Representation and manipulation of filters for test cases.
31*b0d29bc4SBrooks Davis ///
32*b0d29bc4SBrooks Davis /// All the filter classes in this module are supposed to be purely functional:
33*b0d29bc4SBrooks Davis /// they are mere filters that decide whether they match or not the input data
34*b0d29bc4SBrooks Davis /// fed to them.  User-interface filter manipulation must go somewhere else.
35*b0d29bc4SBrooks Davis 
36*b0d29bc4SBrooks Davis #if !defined(ENGINE_FILTERS_HPP)
37*b0d29bc4SBrooks Davis #define ENGINE_FILTERS_HPP
38*b0d29bc4SBrooks Davis 
39*b0d29bc4SBrooks Davis #include "engine/filters_fwd.hpp"
40*b0d29bc4SBrooks Davis 
41*b0d29bc4SBrooks Davis #include <ostream>
42*b0d29bc4SBrooks Davis #include <string>
43*b0d29bc4SBrooks Davis #include <set>
44*b0d29bc4SBrooks Davis #include <utility>
45*b0d29bc4SBrooks Davis 
46*b0d29bc4SBrooks Davis #include "utils/fs/path.hpp"
47*b0d29bc4SBrooks Davis #include "utils/optional.ipp"
48*b0d29bc4SBrooks Davis 
49*b0d29bc4SBrooks Davis 
50*b0d29bc4SBrooks Davis namespace engine {
51*b0d29bc4SBrooks Davis 
52*b0d29bc4SBrooks Davis 
53*b0d29bc4SBrooks Davis /// Filter for test cases.
54*b0d29bc4SBrooks Davis ///
55*b0d29bc4SBrooks Davis /// A filter is one of: the name of a directory containing test cases, the name
56*b0d29bc4SBrooks Davis /// of a test program, or the name of a test program plus the name of a test
57*b0d29bc4SBrooks Davis /// case.
58*b0d29bc4SBrooks Davis class test_filter {
59*b0d29bc4SBrooks Davis public:
60*b0d29bc4SBrooks Davis     /// The name of the test program or subdirectory to match.
61*b0d29bc4SBrooks Davis     utils::fs::path test_program;
62*b0d29bc4SBrooks Davis 
63*b0d29bc4SBrooks Davis     /// The name of the test case to match; if empty, represents any test case.
64*b0d29bc4SBrooks Davis     std::string test_case;
65*b0d29bc4SBrooks Davis 
66*b0d29bc4SBrooks Davis     test_filter(const utils::fs::path&, const std::string&);
67*b0d29bc4SBrooks Davis     static test_filter parse(const std::string&);
68*b0d29bc4SBrooks Davis 
69*b0d29bc4SBrooks Davis     std::string str(void) const;
70*b0d29bc4SBrooks Davis 
71*b0d29bc4SBrooks Davis     bool contains(const test_filter&) const;
72*b0d29bc4SBrooks Davis     bool matches_test_program(const utils::fs::path&) const;
73*b0d29bc4SBrooks Davis     bool matches_test_case(const utils::fs::path&, const std::string&) const;
74*b0d29bc4SBrooks Davis 
75*b0d29bc4SBrooks Davis     bool operator<(const test_filter&) const;
76*b0d29bc4SBrooks Davis     bool operator==(const test_filter&) const;
77*b0d29bc4SBrooks Davis     bool operator!=(const test_filter&) const;
78*b0d29bc4SBrooks Davis };
79*b0d29bc4SBrooks Davis 
80*b0d29bc4SBrooks Davis 
81*b0d29bc4SBrooks Davis std::ostream& operator<<(std::ostream&, const test_filter&);
82*b0d29bc4SBrooks Davis 
83*b0d29bc4SBrooks Davis 
84*b0d29bc4SBrooks Davis /// Collection of user-provided filters to select test cases.
85*b0d29bc4SBrooks Davis ///
86*b0d29bc4SBrooks Davis /// An empty collection of filters is considered to match any test case.
87*b0d29bc4SBrooks Davis ///
88*b0d29bc4SBrooks Davis /// In general, the filters maintained by this class should be disjoint.  If
89*b0d29bc4SBrooks Davis /// they are not, some filters may never have a chance to do a match, which is
90*b0d29bc4SBrooks Davis /// most likely the fault of the user.  To check for non-disjoint filters before
91*b0d29bc4SBrooks Davis /// constructing this object, use check_disjoint_filters.
92*b0d29bc4SBrooks Davis class test_filters {
93*b0d29bc4SBrooks Davis     /// The user-provided filters.
94*b0d29bc4SBrooks Davis     std::set< test_filter > _filters;
95*b0d29bc4SBrooks Davis 
96*b0d29bc4SBrooks Davis public:
97*b0d29bc4SBrooks Davis     explicit test_filters(const std::set< test_filter >&);
98*b0d29bc4SBrooks Davis 
99*b0d29bc4SBrooks Davis     /// Return type of match_test_case.  Indicates whether the filters have
100*b0d29bc4SBrooks Davis     /// matched a particular test case and, if they have, which filter did the
101*b0d29bc4SBrooks Davis     /// match (if any).
102*b0d29bc4SBrooks Davis     typedef std::pair< bool, utils::optional< test_filter > > match;
103*b0d29bc4SBrooks Davis 
104*b0d29bc4SBrooks Davis     bool match_test_program(const utils::fs::path&) const;
105*b0d29bc4SBrooks Davis     match match_test_case(const utils::fs::path&, const std::string&) const;
106*b0d29bc4SBrooks Davis 
107*b0d29bc4SBrooks Davis     std::set< test_filter > difference(const std::set< test_filter >&) const;
108*b0d29bc4SBrooks Davis };
109*b0d29bc4SBrooks Davis 
110*b0d29bc4SBrooks Davis 
111*b0d29bc4SBrooks Davis void check_disjoint_filters(const std::set< test_filter >&);
112*b0d29bc4SBrooks Davis 
113*b0d29bc4SBrooks Davis 
114*b0d29bc4SBrooks Davis /// Tracks state of the filters that have matched tests during execution.
115*b0d29bc4SBrooks Davis class filters_state {
116*b0d29bc4SBrooks Davis     /// The user-provided filters.
117*b0d29bc4SBrooks Davis     test_filters _filters;
118*b0d29bc4SBrooks Davis 
119*b0d29bc4SBrooks Davis     /// Collection of filters that have matched test cases so far.
120*b0d29bc4SBrooks Davis     std::set< test_filter > _used_filters;
121*b0d29bc4SBrooks Davis 
122*b0d29bc4SBrooks Davis public:
123*b0d29bc4SBrooks Davis     explicit filters_state(const std::set< test_filter >&);
124*b0d29bc4SBrooks Davis 
125*b0d29bc4SBrooks Davis     bool match_test_program(const utils::fs::path&) const;
126*b0d29bc4SBrooks Davis     bool match_test_case(const utils::fs::path&, const std::string&);
127*b0d29bc4SBrooks Davis 
128*b0d29bc4SBrooks Davis     std::set< test_filter > unused(void) const;
129*b0d29bc4SBrooks Davis };
130*b0d29bc4SBrooks Davis 
131*b0d29bc4SBrooks Davis 
132*b0d29bc4SBrooks Davis }  // namespace engine
133*b0d29bc4SBrooks Davis 
134*b0d29bc4SBrooks Davis #endif  // !defined(ENGINE_FILTERS_HPP)
135