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.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file tests the built-in cardinalities.
35 #include "gmock/gmock.h"
36 #include "gtest/gtest.h"
37 #include "gtest/gtest-spi.h"
41 using std::stringstream;
42 using testing::AnyNumber;
43 using testing::AtLeast;
44 using testing::AtMost;
45 using testing::Between;
46 using testing::Cardinality;
47 using testing::CardinalityInterface;
48 using testing::Exactly;
49 using testing::IsSubstring;
50 using testing::MakeCardinality;
55 MOCK_METHOD0(Bar, int()); // NOLINT
58 GTEST_DISALLOW_COPY_AND_ASSIGN_(MockFoo);
61 // Tests that Cardinality objects can be default constructed.
62 TEST(CardinalityTest, IsDefaultConstructable) {
66 // Tests that Cardinality objects are copyable.
67 TEST(CardinalityTest, IsCopyable) {
68 // Tests the copy constructor.
69 Cardinality c = Exactly(1);
70 EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
71 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
72 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
74 // Tests the assignment operator.
76 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
77 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
78 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
81 TEST(CardinalityTest, IsOverSaturatedByCallCountWorks) {
82 const Cardinality c = AtMost(5);
83 EXPECT_FALSE(c.IsOverSaturatedByCallCount(4));
84 EXPECT_FALSE(c.IsOverSaturatedByCallCount(5));
85 EXPECT_TRUE(c.IsOverSaturatedByCallCount(6));
88 // Tests that Cardinality::DescribeActualCallCountTo() creates the
89 // correct description.
90 TEST(CardinalityTest, CanDescribeActualCallCount) {
92 Cardinality::DescribeActualCallCountTo(0, &ss0);
93 EXPECT_EQ("never called", ss0.str());
96 Cardinality::DescribeActualCallCountTo(1, &ss1);
97 EXPECT_EQ("called once", ss1.str());
100 Cardinality::DescribeActualCallCountTo(2, &ss2);
101 EXPECT_EQ("called twice", ss2.str());
104 Cardinality::DescribeActualCallCountTo(3, &ss3);
105 EXPECT_EQ("called 3 times", ss3.str());
109 TEST(AnyNumber, Works) {
110 const Cardinality c = AnyNumber();
111 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
112 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
114 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
115 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
117 EXPECT_TRUE(c.IsSatisfiedByCallCount(9));
118 EXPECT_FALSE(c.IsSaturatedByCallCount(9));
122 EXPECT_PRED_FORMAT2(IsSubstring, "called any number of times",
126 TEST(AnyNumberTest, HasCorrectBounds) {
127 const Cardinality c = AnyNumber();
128 EXPECT_EQ(0, c.ConservativeLowerBound());
129 EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
134 TEST(AtLeastTest, OnNegativeNumber) {
135 EXPECT_NONFATAL_FAILURE({ // NOLINT
137 }, "The invocation lower bound must be >= 0");
140 TEST(AtLeastTest, OnZero) {
141 const Cardinality c = AtLeast(0);
142 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
143 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
145 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
146 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
150 EXPECT_PRED_FORMAT2(IsSubstring, "any number of times",
154 TEST(AtLeastTest, OnPositiveNumber) {
155 const Cardinality c = AtLeast(2);
156 EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
157 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
159 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
160 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
162 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
163 EXPECT_FALSE(c.IsSaturatedByCallCount(2));
166 AtLeast(1).DescribeTo(&ss1);
167 EXPECT_PRED_FORMAT2(IsSubstring, "at least once",
172 EXPECT_PRED_FORMAT2(IsSubstring, "at least twice",
176 AtLeast(3).DescribeTo(&ss3);
177 EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times",
181 TEST(AtLeastTest, HasCorrectBounds) {
182 const Cardinality c = AtLeast(2);
183 EXPECT_EQ(2, c.ConservativeLowerBound());
184 EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());
189 TEST(AtMostTest, OnNegativeNumber) {
190 EXPECT_NONFATAL_FAILURE({ // NOLINT
192 }, "The invocation upper bound must be >= 0");
195 TEST(AtMostTest, OnZero) {
196 const Cardinality c = AtMost(0);
197 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
198 EXPECT_TRUE(c.IsSaturatedByCallCount(0));
200 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
201 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
205 EXPECT_PRED_FORMAT2(IsSubstring, "never called",
209 TEST(AtMostTest, OnPositiveNumber) {
210 const Cardinality c = AtMost(2);
211 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
212 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
214 EXPECT_TRUE(c.IsSatisfiedByCallCount(1));
215 EXPECT_FALSE(c.IsSaturatedByCallCount(1));
217 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
218 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
221 AtMost(1).DescribeTo(&ss1);
222 EXPECT_PRED_FORMAT2(IsSubstring, "called at most once",
227 EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
231 AtMost(3).DescribeTo(&ss3);
232 EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times",
236 TEST(AtMostTest, HasCorrectBounds) {
237 const Cardinality c = AtMost(2);
238 EXPECT_EQ(0, c.ConservativeLowerBound());
239 EXPECT_EQ(2, c.ConservativeUpperBound());
242 // Tests Between(m, n).
244 TEST(BetweenTest, OnNegativeStart) {
245 EXPECT_NONFATAL_FAILURE({ // NOLINT
247 }, "The invocation lower bound must be >= 0, but is actually -1");
250 TEST(BetweenTest, OnNegativeEnd) {
251 EXPECT_NONFATAL_FAILURE({ // NOLINT
253 }, "The invocation upper bound must be >= 0, but is actually -2");
256 TEST(BetweenTest, OnStartBiggerThanEnd) {
257 EXPECT_NONFATAL_FAILURE({ // NOLINT
259 }, "The invocation upper bound (1) must be >= "
260 "the invocation lower bound (2)");
263 TEST(BetweenTest, OnZeroStartAndZeroEnd) {
264 const Cardinality c = Between(0, 0);
266 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
267 EXPECT_TRUE(c.IsSaturatedByCallCount(0));
269 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
270 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
274 EXPECT_PRED_FORMAT2(IsSubstring, "never called",
278 TEST(BetweenTest, OnZeroStartAndNonZeroEnd) {
279 const Cardinality c = Between(0, 2);
281 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
282 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
284 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
285 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
287 EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
288 EXPECT_TRUE(c.IsSaturatedByCallCount(4));
292 EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice",
296 TEST(BetweenTest, OnSameStartAndEnd) {
297 const Cardinality c = Between(3, 3);
299 EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
300 EXPECT_FALSE(c.IsSaturatedByCallCount(2));
302 EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
303 EXPECT_TRUE(c.IsSaturatedByCallCount(3));
305 EXPECT_FALSE(c.IsSatisfiedByCallCount(4));
306 EXPECT_TRUE(c.IsSaturatedByCallCount(4));
310 EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
314 TEST(BetweenTest, OnDifferentStartAndEnd) {
315 const Cardinality c = Between(3, 5);
317 EXPECT_FALSE(c.IsSatisfiedByCallCount(2));
318 EXPECT_FALSE(c.IsSaturatedByCallCount(2));
320 EXPECT_TRUE(c.IsSatisfiedByCallCount(3));
321 EXPECT_FALSE(c.IsSaturatedByCallCount(3));
323 EXPECT_TRUE(c.IsSatisfiedByCallCount(5));
324 EXPECT_TRUE(c.IsSaturatedByCallCount(5));
326 EXPECT_FALSE(c.IsSatisfiedByCallCount(6));
327 EXPECT_TRUE(c.IsSaturatedByCallCount(6));
331 EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times",
335 TEST(BetweenTest, HasCorrectBounds) {
336 const Cardinality c = Between(3, 5);
337 EXPECT_EQ(3, c.ConservativeLowerBound());
338 EXPECT_EQ(5, c.ConservativeUpperBound());
343 TEST(ExactlyTest, OnNegativeNumber) {
344 EXPECT_NONFATAL_FAILURE({ // NOLINT
346 }, "The invocation lower bound must be >= 0");
349 TEST(ExactlyTest, OnZero) {
350 const Cardinality c = Exactly(0);
351 EXPECT_TRUE(c.IsSatisfiedByCallCount(0));
352 EXPECT_TRUE(c.IsSaturatedByCallCount(0));
354 EXPECT_FALSE(c.IsSatisfiedByCallCount(1));
355 EXPECT_TRUE(c.IsSaturatedByCallCount(1));
359 EXPECT_PRED_FORMAT2(IsSubstring, "never called",
363 TEST(ExactlyTest, OnPositiveNumber) {
364 const Cardinality c = Exactly(2);
365 EXPECT_FALSE(c.IsSatisfiedByCallCount(0));
366 EXPECT_FALSE(c.IsSaturatedByCallCount(0));
368 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
369 EXPECT_TRUE(c.IsSaturatedByCallCount(2));
372 Exactly(1).DescribeTo(&ss1);
373 EXPECT_PRED_FORMAT2(IsSubstring, "called once",
378 EXPECT_PRED_FORMAT2(IsSubstring, "called twice",
382 Exactly(3).DescribeTo(&ss3);
383 EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times",
387 TEST(ExactlyTest, HasCorrectBounds) {
388 const Cardinality c = Exactly(3);
389 EXPECT_EQ(3, c.ConservativeLowerBound());
390 EXPECT_EQ(3, c.ConservativeUpperBound());
393 // Tests that a user can make their own cardinality by implementing
394 // CardinalityInterface and calling MakeCardinality().
396 class EvenCardinality : public CardinalityInterface {
398 // Returns true iff call_count calls will satisfy this cardinality.
399 virtual bool IsSatisfiedByCallCount(int call_count) const {
400 return (call_count % 2 == 0);
403 // Returns true iff call_count calls will saturate this cardinality.
404 virtual bool IsSaturatedByCallCount(int /* call_count */) const {
408 // Describes self to an ostream.
409 virtual void DescribeTo(::std::ostream* ss) const {
410 *ss << "called even number of times";
414 TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {
415 const Cardinality c = MakeCardinality(new EvenCardinality);
417 EXPECT_TRUE(c.IsSatisfiedByCallCount(2));
418 EXPECT_FALSE(c.IsSatisfiedByCallCount(3));
420 EXPECT_FALSE(c.IsSaturatedByCallCount(10000));
424 EXPECT_EQ("called even number of times", ss.str());
427 } // Unnamed namespace