/
catch_matchers_quantifiers.hpp
165 lines (140 loc) · 5.37 KB
/
catch_matchers_quantifiers.hpp
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
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_MATCHERS_QUANTIFIERS_HPP_INCLUDED
#define CATCH_MATCHERS_QUANTIFIERS_HPP_INCLUDED
#include <catch2/matchers/catch_matchers_templated.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
namespace Catch {
namespace Matchers {
// Matcher for checking that all elements in range matches a given matcher.
template <typename Matcher>
class AllMatchMatcher final : public MatcherGenericBase {
Matcher m_matcher;
public:
AllMatchMatcher(Matcher matcher):
m_matcher(CATCH_MOVE(matcher))
{}
std::string describe() const override {
return "all match " + m_matcher.describe();
}
template <typename RangeLike>
bool match(RangeLike&& rng) const {
for (auto&& elem : rng) {
if (!m_matcher.match(elem)) {
return false;
}
}
return true;
}
};
// Matcher for checking that no element in range matches a given matcher.
template <typename Matcher>
class NoneMatchMatcher final : public MatcherGenericBase {
Matcher m_matcher;
public:
NoneMatchMatcher(Matcher matcher):
m_matcher(CATCH_MOVE(matcher))
{}
std::string describe() const override {
return "none match " + m_matcher.describe();
}
template <typename RangeLike>
bool match(RangeLike&& rng) const {
for (auto&& elem : rng) {
if (m_matcher.match(elem)) {
return false;
}
}
return true;
}
};
// Matcher for checking that at least one element in range matches a given matcher.
template <typename Matcher>
class AnyMatchMatcher final : public MatcherGenericBase {
Matcher m_matcher;
public:
AnyMatchMatcher(Matcher matcher):
m_matcher(CATCH_MOVE(matcher))
{}
std::string describe() const override {
return "any match " + m_matcher.describe();
}
template <typename RangeLike>
bool match(RangeLike&& rng) const {
for (auto&& elem : rng) {
if (m_matcher.match(elem)) {
return true;
}
}
return false;
}
};
// Matcher for checking that all elements in range are true.
class AllTrueMatcher final : public MatcherGenericBase {
public:
std::string describe() const override;
template <typename RangeLike>
bool match(RangeLike&& rng) const {
for (auto&& elem : rng) {
if (!elem) {
return false;
}
}
return true;
}
};
// Matcher for checking that no element in range is true.
class NoneTrueMatcher final : public MatcherGenericBase {
public:
std::string describe() const override;
template <typename RangeLike>
bool match(RangeLike&& rng) const {
for (auto&& elem : rng) {
if (elem) {
return false;
}
}
return true;
}
};
// Matcher for checking that any element in range is true.
class AnyTrueMatcher final : public MatcherGenericBase {
public:
std::string describe() const override;
template <typename RangeLike>
bool match(RangeLike&& rng) const {
for (auto&& elem : rng) {
if (elem) {
return true;
}
}
return false;
}
};
// Creates a matcher that checks whether all elements in a range match a matcher
template <typename Matcher>
AllMatchMatcher<Matcher> AllMatch(Matcher&& matcher) {
return { CATCH_FORWARD(matcher) };
}
// Creates a matcher that checks whether no element in a range matches a matcher.
template <typename Matcher>
NoneMatchMatcher<Matcher> NoneMatch(Matcher&& matcher) {
return { CATCH_FORWARD(matcher) };
}
// Creates a matcher that checks whether any element in a range matches a matcher.
template <typename Matcher>
AnyMatchMatcher<Matcher> AnyMatch(Matcher&& matcher) {
return { CATCH_FORWARD(matcher) };
}
// Creates a matcher that checks whether all elements in a range are true
AllTrueMatcher AllTrue();
// Creates a matcher that checks whether no element in a range is true
NoneTrueMatcher NoneTrue();
// Creates a matcher that checks whether any element in a range is true
AnyTrueMatcher AnyTrue();
}
}
#endif // CATCH_MATCHERS_QUANTIFIERS_HPP_INCLUDED