1 |
2 |
jamieiles |
$$ -*- mode: c++; -*-
|
2 |
|
|
$$ This is a Pump source file. Please use Pump to convert it to
|
3 |
|
|
$$ gmock-generated-actions.h.
|
4 |
|
|
$$
|
5 |
|
|
$var n = 10 $$ The maximum arity we support.
|
6 |
|
|
$$ }} This line fixes auto-indentation of the following code in Emacs.
|
7 |
|
|
// Copyright 2008, Google Inc.
|
8 |
|
|
// All rights reserved.
|
9 |
|
|
//
|
10 |
|
|
// Redistribution and use in source and binary forms, with or without
|
11 |
|
|
// modification, are permitted provided that the following conditions are
|
12 |
|
|
// met:
|
13 |
|
|
//
|
14 |
|
|
// * Redistributions of source code must retain the above copyright
|
15 |
|
|
// notice, this list of conditions and the following disclaimer.
|
16 |
|
|
// * Redistributions in binary form must reproduce the above
|
17 |
|
|
// copyright notice, this list of conditions and the following disclaimer
|
18 |
|
|
// in the documentation and/or other materials provided with the
|
19 |
|
|
// distribution.
|
20 |
|
|
// * Neither the name of Google Inc. nor the names of its
|
21 |
|
|
// contributors may be used to endorse or promote products derived from
|
22 |
|
|
// this software without specific prior written permission.
|
23 |
|
|
//
|
24 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
25 |
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
26 |
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
27 |
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
28 |
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
29 |
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
30 |
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
31 |
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
32 |
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
33 |
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
34 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
35 |
|
|
|
36 |
|
|
// Google Mock - a framework for writing C++ mock classes.
|
37 |
|
|
//
|
38 |
|
|
// This file implements some commonly used variadic matchers.
|
39 |
|
|
|
40 |
|
|
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
|
41 |
|
|
#define GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
|
42 |
|
|
|
43 |
|
|
#include
|
44 |
|
|
#include
|
45 |
|
|
#include
|
46 |
|
|
#include
|
47 |
|
|
#include "gmock/gmock-matchers.h"
|
48 |
|
|
|
49 |
|
|
namespace testing {
|
50 |
|
|
namespace internal {
|
51 |
|
|
|
52 |
|
|
$range i 0..n-1
|
53 |
|
|
|
54 |
|
|
// The type of the i-th (0-based) field of Tuple.
|
55 |
|
|
#define GMOCK_FIELD_TYPE_(Tuple, i) \
|
56 |
|
|
typename ::testing::tuple_element::type
|
57 |
|
|
|
58 |
|
|
// TupleFields is for selecting fields from a
|
59 |
|
|
// tuple of type Tuple. It has two members:
|
60 |
|
|
//
|
61 |
|
|
// type: a tuple type whose i-th field is the ki-th field of Tuple.
|
62 |
|
|
// GetSelectedFields(t): returns fields k0, ..., and kn of t as a tuple.
|
63 |
|
|
//
|
64 |
|
|
// For example, in class TupleFields, 2, 0>, we have:
|
65 |
|
|
//
|
66 |
|
|
// type is tuple, and
|
67 |
|
|
// GetSelectedFields(make_tuple(true, 'a', 42)) is (42, true).
|
68 |
|
|
|
69 |
|
|
template
|
70 |
|
|
class TupleFields;
|
71 |
|
|
|
72 |
|
|
// This generic version is used when there are $n selectors.
|
73 |
|
|
template
|
74 |
|
|
class TupleFields {
|
75 |
|
|
public:
|
76 |
|
|
typedef ::testing::tuple<$for i, [[GMOCK_FIELD_TYPE_(Tuple, k$i)]]> type;
|
77 |
|
|
static type GetSelectedFields(const Tuple& t) {
|
78 |
|
|
return type($for i, [[get(t)]]);
|
79 |
|
|
}
|
80 |
|
|
};
|
81 |
|
|
|
82 |
|
|
// The following specialization is used for 0 ~ $(n-1) selectors.
|
83 |
|
|
|
84 |
|
|
$for i [[
|
85 |
|
|
$$ }}}
|
86 |
|
|
$range j 0..i-1
|
87 |
|
|
$range k 0..n-1
|
88 |
|
|
|
89 |
|
|
template
|
90 |
|
|
class TupleFields {
|
91 |
|
|
public:
|
92 |
|
|
typedef ::testing::tuple<$for j, [[GMOCK_FIELD_TYPE_(Tuple, k$j)]]> type;
|
93 |
|
|
static type GetSelectedFields(const Tuple& $if i==0 [[/* t */]] $else [[t]]) {
|
94 |
|
|
return type($for j, [[get(t)]]);
|
95 |
|
|
}
|
96 |
|
|
};
|
97 |
|
|
|
98 |
|
|
]]
|
99 |
|
|
|
100 |
|
|
#undef GMOCK_FIELD_TYPE_
|
101 |
|
|
|
102 |
|
|
// Implements the Args() matcher.
|
103 |
|
|
|
104 |
|
|
$var ks = [[$for i, [[k$i]]]]
|
105 |
|
|
template
|
106 |
|
|
class ArgsMatcherImpl : public MatcherInterface {
|
107 |
|
|
public:
|
108 |
|
|
// ArgsTuple may have top-level const or reference modifiers.
|
109 |
|
|
typedef GTEST_REMOVE_REFERENCE_AND_CONST_(ArgsTuple) RawArgsTuple;
|
110 |
|
|
typedef typename internal::TupleFields::type SelectedArgs;
|
111 |
|
|
typedef Matcher MonomorphicInnerMatcher;
|
112 |
|
|
|
113 |
|
|
template
|
114 |
|
|
explicit ArgsMatcherImpl(const InnerMatcher& inner_matcher)
|
115 |
|
|
: inner_matcher_(SafeMatcherCast(inner_matcher)) {}
|
116 |
|
|
|
117 |
|
|
virtual bool MatchAndExplain(ArgsTuple args,
|
118 |
|
|
MatchResultListener* listener) const {
|
119 |
|
|
const SelectedArgs& selected_args = GetSelectedArgs(args);
|
120 |
|
|
if (!listener->IsInterested())
|
121 |
|
|
return inner_matcher_.Matches(selected_args);
|
122 |
|
|
|
123 |
|
|
PrintIndices(listener->stream());
|
124 |
|
|
*listener << "are " << PrintToString(selected_args);
|
125 |
|
|
|
126 |
|
|
StringMatchResultListener inner_listener;
|
127 |
|
|
const bool match = inner_matcher_.MatchAndExplain(selected_args,
|
128 |
|
|
&inner_listener);
|
129 |
|
|
PrintIfNotEmpty(inner_listener.str(), listener->stream());
|
130 |
|
|
return match;
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
virtual void DescribeTo(::std::ostream* os) const {
|
134 |
|
|
*os << "are a tuple ";
|
135 |
|
|
PrintIndices(os);
|
136 |
|
|
inner_matcher_.DescribeTo(os);
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
virtual void DescribeNegationTo(::std::ostream* os) const {
|
140 |
|
|
*os << "are a tuple ";
|
141 |
|
|
PrintIndices(os);
|
142 |
|
|
inner_matcher_.DescribeNegationTo(os);
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
private:
|
146 |
|
|
static SelectedArgs GetSelectedArgs(ArgsTuple args) {
|
147 |
|
|
return TupleFields::GetSelectedFields(args);
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
// Prints the indices of the selected fields.
|
151 |
|
|
static void PrintIndices(::std::ostream* os) {
|
152 |
|
|
*os << "whose fields (";
|
153 |
|
|
const int indices[$n] = { $ks };
|
154 |
|
|
for (int i = 0; i < $n; i++) {
|
155 |
|
|
if (indices[i] < 0)
|
156 |
|
|
break;
|
157 |
|
|
|
158 |
|
|
if (i >= 1)
|
159 |
|
|
*os << ", ";
|
160 |
|
|
|
161 |
|
|
*os << "#" << indices[i];
|
162 |
|
|
}
|
163 |
|
|
*os << ") ";
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
const MonomorphicInnerMatcher inner_matcher_;
|
167 |
|
|
|
168 |
|
|
GTEST_DISALLOW_ASSIGN_(ArgsMatcherImpl);
|
169 |
|
|
};
|
170 |
|
|
|
171 |
|
|
template
|
172 |
|
|
class ArgsMatcher {
|
173 |
|
|
public:
|
174 |
|
|
explicit ArgsMatcher(const InnerMatcher& inner_matcher)
|
175 |
|
|
: inner_matcher_(inner_matcher) {}
|
176 |
|
|
|
177 |
|
|
template
|
178 |
|
|
operator Matcher() const {
|
179 |
|
|
return MakeMatcher(new ArgsMatcherImpl(inner_matcher_));
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
private:
|
183 |
|
|
const InnerMatcher inner_matcher_;
|
184 |
|
|
|
185 |
|
|
GTEST_DISALLOW_ASSIGN_(ArgsMatcher);
|
186 |
|
|
};
|
187 |
|
|
|
188 |
|
|
// A set of metafunctions for computing the result type of AllOf.
|
189 |
|
|
// AllOf(m1, ..., mN) returns
|
190 |
|
|
// AllOfResultN::type.
|
191 |
|
|
|
192 |
|
|
// Although AllOf isn't defined for one argument, AllOfResult1 is defined
|
193 |
|
|
// to simplify the implementation.
|
194 |
|
|
template
|
195 |
|
|
struct AllOfResult1 {
|
196 |
|
|
typedef M1 type;
|
197 |
|
|
};
|
198 |
|
|
|
199 |
|
|
$range i 1..n
|
200 |
|
|
|
201 |
|
|
$range i 2..n
|
202 |
|
|
$for i [[
|
203 |
|
|
$range j 2..i
|
204 |
|
|
$var m = i/2
|
205 |
|
|
$range k 1..m
|
206 |
|
|
$range t m+1..i
|
207 |
|
|
|
208 |
|
|
template
|
209 |
|
|
struct AllOfResult$i {
|
210 |
|
|
typedef BothOfMatcher<
|
211 |
|
|
typename AllOfResult$m<$for k, [[M$k]]>::type,
|
212 |
|
|
typename AllOfResult$(i-m)<$for t, [[M$t]]>::type
|
213 |
|
|
> type;
|
214 |
|
|
};
|
215 |
|
|
|
216 |
|
|
]]
|
217 |
|
|
|
218 |
|
|
// A set of metafunctions for computing the result type of AnyOf.
|
219 |
|
|
// AnyOf(m1, ..., mN) returns
|
220 |
|
|
// AnyOfResultN::type.
|
221 |
|
|
|
222 |
|
|
// Although AnyOf isn't defined for one argument, AnyOfResult1 is defined
|
223 |
|
|
// to simplify the implementation.
|
224 |
|
|
template
|
225 |
|
|
struct AnyOfResult1 {
|
226 |
|
|
typedef M1 type;
|
227 |
|
|
};
|
228 |
|
|
|
229 |
|
|
$range i 1..n
|
230 |
|
|
|
231 |
|
|
$range i 2..n
|
232 |
|
|
$for i [[
|
233 |
|
|
$range j 2..i
|
234 |
|
|
$var m = i/2
|
235 |
|
|
$range k 1..m
|
236 |
|
|
$range t m+1..i
|
237 |
|
|
|
238 |
|
|
template
|
239 |
|
|
struct AnyOfResult$i {
|
240 |
|
|
typedef EitherOfMatcher<
|
241 |
|
|
typename AnyOfResult$m<$for k, [[M$k]]>::type,
|
242 |
|
|
typename AnyOfResult$(i-m)<$for t, [[M$t]]>::type
|
243 |
|
|
> type;
|
244 |
|
|
};
|
245 |
|
|
|
246 |
|
|
]]
|
247 |
|
|
|
248 |
|
|
} // namespace internal
|
249 |
|
|
|
250 |
|
|
// Args(a_matcher) matches a tuple if the selected
|
251 |
|
|
// fields of it matches a_matcher. C++ doesn't support default
|
252 |
|
|
// arguments for function templates, so we have to overload it.
|
253 |
|
|
|
254 |
|
|
$range i 0..n
|
255 |
|
|
$for i [[
|
256 |
|
|
$range j 1..i
|
257 |
|
|
template <$for j [[int k$j, ]]typename InnerMatcher>
|
258 |
|
|
inline internal::ArgsMatcher
|
259 |
|
|
Args(const InnerMatcher& matcher) {
|
260 |
|
|
return internal::ArgsMatcher(matcher);
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
]]
|
265 |
|
|
// ElementsAre(e_1, e_2, ... e_n) matches an STL-style container with
|
266 |
|
|
// n elements, where the i-th element in the container must
|
267 |
|
|
// match the i-th argument in the list. Each argument of
|
268 |
|
|
// ElementsAre() can be either a value or a matcher. We support up to
|
269 |
|
|
// $n arguments.
|
270 |
|
|
//
|
271 |
|
|
// The use of DecayArray in the implementation allows ElementsAre()
|
272 |
|
|
// to accept string literals, whose type is const char[N], but we
|
273 |
|
|
// want to treat them as const char*.
|
274 |
|
|
//
|
275 |
|
|
// NOTE: Since ElementsAre() cares about the order of the elements, it
|
276 |
|
|
// must not be used with containers whose elements's order is
|
277 |
|
|
// undefined (e.g. hash_map).
|
278 |
|
|
|
279 |
|
|
$range i 0..n
|
280 |
|
|
$for i [[
|
281 |
|
|
|
282 |
|
|
$range j 1..i
|
283 |
|
|
|
284 |
|
|
$if i>0 [[
|
285 |
|
|
|
286 |
|
|
template <$for j, [[typename T$j]]>
|
287 |
|
|
]]
|
288 |
|
|
|
289 |
|
|
inline internal::ElementsAreMatcher<
|
290 |
|
|
::testing::tuple<
|
291 |
|
|
$for j, [[
|
292 |
|
|
|
293 |
|
|
typename internal::DecayArray::type]]> >
|
294 |
|
|
ElementsAre($for j, [[const T$j& e$j]]) {
|
295 |
|
|
typedef ::testing::tuple<
|
296 |
|
|
$for j, [[
|
297 |
|
|
|
298 |
|
|
typename internal::DecayArray::type]]> Args;
|
299 |
|
|
return internal::ElementsAreMatcher(Args($for j, [[e$j]]));
|
300 |
|
|
}
|
301 |
|
|
|
302 |
|
|
]]
|
303 |
|
|
|
304 |
|
|
// UnorderedElementsAre(e_1, e_2, ..., e_n) is an ElementsAre extension
|
305 |
|
|
// that matches n elements in any order. We support up to n=$n arguments.
|
306 |
|
|
|
307 |
|
|
$range i 0..n
|
308 |
|
|
$for i [[
|
309 |
|
|
|
310 |
|
|
$range j 1..i
|
311 |
|
|
|
312 |
|
|
$if i>0 [[
|
313 |
|
|
|
314 |
|
|
template <$for j, [[typename T$j]]>
|
315 |
|
|
]]
|
316 |
|
|
|
317 |
|
|
inline internal::UnorderedElementsAreMatcher<
|
318 |
|
|
::testing::tuple<
|
319 |
|
|
$for j, [[
|
320 |
|
|
|
321 |
|
|
typename internal::DecayArray::type]]> >
|
322 |
|
|
UnorderedElementsAre($for j, [[const T$j& e$j]]) {
|
323 |
|
|
typedef ::testing::tuple<
|
324 |
|
|
$for j, [[
|
325 |
|
|
|
326 |
|
|
typename internal::DecayArray::type]]> Args;
|
327 |
|
|
return internal::UnorderedElementsAreMatcher(Args($for j, [[e$j]]));
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
]]
|
331 |
|
|
|
332 |
|
|
// AllOf(m1, m2, ..., mk) matches any value that matches all of the given
|
333 |
|
|
// sub-matchers. AllOf is called fully qualified to prevent ADL from firing.
|
334 |
|
|
|
335 |
|
|
$range i 2..n
|
336 |
|
|
$for i [[
|
337 |
|
|
$range j 1..i
|
338 |
|
|
$var m = i/2
|
339 |
|
|
$range k 1..m
|
340 |
|
|
$range t m+1..i
|
341 |
|
|
|
342 |
|
|
template <$for j, [[typename M$j]]>
|
343 |
|
|
inline typename internal::AllOfResult$i<$for j, [[M$j]]>::type
|
344 |
|
|
AllOf($for j, [[M$j m$j]]) {
|
345 |
|
|
return typename internal::AllOfResult$i<$for j, [[M$j]]>::type(
|
346 |
|
|
$if m == 1 [[m1]] $else [[::testing::AllOf($for k, [[m$k]])]],
|
347 |
|
|
$if m+1 == i [[m$i]] $else [[::testing::AllOf($for t, [[m$t]])]]);
|
348 |
|
|
}
|
349 |
|
|
|
350 |
|
|
]]
|
351 |
|
|
|
352 |
|
|
// AnyOf(m1, m2, ..., mk) matches any value that matches any of the given
|
353 |
|
|
// sub-matchers. AnyOf is called fully qualified to prevent ADL from firing.
|
354 |
|
|
|
355 |
|
|
$range i 2..n
|
356 |
|
|
$for i [[
|
357 |
|
|
$range j 1..i
|
358 |
|
|
$var m = i/2
|
359 |
|
|
$range k 1..m
|
360 |
|
|
$range t m+1..i
|
361 |
|
|
|
362 |
|
|
template <$for j, [[typename M$j]]>
|
363 |
|
|
inline typename internal::AnyOfResult$i<$for j, [[M$j]]>::type
|
364 |
|
|
AnyOf($for j, [[M$j m$j]]) {
|
365 |
|
|
return typename internal::AnyOfResult$i<$for j, [[M$j]]>::type(
|
366 |
|
|
$if m == 1 [[m1]] $else [[::testing::AnyOf($for k, [[m$k]])]],
|
367 |
|
|
$if m+1 == i [[m$i]] $else [[::testing::AnyOf($for t, [[m$t]])]]);
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
]]
|
371 |
|
|
|
372 |
|
|
} // namespace testing
|
373 |
|
|
$$ } // This Pump meta comment fixes auto-indentation in Emacs. It will not
|
374 |
|
|
$$ // show up in the generated code.
|
375 |
|
|
|
376 |
|
|
|
377 |
|
|
// The MATCHER* family of macros can be used in a namespace scope to
|
378 |
|
|
// define custom matchers easily.
|
379 |
|
|
//
|
380 |
|
|
// Basic Usage
|
381 |
|
|
// ===========
|
382 |
|
|
//
|
383 |
|
|
// The syntax
|
384 |
|
|
//
|
385 |
|
|
// MATCHER(name, description_string) { statements; }
|
386 |
|
|
//
|
387 |
|
|
// defines a matcher with the given name that executes the statements,
|
388 |
|
|
// which must return a bool to indicate if the match succeeds. Inside
|
389 |
|
|
// the statements, you can refer to the value being matched by 'arg',
|
390 |
|
|
// and refer to its type by 'arg_type'.
|
391 |
|
|
//
|
392 |
|
|
// The description string documents what the matcher does, and is used
|
393 |
|
|
// to generate the failure message when the match fails. Since a
|
394 |
|
|
// MATCHER() is usually defined in a header file shared by multiple
|
395 |
|
|
// C++ source files, we require the description to be a C-string
|
396 |
|
|
// literal to avoid possible side effects. It can be empty, in which
|
397 |
|
|
// case we'll use the sequence of words in the matcher name as the
|
398 |
|
|
// description.
|
399 |
|
|
//
|
400 |
|
|
// For example:
|
401 |
|
|
//
|
402 |
|
|
// MATCHER(IsEven, "") { return (arg % 2) == 0; }
|
403 |
|
|
//
|
404 |
|
|
// allows you to write
|
405 |
|
|
//
|
406 |
|
|
// // Expects mock_foo.Bar(n) to be called where n is even.
|
407 |
|
|
// EXPECT_CALL(mock_foo, Bar(IsEven()));
|
408 |
|
|
//
|
409 |
|
|
// or,
|
410 |
|
|
//
|
411 |
|
|
// // Verifies that the value of some_expression is even.
|
412 |
|
|
// EXPECT_THAT(some_expression, IsEven());
|
413 |
|
|
//
|
414 |
|
|
// If the above assertion fails, it will print something like:
|
415 |
|
|
//
|
416 |
|
|
// Value of: some_expression
|
417 |
|
|
// Expected: is even
|
418 |
|
|
// Actual: 7
|
419 |
|
|
//
|
420 |
|
|
// where the description "is even" is automatically calculated from the
|
421 |
|
|
// matcher name IsEven.
|
422 |
|
|
//
|
423 |
|
|
// Argument Type
|
424 |
|
|
// =============
|
425 |
|
|
//
|
426 |
|
|
// Note that the type of the value being matched (arg_type) is
|
427 |
|
|
// determined by the context in which you use the matcher and is
|
428 |
|
|
// supplied to you by the compiler, so you don't need to worry about
|
429 |
|
|
// declaring it (nor can you). This allows the matcher to be
|
430 |
|
|
// polymorphic. For example, IsEven() can be used to match any type
|
431 |
|
|
// where the value of "(arg % 2) == 0" can be implicitly converted to
|
432 |
|
|
// a bool. In the "Bar(IsEven())" example above, if method Bar()
|
433 |
|
|
// takes an int, 'arg_type' will be int; if it takes an unsigned long,
|
434 |
|
|
// 'arg_type' will be unsigned long; and so on.
|
435 |
|
|
//
|
436 |
|
|
// Parameterizing Matchers
|
437 |
|
|
// =======================
|
438 |
|
|
//
|
439 |
|
|
// Sometimes you'll want to parameterize the matcher. For that you
|
440 |
|
|
// can use another macro:
|
441 |
|
|
//
|
442 |
|
|
// MATCHER_P(name, param_name, description_string) { statements; }
|
443 |
|
|
//
|
444 |
|
|
// For example:
|
445 |
|
|
//
|
446 |
|
|
// MATCHER_P(HasAbsoluteValue, value, "") { return abs(arg) == value; }
|
447 |
|
|
//
|
448 |
|
|
// will allow you to write:
|
449 |
|
|
//
|
450 |
|
|
// EXPECT_THAT(Blah("a"), HasAbsoluteValue(n));
|
451 |
|
|
//
|
452 |
|
|
// which may lead to this message (assuming n is 10):
|
453 |
|
|
//
|
454 |
|
|
// Value of: Blah("a")
|
455 |
|
|
// Expected: has absolute value 10
|
456 |
|
|
// Actual: -9
|
457 |
|
|
//
|
458 |
|
|
// Note that both the matcher description and its parameter are
|
459 |
|
|
// printed, making the message human-friendly.
|
460 |
|
|
//
|
461 |
|
|
// In the matcher definition body, you can write 'foo_type' to
|
462 |
|
|
// reference the type of a parameter named 'foo'. For example, in the
|
463 |
|
|
// body of MATCHER_P(HasAbsoluteValue, value) above, you can write
|
464 |
|
|
// 'value_type' to refer to the type of 'value'.
|
465 |
|
|
//
|
466 |
|
|
// We also provide MATCHER_P2, MATCHER_P3, ..., up to MATCHER_P$n to
|
467 |
|
|
// support multi-parameter matchers.
|
468 |
|
|
//
|
469 |
|
|
// Describing Parameterized Matchers
|
470 |
|
|
// =================================
|
471 |
|
|
//
|
472 |
|
|
// The last argument to MATCHER*() is a string-typed expression. The
|
473 |
|
|
// expression can reference all of the matcher's parameters and a
|
474 |
|
|
// special bool-typed variable named 'negation'. When 'negation' is
|
475 |
|
|
// false, the expression should evaluate to the matcher's description;
|
476 |
|
|
// otherwise it should evaluate to the description of the negation of
|
477 |
|
|
// the matcher. For example,
|
478 |
|
|
//
|
479 |
|
|
// using testing::PrintToString;
|
480 |
|
|
//
|
481 |
|
|
// MATCHER_P2(InClosedRange, low, hi,
|
482 |
|
|
// string(negation ? "is not" : "is") + " in range [" +
|
483 |
|
|
// PrintToString(low) + ", " + PrintToString(hi) + "]") {
|
484 |
|
|
// return low <= arg && arg <= hi;
|
485 |
|
|
// }
|
486 |
|
|
// ...
|
487 |
|
|
// EXPECT_THAT(3, InClosedRange(4, 6));
|
488 |
|
|
// EXPECT_THAT(3, Not(InClosedRange(2, 4)));
|
489 |
|
|
//
|
490 |
|
|
// would generate two failures that contain the text:
|
491 |
|
|
//
|
492 |
|
|
// Expected: is in range [4, 6]
|
493 |
|
|
// ...
|
494 |
|
|
// Expected: is not in range [2, 4]
|
495 |
|
|
//
|
496 |
|
|
// If you specify "" as the description, the failure message will
|
497 |
|
|
// contain the sequence of words in the matcher name followed by the
|
498 |
|
|
// parameter values printed as a tuple. For example,
|
499 |
|
|
//
|
500 |
|
|
// MATCHER_P2(InClosedRange, low, hi, "") { ... }
|
501 |
|
|
// ...
|
502 |
|
|
// EXPECT_THAT(3, InClosedRange(4, 6));
|
503 |
|
|
// EXPECT_THAT(3, Not(InClosedRange(2, 4)));
|
504 |
|
|
//
|
505 |
|
|
// would generate two failures that contain the text:
|
506 |
|
|
//
|
507 |
|
|
// Expected: in closed range (4, 6)
|
508 |
|
|
// ...
|
509 |
|
|
// Expected: not (in closed range (2, 4))
|
510 |
|
|
//
|
511 |
|
|
// Types of Matcher Parameters
|
512 |
|
|
// ===========================
|
513 |
|
|
//
|
514 |
|
|
// For the purpose of typing, you can view
|
515 |
|
|
//
|
516 |
|
|
// MATCHER_Pk(Foo, p1, ..., pk, description_string) { ... }
|
517 |
|
|
//
|
518 |
|
|
// as shorthand for
|
519 |
|
|
//
|
520 |
|
|
// template
|
521 |
|
|
// FooMatcherPk
|
522 |
|
|
// Foo(p1_type p1, ..., pk_type pk) { ... }
|
523 |
|
|
//
|
524 |
|
|
// When you write Foo(v1, ..., vk), the compiler infers the types of
|
525 |
|
|
// the parameters v1, ..., and vk for you. If you are not happy with
|
526 |
|
|
// the result of the type inference, you can specify the types by
|
527 |
|
|
// explicitly instantiating the template, as in Foo(5,
|
528 |
|
|
// false). As said earlier, you don't get to (or need to) specify
|
529 |
|
|
// 'arg_type' as that's determined by the context in which the matcher
|
530 |
|
|
// is used. You can assign the result of expression Foo(p1, ..., pk)
|
531 |
|
|
// to a variable of type FooMatcherPk. This
|
532 |
|
|
// can be useful when composing matchers.
|
533 |
|
|
//
|
534 |
|
|
// While you can instantiate a matcher template with reference types,
|
535 |
|
|
// passing the parameters by pointer usually makes your code more
|
536 |
|
|
// readable. If, however, you still want to pass a parameter by
|
537 |
|
|
// reference, be aware that in the failure message generated by the
|
538 |
|
|
// matcher you will see the value of the referenced object but not its
|
539 |
|
|
// address.
|
540 |
|
|
//
|
541 |
|
|
// Explaining Match Results
|
542 |
|
|
// ========================
|
543 |
|
|
//
|
544 |
|
|
// Sometimes the matcher description alone isn't enough to explain why
|
545 |
|
|
// the match has failed or succeeded. For example, when expecting a
|
546 |
|
|
// long string, it can be very helpful to also print the diff between
|
547 |
|
|
// the expected string and the actual one. To achieve that, you can
|
548 |
|
|
// optionally stream additional information to a special variable
|
549 |
|
|
// named result_listener, whose type is a pointer to class
|
550 |
|
|
// MatchResultListener:
|
551 |
|
|
//
|
552 |
|
|
// MATCHER_P(EqualsLongString, str, "") {
|
553 |
|
|
// if (arg == str) return true;
|
554 |
|
|
//
|
555 |
|
|
// *result_listener << "the difference: "
|
556 |
|
|
/// << DiffStrings(str, arg);
|
557 |
|
|
// return false;
|
558 |
|
|
// }
|
559 |
|
|
//
|
560 |
|
|
// Overloading Matchers
|
561 |
|
|
// ====================
|
562 |
|
|
//
|
563 |
|
|
// You can overload matchers with different numbers of parameters:
|
564 |
|
|
//
|
565 |
|
|
// MATCHER_P(Blah, a, description_string1) { ... }
|
566 |
|
|
// MATCHER_P2(Blah, a, b, description_string2) { ... }
|
567 |
|
|
//
|
568 |
|
|
// Caveats
|
569 |
|
|
// =======
|
570 |
|
|
//
|
571 |
|
|
// When defining a new matcher, you should also consider implementing
|
572 |
|
|
// MatcherInterface or using MakePolymorphicMatcher(). These
|
573 |
|
|
// approaches require more work than the MATCHER* macros, but also
|
574 |
|
|
// give you more control on the types of the value being matched and
|
575 |
|
|
// the matcher parameters, which may leads to better compiler error
|
576 |
|
|
// messages when the matcher is used wrong. They also allow
|
577 |
|
|
// overloading matchers based on parameter types (as opposed to just
|
578 |
|
|
// based on the number of parameters).
|
579 |
|
|
//
|
580 |
|
|
// MATCHER*() can only be used in a namespace scope. The reason is
|
581 |
|
|
// that C++ doesn't yet allow function-local types to be used to
|
582 |
|
|
// instantiate templates. The up-coming C++0x standard will fix this.
|
583 |
|
|
// Once that's done, we'll consider supporting using MATCHER*() inside
|
584 |
|
|
// a function.
|
585 |
|
|
//
|
586 |
|
|
// More Information
|
587 |
|
|
// ================
|
588 |
|
|
//
|
589 |
|
|
// To learn more about using these macros, please search for 'MATCHER'
|
590 |
|
|
// on http://code.google.com/p/googlemock/wiki/CookBook.
|
591 |
|
|
|
592 |
|
|
$range i 0..n
|
593 |
|
|
$for i
|
594 |
|
|
|
595 |
|
|
[[
|
596 |
|
|
$var macro_name = [[$if i==0 [[MATCHER]] $elif i==1 [[MATCHER_P]]
|
597 |
|
|
$else [[MATCHER_P$i]]]]
|
598 |
|
|
$var class_name = [[name##Matcher[[$if i==0 [[]] $elif i==1 [[P]]
|
599 |
|
|
$else [[P$i]]]]]]
|
600 |
|
|
$range j 0..i-1
|
601 |
|
|
$var template = [[$if i==0 [[]] $else [[
|
602 |
|
|
|
603 |
|
|
template <$for j, [[typename p$j##_type]]>\
|
604 |
|
|
]]]]
|
605 |
|
|
$var ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
|
606 |
|
|
$var impl_ctor_param_list = [[$for j, [[p$j##_type gmock_p$j]]]]
|
607 |
|
|
$var impl_inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
|
608 |
|
|
$var inits = [[$if i==0 [[]] $else [[ : $for j, [[p$j(gmock_p$j)]]]]]]
|
609 |
|
|
$var params = [[$for j, [[p$j]]]]
|
610 |
|
|
$var param_types = [[$if i==0 [[]] $else [[<$for j, [[p$j##_type]]>]]]]
|
611 |
|
|
$var param_types_and_names = [[$for j, [[p$j##_type p$j]]]]
|
612 |
|
|
$var param_field_decls = [[$for j
|
613 |
|
|
[[
|
614 |
|
|
|
615 |
|
|
p$j##_type p$j;\
|
616 |
|
|
]]]]
|
617 |
|
|
$var param_field_decls2 = [[$for j
|
618 |
|
|
[[
|
619 |
|
|
|
620 |
|
|
p$j##_type p$j;\
|
621 |
|
|
]]]]
|
622 |
|
|
|
623 |
|
|
#define $macro_name(name$for j [[, p$j]], description)\$template
|
624 |
|
|
class $class_name {\
|
625 |
|
|
public:\
|
626 |
|
|
template \
|
627 |
|
|
class gmock_Impl : public ::testing::MatcherInterface {\
|
628 |
|
|
public:\
|
629 |
|
|
[[$if i==1 [[explicit ]]]]gmock_Impl($impl_ctor_param_list)\
|
630 |
|
|
$impl_inits {}\
|
631 |
|
|
virtual bool MatchAndExplain(\
|
632 |
|
|
arg_type arg, ::testing::MatchResultListener* result_listener) const;\
|
633 |
|
|
virtual void DescribeTo(::std::ostream* gmock_os) const {\
|
634 |
|
|
*gmock_os << FormatDescription(false);\
|
635 |
|
|
}\
|
636 |
|
|
virtual void DescribeNegationTo(::std::ostream* gmock_os) const {\
|
637 |
|
|
*gmock_os << FormatDescription(true);\
|
638 |
|
|
}\$param_field_decls
|
639 |
|
|
private:\
|
640 |
|
|
::testing::internal::string FormatDescription(bool negation) const {\
|
641 |
|
|
const ::testing::internal::string gmock_description = (description);\
|
642 |
|
|
if (!gmock_description.empty())\
|
643 |
|
|
return gmock_description;\
|
644 |
|
|
return ::testing::internal::FormatMatcherDescription(\
|
645 |
|
|
negation, #name, \
|
646 |
|
|
::testing::internal::UniversalTersePrintTupleFieldsToStrings(\
|
647 |
|
|
::testing::tuple<$for j, [[p$j##_type]]>($for j, [[p$j]])));\
|
648 |
|
|
}\
|
649 |
|
|
GTEST_DISALLOW_ASSIGN_(gmock_Impl);\
|
650 |
|
|
};\
|
651 |
|
|
template \
|
652 |
|
|
operator ::testing::Matcher() const {\
|
653 |
|
|
return ::testing::Matcher(\
|
654 |
|
|
new gmock_Impl($params));\
|
655 |
|
|
}\
|
656 |
|
|
[[$if i==1 [[explicit ]]]]$class_name($ctor_param_list)$inits {\
|
657 |
|
|
}\$param_field_decls2
|
658 |
|
|
private:\
|
659 |
|
|
GTEST_DISALLOW_ASSIGN_($class_name);\
|
660 |
|
|
};\$template
|
661 |
|
|
inline $class_name$param_types name($param_types_and_names) {\
|
662 |
|
|
return $class_name$param_types($params);\
|
663 |
|
|
}\$template
|
664 |
|
|
template \
|
665 |
|
|
bool $class_name$param_types::gmock_Impl::MatchAndExplain(\
|
666 |
|
|
arg_type arg, \
|
667 |
|
|
::testing::MatchResultListener* result_listener GTEST_ATTRIBUTE_UNUSED_)\
|
668 |
|
|
const
|
669 |
|
|
]]
|
670 |
|
|
|
671 |
|
|
|
672 |
|
|
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_MATCHERS_H_
|