liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
 
#ifndef BOOST_BEAST_UNIT_TEST_MATCH_HPP
#define BOOST_BEAST_UNIT_TEST_MATCH_HPP
 
#include <boost/beast/_experimental/unit_test/suite_info.hpp>
#include <string>
 
namespace boost {
namespace beast {
namespace unit_test {
 
// Predicate for implementing matches
class selector
{
public:
    enum mode_t
    {
        // Run all tests except manual ones
        all,
 
        // Run tests that match in any field
        automatch,
 
        // Match on suite
        suite,
 
        // Match on library
        library,
 
        // Match on module (used internally)
        module,
 
        // Match nothing (used internally)
        none
    };
 
private:
    mode_t mode_;
    std::string pat_;
    std::string library_;
 
public:
    template<class = void>
    explicit
    selector(mode_t mode, std::string const& pattern = "");
 
    template<class = void>
    bool
    operator()(suite_info const& s);
};
 
//------------------------------------------------------------------------------
 
template<class>
selector::selector(mode_t mode, std::string const& pattern)
    : mode_(mode)
    , pat_(pattern)
{
    if(mode_ == automatch && pattern.empty())
        mode_ = all;
}
 
template<class>
bool
selector::operator()(suite_info const& s)
{
    switch(mode_)
    {
    case automatch:
        // suite or full name
        if(s.name() == pat_ || s.full_name() == pat_)
        {
            mode_ = none;
            return true;
        }
 
        // check module
        if(pat_ == s.module())
        {
            mode_ = module;
            library_ = s.library();
            return ! s.manual();
        }
 
        // check library
        if(pat_ == s.library())
        {
            mode_ = library;
            return ! s.manual();
        }
 
        return false;
 
    case suite:
        return pat_ == s.name();
 
    case module:
        return pat_ == s.module() && ! s.manual();
 
    case library:
        return pat_ == s.library() && ! s.manual();
 
    case none:
        return false;
 
    case all:
    default:
        // fall through
        break;
    };
 
    return ! s.manual();
}
 
//------------------------------------------------------------------------------
 
// Utility functions for producing predicates to select suites.
 
/** Returns a predicate that implements a smart matching rule.
    The predicate checks the suite, module, and library fields of the
    suite_info in that order. When it finds a match, it changes modes
    depending on what was found:
 
        If a suite is matched first, then only the suite is selected. The
        suite may be marked manual.
 
        If a module is matched first, then only suites from that module
        and library not marked manual are selected from then on.
 
        If a library is matched first, then only suites from that library
        not marked manual are selected from then on.
 
*/
inline
selector
match_auto(std::string const& name)
{
    return selector(selector::automatch, name);
}
 
/** Return a predicate that matches all suites not marked manual. */
inline
selector
match_all()
{
    return selector(selector::all);
}
 
/** Returns a predicate that matches a specific suite. */
inline
selector
match_suite(std::string const& name)
{
    return selector(selector::suite, name);
}
 
/** Returns a predicate that matches all suites in a library. */
inline
selector
match_library(std::string const& name)
{
    return selector(selector::library, name);
}
 
} // unit_test
} // beast
} // boost
 
#endif