![]() |
|
|||
File indexing completed on 2025-02-21 10:03:05
0001 // Copyright 2007, Google Inc. 0002 // All rights reserved. 0003 // 0004 // Redistribution and use in source and binary forms, with or without 0005 // modification, are permitted provided that the following conditions are 0006 // met: 0007 // 0008 // * Redistributions of source code must retain the above copyright 0009 // notice, this list of conditions and the following disclaimer. 0010 // * Redistributions in binary form must reproduce the above 0011 // copyright notice, this list of conditions and the following disclaimer 0012 // in the documentation and/or other materials provided with the 0013 // distribution. 0014 // * Neither the name of Google Inc. nor the names of its 0015 // contributors may be used to endorse or promote products derived from 0016 // this software without specific prior written permission. 0017 // 0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0029 0030 // Google Mock - a framework for writing C++ mock classes. 0031 // 0032 // This file implements some commonly used cardinalities. More 0033 // cardinalities can be defined by the user implementing the 0034 // CardinalityInterface interface if necessary. 0035 0036 // IWYU pragma: private, include "gmock/gmock.h" 0037 // IWYU pragma: friend gmock/.* 0038 0039 #ifndef GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_ 0040 #define GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_ 0041 0042 #include <limits.h> 0043 0044 #include <memory> 0045 #include <ostream> // NOLINT 0046 0047 #include "gmock/internal/gmock-port.h" 0048 #include "gtest/gtest.h" 0049 0050 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ 0051 /* class A needs to have dll-interface to be used by clients of class B */) 0052 0053 namespace testing { 0054 0055 // To implement a cardinality Foo, define: 0056 // 1. a class FooCardinality that implements the 0057 // CardinalityInterface interface, and 0058 // 2. a factory function that creates a Cardinality object from a 0059 // const FooCardinality*. 0060 // 0061 // The two-level delegation design follows that of Matcher, providing 0062 // consistency for extension developers. It also eases ownership 0063 // management as Cardinality objects can now be copied like plain values. 0064 0065 // The implementation of a cardinality. 0066 class CardinalityInterface { 0067 public: 0068 virtual ~CardinalityInterface() {} 0069 0070 // Conservative estimate on the lower/upper bound of the number of 0071 // calls allowed. 0072 virtual int ConservativeLowerBound() const { return 0; } 0073 virtual int ConservativeUpperBound() const { return INT_MAX; } 0074 0075 // Returns true if and only if call_count calls will satisfy this 0076 // cardinality. 0077 virtual bool IsSatisfiedByCallCount(int call_count) const = 0; 0078 0079 // Returns true if and only if call_count calls will saturate this 0080 // cardinality. 0081 virtual bool IsSaturatedByCallCount(int call_count) const = 0; 0082 0083 // Describes self to an ostream. 0084 virtual void DescribeTo(::std::ostream* os) const = 0; 0085 }; 0086 0087 // A Cardinality is a copyable and IMMUTABLE (except by assignment) 0088 // object that specifies how many times a mock function is expected to 0089 // be called. The implementation of Cardinality is just a std::shared_ptr 0090 // to const CardinalityInterface. Don't inherit from Cardinality! 0091 class GTEST_API_ Cardinality { 0092 public: 0093 // Constructs a null cardinality. Needed for storing Cardinality 0094 // objects in STL containers. 0095 Cardinality() {} 0096 0097 // Constructs a Cardinality from its implementation. 0098 explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {} 0099 0100 // Conservative estimate on the lower/upper bound of the number of 0101 // calls allowed. 0102 int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); } 0103 int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); } 0104 0105 // Returns true if and only if call_count calls will satisfy this 0106 // cardinality. 0107 bool IsSatisfiedByCallCount(int call_count) const { 0108 return impl_->IsSatisfiedByCallCount(call_count); 0109 } 0110 0111 // Returns true if and only if call_count calls will saturate this 0112 // cardinality. 0113 bool IsSaturatedByCallCount(int call_count) const { 0114 return impl_->IsSaturatedByCallCount(call_count); 0115 } 0116 0117 // Returns true if and only if call_count calls will over-saturate this 0118 // cardinality, i.e. exceed the maximum number of allowed calls. 0119 bool IsOverSaturatedByCallCount(int call_count) const { 0120 return impl_->IsSaturatedByCallCount(call_count) && 0121 !impl_->IsSatisfiedByCallCount(call_count); 0122 } 0123 0124 // Describes self to an ostream 0125 void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } 0126 0127 // Describes the given actual call count to an ostream. 0128 static void DescribeActualCallCountTo(int actual_call_count, 0129 ::std::ostream* os); 0130 0131 private: 0132 std::shared_ptr<const CardinalityInterface> impl_; 0133 }; 0134 0135 // Creates a cardinality that allows at least n calls. 0136 GTEST_API_ Cardinality AtLeast(int n); 0137 0138 // Creates a cardinality that allows at most n calls. 0139 GTEST_API_ Cardinality AtMost(int n); 0140 0141 // Creates a cardinality that allows any number of calls. 0142 GTEST_API_ Cardinality AnyNumber(); 0143 0144 // Creates a cardinality that allows between min and max calls. 0145 GTEST_API_ Cardinality Between(int min, int max); 0146 0147 // Creates a cardinality that allows exactly n calls. 0148 GTEST_API_ Cardinality Exactly(int n); 0149 0150 // Creates a cardinality from its implementation. 0151 inline Cardinality MakeCardinality(const CardinalityInterface* c) { 0152 return Cardinality(c); 0153 } 0154 0155 } // namespace testing 0156 0157 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 0158 0159 #endif // GOOGLEMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |