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