Path: blob/main/contrib/googletest/googlemock/test/gmock-cardinalities_test.cc
48255 views
// Copyright 2007, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Google Mock - a framework for writing C++ mock classes.30//31// This file tests the built-in cardinalities.3233#include <ostream>3435#include "gmock/gmock.h"36#include "gtest/gtest-spi.h"37#include "gtest/gtest.h"3839namespace {4041using std::stringstream;42using testing::AnyNumber;43using testing::AtLeast;44using testing::AtMost;45using testing::Between;46using testing::Cardinality;47using testing::CardinalityInterface;48using testing::Exactly;49using testing::IsSubstring;50using testing::MakeCardinality;5152class MockFoo {53public:54MockFoo() = default;55MOCK_METHOD0(Bar, int()); // NOLINT5657private:58MockFoo(const MockFoo&) = delete;59MockFoo& operator=(const MockFoo&) = delete;60};6162// Tests that Cardinality objects can be default constructed.63TEST(CardinalityTest, IsDefaultConstructable) { Cardinality c; }6465// Tests that Cardinality objects are copyable.66TEST(CardinalityTest, IsCopyable) {67// Tests the copy constructor.68Cardinality c = Exactly(1);69EXPECT_FALSE(c.IsSatisfiedByCallCount(0));70EXPECT_TRUE(c.IsSatisfiedByCallCount(1));71EXPECT_TRUE(c.IsSaturatedByCallCount(1));7273// Tests the assignment operator.74c = Exactly(2);75EXPECT_FALSE(c.IsSatisfiedByCallCount(1));76EXPECT_TRUE(c.IsSatisfiedByCallCount(2));77EXPECT_TRUE(c.IsSaturatedByCallCount(2));78}7980TEST(CardinalityTest, IsOverSaturatedByCallCountWorks) {81const Cardinality c = AtMost(5);82EXPECT_FALSE(c.IsOverSaturatedByCallCount(4));83EXPECT_FALSE(c.IsOverSaturatedByCallCount(5));84EXPECT_TRUE(c.IsOverSaturatedByCallCount(6));85}8687// Tests that Cardinality::DescribeActualCallCountTo() creates the88// correct description.89TEST(CardinalityTest, CanDescribeActualCallCount) {90stringstream ss0;91Cardinality::DescribeActualCallCountTo(0, &ss0);92EXPECT_EQ("never called", ss0.str());9394stringstream ss1;95Cardinality::DescribeActualCallCountTo(1, &ss1);96EXPECT_EQ("called once", ss1.str());9798stringstream ss2;99Cardinality::DescribeActualCallCountTo(2, &ss2);100EXPECT_EQ("called twice", ss2.str());101102stringstream ss3;103Cardinality::DescribeActualCallCountTo(3, &ss3);104EXPECT_EQ("called 3 times", ss3.str());105}106107// Tests AnyNumber()108TEST(AnyNumber, Works) {109const Cardinality c = AnyNumber();110EXPECT_TRUE(c.IsSatisfiedByCallCount(0));111EXPECT_FALSE(c.IsSaturatedByCallCount(0));112113EXPECT_TRUE(c.IsSatisfiedByCallCount(1));114EXPECT_FALSE(c.IsSaturatedByCallCount(1));115116EXPECT_TRUE(c.IsSatisfiedByCallCount(9));117EXPECT_FALSE(c.IsSaturatedByCallCount(9));118119stringstream ss;120c.DescribeTo(&ss);121EXPECT_PRED_FORMAT2(IsSubstring, "called any number of times", ss.str());122}123124TEST(AnyNumberTest, HasCorrectBounds) {125const Cardinality c = AnyNumber();126EXPECT_EQ(0, c.ConservativeLowerBound());127EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());128}129130// Tests AtLeast(n).131132TEST(AtLeastTest, OnNegativeNumber) {133EXPECT_NONFATAL_FAILURE(134{ // NOLINT135AtLeast(-1);136},137"The invocation lower bound must be >= 0");138}139140TEST(AtLeastTest, OnZero) {141const Cardinality c = AtLeast(0);142EXPECT_TRUE(c.IsSatisfiedByCallCount(0));143EXPECT_FALSE(c.IsSaturatedByCallCount(0));144145EXPECT_TRUE(c.IsSatisfiedByCallCount(1));146EXPECT_FALSE(c.IsSaturatedByCallCount(1));147148stringstream ss;149c.DescribeTo(&ss);150EXPECT_PRED_FORMAT2(IsSubstring, "any number of times", ss.str());151}152153TEST(AtLeastTest, OnPositiveNumber) {154const Cardinality c = AtLeast(2);155EXPECT_FALSE(c.IsSatisfiedByCallCount(0));156EXPECT_FALSE(c.IsSaturatedByCallCount(0));157158EXPECT_FALSE(c.IsSatisfiedByCallCount(1));159EXPECT_FALSE(c.IsSaturatedByCallCount(1));160161EXPECT_TRUE(c.IsSatisfiedByCallCount(2));162EXPECT_FALSE(c.IsSaturatedByCallCount(2));163164stringstream ss1;165AtLeast(1).DescribeTo(&ss1);166EXPECT_PRED_FORMAT2(IsSubstring, "at least once", ss1.str());167168stringstream ss2;169c.DescribeTo(&ss2);170EXPECT_PRED_FORMAT2(IsSubstring, "at least twice", ss2.str());171172stringstream ss3;173AtLeast(3).DescribeTo(&ss3);174EXPECT_PRED_FORMAT2(IsSubstring, "at least 3 times", ss3.str());175}176177TEST(AtLeastTest, HasCorrectBounds) {178const Cardinality c = AtLeast(2);179EXPECT_EQ(2, c.ConservativeLowerBound());180EXPECT_EQ(INT_MAX, c.ConservativeUpperBound());181}182183// Tests AtMost(n).184185TEST(AtMostTest, OnNegativeNumber) {186EXPECT_NONFATAL_FAILURE(187{ // NOLINT188AtMost(-1);189},190"The invocation upper bound must be >= 0");191}192193TEST(AtMostTest, OnZero) {194const Cardinality c = AtMost(0);195EXPECT_TRUE(c.IsSatisfiedByCallCount(0));196EXPECT_TRUE(c.IsSaturatedByCallCount(0));197198EXPECT_FALSE(c.IsSatisfiedByCallCount(1));199EXPECT_TRUE(c.IsSaturatedByCallCount(1));200201stringstream ss;202c.DescribeTo(&ss);203EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());204}205206TEST(AtMostTest, OnPositiveNumber) {207const Cardinality c = AtMost(2);208EXPECT_TRUE(c.IsSatisfiedByCallCount(0));209EXPECT_FALSE(c.IsSaturatedByCallCount(0));210211EXPECT_TRUE(c.IsSatisfiedByCallCount(1));212EXPECT_FALSE(c.IsSaturatedByCallCount(1));213214EXPECT_TRUE(c.IsSatisfiedByCallCount(2));215EXPECT_TRUE(c.IsSaturatedByCallCount(2));216217stringstream ss1;218AtMost(1).DescribeTo(&ss1);219EXPECT_PRED_FORMAT2(IsSubstring, "called at most once", ss1.str());220221stringstream ss2;222c.DescribeTo(&ss2);223EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice", ss2.str());224225stringstream ss3;226AtMost(3).DescribeTo(&ss3);227EXPECT_PRED_FORMAT2(IsSubstring, "called at most 3 times", ss3.str());228}229230TEST(AtMostTest, HasCorrectBounds) {231const Cardinality c = AtMost(2);232EXPECT_EQ(0, c.ConservativeLowerBound());233EXPECT_EQ(2, c.ConservativeUpperBound());234}235236// Tests Between(m, n).237238TEST(BetweenTest, OnNegativeStart) {239EXPECT_NONFATAL_FAILURE(240{ // NOLINT241Between(-1, 2);242},243"The invocation lower bound must be >= 0, but is actually -1");244}245246TEST(BetweenTest, OnNegativeEnd) {247EXPECT_NONFATAL_FAILURE(248{ // NOLINT249Between(1, -2);250},251"The invocation upper bound must be >= 0, but is actually -2");252}253254TEST(BetweenTest, OnStartBiggerThanEnd) {255EXPECT_NONFATAL_FAILURE(256{ // NOLINT257Between(2, 1);258},259"The invocation upper bound (1) must be >= "260"the invocation lower bound (2)");261}262263TEST(BetweenTest, OnZeroStartAndZeroEnd) {264const Cardinality c = Between(0, 0);265266EXPECT_TRUE(c.IsSatisfiedByCallCount(0));267EXPECT_TRUE(c.IsSaturatedByCallCount(0));268269EXPECT_FALSE(c.IsSatisfiedByCallCount(1));270EXPECT_TRUE(c.IsSaturatedByCallCount(1));271272stringstream ss;273c.DescribeTo(&ss);274EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());275}276277TEST(BetweenTest, OnZeroStartAndNonZeroEnd) {278const Cardinality c = Between(0, 2);279280EXPECT_TRUE(c.IsSatisfiedByCallCount(0));281EXPECT_FALSE(c.IsSaturatedByCallCount(0));282283EXPECT_TRUE(c.IsSatisfiedByCallCount(2));284EXPECT_TRUE(c.IsSaturatedByCallCount(2));285286EXPECT_FALSE(c.IsSatisfiedByCallCount(4));287EXPECT_TRUE(c.IsSaturatedByCallCount(4));288289stringstream ss;290c.DescribeTo(&ss);291EXPECT_PRED_FORMAT2(IsSubstring, "called at most twice", ss.str());292}293294TEST(BetweenTest, OnSameStartAndEnd) {295const Cardinality c = Between(3, 3);296297EXPECT_FALSE(c.IsSatisfiedByCallCount(2));298EXPECT_FALSE(c.IsSaturatedByCallCount(2));299300EXPECT_TRUE(c.IsSatisfiedByCallCount(3));301EXPECT_TRUE(c.IsSaturatedByCallCount(3));302303EXPECT_FALSE(c.IsSatisfiedByCallCount(4));304EXPECT_TRUE(c.IsSaturatedByCallCount(4));305306stringstream ss;307c.DescribeTo(&ss);308EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", ss.str());309}310311TEST(BetweenTest, OnDifferentStartAndEnd) {312const Cardinality c = Between(3, 5);313314EXPECT_FALSE(c.IsSatisfiedByCallCount(2));315EXPECT_FALSE(c.IsSaturatedByCallCount(2));316317EXPECT_TRUE(c.IsSatisfiedByCallCount(3));318EXPECT_FALSE(c.IsSaturatedByCallCount(3));319320EXPECT_TRUE(c.IsSatisfiedByCallCount(5));321EXPECT_TRUE(c.IsSaturatedByCallCount(5));322323EXPECT_FALSE(c.IsSatisfiedByCallCount(6));324EXPECT_TRUE(c.IsSaturatedByCallCount(6));325326stringstream ss;327c.DescribeTo(&ss);328EXPECT_PRED_FORMAT2(IsSubstring, "called between 3 and 5 times", ss.str());329}330331TEST(BetweenTest, HasCorrectBounds) {332const Cardinality c = Between(3, 5);333EXPECT_EQ(3, c.ConservativeLowerBound());334EXPECT_EQ(5, c.ConservativeUpperBound());335}336337// Tests Exactly(n).338339TEST(ExactlyTest, OnNegativeNumber) {340EXPECT_NONFATAL_FAILURE(341{ // NOLINT342Exactly(-1);343},344"The invocation lower bound must be >= 0");345}346347TEST(ExactlyTest, OnZero) {348const Cardinality c = Exactly(0);349EXPECT_TRUE(c.IsSatisfiedByCallCount(0));350EXPECT_TRUE(c.IsSaturatedByCallCount(0));351352EXPECT_FALSE(c.IsSatisfiedByCallCount(1));353EXPECT_TRUE(c.IsSaturatedByCallCount(1));354355stringstream ss;356c.DescribeTo(&ss);357EXPECT_PRED_FORMAT2(IsSubstring, "never called", ss.str());358}359360TEST(ExactlyTest, OnPositiveNumber) {361const Cardinality c = Exactly(2);362EXPECT_FALSE(c.IsSatisfiedByCallCount(0));363EXPECT_FALSE(c.IsSaturatedByCallCount(0));364365EXPECT_TRUE(c.IsSatisfiedByCallCount(2));366EXPECT_TRUE(c.IsSaturatedByCallCount(2));367368stringstream ss1;369Exactly(1).DescribeTo(&ss1);370EXPECT_PRED_FORMAT2(IsSubstring, "called once", ss1.str());371372stringstream ss2;373c.DescribeTo(&ss2);374EXPECT_PRED_FORMAT2(IsSubstring, "called twice", ss2.str());375376stringstream ss3;377Exactly(3).DescribeTo(&ss3);378EXPECT_PRED_FORMAT2(IsSubstring, "called 3 times", ss3.str());379}380381TEST(ExactlyTest, HasCorrectBounds) {382const Cardinality c = Exactly(3);383EXPECT_EQ(3, c.ConservativeLowerBound());384EXPECT_EQ(3, c.ConservativeUpperBound());385}386387// Tests that a user can make their own cardinality by implementing388// CardinalityInterface and calling MakeCardinality().389390class EvenCardinality : public CardinalityInterface {391public:392// Returns true if and only if call_count calls will satisfy this393// cardinality.394bool IsSatisfiedByCallCount(int call_count) const override {395return (call_count % 2 == 0);396}397398// Returns true if and only if call_count calls will saturate this399// cardinality.400bool IsSaturatedByCallCount(int /* call_count */) const override {401return false;402}403404// Describes self to an ostream.405void DescribeTo(::std::ostream* ss) const override {406*ss << "called even number of times";407}408};409410TEST(MakeCardinalityTest, ConstructsCardinalityFromInterface) {411const Cardinality c = MakeCardinality(new EvenCardinality);412413EXPECT_TRUE(c.IsSatisfiedByCallCount(2));414EXPECT_FALSE(c.IsSatisfiedByCallCount(3));415416EXPECT_FALSE(c.IsSaturatedByCallCount(10000));417418stringstream ss;419c.DescribeTo(&ss);420EXPECT_EQ("called even number of times", ss.str());421}422423} // Unnamed namespace424425426