1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Google Mock - a framework for writing C++ mock classes.
34 // This file tests the built-in cardinalities.
36 #include "gmock/gmock.h"
37 #include "gtest/gtest.h"
38 #include "gtest/gtest-spi.h"
42 using std::stringstream
;
43 using testing::AnyNumber
;
44 using testing::AtLeast
;
45 using testing::AtMost
;
46 using testing::Between
;
47 using testing::Cardinality
;
48 using testing::CardinalityInterface
;
49 using testing::Exactly
;
50 using testing::IsSubstring
;
51 using testing::MakeCardinality
;
56 MOCK_METHOD0(Bar
, int()); // NOLINT
59 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo
);
62 // Tests that Cardinality objects can be default constructed.
63 TEST(CardinalityTest
, IsDefaultConstructable
) {
67 // Tests that Cardinality objects are copyable.
68 TEST(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));
75 // Tests the assignment operator.
77 EXPECT_FALSE(c
.IsSatisfiedByCallCount(1));
78 EXPECT_TRUE(c
.IsSatisfiedByCallCount(2));
79 EXPECT_TRUE(c
.IsSaturatedByCallCount(2));
82 TEST(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));
89 // Tests that Cardinality::DescribeActualCallCountTo() creates the
90 // correct description.
91 TEST(CardinalityTest
, CanDescribeActualCallCount
) {
93 Cardinality::DescribeActualCallCountTo(0, &ss0
);
94 EXPECT_EQ("never called", ss0
.str());
97 Cardinality::DescribeActualCallCountTo(1, &ss1
);
98 EXPECT_EQ("called once", ss1
.str());
101 Cardinality::DescribeActualCallCountTo(2, &ss2
);
102 EXPECT_EQ("called twice", ss2
.str());
105 Cardinality::DescribeActualCallCountTo(3, &ss3
);
106 EXPECT_EQ("called 3 times", ss3
.str());
110 TEST(AnyNumber
, Works
) {
111 const Cardinality c
= AnyNumber();
112 EXPECT_TRUE(c
.IsSatisfiedByCallCount(0));
113 EXPECT_FALSE(c
.IsSaturatedByCallCount(0));
115 EXPECT_TRUE(c
.IsSatisfiedByCallCount(1));
116 EXPECT_FALSE(c
.IsSaturatedByCallCount(1));
118 EXPECT_TRUE(c
.IsSatisfiedByCallCount(9));
119 EXPECT_FALSE(c
.IsSaturatedByCallCount(9));
123 EXPECT_PRED_FORMAT2(IsSubstring
, "called any number of times",
127 TEST(AnyNumberTest
, HasCorrectBounds
) {
128 const Cardinality c
= AnyNumber();
129 EXPECT_EQ(0, c
.ConservativeLowerBound());
130 EXPECT_EQ(INT_MAX
, c
.ConservativeUpperBound());
135 TEST(AtLeastTest
, OnNegativeNumber
) {
136 EXPECT_NONFATAL_FAILURE({ // NOLINT
138 }, "The invocation lower bound must be >= 0");
141 TEST(AtLeastTest
, OnZero
) {
142 const Cardinality c
= AtLeast(0);
143 EXPECT_TRUE(c
.IsSatisfiedByCallCount(0));
144 EXPECT_FALSE(c
.IsSaturatedByCallCount(0));
146 EXPECT_TRUE(c
.IsSatisfiedByCallCount(1));
147 EXPECT_FALSE(c
.IsSaturatedByCallCount(1));
151 EXPECT_PRED_FORMAT2(IsSubstring
, "any number of times",
155 TEST(AtLeastTest
, OnPositiveNumber
) {
156 const Cardinality c
= AtLeast(2);
157 EXPECT_FALSE(c
.IsSatisfiedByCallCount(0));
158 EXPECT_FALSE(c
.IsSaturatedByCallCount(0));
160 EXPECT_FALSE(c
.IsSatisfiedByCallCount(1));
161 EXPECT_FALSE(c
.IsSaturatedByCallCount(1));
163 EXPECT_TRUE(c
.IsSatisfiedByCallCount(2));
164 EXPECT_FALSE(c
.IsSaturatedByCallCount(2));
167 AtLeast(1).DescribeTo(&ss1
);
168 EXPECT_PRED_FORMAT2(IsSubstring
, "at least once",
173 EXPECT_PRED_FORMAT2(IsSubstring
, "at least twice",
177 AtLeast(3).DescribeTo(&ss3
);
178 EXPECT_PRED_FORMAT2(IsSubstring
, "at least 3 times",
182 TEST(AtLeastTest
, HasCorrectBounds
) {
183 const Cardinality c
= AtLeast(2);
184 EXPECT_EQ(2, c
.ConservativeLowerBound());
185 EXPECT_EQ(INT_MAX
, c
.ConservativeUpperBound());
190 TEST(AtMostTest
, OnNegativeNumber
) {
191 EXPECT_NONFATAL_FAILURE({ // NOLINT
193 }, "The invocation upper bound must be >= 0");
196 TEST(AtMostTest
, OnZero
) {
197 const Cardinality c
= AtMost(0);
198 EXPECT_TRUE(c
.IsSatisfiedByCallCount(0));
199 EXPECT_TRUE(c
.IsSaturatedByCallCount(0));
201 EXPECT_FALSE(c
.IsSatisfiedByCallCount(1));
202 EXPECT_TRUE(c
.IsSaturatedByCallCount(1));
206 EXPECT_PRED_FORMAT2(IsSubstring
, "never called",
210 TEST(AtMostTest
, OnPositiveNumber
) {
211 const Cardinality c
= AtMost(2);
212 EXPECT_TRUE(c
.IsSatisfiedByCallCount(0));
213 EXPECT_FALSE(c
.IsSaturatedByCallCount(0));
215 EXPECT_TRUE(c
.IsSatisfiedByCallCount(1));
216 EXPECT_FALSE(c
.IsSaturatedByCallCount(1));
218 EXPECT_TRUE(c
.IsSatisfiedByCallCount(2));
219 EXPECT_TRUE(c
.IsSaturatedByCallCount(2));
222 AtMost(1).DescribeTo(&ss1
);
223 EXPECT_PRED_FORMAT2(IsSubstring
, "called at most once",
228 EXPECT_PRED_FORMAT2(IsSubstring
, "called at most twice",
232 AtMost(3).DescribeTo(&ss3
);
233 EXPECT_PRED_FORMAT2(IsSubstring
, "called at most 3 times",
237 TEST(AtMostTest
, HasCorrectBounds
) {
238 const Cardinality c
= AtMost(2);
239 EXPECT_EQ(0, c
.ConservativeLowerBound());
240 EXPECT_EQ(2, c
.ConservativeUpperBound());
243 // Tests Between(m, n).
245 TEST(BetweenTest
, OnNegativeStart
) {
246 EXPECT_NONFATAL_FAILURE({ // NOLINT
248 }, "The invocation lower bound must be >= 0, but is actually -1");
251 TEST(BetweenTest
, OnNegativeEnd
) {
252 EXPECT_NONFATAL_FAILURE({ // NOLINT
254 }, "The invocation upper bound must be >= 0, but is actually -2");
257 TEST(BetweenTest
, OnStartBiggerThanEnd
) {
258 EXPECT_NONFATAL_FAILURE({ // NOLINT
260 }, "The invocation upper bound (1) must be >= "
261 "the invocation lower bound (2)");
264 TEST(BetweenTest
, OnZeroStartAndZeroEnd
) {
265 const Cardinality c
= Between(0, 0);
267 EXPECT_TRUE(c
.IsSatisfiedByCallCount(0));
268 EXPECT_TRUE(c
.IsSaturatedByCallCount(0));
270 EXPECT_FALSE(c
.IsSatisfiedByCallCount(1));
271 EXPECT_TRUE(c
.IsSaturatedByCallCount(1));
275 EXPECT_PRED_FORMAT2(IsSubstring
, "never called",
279 TEST(BetweenTest
, OnZeroStartAndNonZeroEnd
) {
280 const Cardinality c
= Between(0, 2);
282 EXPECT_TRUE(c
.IsSatisfiedByCallCount(0));
283 EXPECT_FALSE(c
.IsSaturatedByCallCount(0));
285 EXPECT_TRUE(c
.IsSatisfiedByCallCount(2));
286 EXPECT_TRUE(c
.IsSaturatedByCallCount(2));
288 EXPECT_FALSE(c
.IsSatisfiedByCallCount(4));
289 EXPECT_TRUE(c
.IsSaturatedByCallCount(4));
293 EXPECT_PRED_FORMAT2(IsSubstring
, "called at most twice",
297 TEST(BetweenTest
, OnSameStartAndEnd
) {
298 const Cardinality c
= Between(3, 3);
300 EXPECT_FALSE(c
.IsSatisfiedByCallCount(2));
301 EXPECT_FALSE(c
.IsSaturatedByCallCount(2));
303 EXPECT_TRUE(c
.IsSatisfiedByCallCount(3));
304 EXPECT_TRUE(c
.IsSaturatedByCallCount(3));
306 EXPECT_FALSE(c
.IsSatisfiedByCallCount(4));
307 EXPECT_TRUE(c
.IsSaturatedByCallCount(4));
311 EXPECT_PRED_FORMAT2(IsSubstring
, "called 3 times",
315 TEST(BetweenTest
, OnDifferentStartAndEnd
) {
316 const Cardinality c
= Between(3, 5);
318 EXPECT_FALSE(c
.IsSatisfiedByCallCount(2));
319 EXPECT_FALSE(c
.IsSaturatedByCallCount(2));
321 EXPECT_TRUE(c
.IsSatisfiedByCallCount(3));
322 EXPECT_FALSE(c
.IsSaturatedByCallCount(3));
324 EXPECT_TRUE(c
.IsSatisfiedByCallCount(5));
325 EXPECT_TRUE(c
.IsSaturatedByCallCount(5));
327 EXPECT_FALSE(c
.IsSatisfiedByCallCount(6));
328 EXPECT_TRUE(c
.IsSaturatedByCallCount(6));
332 EXPECT_PRED_FORMAT2(IsSubstring
, "called between 3 and 5 times",
336 TEST(BetweenTest
, HasCorrectBounds
) {
337 const Cardinality c
= Between(3, 5);
338 EXPECT_EQ(3, c
.ConservativeLowerBound());
339 EXPECT_EQ(5, c
.ConservativeUpperBound());
344 TEST(ExactlyTest
, OnNegativeNumber
) {
345 EXPECT_NONFATAL_FAILURE({ // NOLINT
347 }, "The invocation lower bound must be >= 0");
350 TEST(ExactlyTest
, OnZero
) {
351 const Cardinality c
= Exactly(0);
352 EXPECT_TRUE(c
.IsSatisfiedByCallCount(0));
353 EXPECT_TRUE(c
.IsSaturatedByCallCount(0));
355 EXPECT_FALSE(c
.IsSatisfiedByCallCount(1));
356 EXPECT_TRUE(c
.IsSaturatedByCallCount(1));
360 EXPECT_PRED_FORMAT2(IsSubstring
, "never called",
364 TEST(ExactlyTest
, OnPositiveNumber
) {
365 const Cardinality c
= Exactly(2);
366 EXPECT_FALSE(c
.IsSatisfiedByCallCount(0));
367 EXPECT_FALSE(c
.IsSaturatedByCallCount(0));
369 EXPECT_TRUE(c
.IsSatisfiedByCallCount(2));
370 EXPECT_TRUE(c
.IsSaturatedByCallCount(2));
373 Exactly(1).DescribeTo(&ss1
);
374 EXPECT_PRED_FORMAT2(IsSubstring
, "called once",
379 EXPECT_PRED_FORMAT2(IsSubstring
, "called twice",
383 Exactly(3).DescribeTo(&ss3
);
384 EXPECT_PRED_FORMAT2(IsSubstring
, "called 3 times",
388 TEST(ExactlyTest
, HasCorrectBounds
) {
389 const Cardinality c
= Exactly(3);
390 EXPECT_EQ(3, c
.ConservativeLowerBound());
391 EXPECT_EQ(3, c
.ConservativeUpperBound());
394 // Tests that a user can make his own cardinality by implementing
395 // CardinalityInterface and calling MakeCardinality().
397 class EvenCardinality
: public CardinalityInterface
{
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);
404 // Returns true iff call_count calls will saturate this cardinality.
405 virtual bool IsSaturatedByCallCount(int /* call_count */) const {
409 // Describes self to an ostream.
410 virtual void DescribeTo(::std::ostream
* ss
) const {
411 *ss
<< "called even number of times";
415 TEST(MakeCardinalityTest
, ConstructsCardinalityFromInterface
) {
416 const Cardinality c
= MakeCardinality(new EvenCardinality
);
418 EXPECT_TRUE(c
.IsSatisfiedByCallCount(2));
419 EXPECT_FALSE(c
.IsSatisfiedByCallCount(3));
421 EXPECT_FALSE(c
.IsSaturatedByCallCount(10000));
425 EXPECT_EQ("called even number of times", ss
.str());
428 } // Unnamed namespace