1// Copyright 2006, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Implements a family of generic predicate assertion macros.
31
32// IWYU pragma: private, include "gtest/gtest.h"
33// IWYU pragma: friend gtest/.*
34// IWYU pragma: friend gmock/.*
35
36#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
37#define GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
38
39#include "gtest/gtest-assertion-result.h"
40#include "gtest/internal/gtest-internal.h"
41#include "gtest/internal/gtest-port.h"
42
43namespace testing {
44
45// This header implements a family of generic predicate assertion
46// macros:
47//
48// ASSERT_PRED_FORMAT1(pred_format, v1)
49// ASSERT_PRED_FORMAT2(pred_format, v1, v2)
50// ...
51//
52// where pred_format is a function or functor that takes n (in the
53// case of ASSERT_PRED_FORMATn) values and their source expression
54// text, and returns a testing::AssertionResult. See the definition
55// of ASSERT_EQ in gtest.h for an example.
56//
57// If you don't care about formatting, you can use the more
58// restrictive version:
59//
60// ASSERT_PRED1(pred, v1)
61// ASSERT_PRED2(pred, v1, v2)
62// ...
63//
64// where pred is an n-ary function or functor that returns bool,
65// and the values v1, v2, ..., must support the << operator for
66// streaming to std::ostream.
67//
68// We also define the EXPECT_* variations.
69//
70// For now we only support predicates whose arity is at most 5.
71// Please email googletestframework@googlegroups.com if you need
72// support for higher arities.
73
74// GTEST_ASSERT_ is the basic statement to which all of the assertions
75// in this file reduce. Don't use this in your code.
76
77#define GTEST_ASSERT_(expression, on_failure) \
78 GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
79 if (const ::testing::AssertionResult gtest_ar = (expression)) \
80 ; \
81 else \
82 on_failure(gtest_ar.failure_message())
83
84// Helper function for implementing {EXPECT|ASSERT}_PRED1. Don't use
85// this in your code.
86template <typename Pred, typename T1>
87AssertionResult AssertPred1Helper(const char* pred_text, const char* e1,
88 Pred pred, const T1& v1) {
89 if (pred(v1)) return AssertionSuccess();
90
91 return AssertionFailure()
92 << pred_text << "(" << e1 << ") evaluates to false, where"
93 << "\n"
94 << e1 << " evaluates to " << ::testing::PrintToString(v1);
95}
96
97// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT1.
98// Don't use this in your code.
99#define GTEST_PRED_FORMAT1_(pred_format, v1, on_failure) \
100 GTEST_ASSERT_(pred_format(#v1, v1), on_failure)
101
102// Internal macro for implementing {EXPECT|ASSERT}_PRED1. Don't use
103// this in your code.
104#define GTEST_PRED1_(pred, v1, on_failure) \
105 GTEST_ASSERT_(::testing::AssertPred1Helper(#pred, #v1, pred, v1), on_failure)
106
107// Unary predicate assertion macros.
108#define EXPECT_PRED_FORMAT1(pred_format, v1) \
109 GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_NONFATAL_FAILURE_)
110#define EXPECT_PRED1(pred, v1) GTEST_PRED1_(pred, v1, GTEST_NONFATAL_FAILURE_)
111#define ASSERT_PRED_FORMAT1(pred_format, v1) \
112 GTEST_PRED_FORMAT1_(pred_format, v1, GTEST_FATAL_FAILURE_)
113#define ASSERT_PRED1(pred, v1) GTEST_PRED1_(pred, v1, GTEST_FATAL_FAILURE_)
114
115// Helper function for implementing {EXPECT|ASSERT}_PRED2. Don't use
116// this in your code.
117template <typename Pred, typename T1, typename T2>
118AssertionResult AssertPred2Helper(const char* pred_text, const char* e1,
119 const char* e2, Pred pred, const T1& v1,
120 const T2& v2) {
121 if (pred(v1, v2)) return AssertionSuccess();
122
123 return AssertionFailure()
124 << pred_text << "(" << e1 << ", " << e2
125 << ") evaluates to false, where"
126 << "\n"
127 << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
128 << e2 << " evaluates to " << ::testing::PrintToString(v2);
129}
130
131// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT2.
132// Don't use this in your code.
133#define GTEST_PRED_FORMAT2_(pred_format, v1, v2, on_failure) \
134 GTEST_ASSERT_(pred_format(#v1, #v2, v1, v2), on_failure)
135
136// Internal macro for implementing {EXPECT|ASSERT}_PRED2. Don't use
137// this in your code.
138#define GTEST_PRED2_(pred, v1, v2, on_failure) \
139 GTEST_ASSERT_(::testing::AssertPred2Helper(#pred, #v1, #v2, pred, v1, v2), \
140 on_failure)
141
142// Binary predicate assertion macros.
143#define EXPECT_PRED_FORMAT2(pred_format, v1, v2) \
144 GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_NONFATAL_FAILURE_)
145#define EXPECT_PRED2(pred, v1, v2) \
146 GTEST_PRED2_(pred, v1, v2, GTEST_NONFATAL_FAILURE_)
147#define ASSERT_PRED_FORMAT2(pred_format, v1, v2) \
148 GTEST_PRED_FORMAT2_(pred_format, v1, v2, GTEST_FATAL_FAILURE_)
149#define ASSERT_PRED2(pred, v1, v2) \
150 GTEST_PRED2_(pred, v1, v2, GTEST_FATAL_FAILURE_)
151
152// Helper function for implementing {EXPECT|ASSERT}_PRED3. Don't use
153// this in your code.
154template <typename Pred, typename T1, typename T2, typename T3>
155AssertionResult AssertPred3Helper(const char* pred_text, const char* e1,
156 const char* e2, const char* e3, Pred pred,
157 const T1& v1, const T2& v2, const T3& v3) {
158 if (pred(v1, v2, v3)) return AssertionSuccess();
159
160 return AssertionFailure()
161 << pred_text << "(" << e1 << ", " << e2 << ", " << e3
162 << ") evaluates to false, where"
163 << "\n"
164 << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
165 << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n"
166 << e3 << " evaluates to " << ::testing::PrintToString(v3);
167}
168
169// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT3.
170// Don't use this in your code.
171#define GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, on_failure) \
172 GTEST_ASSERT_(pred_format(#v1, #v2, #v3, v1, v2, v3), on_failure)
173
174// Internal macro for implementing {EXPECT|ASSERT}_PRED3. Don't use
175// this in your code.
176#define GTEST_PRED3_(pred, v1, v2, v3, on_failure) \
177 GTEST_ASSERT_( \
178 ::testing::AssertPred3Helper(#pred, #v1, #v2, #v3, pred, v1, v2, v3), \
179 on_failure)
180
181// Ternary predicate assertion macros.
182#define EXPECT_PRED_FORMAT3(pred_format, v1, v2, v3) \
183 GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_NONFATAL_FAILURE_)
184#define EXPECT_PRED3(pred, v1, v2, v3) \
185 GTEST_PRED3_(pred, v1, v2, v3, GTEST_NONFATAL_FAILURE_)
186#define ASSERT_PRED_FORMAT3(pred_format, v1, v2, v3) \
187 GTEST_PRED_FORMAT3_(pred_format, v1, v2, v3, GTEST_FATAL_FAILURE_)
188#define ASSERT_PRED3(pred, v1, v2, v3) \
189 GTEST_PRED3_(pred, v1, v2, v3, GTEST_FATAL_FAILURE_)
190
191// Helper function for implementing {EXPECT|ASSERT}_PRED4. Don't use
192// this in your code.
193template <typename Pred, typename T1, typename T2, typename T3, typename T4>
194AssertionResult AssertPred4Helper(const char* pred_text, const char* e1,
195 const char* e2, const char* e3,
196 const char* e4, Pred pred, const T1& v1,
197 const T2& v2, const T3& v3, const T4& v4) {
198 if (pred(v1, v2, v3, v4)) return AssertionSuccess();
199
200 return AssertionFailure()
201 << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4
202 << ") evaluates to false, where"
203 << "\n"
204 << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
205 << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n"
206 << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n"
207 << e4 << " evaluates to " << ::testing::PrintToString(v4);
208}
209
210// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT4.
211// Don't use this in your code.
212#define GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, on_failure) \
213 GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, v1, v2, v3, v4), on_failure)
214
215// Internal macro for implementing {EXPECT|ASSERT}_PRED4. Don't use
216// this in your code.
217#define GTEST_PRED4_(pred, v1, v2, v3, v4, on_failure) \
218 GTEST_ASSERT_(::testing::AssertPred4Helper(#pred, #v1, #v2, #v3, #v4, pred, \
219 v1, v2, v3, v4), \
220 on_failure)
221
222// 4-ary predicate assertion macros.
223#define EXPECT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
224 GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_)
225#define EXPECT_PRED4(pred, v1, v2, v3, v4) \
226 GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_NONFATAL_FAILURE_)
227#define ASSERT_PRED_FORMAT4(pred_format, v1, v2, v3, v4) \
228 GTEST_PRED_FORMAT4_(pred_format, v1, v2, v3, v4, GTEST_FATAL_FAILURE_)
229#define ASSERT_PRED4(pred, v1, v2, v3, v4) \
230 GTEST_PRED4_(pred, v1, v2, v3, v4, GTEST_FATAL_FAILURE_)
231
232// Helper function for implementing {EXPECT|ASSERT}_PRED5. Don't use
233// this in your code.
234template <typename Pred, typename T1, typename T2, typename T3, typename T4,
235 typename T5>
236AssertionResult AssertPred5Helper(const char* pred_text, const char* e1,
237 const char* e2, const char* e3,
238 const char* e4, const char* e5, Pred pred,
239 const T1& v1, const T2& v2, const T3& v3,
240 const T4& v4, const T5& v5) {
241 if (pred(v1, v2, v3, v4, v5)) return AssertionSuccess();
242
243 return AssertionFailure()
244 << pred_text << "(" << e1 << ", " << e2 << ", " << e3 << ", " << e4
245 << ", " << e5 << ") evaluates to false, where"
246 << "\n"
247 << e1 << " evaluates to " << ::testing::PrintToString(v1) << "\n"
248 << e2 << " evaluates to " << ::testing::PrintToString(v2) << "\n"
249 << e3 << " evaluates to " << ::testing::PrintToString(v3) << "\n"
250 << e4 << " evaluates to " << ::testing::PrintToString(v4) << "\n"
251 << e5 << " evaluates to " << ::testing::PrintToString(v5);
252}
253
254// Internal macro for implementing {EXPECT|ASSERT}_PRED_FORMAT5.
255// Don't use this in your code.
256#define GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, on_failure) \
257 GTEST_ASSERT_(pred_format(#v1, #v2, #v3, #v4, #v5, v1, v2, v3, v4, v5), \
258 on_failure)
259
260// Internal macro for implementing {EXPECT|ASSERT}_PRED5. Don't use
261// this in your code.
262#define GTEST_PRED5_(pred, v1, v2, v3, v4, v5, on_failure) \
263 GTEST_ASSERT_(::testing::AssertPred5Helper(#pred, #v1, #v2, #v3, #v4, #v5, \
264 pred, v1, v2, v3, v4, v5), \
265 on_failure)
266
267// 5-ary predicate assertion macros.
268#define EXPECT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
269 GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_)
270#define EXPECT_PRED5(pred, v1, v2, v3, v4, v5) \
271 GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_NONFATAL_FAILURE_)
272#define ASSERT_PRED_FORMAT5(pred_format, v1, v2, v3, v4, v5) \
273 GTEST_PRED_FORMAT5_(pred_format, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_)
274#define ASSERT_PRED5(pred, v1, v2, v3, v4, v5) \
275 GTEST_PRED5_(pred, v1, v2, v3, v4, v5, GTEST_FATAL_FAILURE_)
276
277} // namespace testing
278
279#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PRED_IMPL_H_
280

source code of third-party/unittest/googletest/include/gtest/gtest_pred_impl.h