1 | // Copyright 2008, 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 | // Google Mock - a framework for writing C++ mock classes. |
31 | // |
32 | // This file tests the built-in matchers generated by a script. |
33 | |
34 | // Silence warning C4244: 'initializing': conversion from 'int' to 'short', |
35 | // possible loss of data and C4100, unreferenced local parameter |
36 | #ifdef _MSC_VER |
37 | # pragma warning(push) |
38 | # pragma warning(disable:4244) |
39 | # pragma warning(disable:4100) |
40 | #endif |
41 | |
42 | #include "gmock/gmock-generated-matchers.h" |
43 | |
44 | #include <list> |
45 | #include <map> |
46 | #include <memory> |
47 | #include <set> |
48 | #include <sstream> |
49 | #include <string> |
50 | #include <utility> |
51 | #include <vector> |
52 | |
53 | #include "gmock/gmock.h" |
54 | #include "gtest/gtest.h" |
55 | #include "gtest/gtest-spi.h" |
56 | |
57 | namespace { |
58 | |
59 | using std::list; |
60 | using std::map; |
61 | using std::pair; |
62 | using std::set; |
63 | using std::stringstream; |
64 | using std::vector; |
65 | using testing::_; |
66 | using testing::AllOf; |
67 | using testing::AllOfArray; |
68 | using testing::AnyOf; |
69 | using testing::AnyOfArray; |
70 | using testing::Args; |
71 | using testing::Contains; |
72 | using testing::ElementsAre; |
73 | using testing::ElementsAreArray; |
74 | using testing::Eq; |
75 | using testing::Ge; |
76 | using testing::Gt; |
77 | using testing::Le; |
78 | using testing::Lt; |
79 | using testing::MakeMatcher; |
80 | using testing::Matcher; |
81 | using testing::MatcherInterface; |
82 | using testing::MatchResultListener; |
83 | using testing::Ne; |
84 | using testing::Not; |
85 | using testing::Pointee; |
86 | using testing::PrintToString; |
87 | using testing::Ref; |
88 | using testing::StaticAssertTypeEq; |
89 | using testing::StrEq; |
90 | using testing::Value; |
91 | using testing::internal::ElementsAreArrayMatcher; |
92 | |
93 | // Returns the description of the given matcher. |
94 | template <typename T> |
95 | std::string Describe(const Matcher<T>& m) { |
96 | stringstream ss; |
97 | m.DescribeTo(&ss); |
98 | return ss.str(); |
99 | } |
100 | |
101 | // Returns the description of the negation of the given matcher. |
102 | template <typename T> |
103 | std::string DescribeNegation(const Matcher<T>& m) { |
104 | stringstream ss; |
105 | m.DescribeNegationTo(&ss); |
106 | return ss.str(); |
107 | } |
108 | |
109 | // Returns the reason why x matches, or doesn't match, m. |
110 | template <typename MatcherType, typename Value> |
111 | std::string Explain(const MatcherType& m, const Value& x) { |
112 | stringstream ss; |
113 | m.ExplainMatchResultTo(x, &ss); |
114 | return ss.str(); |
115 | } |
116 | |
117 | // For testing ExplainMatchResultTo(). |
118 | class GreaterThanMatcher : public MatcherInterface<int> { |
119 | public: |
120 | explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} |
121 | |
122 | void DescribeTo(::std::ostream* os) const override { |
123 | *os << "is greater than " << rhs_; |
124 | } |
125 | |
126 | bool MatchAndExplain(int lhs, MatchResultListener* listener) const override { |
127 | const int diff = lhs - rhs_; |
128 | if (diff > 0) { |
129 | *listener << "which is " << diff << " more than " << rhs_; |
130 | } else if (diff == 0) { |
131 | *listener << "which is the same as " << rhs_; |
132 | } else { |
133 | *listener << "which is " << -diff << " less than " << rhs_; |
134 | } |
135 | |
136 | return lhs > rhs_; |
137 | } |
138 | |
139 | private: |
140 | int rhs_; |
141 | }; |
142 | |
143 | Matcher<int> GreaterThan(int n) { |
144 | return MakeMatcher(new GreaterThanMatcher(n)); |
145 | } |
146 | |
147 | // Tests for ElementsAre(). |
148 | |
149 | TEST(ElementsAreTest, CanDescribeExpectingNoElement) { |
150 | Matcher<const vector<int>&> m = ElementsAre(); |
151 | EXPECT_EQ("is empty" , Describe(m)); |
152 | } |
153 | |
154 | TEST(ElementsAreTest, CanDescribeExpectingOneElement) { |
155 | Matcher<vector<int> > m = ElementsAre(Gt(5)); |
156 | EXPECT_EQ("has 1 element that is > 5" , Describe(m)); |
157 | } |
158 | |
159 | TEST(ElementsAreTest, CanDescribeExpectingManyElements) { |
160 | Matcher<list<std::string> > m = ElementsAre(StrEq("one" ), "two" ); |
161 | EXPECT_EQ("has 2 elements where\n" |
162 | "element #0 is equal to \"one\",\n" |
163 | "element #1 is equal to \"two\"" , Describe(m)); |
164 | } |
165 | |
166 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingNoElement) { |
167 | Matcher<vector<int> > m = ElementsAre(); |
168 | EXPECT_EQ("isn't empty" , DescribeNegation(m)); |
169 | } |
170 | |
171 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingOneElment) { |
172 | Matcher<const list<int>& > m = ElementsAre(Gt(5)); |
173 | EXPECT_EQ("doesn't have 1 element, or\n" |
174 | "element #0 isn't > 5" , DescribeNegation(m)); |
175 | } |
176 | |
177 | TEST(ElementsAreTest, CanDescribeNegationOfExpectingManyElements) { |
178 | Matcher<const list<std::string>&> m = ElementsAre("one" , "two" ); |
179 | EXPECT_EQ("doesn't have 2 elements, or\n" |
180 | "element #0 isn't equal to \"one\", or\n" |
181 | "element #1 isn't equal to \"two\"" , DescribeNegation(m)); |
182 | } |
183 | |
184 | TEST(ElementsAreTest, DoesNotExplainTrivialMatch) { |
185 | Matcher<const list<int>& > m = ElementsAre(1, Ne(2)); |
186 | |
187 | list<int> test_list; |
188 | test_list.push_back(1); |
189 | test_list.push_back(3); |
190 | EXPECT_EQ("" , Explain(m, test_list)); // No need to explain anything. |
191 | } |
192 | |
193 | TEST(ElementsAreTest, ExplainsNonTrivialMatch) { |
194 | Matcher<const vector<int>& > m = |
195 | ElementsAre(GreaterThan(1), 0, GreaterThan(2)); |
196 | |
197 | const int a[] = { 10, 0, 100 }; |
198 | vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
199 | EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n" |
200 | "and whose element #2 matches, which is 98 more than 2" , |
201 | Explain(m, test_vector)); |
202 | } |
203 | |
204 | TEST(ElementsAreTest, CanExplainMismatchWrongSize) { |
205 | Matcher<const list<int>& > m = ElementsAre(1, 3); |
206 | |
207 | list<int> test_list; |
208 | // No need to explain when the container is empty. |
209 | EXPECT_EQ("" , Explain(m, test_list)); |
210 | |
211 | test_list.push_back(1); |
212 | EXPECT_EQ("which has 1 element" , Explain(m, test_list)); |
213 | } |
214 | |
215 | TEST(ElementsAreTest, CanExplainMismatchRightSize) { |
216 | Matcher<const vector<int>& > m = ElementsAre(1, GreaterThan(5)); |
217 | |
218 | vector<int> v; |
219 | v.push_back(2); |
220 | v.push_back(1); |
221 | EXPECT_EQ("whose element #0 doesn't match" , Explain(m, v)); |
222 | |
223 | v[0] = 1; |
224 | EXPECT_EQ("whose element #1 doesn't match, which is 4 less than 5" , |
225 | Explain(m, v)); |
226 | } |
227 | |
228 | TEST(ElementsAreTest, MatchesOneElementVector) { |
229 | vector<std::string> test_vector; |
230 | test_vector.push_back("test string" ); |
231 | |
232 | EXPECT_THAT(test_vector, ElementsAre(StrEq("test string" ))); |
233 | } |
234 | |
235 | TEST(ElementsAreTest, MatchesOneElementList) { |
236 | list<std::string> test_list; |
237 | test_list.push_back("test string" ); |
238 | |
239 | EXPECT_THAT(test_list, ElementsAre("test string" )); |
240 | } |
241 | |
242 | TEST(ElementsAreTest, MatchesThreeElementVector) { |
243 | vector<std::string> test_vector; |
244 | test_vector.push_back("one" ); |
245 | test_vector.push_back("two" ); |
246 | test_vector.push_back("three" ); |
247 | |
248 | EXPECT_THAT(test_vector, ElementsAre("one" , StrEq("two" ), _)); |
249 | } |
250 | |
251 | TEST(ElementsAreTest, MatchesOneElementEqMatcher) { |
252 | vector<int> test_vector; |
253 | test_vector.push_back(4); |
254 | |
255 | EXPECT_THAT(test_vector, ElementsAre(Eq(4))); |
256 | } |
257 | |
258 | TEST(ElementsAreTest, MatchesOneElementAnyMatcher) { |
259 | vector<int> test_vector; |
260 | test_vector.push_back(4); |
261 | |
262 | EXPECT_THAT(test_vector, ElementsAre(_)); |
263 | } |
264 | |
265 | TEST(ElementsAreTest, MatchesOneElementValue) { |
266 | vector<int> test_vector; |
267 | test_vector.push_back(4); |
268 | |
269 | EXPECT_THAT(test_vector, ElementsAre(4)); |
270 | } |
271 | |
272 | TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { |
273 | vector<int> test_vector; |
274 | test_vector.push_back(1); |
275 | test_vector.push_back(2); |
276 | test_vector.push_back(3); |
277 | |
278 | EXPECT_THAT(test_vector, ElementsAre(1, Eq(2), _)); |
279 | } |
280 | |
281 | TEST(ElementsAreTest, MatchesTenElementVector) { |
282 | const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; |
283 | vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
284 | |
285 | EXPECT_THAT(test_vector, |
286 | // The element list can contain values and/or matchers |
287 | // of different types. |
288 | ElementsAre(0, Ge(0), _, 3, 4, Ne(2), Eq(6), 7, 8, _)); |
289 | } |
290 | |
291 | TEST(ElementsAreTest, DoesNotMatchWrongSize) { |
292 | vector<std::string> test_vector; |
293 | test_vector.push_back("test string" ); |
294 | test_vector.push_back("test string" ); |
295 | |
296 | Matcher<vector<std::string> > m = ElementsAre(StrEq("test string" )); |
297 | EXPECT_FALSE(m.Matches(test_vector)); |
298 | } |
299 | |
300 | TEST(ElementsAreTest, DoesNotMatchWrongValue) { |
301 | vector<std::string> test_vector; |
302 | test_vector.push_back("other string" ); |
303 | |
304 | Matcher<vector<std::string> > m = ElementsAre(StrEq("test string" )); |
305 | EXPECT_FALSE(m.Matches(test_vector)); |
306 | } |
307 | |
308 | TEST(ElementsAreTest, DoesNotMatchWrongOrder) { |
309 | vector<std::string> test_vector; |
310 | test_vector.push_back("one" ); |
311 | test_vector.push_back("three" ); |
312 | test_vector.push_back("two" ); |
313 | |
314 | Matcher<vector<std::string> > m = |
315 | ElementsAre(StrEq("one" ), StrEq("two" ), StrEq("three" )); |
316 | EXPECT_FALSE(m.Matches(test_vector)); |
317 | } |
318 | |
319 | TEST(ElementsAreTest, WorksForNestedContainer) { |
320 | const char* strings[] = { |
321 | "Hi" , |
322 | "world" |
323 | }; |
324 | |
325 | vector<list<char> > nested; |
326 | for (size_t i = 0; i < GTEST_ARRAY_SIZE_(strings); i++) { |
327 | nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); |
328 | } |
329 | |
330 | EXPECT_THAT(nested, ElementsAre(ElementsAre('H', Ne('e')), |
331 | ElementsAre('w', 'o', _, _, 'd'))); |
332 | EXPECT_THAT(nested, Not(ElementsAre(ElementsAre('H', 'e'), |
333 | ElementsAre('w', 'o', _, _, 'd')))); |
334 | } |
335 | |
336 | TEST(ElementsAreTest, WorksWithByRefElementMatchers) { |
337 | int a[] = { 0, 1, 2 }; |
338 | vector<int> v(a, a + GTEST_ARRAY_SIZE_(a)); |
339 | |
340 | EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); |
341 | EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); |
342 | } |
343 | |
344 | TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { |
345 | int a[] = { 0, 1, 2 }; |
346 | vector<int> v(a, a + GTEST_ARRAY_SIZE_(a)); |
347 | |
348 | EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); |
349 | EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); |
350 | } |
351 | |
352 | TEST(ElementsAreTest, WorksWithNativeArrayPassedByReference) { |
353 | int array[] = { 0, 1, 2 }; |
354 | EXPECT_THAT(array, ElementsAre(0, 1, _)); |
355 | EXPECT_THAT(array, Not(ElementsAre(1, _, _))); |
356 | EXPECT_THAT(array, Not(ElementsAre(0, _))); |
357 | } |
358 | |
359 | class NativeArrayPassedAsPointerAndSize { |
360 | public: |
361 | NativeArrayPassedAsPointerAndSize() {} |
362 | |
363 | MOCK_METHOD2(Helper, void(int* array, int size)); |
364 | |
365 | private: |
366 | GTEST_DISALLOW_COPY_AND_ASSIGN_(NativeArrayPassedAsPointerAndSize); |
367 | }; |
368 | |
369 | TEST(ElementsAreTest, WorksWithNativeArrayPassedAsPointerAndSize) { |
370 | int array[] = { 0, 1 }; |
371 | ::std::tuple<int*, size_t> array_as_tuple(array, 2); |
372 | EXPECT_THAT(array_as_tuple, ElementsAre(0, 1)); |
373 | EXPECT_THAT(array_as_tuple, Not(ElementsAre(0))); |
374 | |
375 | NativeArrayPassedAsPointerAndSize helper; |
376 | EXPECT_CALL(helper, Helper(_, _)) |
377 | .With(ElementsAre(0, 1)); |
378 | helper.Helper(array, 2); |
379 | } |
380 | |
381 | TEST(ElementsAreTest, WorksWithTwoDimensionalNativeArray) { |
382 | const char a2[][3] = { "hi" , "lo" }; |
383 | EXPECT_THAT(a2, ElementsAre(ElementsAre('h', 'i', '\0'), |
384 | ElementsAre('l', 'o', '\0'))); |
385 | EXPECT_THAT(a2, ElementsAre(StrEq("hi" ), StrEq("lo" ))); |
386 | EXPECT_THAT(a2, ElementsAre(Not(ElementsAre('h', 'o', '\0')), |
387 | ElementsAre('l', 'o', '\0'))); |
388 | } |
389 | |
390 | TEST(ElementsAreTest, AcceptsStringLiteral) { |
391 | std::string array[] = {"hi" , "one" , "two" }; |
392 | EXPECT_THAT(array, ElementsAre("hi" , "one" , "two" )); |
393 | EXPECT_THAT(array, Not(ElementsAre("hi" , "one" , "too" ))); |
394 | } |
395 | |
396 | #ifndef _MSC_VER |
397 | |
398 | // The following test passes a value of type const char[] to a |
399 | // function template that expects const T&. Some versions of MSVC |
400 | // generates a compiler error C2665 for that. We believe it's a bug |
401 | // in MSVC. Therefore this test is #if-ed out for MSVC. |
402 | |
403 | // Declared here with the size unknown. Defined AFTER the following test. |
404 | extern const char kHi[]; |
405 | |
406 | TEST(ElementsAreTest, AcceptsArrayWithUnknownSize) { |
407 | // The size of kHi is not known in this test, but ElementsAre() should |
408 | // still accept it. |
409 | |
410 | std::string array1[] = {"hi" }; |
411 | EXPECT_THAT(array1, ElementsAre(kHi)); |
412 | |
413 | std::string array2[] = {"ho" }; |
414 | EXPECT_THAT(array2, Not(ElementsAre(kHi))); |
415 | } |
416 | |
417 | const char kHi[] = "hi" ; |
418 | |
419 | #endif // _MSC_VER |
420 | |
421 | TEST(ElementsAreTest, MakesCopyOfArguments) { |
422 | int x = 1; |
423 | int y = 2; |
424 | // This should make a copy of x and y. |
425 | ::testing::internal::ElementsAreMatcher<std::tuple<int, int> > |
426 | polymorphic_matcher = ElementsAre(x, y); |
427 | // Changing x and y now shouldn't affect the meaning of the above matcher. |
428 | x = y = 0; |
429 | const int array1[] = { 1, 2 }; |
430 | EXPECT_THAT(array1, polymorphic_matcher); |
431 | const int array2[] = { 0, 0 }; |
432 | EXPECT_THAT(array2, Not(polymorphic_matcher)); |
433 | } |
434 | |
435 | |
436 | // Tests for ElementsAreArray(). Since ElementsAreArray() shares most |
437 | // of the implementation with ElementsAre(), we don't test it as |
438 | // thoroughly here. |
439 | |
440 | TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { |
441 | const int a[] = { 1, 2, 3 }; |
442 | |
443 | vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
444 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
445 | |
446 | test_vector[2] = 0; |
447 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
448 | } |
449 | |
450 | TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { |
451 | const char* a[] = { "one" , "two" , "three" }; |
452 | |
453 | vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
454 | EXPECT_THAT(test_vector, ElementsAreArray(a, GTEST_ARRAY_SIZE_(a))); |
455 | |
456 | const char** p = a; |
457 | test_vector[0] = "1" ; |
458 | EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GTEST_ARRAY_SIZE_(a)))); |
459 | } |
460 | |
461 | TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { |
462 | const char* a[] = { "one" , "two" , "three" }; |
463 | |
464 | vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
465 | EXPECT_THAT(test_vector, ElementsAreArray(a)); |
466 | |
467 | test_vector[0] = "1" ; |
468 | EXPECT_THAT(test_vector, Not(ElementsAreArray(a))); |
469 | } |
470 | |
471 | TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { |
472 | const Matcher<std::string> kMatcherArray[] = {StrEq("one" ), StrEq("two" ), |
473 | StrEq("three" )}; |
474 | |
475 | vector<std::string> test_vector; |
476 | test_vector.push_back("one" ); |
477 | test_vector.push_back("two" ); |
478 | test_vector.push_back("three" ); |
479 | EXPECT_THAT(test_vector, ElementsAreArray(kMatcherArray)); |
480 | |
481 | test_vector.push_back("three" ); |
482 | EXPECT_THAT(test_vector, Not(ElementsAreArray(kMatcherArray))); |
483 | } |
484 | |
485 | TEST(ElementsAreArrayTest, CanBeCreatedWithVector) { |
486 | const int a[] = { 1, 2, 3 }; |
487 | vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
488 | const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a)); |
489 | EXPECT_THAT(test_vector, ElementsAreArray(expected)); |
490 | test_vector.push_back(4); |
491 | EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); |
492 | } |
493 | |
494 | |
495 | TEST(ElementsAreArrayTest, TakesInitializerList) { |
496 | const int a[5] = { 1, 2, 3, 4, 5 }; |
497 | EXPECT_THAT(a, ElementsAreArray({ 1, 2, 3, 4, 5 })); |
498 | EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 5, 4 }))); |
499 | EXPECT_THAT(a, Not(ElementsAreArray({ 1, 2, 3, 4, 6 }))); |
500 | } |
501 | |
502 | TEST(ElementsAreArrayTest, TakesInitializerListOfCStrings) { |
503 | const std::string a[5] = {"a" , "b" , "c" , "d" , "e" }; |
504 | EXPECT_THAT(a, ElementsAreArray({ "a" , "b" , "c" , "d" , "e" })); |
505 | EXPECT_THAT(a, Not(ElementsAreArray({ "a" , "b" , "c" , "e" , "d" }))); |
506 | EXPECT_THAT(a, Not(ElementsAreArray({ "a" , "b" , "c" , "d" , "ef" }))); |
507 | } |
508 | |
509 | TEST(ElementsAreArrayTest, TakesInitializerListOfSameTypedMatchers) { |
510 | const int a[5] = { 1, 2, 3, 4, 5 }; |
511 | EXPECT_THAT(a, ElementsAreArray( |
512 | { Eq(1), Eq(2), Eq(3), Eq(4), Eq(5) })); |
513 | EXPECT_THAT(a, Not(ElementsAreArray( |
514 | { Eq(1), Eq(2), Eq(3), Eq(4), Eq(6) }))); |
515 | } |
516 | |
517 | TEST(ElementsAreArrayTest, |
518 | TakesInitializerListOfDifferentTypedMatchers) { |
519 | const int a[5] = { 1, 2, 3, 4, 5 }; |
520 | // The compiler cannot infer the type of the initializer list if its |
521 | // elements have different types. We must explicitly specify the |
522 | // unified element type in this case. |
523 | EXPECT_THAT(a, ElementsAreArray<Matcher<int> >( |
524 | { Eq(1), Ne(-2), Ge(3), Le(4), Eq(5) })); |
525 | EXPECT_THAT(a, Not(ElementsAreArray<Matcher<int> >( |
526 | { Eq(1), Ne(-2), Ge(3), Le(4), Eq(6) }))); |
527 | } |
528 | |
529 | |
530 | TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) { |
531 | const int a[] = { 1, 2, 3 }; |
532 | const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) }; |
533 | vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
534 | const vector<Matcher<int> > expected( |
535 | kMatchers, kMatchers + GTEST_ARRAY_SIZE_(kMatchers)); |
536 | EXPECT_THAT(test_vector, ElementsAreArray(expected)); |
537 | test_vector.push_back(4); |
538 | EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); |
539 | } |
540 | |
541 | TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) { |
542 | const int a[] = { 1, 2, 3 }; |
543 | const vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
544 | const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a)); |
545 | EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end())); |
546 | // Pointers are iterators, too. |
547 | EXPECT_THAT(test_vector, ElementsAreArray(a, a + GTEST_ARRAY_SIZE_(a))); |
548 | // The empty range of NULL pointers should also be okay. |
549 | int* const null_int = nullptr; |
550 | EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int))); |
551 | EXPECT_THAT((vector<int>()), ElementsAreArray(null_int, null_int)); |
552 | } |
553 | |
554 | // Since ElementsAre() and ElementsAreArray() share much of the |
555 | // implementation, we only do a sanity test for native arrays here. |
556 | TEST(ElementsAreArrayTest, WorksWithNativeArray) { |
557 | ::std::string a[] = { "hi" , "ho" }; |
558 | ::std::string b[] = { "hi" , "ho" }; |
559 | |
560 | EXPECT_THAT(a, ElementsAreArray(b)); |
561 | EXPECT_THAT(a, ElementsAreArray(b, 2)); |
562 | EXPECT_THAT(a, Not(ElementsAreArray(b, 1))); |
563 | } |
564 | |
565 | TEST(ElementsAreArrayTest, SourceLifeSpan) { |
566 | const int a[] = { 1, 2, 3 }; |
567 | vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); |
568 | vector<int> expect(a, a + GTEST_ARRAY_SIZE_(a)); |
569 | ElementsAreArrayMatcher<int> matcher_maker = |
570 | ElementsAreArray(expect.begin(), expect.end()); |
571 | EXPECT_THAT(test_vector, matcher_maker); |
572 | // Changing in place the values that initialized matcher_maker should not |
573 | // affect matcher_maker anymore. It should have made its own copy of them. |
574 | typedef vector<int>::iterator Iter; |
575 | for (Iter it = expect.begin(); it != expect.end(); ++it) { *it += 10; } |
576 | EXPECT_THAT(test_vector, matcher_maker); |
577 | test_vector.push_back(3); |
578 | EXPECT_THAT(test_vector, Not(matcher_maker)); |
579 | } |
580 | |
581 | // Tests for the MATCHER*() macro family. |
582 | |
583 | // Tests that a simple MATCHER() definition works. |
584 | |
585 | MATCHER(IsEven, "" ) { return (arg % 2) == 0; } |
586 | |
587 | TEST(MatcherMacroTest, Works) { |
588 | const Matcher<int> m = IsEven(); |
589 | EXPECT_TRUE(m.Matches(6)); |
590 | EXPECT_FALSE(m.Matches(7)); |
591 | |
592 | EXPECT_EQ("is even" , Describe(m)); |
593 | EXPECT_EQ("not (is even)" , DescribeNegation(m)); |
594 | EXPECT_EQ("" , Explain(m, 6)); |
595 | EXPECT_EQ("" , Explain(m, 7)); |
596 | } |
597 | |
598 | // This also tests that the description string can reference 'negation'. |
599 | MATCHER(IsEven2, negation ? "is odd" : "is even" ) { |
600 | if ((arg % 2) == 0) { |
601 | // Verifies that we can stream to result_listener, a listener |
602 | // supplied by the MATCHER macro implicitly. |
603 | *result_listener << "OK" ; |
604 | return true; |
605 | } else { |
606 | *result_listener << "% 2 == " << (arg % 2); |
607 | return false; |
608 | } |
609 | } |
610 | |
611 | // This also tests that the description string can reference matcher |
612 | // parameters. |
613 | MATCHER_P2(EqSumOf, x, y, std::string(negation ? "doesn't equal" : "equals" ) + |
614 | " the sum of " + PrintToString(x) + " and " + |
615 | PrintToString(y)) { |
616 | if (arg == (x + y)) { |
617 | *result_listener << "OK" ; |
618 | return true; |
619 | } else { |
620 | // Verifies that we can stream to the underlying stream of |
621 | // result_listener. |
622 | if (result_listener->stream() != nullptr) { |
623 | *result_listener->stream() << "diff == " << (x + y - arg); |
624 | } |
625 | return false; |
626 | } |
627 | } |
628 | |
629 | // Tests that the matcher description can reference 'negation' and the |
630 | // matcher parameters. |
631 | TEST(MatcherMacroTest, DescriptionCanReferenceNegationAndParameters) { |
632 | const Matcher<int> m1 = IsEven2(); |
633 | EXPECT_EQ("is even" , Describe(m1)); |
634 | EXPECT_EQ("is odd" , DescribeNegation(m1)); |
635 | |
636 | const Matcher<int> m2 = EqSumOf(5, 9); |
637 | EXPECT_EQ("equals the sum of 5 and 9" , Describe(m2)); |
638 | EXPECT_EQ("doesn't equal the sum of 5 and 9" , DescribeNegation(m2)); |
639 | } |
640 | |
641 | // Tests explaining match result in a MATCHER* macro. |
642 | TEST(MatcherMacroTest, CanExplainMatchResult) { |
643 | const Matcher<int> m1 = IsEven2(); |
644 | EXPECT_EQ("OK" , Explain(m1, 4)); |
645 | EXPECT_EQ("% 2 == 1" , Explain(m1, 5)); |
646 | |
647 | const Matcher<int> m2 = EqSumOf(1, 2); |
648 | EXPECT_EQ("OK" , Explain(m2, 3)); |
649 | EXPECT_EQ("diff == -1" , Explain(m2, 4)); |
650 | } |
651 | |
652 | // Tests that the body of MATCHER() can reference the type of the |
653 | // value being matched. |
654 | |
655 | MATCHER(IsEmptyString, "" ) { |
656 | StaticAssertTypeEq< ::std::string, arg_type>(); |
657 | return arg == "" ; |
658 | } |
659 | |
660 | MATCHER(IsEmptyStringByRef, "" ) { |
661 | StaticAssertTypeEq<const ::std::string&, arg_type>(); |
662 | return arg == "" ; |
663 | } |
664 | |
665 | TEST(MatcherMacroTest, CanReferenceArgType) { |
666 | const Matcher< ::std::string> m1 = IsEmptyString(); |
667 | EXPECT_TRUE(m1.Matches("" )); |
668 | |
669 | const Matcher<const ::std::string&> m2 = IsEmptyStringByRef(); |
670 | EXPECT_TRUE(m2.Matches("" )); |
671 | } |
672 | |
673 | // Tests that MATCHER() can be used in a namespace. |
674 | |
675 | namespace matcher_test { |
676 | MATCHER(IsOdd, "" ) { return (arg % 2) != 0; } |
677 | } // namespace matcher_test |
678 | |
679 | TEST(MatcherMacroTest, WorksInNamespace) { |
680 | Matcher<int> m = matcher_test::IsOdd(); |
681 | EXPECT_FALSE(m.Matches(4)); |
682 | EXPECT_TRUE(m.Matches(5)); |
683 | } |
684 | |
685 | // Tests that Value() can be used to compose matchers. |
686 | MATCHER(IsPositiveOdd, "" ) { |
687 | return Value(arg, matcher_test::IsOdd()) && arg > 0; |
688 | } |
689 | |
690 | TEST(MatcherMacroTest, CanBeComposedUsingValue) { |
691 | EXPECT_THAT(3, IsPositiveOdd()); |
692 | EXPECT_THAT(4, Not(IsPositiveOdd())); |
693 | EXPECT_THAT(-1, Not(IsPositiveOdd())); |
694 | } |
695 | |
696 | // Tests that a simple MATCHER_P() definition works. |
697 | |
698 | MATCHER_P(IsGreaterThan32And, n, "" ) { return arg > 32 && arg > n; } |
699 | |
700 | TEST(MatcherPMacroTest, Works) { |
701 | const Matcher<int> m = IsGreaterThan32And(5); |
702 | EXPECT_TRUE(m.Matches(36)); |
703 | EXPECT_FALSE(m.Matches(5)); |
704 | |
705 | EXPECT_EQ("is greater than 32 and 5" , Describe(m)); |
706 | EXPECT_EQ("not (is greater than 32 and 5)" , DescribeNegation(m)); |
707 | EXPECT_EQ("" , Explain(m, 36)); |
708 | EXPECT_EQ("" , Explain(m, 5)); |
709 | } |
710 | |
711 | // Tests that the description is calculated correctly from the matcher name. |
712 | MATCHER_P(_is_Greater_Than32and_, n, "" ) { return arg > 32 && arg > n; } |
713 | |
714 | TEST(MatcherPMacroTest, GeneratesCorrectDescription) { |
715 | const Matcher<int> m = _is_Greater_Than32and_(5); |
716 | |
717 | EXPECT_EQ("is greater than 32 and 5" , Describe(m)); |
718 | EXPECT_EQ("not (is greater than 32 and 5)" , DescribeNegation(m)); |
719 | EXPECT_EQ("" , Explain(m, 36)); |
720 | EXPECT_EQ("" , Explain(m, 5)); |
721 | } |
722 | |
723 | // Tests that a MATCHER_P matcher can be explicitly instantiated with |
724 | // a reference parameter type. |
725 | |
726 | class UncopyableFoo { |
727 | public: |
728 | explicit UncopyableFoo(char value) : value_(value) {} |
729 | private: |
730 | UncopyableFoo(const UncopyableFoo&); |
731 | void operator=(const UncopyableFoo&); |
732 | |
733 | char value_; |
734 | }; |
735 | |
736 | MATCHER_P(ReferencesUncopyable, variable, "" ) { return &arg == &variable; } |
737 | |
738 | TEST(MatcherPMacroTest, WorksWhenExplicitlyInstantiatedWithReference) { |
739 | UncopyableFoo foo1('1'), foo2('2'); |
740 | const Matcher<const UncopyableFoo&> m = |
741 | ReferencesUncopyable<const UncopyableFoo&>(foo1); |
742 | |
743 | EXPECT_TRUE(m.Matches(foo1)); |
744 | EXPECT_FALSE(m.Matches(foo2)); |
745 | |
746 | // We don't want the address of the parameter printed, as most |
747 | // likely it will just annoy the user. If the address is |
748 | // interesting, the user should consider passing the parameter by |
749 | // pointer instead. |
750 | EXPECT_EQ("references uncopyable 1-byte object <31>" , Describe(m)); |
751 | } |
752 | |
753 | |
754 | // Tests that the body of MATCHER_Pn() can reference the parameter |
755 | // types. |
756 | |
757 | MATCHER_P3(ParamTypesAreIntLongAndChar, foo, bar, baz, "" ) { |
758 | StaticAssertTypeEq<int, foo_type>(); |
759 | StaticAssertTypeEq<long, bar_type>(); // NOLINT |
760 | StaticAssertTypeEq<char, baz_type>(); |
761 | return arg == 0; |
762 | } |
763 | |
764 | TEST(MatcherPnMacroTest, CanReferenceParamTypes) { |
765 | EXPECT_THAT(0, ParamTypesAreIntLongAndChar(10, 20L, 'a')); |
766 | } |
767 | |
768 | // Tests that a MATCHER_Pn matcher can be explicitly instantiated with |
769 | // reference parameter types. |
770 | |
771 | MATCHER_P2(ReferencesAnyOf, variable1, variable2, "" ) { |
772 | return &arg == &variable1 || &arg == &variable2; |
773 | } |
774 | |
775 | TEST(MatcherPnMacroTest, WorksWhenExplicitlyInstantiatedWithReferences) { |
776 | UncopyableFoo foo1('1'), foo2('2'), foo3('3'); |
777 | const Matcher<const UncopyableFoo&> m = |
778 | ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
779 | |
780 | EXPECT_TRUE(m.Matches(foo1)); |
781 | EXPECT_TRUE(m.Matches(foo2)); |
782 | EXPECT_FALSE(m.Matches(foo3)); |
783 | } |
784 | |
785 | TEST(MatcherPnMacroTest, |
786 | GeneratesCorretDescriptionWhenExplicitlyInstantiatedWithReferences) { |
787 | UncopyableFoo foo1('1'), foo2('2'); |
788 | const Matcher<const UncopyableFoo&> m = |
789 | ReferencesAnyOf<const UncopyableFoo&, const UncopyableFoo&>(foo1, foo2); |
790 | |
791 | // We don't want the addresses of the parameters printed, as most |
792 | // likely they will just annoy the user. If the addresses are |
793 | // interesting, the user should consider passing the parameters by |
794 | // pointers instead. |
795 | EXPECT_EQ("references any of (1-byte object <31>, 1-byte object <32>)" , |
796 | Describe(m)); |
797 | } |
798 | |
799 | // Tests that a simple MATCHER_P2() definition works. |
800 | |
801 | MATCHER_P2(IsNotInClosedRange, low, hi, "" ) { return arg < low || arg > hi; } |
802 | |
803 | TEST(MatcherPnMacroTest, Works) { |
804 | const Matcher<const long&> m = IsNotInClosedRange(10, 20); // NOLINT |
805 | EXPECT_TRUE(m.Matches(36L)); |
806 | EXPECT_FALSE(m.Matches(15L)); |
807 | |
808 | EXPECT_EQ("is not in closed range (10, 20)" , Describe(m)); |
809 | EXPECT_EQ("not (is not in closed range (10, 20))" , DescribeNegation(m)); |
810 | EXPECT_EQ("" , Explain(m, 36L)); |
811 | EXPECT_EQ("" , Explain(m, 15L)); |
812 | } |
813 | |
814 | // Tests that MATCHER*() definitions can be overloaded on the number |
815 | // of parameters; also tests MATCHER_Pn() where n >= 3. |
816 | |
817 | MATCHER(EqualsSumOf, "" ) { return arg == 0; } |
818 | MATCHER_P(EqualsSumOf, a, "" ) { return arg == a; } |
819 | MATCHER_P2(EqualsSumOf, a, b, "" ) { return arg == a + b; } |
820 | MATCHER_P3(EqualsSumOf, a, b, c, "" ) { return arg == a + b + c; } |
821 | MATCHER_P4(EqualsSumOf, a, b, c, d, "" ) { return arg == a + b + c + d; } |
822 | MATCHER_P5(EqualsSumOf, a, b, c, d, e, "" ) { return arg == a + b + c + d + e; } |
823 | MATCHER_P6(EqualsSumOf, a, b, c, d, e, f, "" ) { |
824 | return arg == a + b + c + d + e + f; |
825 | } |
826 | MATCHER_P7(EqualsSumOf, a, b, c, d, e, f, g, "" ) { |
827 | return arg == a + b + c + d + e + f + g; |
828 | } |
829 | MATCHER_P8(EqualsSumOf, a, b, c, d, e, f, g, h, "" ) { |
830 | return arg == a + b + c + d + e + f + g + h; |
831 | } |
832 | MATCHER_P9(EqualsSumOf, a, b, c, d, e, f, g, h, i, "" ) { |
833 | return arg == a + b + c + d + e + f + g + h + i; |
834 | } |
835 | MATCHER_P10(EqualsSumOf, a, b, c, d, e, f, g, h, i, j, "" ) { |
836 | return arg == a + b + c + d + e + f + g + h + i + j; |
837 | } |
838 | |
839 | TEST(MatcherPnMacroTest, CanBeOverloadedOnNumberOfParameters) { |
840 | EXPECT_THAT(0, EqualsSumOf()); |
841 | EXPECT_THAT(1, EqualsSumOf(1)); |
842 | EXPECT_THAT(12, EqualsSumOf(10, 2)); |
843 | EXPECT_THAT(123, EqualsSumOf(100, 20, 3)); |
844 | EXPECT_THAT(1234, EqualsSumOf(1000, 200, 30, 4)); |
845 | EXPECT_THAT(12345, EqualsSumOf(10000, 2000, 300, 40, 5)); |
846 | EXPECT_THAT("abcdef" , |
847 | EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f')); |
848 | EXPECT_THAT("abcdefg" , |
849 | EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', 'g')); |
850 | EXPECT_THAT("abcdefgh" , |
851 | EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', 'g', |
852 | "h" )); |
853 | EXPECT_THAT("abcdefghi" , |
854 | EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', 'g', |
855 | "h" , 'i')); |
856 | EXPECT_THAT("abcdefghij" , |
857 | EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', 'g', |
858 | "h" , 'i', ::std::string("j" ))); |
859 | |
860 | EXPECT_THAT(1, Not(EqualsSumOf())); |
861 | EXPECT_THAT(-1, Not(EqualsSumOf(1))); |
862 | EXPECT_THAT(-12, Not(EqualsSumOf(10, 2))); |
863 | EXPECT_THAT(-123, Not(EqualsSumOf(100, 20, 3))); |
864 | EXPECT_THAT(-1234, Not(EqualsSumOf(1000, 200, 30, 4))); |
865 | EXPECT_THAT(-12345, Not(EqualsSumOf(10000, 2000, 300, 40, 5))); |
866 | EXPECT_THAT("abcdef " , |
867 | Not(EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f'))); |
868 | EXPECT_THAT("abcdefg " , |
869 | Not(EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', |
870 | 'g'))); |
871 | EXPECT_THAT("abcdefgh " , |
872 | Not(EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', 'g', |
873 | "h" ))); |
874 | EXPECT_THAT("abcdefghi " , |
875 | Not(EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', 'g', |
876 | "h" , 'i'))); |
877 | EXPECT_THAT("abcdefghij " , |
878 | Not(EqualsSumOf(::std::string("a" ), 'b', 'c', "d" , "e" , 'f', 'g', |
879 | "h" , 'i', ::std::string("j" )))); |
880 | } |
881 | |
882 | // Tests that a MATCHER_Pn() definition can be instantiated with any |
883 | // compatible parameter types. |
884 | TEST(MatcherPnMacroTest, WorksForDifferentParameterTypes) { |
885 | EXPECT_THAT(123, EqualsSumOf(100L, 20, static_cast<char>(3))); |
886 | EXPECT_THAT("abcd" , EqualsSumOf(::std::string("a" ), "b" , 'c', "d" )); |
887 | |
888 | EXPECT_THAT(124, Not(EqualsSumOf(100L, 20, static_cast<char>(3)))); |
889 | EXPECT_THAT("abcde" , Not(EqualsSumOf(::std::string("a" ), "b" , 'c', "d" ))); |
890 | } |
891 | |
892 | // Tests that the matcher body can promote the parameter types. |
893 | |
894 | MATCHER_P2(EqConcat, prefix, suffix, "" ) { |
895 | // The following lines promote the two parameters to desired types. |
896 | std::string prefix_str(prefix); |
897 | char suffix_char = static_cast<char>(suffix); |
898 | return arg == prefix_str + suffix_char; |
899 | } |
900 | |
901 | TEST(MatcherPnMacroTest, SimpleTypePromotion) { |
902 | Matcher<std::string> no_promo = |
903 | EqConcat(std::string("foo" ), 't'); |
904 | Matcher<const std::string&> promo = |
905 | EqConcat("foo" , static_cast<int>('t')); |
906 | EXPECT_FALSE(no_promo.Matches("fool" )); |
907 | EXPECT_FALSE(promo.Matches("fool" )); |
908 | EXPECT_TRUE(no_promo.Matches("foot" )); |
909 | EXPECT_TRUE(promo.Matches("foot" )); |
910 | } |
911 | |
912 | // Verifies the type of a MATCHER*. |
913 | |
914 | TEST(MatcherPnMacroTest, TypesAreCorrect) { |
915 | // EqualsSumOf() must be assignable to a EqualsSumOfMatcher variable. |
916 | EqualsSumOfMatcher a0 = EqualsSumOf(); |
917 | |
918 | // EqualsSumOf(1) must be assignable to a EqualsSumOfMatcherP variable. |
919 | EqualsSumOfMatcherP<int> a1 = EqualsSumOf(1); |
920 | |
921 | // EqualsSumOf(p1, ..., pk) must be assignable to a EqualsSumOfMatcherPk |
922 | // variable, and so on. |
923 | EqualsSumOfMatcherP2<int, char> a2 = EqualsSumOf(1, '2'); |
924 | EqualsSumOfMatcherP3<int, int, char> a3 = EqualsSumOf(1, 2, '3'); |
925 | EqualsSumOfMatcherP4<int, int, int, char> a4 = EqualsSumOf(1, 2, 3, '4'); |
926 | EqualsSumOfMatcherP5<int, int, int, int, char> a5 = |
927 | EqualsSumOf(1, 2, 3, 4, '5'); |
928 | EqualsSumOfMatcherP6<int, int, int, int, int, char> a6 = |
929 | EqualsSumOf(1, 2, 3, 4, 5, '6'); |
930 | EqualsSumOfMatcherP7<int, int, int, int, int, int, char> a7 = |
931 | EqualsSumOf(1, 2, 3, 4, 5, 6, '7'); |
932 | EqualsSumOfMatcherP8<int, int, int, int, int, int, int, char> a8 = |
933 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, '8'); |
934 | EqualsSumOfMatcherP9<int, int, int, int, int, int, int, int, char> a9 = |
935 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, '9'); |
936 | EqualsSumOfMatcherP10<int, int, int, int, int, int, int, int, int, char> a10 = |
937 | EqualsSumOf(1, 2, 3, 4, 5, 6, 7, 8, 9, '0'); |
938 | |
939 | // Avoid "unused variable" warnings. |
940 | (void)a0; |
941 | (void)a1; |
942 | (void)a2; |
943 | (void)a3; |
944 | (void)a4; |
945 | (void)a5; |
946 | (void)a6; |
947 | (void)a7; |
948 | (void)a8; |
949 | (void)a9; |
950 | (void)a10; |
951 | } |
952 | |
953 | // Tests that matcher-typed parameters can be used in Value() inside a |
954 | // MATCHER_Pn definition. |
955 | |
956 | // Succeeds if arg matches exactly 2 of the 3 matchers. |
957 | MATCHER_P3(TwoOf, m1, m2, m3, "" ) { |
958 | const int count = static_cast<int>(Value(arg, m1)) |
959 | + static_cast<int>(Value(arg, m2)) + static_cast<int>(Value(arg, m3)); |
960 | return count == 2; |
961 | } |
962 | |
963 | TEST(MatcherPnMacroTest, CanUseMatcherTypedParameterInValue) { |
964 | EXPECT_THAT(42, TwoOf(Gt(0), Lt(50), Eq(10))); |
965 | EXPECT_THAT(0, Not(TwoOf(Gt(-1), Lt(1), Eq(0)))); |
966 | } |
967 | |
968 | // Tests Contains(). |
969 | |
970 | TEST(ContainsTest, ListMatchesWhenElementIsInContainer) { |
971 | list<int> some_list; |
972 | some_list.push_back(3); |
973 | some_list.push_back(1); |
974 | some_list.push_back(2); |
975 | EXPECT_THAT(some_list, Contains(1)); |
976 | EXPECT_THAT(some_list, Contains(Gt(2.5))); |
977 | EXPECT_THAT(some_list, Contains(Eq(2.0f))); |
978 | |
979 | list<std::string> another_list; |
980 | another_list.push_back("fee" ); |
981 | another_list.push_back("fie" ); |
982 | another_list.push_back("foe" ); |
983 | another_list.push_back("fum" ); |
984 | EXPECT_THAT(another_list, Contains(std::string("fee" ))); |
985 | } |
986 | |
987 | TEST(ContainsTest, ListDoesNotMatchWhenElementIsNotInContainer) { |
988 | list<int> some_list; |
989 | some_list.push_back(3); |
990 | some_list.push_back(1); |
991 | EXPECT_THAT(some_list, Not(Contains(4))); |
992 | } |
993 | |
994 | TEST(ContainsTest, SetMatchesWhenElementIsInContainer) { |
995 | set<int> some_set; |
996 | some_set.insert(3); |
997 | some_set.insert(1); |
998 | some_set.insert(2); |
999 | EXPECT_THAT(some_set, Contains(Eq(1.0))); |
1000 | EXPECT_THAT(some_set, Contains(Eq(3.0f))); |
1001 | EXPECT_THAT(some_set, Contains(2)); |
1002 | |
1003 | set<const char*> another_set; |
1004 | another_set.insert("fee" ); |
1005 | another_set.insert("fie" ); |
1006 | another_set.insert("foe" ); |
1007 | another_set.insert("fum" ); |
1008 | EXPECT_THAT(another_set, Contains(Eq(std::string("fum" )))); |
1009 | } |
1010 | |
1011 | TEST(ContainsTest, SetDoesNotMatchWhenElementIsNotInContainer) { |
1012 | set<int> some_set; |
1013 | some_set.insert(3); |
1014 | some_set.insert(1); |
1015 | EXPECT_THAT(some_set, Not(Contains(4))); |
1016 | |
1017 | set<const char*> c_string_set; |
1018 | c_string_set.insert("hello" ); |
1019 | EXPECT_THAT(c_string_set, Not(Contains(std::string("hello" ).c_str()))); |
1020 | } |
1021 | |
1022 | TEST(ContainsTest, ExplainsMatchResultCorrectly) { |
1023 | const int a[2] = { 1, 2 }; |
1024 | Matcher<const int (&)[2]> m = Contains(2); |
1025 | EXPECT_EQ("whose element #1 matches" , Explain(m, a)); |
1026 | |
1027 | m = Contains(3); |
1028 | EXPECT_EQ("" , Explain(m, a)); |
1029 | |
1030 | m = Contains(GreaterThan(0)); |
1031 | EXPECT_EQ("whose element #0 matches, which is 1 more than 0" , Explain(m, a)); |
1032 | |
1033 | m = Contains(GreaterThan(10)); |
1034 | EXPECT_EQ("" , Explain(m, a)); |
1035 | } |
1036 | |
1037 | TEST(ContainsTest, DescribesItselfCorrectly) { |
1038 | Matcher<vector<int> > m = Contains(1); |
1039 | EXPECT_EQ("contains at least one element that is equal to 1" , Describe(m)); |
1040 | |
1041 | Matcher<vector<int> > m2 = Not(m); |
1042 | EXPECT_EQ("doesn't contain any element that is equal to 1" , Describe(m2)); |
1043 | } |
1044 | |
1045 | TEST(ContainsTest, MapMatchesWhenElementIsInContainer) { |
1046 | map<const char*, int> my_map; |
1047 | const char* bar = "a string" ; |
1048 | my_map[bar] = 2; |
1049 | EXPECT_THAT(my_map, Contains(pair<const char* const, int>(bar, 2))); |
1050 | |
1051 | map<std::string, int> another_map; |
1052 | another_map["fee" ] = 1; |
1053 | another_map["fie" ] = 2; |
1054 | another_map["foe" ] = 3; |
1055 | another_map["fum" ] = 4; |
1056 | EXPECT_THAT(another_map, |
1057 | Contains(pair<const std::string, int>(std::string("fee" ), 1))); |
1058 | EXPECT_THAT(another_map, Contains(pair<const std::string, int>("fie" , 2))); |
1059 | } |
1060 | |
1061 | TEST(ContainsTest, MapDoesNotMatchWhenElementIsNotInContainer) { |
1062 | map<int, int> some_map; |
1063 | some_map[1] = 11; |
1064 | some_map[2] = 22; |
1065 | EXPECT_THAT(some_map, Not(Contains(pair<const int, int>(2, 23)))); |
1066 | } |
1067 | |
1068 | TEST(ContainsTest, ArrayMatchesWhenElementIsInContainer) { |
1069 | const char* string_array[] = { "fee" , "fie" , "foe" , "fum" }; |
1070 | EXPECT_THAT(string_array, Contains(Eq(std::string("fum" )))); |
1071 | } |
1072 | |
1073 | TEST(ContainsTest, ArrayDoesNotMatchWhenElementIsNotInContainer) { |
1074 | int int_array[] = { 1, 2, 3, 4 }; |
1075 | EXPECT_THAT(int_array, Not(Contains(5))); |
1076 | } |
1077 | |
1078 | TEST(ContainsTest, AcceptsMatcher) { |
1079 | const int a[] = { 1, 2, 3 }; |
1080 | EXPECT_THAT(a, Contains(Gt(2))); |
1081 | EXPECT_THAT(a, Not(Contains(Gt(4)))); |
1082 | } |
1083 | |
1084 | TEST(ContainsTest, WorksForNativeArrayAsTuple) { |
1085 | const int a[] = { 1, 2 }; |
1086 | const int* const pointer = a; |
1087 | EXPECT_THAT(std::make_tuple(pointer, 2), Contains(1)); |
1088 | EXPECT_THAT(std::make_tuple(pointer, 2), Not(Contains(Gt(3)))); |
1089 | } |
1090 | |
1091 | TEST(ContainsTest, WorksForTwoDimensionalNativeArray) { |
1092 | int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; |
1093 | EXPECT_THAT(a, Contains(ElementsAre(4, 5, 6))); |
1094 | EXPECT_THAT(a, Contains(Contains(5))); |
1095 | EXPECT_THAT(a, Not(Contains(ElementsAre(3, 4, 5)))); |
1096 | EXPECT_THAT(a, Contains(Not(Contains(5)))); |
1097 | } |
1098 | |
1099 | TEST(AllOfArrayTest, BasicForms) { |
1100 | // Iterator |
1101 | std::vector<int> v0{}; |
1102 | std::vector<int> v1{1}; |
1103 | std::vector<int> v2{2, 3}; |
1104 | std::vector<int> v3{4, 4, 4}; |
1105 | EXPECT_THAT(0, AllOfArray(v0.begin(), v0.end())); |
1106 | EXPECT_THAT(1, AllOfArray(v1.begin(), v1.end())); |
1107 | EXPECT_THAT(2, Not(AllOfArray(v1.begin(), v1.end()))); |
1108 | EXPECT_THAT(3, Not(AllOfArray(v2.begin(), v2.end()))); |
1109 | EXPECT_THAT(4, AllOfArray(v3.begin(), v3.end())); |
1110 | // Pointer + size |
1111 | int ar[6] = {1, 2, 3, 4, 4, 4}; |
1112 | EXPECT_THAT(0, AllOfArray(ar, 0)); |
1113 | EXPECT_THAT(1, AllOfArray(ar, 1)); |
1114 | EXPECT_THAT(2, Not(AllOfArray(ar, 1))); |
1115 | EXPECT_THAT(3, Not(AllOfArray(ar + 1, 3))); |
1116 | EXPECT_THAT(4, AllOfArray(ar + 3, 3)); |
1117 | // Array |
1118 | // int ar0[0]; Not usable |
1119 | int ar1[1] = {1}; |
1120 | int ar2[2] = {2, 3}; |
1121 | int ar3[3] = {4, 4, 4}; |
1122 | // EXPECT_THAT(0, Not(AllOfArray(ar0))); // Cannot work |
1123 | EXPECT_THAT(1, AllOfArray(ar1)); |
1124 | EXPECT_THAT(2, Not(AllOfArray(ar1))); |
1125 | EXPECT_THAT(3, Not(AllOfArray(ar2))); |
1126 | EXPECT_THAT(4, AllOfArray(ar3)); |
1127 | // Container |
1128 | EXPECT_THAT(0, AllOfArray(v0)); |
1129 | EXPECT_THAT(1, AllOfArray(v1)); |
1130 | EXPECT_THAT(2, Not(AllOfArray(v1))); |
1131 | EXPECT_THAT(3, Not(AllOfArray(v2))); |
1132 | EXPECT_THAT(4, AllOfArray(v3)); |
1133 | // Initializer |
1134 | EXPECT_THAT(0, AllOfArray<int>({})); // Requires template arg. |
1135 | EXPECT_THAT(1, AllOfArray({1})); |
1136 | EXPECT_THAT(2, Not(AllOfArray({1}))); |
1137 | EXPECT_THAT(3, Not(AllOfArray({2, 3}))); |
1138 | EXPECT_THAT(4, AllOfArray({4, 4, 4})); |
1139 | } |
1140 | |
1141 | TEST(AllOfArrayTest, Matchers) { |
1142 | // vector |
1143 | std::vector<Matcher<int>> matchers{Ge(1), Lt(2)}; |
1144 | EXPECT_THAT(0, Not(AllOfArray(matchers))); |
1145 | EXPECT_THAT(1, AllOfArray(matchers)); |
1146 | EXPECT_THAT(2, Not(AllOfArray(matchers))); |
1147 | // initializer_list |
1148 | EXPECT_THAT(0, Not(AllOfArray({Ge(0), Ge(1)}))); |
1149 | EXPECT_THAT(1, AllOfArray({Ge(0), Ge(1)})); |
1150 | } |
1151 | |
1152 | TEST(AnyOfArrayTest, BasicForms) { |
1153 | // Iterator |
1154 | std::vector<int> v0{}; |
1155 | std::vector<int> v1{1}; |
1156 | std::vector<int> v2{2, 3}; |
1157 | EXPECT_THAT(0, Not(AnyOfArray(v0.begin(), v0.end()))); |
1158 | EXPECT_THAT(1, AnyOfArray(v1.begin(), v1.end())); |
1159 | EXPECT_THAT(2, Not(AnyOfArray(v1.begin(), v1.end()))); |
1160 | EXPECT_THAT(3, AnyOfArray(v2.begin(), v2.end())); |
1161 | EXPECT_THAT(4, Not(AnyOfArray(v2.begin(), v2.end()))); |
1162 | // Pointer + size |
1163 | int ar[3] = {1, 2, 3}; |
1164 | EXPECT_THAT(0, Not(AnyOfArray(ar, 0))); |
1165 | EXPECT_THAT(1, AnyOfArray(ar, 1)); |
1166 | EXPECT_THAT(2, Not(AnyOfArray(ar, 1))); |
1167 | EXPECT_THAT(3, AnyOfArray(ar + 1, 2)); |
1168 | EXPECT_THAT(4, Not(AnyOfArray(ar + 1, 2))); |
1169 | // Array |
1170 | // int ar0[0]; Not usable |
1171 | int ar1[1] = {1}; |
1172 | int ar2[2] = {2, 3}; |
1173 | // EXPECT_THAT(0, Not(AnyOfArray(ar0))); // Cannot work |
1174 | EXPECT_THAT(1, AnyOfArray(ar1)); |
1175 | EXPECT_THAT(2, Not(AnyOfArray(ar1))); |
1176 | EXPECT_THAT(3, AnyOfArray(ar2)); |
1177 | EXPECT_THAT(4, Not(AnyOfArray(ar2))); |
1178 | // Container |
1179 | EXPECT_THAT(0, Not(AnyOfArray(v0))); |
1180 | EXPECT_THAT(1, AnyOfArray(v1)); |
1181 | EXPECT_THAT(2, Not(AnyOfArray(v1))); |
1182 | EXPECT_THAT(3, AnyOfArray(v2)); |
1183 | EXPECT_THAT(4, Not(AnyOfArray(v2))); |
1184 | // Initializer |
1185 | EXPECT_THAT(0, Not(AnyOfArray<int>({}))); // Requires template arg. |
1186 | EXPECT_THAT(1, AnyOfArray({1})); |
1187 | EXPECT_THAT(2, Not(AnyOfArray({1}))); |
1188 | EXPECT_THAT(3, AnyOfArray({2, 3})); |
1189 | EXPECT_THAT(4, Not(AnyOfArray({2, 3}))); |
1190 | } |
1191 | |
1192 | TEST(AnyOfArrayTest, Matchers) { |
1193 | // We negate test AllOfArrayTest.Matchers. |
1194 | // vector |
1195 | std::vector<Matcher<int>> matchers{Lt(1), Ge(2)}; |
1196 | EXPECT_THAT(0, AnyOfArray(matchers)); |
1197 | EXPECT_THAT(1, Not(AnyOfArray(matchers))); |
1198 | EXPECT_THAT(2, AnyOfArray(matchers)); |
1199 | // initializer_list |
1200 | EXPECT_THAT(0, AnyOfArray({Lt(0), Lt(1)})); |
1201 | EXPECT_THAT(1, Not(AllOfArray({Lt(0), Lt(1)}))); |
1202 | } |
1203 | |
1204 | TEST(AnyOfArrayTest, ExplainsMatchResultCorrectly) { |
1205 | // AnyOfArray and AllOfArry use the same underlying template-template, |
1206 | // thus it is sufficient to test one here. |
1207 | const std::vector<int> v0{}; |
1208 | const std::vector<int> v1{1}; |
1209 | const std::vector<int> v2{2, 3}; |
1210 | const Matcher<int> m0 = AnyOfArray(v0); |
1211 | const Matcher<int> m1 = AnyOfArray(v1); |
1212 | const Matcher<int> m2 = AnyOfArray(v2); |
1213 | EXPECT_EQ("" , Explain(m0, 0)); |
1214 | EXPECT_EQ("" , Explain(m1, 1)); |
1215 | EXPECT_EQ("" , Explain(m1, 2)); |
1216 | EXPECT_EQ("" , Explain(m2, 3)); |
1217 | EXPECT_EQ("" , Explain(m2, 4)); |
1218 | EXPECT_EQ("()" , Describe(m0)); |
1219 | EXPECT_EQ("(is equal to 1)" , Describe(m1)); |
1220 | EXPECT_EQ("(is equal to 2) or (is equal to 3)" , Describe(m2)); |
1221 | EXPECT_EQ("()" , DescribeNegation(m0)); |
1222 | EXPECT_EQ("(isn't equal to 1)" , DescribeNegation(m1)); |
1223 | EXPECT_EQ("(isn't equal to 2) and (isn't equal to 3)" , DescribeNegation(m2)); |
1224 | // Explain with matchers |
1225 | const Matcher<int> g1 = AnyOfArray({GreaterThan(1)}); |
1226 | const Matcher<int> g2 = AnyOfArray({GreaterThan(1), GreaterThan(2)}); |
1227 | // Explains the first positiv match and all prior negative matches... |
1228 | EXPECT_EQ("which is 1 less than 1" , Explain(g1, 0)); |
1229 | EXPECT_EQ("which is the same as 1" , Explain(g1, 1)); |
1230 | EXPECT_EQ("which is 1 more than 1" , Explain(g1, 2)); |
1231 | EXPECT_EQ("which is 1 less than 1, and which is 2 less than 2" , |
1232 | Explain(g2, 0)); |
1233 | EXPECT_EQ("which is the same as 1, and which is 1 less than 2" , |
1234 | Explain(g2, 1)); |
1235 | EXPECT_EQ("which is 1 more than 1" , // Only the first |
1236 | Explain(g2, 2)); |
1237 | } |
1238 | |
1239 | TEST(AllOfTest, HugeMatcher) { |
1240 | // Verify that using AllOf with many arguments doesn't cause |
1241 | // the compiler to exceed template instantiation depth limit. |
1242 | EXPECT_THAT(0, testing::AllOf(_, _, _, _, _, _, _, _, _, |
1243 | testing::AllOf(_, _, _, _, _, _, _, _, _, _))); |
1244 | } |
1245 | |
1246 | TEST(AnyOfTest, HugeMatcher) { |
1247 | // Verify that using AnyOf with many arguments doesn't cause |
1248 | // the compiler to exceed template instantiation depth limit. |
1249 | EXPECT_THAT(0, testing::AnyOf(_, _, _, _, _, _, _, _, _, |
1250 | testing::AnyOf(_, _, _, _, _, _, _, _, _, _))); |
1251 | } |
1252 | |
1253 | namespace adl_test { |
1254 | |
1255 | // Verifies that the implementation of ::testing::AllOf and ::testing::AnyOf |
1256 | // don't issue unqualified recursive calls. If they do, the argument dependent |
1257 | // name lookup will cause AllOf/AnyOf in the 'adl_test' namespace to be found |
1258 | // as a candidate and the compilation will break due to an ambiguous overload. |
1259 | |
1260 | // The matcher must be in the same namespace as AllOf/AnyOf to make argument |
1261 | // dependent lookup find those. |
1262 | MATCHER(M, "" ) { return true; } |
1263 | |
1264 | template <typename T1, typename T2> |
1265 | bool AllOf(const T1& /*t1*/, const T2& /*t2*/) { return true; } |
1266 | |
1267 | TEST(AllOfTest, DoesNotCallAllOfUnqualified) { |
1268 | EXPECT_THAT(42, testing::AllOf( |
1269 | M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); |
1270 | } |
1271 | |
1272 | template <typename T1, typename T2> bool |
1273 | AnyOf(const T1& t1, const T2& t2) { return true; } |
1274 | |
1275 | TEST(AnyOfTest, DoesNotCallAnyOfUnqualified) { |
1276 | EXPECT_THAT(42, testing::AnyOf( |
1277 | M(), M(), M(), M(), M(), M(), M(), M(), M(), M())); |
1278 | } |
1279 | |
1280 | } // namespace adl_test |
1281 | |
1282 | |
1283 | TEST(AllOfTest, WorksOnMoveOnlyType) { |
1284 | std::unique_ptr<int> p(new int(3)); |
1285 | EXPECT_THAT(p, AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(5)))); |
1286 | EXPECT_THAT(p, Not(AllOf(Pointee(Eq(3)), Pointee(Gt(0)), Pointee(Lt(3))))); |
1287 | } |
1288 | |
1289 | TEST(AnyOfTest, WorksOnMoveOnlyType) { |
1290 | std::unique_ptr<int> p(new int(3)); |
1291 | EXPECT_THAT(p, AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Lt(5)))); |
1292 | EXPECT_THAT(p, Not(AnyOf(Pointee(Eq(5)), Pointee(Lt(0)), Pointee(Gt(5))))); |
1293 | } |
1294 | |
1295 | MATCHER(IsNotNull, "" ) { |
1296 | return arg != nullptr; |
1297 | } |
1298 | |
1299 | // Verifies that a matcher defined using MATCHER() can work on |
1300 | // move-only types. |
1301 | TEST(MatcherMacroTest, WorksOnMoveOnlyType) { |
1302 | std::unique_ptr<int> p(new int(3)); |
1303 | EXPECT_THAT(p, IsNotNull()); |
1304 | EXPECT_THAT(std::unique_ptr<int>(), Not(IsNotNull())); |
1305 | } |
1306 | |
1307 | MATCHER_P(UniquePointee, pointee, "" ) { |
1308 | return *arg == pointee; |
1309 | } |
1310 | |
1311 | // Verifies that a matcher defined using MATCHER_P*() can work on |
1312 | // move-only types. |
1313 | TEST(MatcherPMacroTest, WorksOnMoveOnlyType) { |
1314 | std::unique_ptr<int> p(new int(3)); |
1315 | EXPECT_THAT(p, UniquePointee(3)); |
1316 | EXPECT_THAT(p, Not(UniquePointee(2))); |
1317 | } |
1318 | |
1319 | |
1320 | } // namespace |
1321 | |
1322 | #ifdef _MSC_VER |
1323 | # pragma warning(pop) |
1324 | #endif |
1325 | |