Path: blob/main/contrib/googletest/googlemock/include/gmock/gmock-cardinalities.h
48378 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 implements some commonly used cardinalities. More32// cardinalities can be defined by the user implementing the33// CardinalityInterface interface if necessary.3435// IWYU pragma: private, include "gmock/gmock.h"36// IWYU pragma: friend gmock/.*3738#ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_39#define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_4041#include <limits.h>4243#include <memory>44#include <ostream> // NOLINT4546#include "gmock/internal/gmock-port.h"47#include "gtest/gtest.h"4849GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \50/* class A needs to have dll-interface to be used by clients of class B */)5152namespace testing {5354// To implement a cardinality Foo, define:55// 1. a class FooCardinality that implements the56// CardinalityInterface interface, and57// 2. a factory function that creates a Cardinality object from a58// const FooCardinality*.59//60// The two-level delegation design follows that of Matcher, providing61// consistency for extension developers. It also eases ownership62// management as Cardinality objects can now be copied like plain values.6364// The implementation of a cardinality.65class CardinalityInterface {66public:67virtual ~CardinalityInterface() = default;6869// Conservative estimate on the lower/upper bound of the number of70// calls allowed.71virtual int ConservativeLowerBound() const { return 0; }72virtual int ConservativeUpperBound() const { return INT_MAX; }7374// Returns true if and only if call_count calls will satisfy this75// cardinality.76virtual bool IsSatisfiedByCallCount(int call_count) const = 0;7778// Returns true if and only if call_count calls will saturate this79// cardinality.80virtual bool IsSaturatedByCallCount(int call_count) const = 0;8182// Describes self to an ostream.83virtual void DescribeTo(::std::ostream* os) const = 0;84};8586// A Cardinality is a copyable and IMMUTABLE (except by assignment)87// object that specifies how many times a mock function is expected to88// be called. The implementation of Cardinality is just a std::shared_ptr89// to const CardinalityInterface. Don't inherit from Cardinality!90class GTEST_API_ Cardinality {91public:92// Constructs a null cardinality. Needed for storing Cardinality93// objects in STL containers.94Cardinality() = default;9596// Constructs a Cardinality from its implementation.97explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {}9899// Conservative estimate on the lower/upper bound of the number of100// calls allowed.101int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); }102int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); }103104// Returns true if and only if call_count calls will satisfy this105// cardinality.106bool IsSatisfiedByCallCount(int call_count) const {107return impl_->IsSatisfiedByCallCount(call_count);108}109110// Returns true if and only if call_count calls will saturate this111// cardinality.112bool IsSaturatedByCallCount(int call_count) const {113return impl_->IsSaturatedByCallCount(call_count);114}115116// Returns true if and only if call_count calls will over-saturate this117// cardinality, i.e. exceed the maximum number of allowed calls.118bool IsOverSaturatedByCallCount(int call_count) const {119return impl_->IsSaturatedByCallCount(call_count) &&120!impl_->IsSatisfiedByCallCount(call_count);121}122123// Describes self to an ostream124void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }125126// Describes the given actual call count to an ostream.127static void DescribeActualCallCountTo(int actual_call_count,128::std::ostream* os);129130private:131std::shared_ptr<const CardinalityInterface> impl_;132};133134// Creates a cardinality that allows at least n calls.135GTEST_API_ Cardinality AtLeast(int n);136137// Creates a cardinality that allows at most n calls.138GTEST_API_ Cardinality AtMost(int n);139140// Creates a cardinality that allows any number of calls.141GTEST_API_ Cardinality AnyNumber();142143// Creates a cardinality that allows between min and max calls.144GTEST_API_ Cardinality Between(int min, int max);145146// Creates a cardinality that allows exactly n calls.147GTEST_API_ Cardinality Exactly(int n);148149// Creates a cardinality from its implementation.150inline Cardinality MakeCardinality(const CardinalityInterface* c) {151return Cardinality(c);152}153154} // namespace testing155156GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251157158#endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_159160161