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