Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gmock-cardinalities_test.cc
Go to the documentation of this file.
1// Copyright 2007, 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// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file tests the built-in cardinalities.
35
36#include "gmock/gmock.h"
37#include "gtest/gtest.h"
38#include "gtest/gtest-spi.h"
39
40namespace {
41
42using std::stringstream;
45using testing::AtMost;
52
53class MockFoo {
54 public:
55 MockFoo() {}
56 MOCK_METHOD0(Bar, int()); // NOLINT
57
58 private:
60};
61
62// Tests that Cardinality objects can be default constructed.
63TEST(CardinalityTest, IsDefaultConstructable) {
64 Cardinality c;
65}
66
67// Tests that Cardinality objects are copyable.
68TEST(CardinalityTest, IsCopyable) {
69 // Tests the copy constructor.
70 Cardinality c = Exactly(1);
71 EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
72 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
73 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
74
75 // Tests the assignment operator.
76 c = Exactly(2);
77 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
78 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
79 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
80}
81
82TEST(CardinalityTest, IsOverSaturatedByCallCountWorks) {
83 const Cardinality c = AtMost(5);
84 EXPECT_FALSE(c.IsOverSaturatedByCallCount(4));
85 EXPECT_FALSE(c.IsOverSaturatedByCallCount(5));
86 EXPECT_TRUE(c.IsOverSaturatedByCallCount(6));
87}
88
89// Tests that Cardinality::DescribeActualCallCountTo() creates the
90// correct description.
91TEST(CardinalityTest, CanDescribeActualCallCount) {
92 stringstream ss0;
93 Cardinality::DescribeActualCallCountTo(0, &ss0);
94 EXPECT_EQ("never called", ss0.str());
95
96 stringstream ss1;
97 Cardinality::DescribeActualCallCountTo(1, &ss1);
98 EXPECT_EQ("called once", ss1.str());
99
100 stringstream ss2;
101 Cardinality::DescribeActualCallCountTo(2, &ss2);
102 EXPECT_EQ("called twice", ss2.str());
103
104 stringstream ss3;
105 Cardinality::DescribeActualCallCountTo(3, &ss3);
106 EXPECT_EQ("called 3 times", ss3.str());
107}
108
109// Tests AnyNumber()
110TEST(AnyNumber, Works) {
111 const Cardinality c = AnyNumber();
112 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
113 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
114
115 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
116 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
117
118 EXPECT_TRUE(c.IsSatisfiedByCallCount(9));
119 EXPECT_FALSE(c.IsSaturatedByCallCount(9));
120
121 stringstream ss;
122 c.DescribeTo(&ss);
123 EXPECT_PRED_FORMAT2(IsSubstring, "called any number of times",
124 ss.str());
125}
126
127TEST(AnyNumberTest, HasCorrectBounds) {
128 const Cardinality c = AnyNumber();
129 EXPECT_EQ(0, c.ConservativeLowerBound());
130 EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
131}
132
133// Tests AtLeast(n).
134
135TEST(AtLeastTest, OnNegativeNumber) {
136 EXPECT_NONFATAL_FAILURE({ // NOLINT
137 AtLeast(-1);
138 }, "The invocation lower bound must be >= 0");
139}
140
141TEST(AtLeastTest, OnZero) {
142 const Cardinality c = AtLeast(0);
143 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
144 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
145
146 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
147 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
148
149 stringstream ss;
150 c.DescribeTo(&ss);
151 EXPECT_PRED_FORMAT2(IsSubstring, "any number of times",
152 ss.str());
153}
154
155TEST(AtLeastTest, OnPositiveNumber) {
156 const Cardinality c = AtLeast(2);
157 EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
158 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
159
160 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
161 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
162
163 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
164 EXPECT_FALSE(c.IsSaturatedByCallCount(2));
165
166 stringstream ss1;
167 AtLeast(1).DescribeTo(&ss1);
168 EXPECT_PRED_FORMAT2(IsSubstring, "at least once",
169 ss1.str());
170
171 stringstream ss2;
172 c.DescribeTo(&ss2);
173 EXPECT_PRED_FORMAT2(IsSubstring, "at least twice",
174 ss2.str());
175
176 stringstream ss3;
177 AtLeast(3).DescribeTo(&ss3);
178 EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times",
179 ss3.str());
180}
181
182TEST(AtLeastTest, HasCorrectBounds) {
183 const Cardinality c = AtLeast(2);
184 EXPECT_EQ(2, c.ConservativeLowerBound());
185 EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
186}
187
188// Tests AtMost(n).
189
190TEST(AtMostTest, OnNegativeNumber) {
191 EXPECT_NONFATAL_FAILURE({ // NOLINT
192 AtMost(-1);
193 }, "The invocation upper bound must be >= 0");
194}
195
196TEST(AtMostTest, OnZero) {
197 const Cardinality c = AtMost(0);
198 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
199 EXPECT_TRUE(c.IsSaturatedByCallCount(0));
200
201 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
202 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
203
204 stringstream ss;
205 c.DescribeTo(&ss);
206 EXPECT_PRED_FORMAT2(IsSubstring, "never called",
207 ss.str());
208}
209
210TEST(AtMostTest, OnPositiveNumber) {
211 const Cardinality c = AtMost(2);
212 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
213 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
214
215 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
216 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
217
218 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
219 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
220
221 stringstream ss1;
222 AtMost(1).DescribeTo(&ss1);
223 EXPECT_PRED_FORMAT2(IsSubstring, "called at most once",
224 ss1.str());
225
226 stringstream ss2;
227 c.DescribeTo(&ss2);
228 EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
229 ss2.str());
230
231 stringstream ss3;
232 AtMost(3).DescribeTo(&ss3);
233 EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times",
234 ss3.str());
235}
236
237TEST(AtMostTest, HasCorrectBounds) {
238 const Cardinality c = AtMost(2);
239 EXPECT_EQ(0, c.ConservativeLowerBound());
240 EXPECT_EQ(2, c.ConservativeUpperBound());
241}
242
243// Tests Between(m, n).
244
245TEST(BetweenTest, OnNegativeStart) {
246 EXPECT_NONFATAL_FAILURE({ // NOLINT
247 Between(-1, 2);
248 }, "The invocation lower bound must be >= 0, but is actually -1");
249}
250
251TEST(BetweenTest, OnNegativeEnd) {
252 EXPECT_NONFATAL_FAILURE({ // NOLINT
253 Between(1, -2);
254 }, "The invocation upper bound must be >= 0, but is actually -2");
255}
256
257TEST(BetweenTest, OnStartBiggerThanEnd) {
258 EXPECT_NONFATAL_FAILURE({ // NOLINT
259 Between(2, 1);
260 }, "The invocation upper bound (1) must be >= "
261 "the invocation lower bound (2)");
262}
263
264TEST(BetweenTest, OnZeroStartAndZeroEnd) {
265 const Cardinality c = Between(0, 0);
266
267 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
268 EXPECT_TRUE(c.IsSaturatedByCallCount(0));
269
270 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
271 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
272
273 stringstream ss;
274 c.DescribeTo(&ss);
275 EXPECT_PRED_FORMAT2(IsSubstring, "never called",
276 ss.str());
277}
278
279TEST(BetweenTest, OnZeroStartAndNonZeroEnd) {
280 const Cardinality c = Between(0, 2);
281
282 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
283 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
284
285 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
286 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
287
288 EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
289 EXPECT_TRUE(c.IsSaturatedByCallCount(4));
290
291 stringstream ss;
292 c.DescribeTo(&ss);
293 EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
294 ss.str());
295}
296
297TEST(BetweenTest, OnSameStartAndEnd) {
298 const Cardinality c = Between(3, 3);
299
300 EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
301 EXPECT_FALSE(c.IsSaturatedByCallCount(2));
302
303 EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
304 EXPECT_TRUE(c.IsSaturatedByCallCount(3));
305
306 EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
307 EXPECT_TRUE(c.IsSaturatedByCallCount(4));
308
309 stringstream ss;
310 c.DescribeTo(&ss);
311 EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
312 ss.str());
313}
314
315TEST(BetweenTest, OnDifferentStartAndEnd) {
316 const Cardinality c = Between(3, 5);
317
318 EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
319 EXPECT_FALSE(c.IsSaturatedByCallCount(2));
320
321 EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
322 EXPECT_FALSE(c.IsSaturatedByCallCount(3));
323
324 EXPECT_TRUE(c.IsSatisfiedByCallCount(5));
325 EXPECT_TRUE(c.IsSaturatedByCallCount(5));
326
327 EXPECT_FALSE(c.IsSatisfiedByCallCount(6));
328 EXPECT_TRUE(c.IsSaturatedByCallCount(6));
329
330 stringstream ss;
331 c.DescribeTo(&ss);
332 EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times",
333 ss.str());
334}
335
336TEST(BetweenTest, HasCorrectBounds) {
337 const Cardinality c = Between(3, 5);
338 EXPECT_EQ(3, c.ConservativeLowerBound());
339 EXPECT_EQ(5, c.ConservativeUpperBound());
340}
341
342// Tests Exactly(n).
343
344TEST(ExactlyTest, OnNegativeNumber) {
345 EXPECT_NONFATAL_FAILURE({ // NOLINT
346 Exactly(-1);
347 }, "The invocation lower bound must be >= 0");
348}
349
350TEST(ExactlyTest, OnZero) {
351 const Cardinality c = Exactly(0);
352 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
353 EXPECT_TRUE(c.IsSaturatedByCallCount(0));
354
355 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
356 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
357
358 stringstream ss;
359 c.DescribeTo(&ss);
360 EXPECT_PRED_FORMAT2(IsSubstring, "never called",
361 ss.str());
362}
363
364TEST(ExactlyTest, OnPositiveNumber) {
365 const Cardinality c = Exactly(2);
366 EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
367 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
368
369 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
370 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
371
372 stringstream ss1;
373 Exactly(1).DescribeTo(&ss1);
374 EXPECT_PRED_FORMAT2(IsSubstring, "called once",
375 ss1.str());
376
377 stringstream ss2;
378 c.DescribeTo(&ss2);
379 EXPECT_PRED_FORMAT2(IsSubstring, "called twice",
380 ss2.str());
381
382 stringstream ss3;
383 Exactly(3).DescribeTo(&ss3);
384 EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
385 ss3.str());
386}
387
388TEST(ExactlyTest, HasCorrectBounds) {
389 const Cardinality c = Exactly(3);
390 EXPECT_EQ(3, c.ConservativeLowerBound());
391 EXPECT_EQ(3, c.ConservativeUpperBound());
392}
393
394// Tests that a user can make their own cardinality by implementing
395// CardinalityInterface and calling MakeCardinality().
396
397class EvenCardinality : public CardinalityInterface {
398 public:
399 // Returns true iff call_count calls will satisfy this cardinality.
400 virtual bool IsSatisfiedByCallCount(int call_count) const {
401 return (call_count % 2 == 0);
402 }
403
404 // Returns true iff call_count calls will saturate this cardinality.
405 virtual bool IsSaturatedByCallCount(int /* call_count */) const {
406 return false;
407 }
408
409 // Describes self to an ostream.
410 virtual void DescribeTo(::std::ostream* ss) const {
411 *ss << "called even number of times";
412 }
413};
414
415TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {
416 const Cardinality c = MakeCardinality(new EvenCardinality);
417
418 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
419 EXPECT_FALSE(c.IsSatisfiedByCallCount(3));
420
421 EXPECT_FALSE(c.IsSaturatedByCallCount(10000));
422
423 stringstream ss;
424 c.DescribeTo(&ss);
425 EXPECT_EQ("called even number of times", ss.str());
426}
427
428} // Unnamed namespace
#define MOCK_METHOD0(m,...)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition gtest-port.h:917
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Definition gtest-spi.h:203
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define EXPECT_FALSE(condition)
Definition gtest.h:1898
#define EXPECT_PRED_FORMAT2(pred_format, v1, v2)
static const Segment ss(Segment::ss)
GTEST_API_ Cardinality AtLeast(int n)
GTEST_API_ AssertionResult IsSubstring(const char *needle_expr, const char *haystack_expr, const char *needle, const char *haystack)
Definition gtest.cc:1621
GTEST_API_ Cardinality Between(int min, int max)
GTEST_API_ Cardinality AtMost(int n)
GTEST_API_ Cardinality AnyNumber()
GTEST_API_ Cardinality Exactly(int n)
Cardinality MakeCardinality(const CardinalityInterface *c)