00001 // Copyright 2007, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: wan@google.com (Zhanyong Wan) 00031 00032 // Google Mock - a framework for writing C++ mock classes. 00033 // 00034 // This file implements some commonly used cardinalities. More 00035 // cardinalities can be defined by the user implementing the 00036 // CardinalityInterface interface if necessary. 00037 00038 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_ 00039 #define GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_ 00040 00041 #include <limits.h> 00042 #include <ostream> // NOLINT 00043 #include "gmock/internal/gmock-port.h" 00044 #include "gtest/gtest.h" 00045 00046 namespace testing { 00047 00048 // To implement a cardinality Foo, define: 00049 // 1. a class FooCardinality that implements the 00050 // CardinalityInterface interface, and 00051 // 2. a factory function that creates a Cardinality object from a 00052 // const FooCardinality*. 00053 // 00054 // The two-level delegation design follows that of Matcher, providing 00055 // consistency for extension developers. It also eases ownership 00056 // management as Cardinality objects can now be copied like plain values. 00057 00058 // The implementation of a cardinality. 00059 class CardinalityInterface { 00060 public: 00061 virtual ~CardinalityInterface() {} 00062 00063 // Conservative estimate on the lower/upper bound of the number of 00064 // calls allowed. 00065 virtual int ConservativeLowerBound() const { return 0; } 00066 virtual int ConservativeUpperBound() const { return INT_MAX; } 00067 00068 // Returns true iff call_count calls will satisfy this cardinality. 00069 virtual bool IsSatisfiedByCallCount(int call_count) const = 0; 00070 00071 // Returns true iff call_count calls will saturate this cardinality. 00072 virtual bool IsSaturatedByCallCount(int call_count) const = 0; 00073 00074 // Describes self to an ostream. 00075 virtual void DescribeTo(::std::ostream* os) const = 0; 00076 }; 00077 00078 // A Cardinality is a copyable and IMMUTABLE (except by assignment) 00079 // object that specifies how many times a mock function is expected to 00080 // be called. The implementation of Cardinality is just a linked_ptr 00081 // to const CardinalityInterface, so copying is fairly cheap. 00082 // Don't inherit from Cardinality! 00083 class GTEST_API_ Cardinality { 00084 public: 00085 // Constructs a null cardinality. Needed for storing Cardinality 00086 // objects in STL containers. 00087 Cardinality() {} 00088 00089 // Constructs a Cardinality from its implementation. 00090 explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {} 00091 00092 // Conservative estimate on the lower/upper bound of the number of 00093 // calls allowed. 00094 int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); } 00095 int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); } 00096 00097 // Returns true iff call_count calls will satisfy this cardinality. 00098 bool IsSatisfiedByCallCount(int call_count) const { 00099 return impl_->IsSatisfiedByCallCount(call_count); 00100 } 00101 00102 // Returns true iff call_count calls will saturate this cardinality. 00103 bool IsSaturatedByCallCount(int call_count) const { 00104 return impl_->IsSaturatedByCallCount(call_count); 00105 } 00106 00107 // Returns true iff call_count calls will over-saturate this 00108 // cardinality, i.e. exceed the maximum number of allowed calls. 00109 bool IsOverSaturatedByCallCount(int call_count) const { 00110 return impl_->IsSaturatedByCallCount(call_count) && 00111 !impl_->IsSatisfiedByCallCount(call_count); 00112 } 00113 00114 // Describes self to an ostream 00115 void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); } 00116 00117 // Describes the given actual call count to an ostream. 00118 static void DescribeActualCallCountTo(int actual_call_count, 00119 ::std::ostream* os); 00120 00121 private: 00122 internal::linked_ptr<const CardinalityInterface> impl_; 00123 }; 00124 00125 // Creates a cardinality that allows at least n calls. 00126 GTEST_API_ Cardinality AtLeast(int n); 00127 00128 // Creates a cardinality that allows at most n calls. 00129 GTEST_API_ Cardinality AtMost(int n); 00130 00131 // Creates a cardinality that allows any number of calls. 00132 GTEST_API_ Cardinality AnyNumber(); 00133 00134 // Creates a cardinality that allows between min and max calls. 00135 GTEST_API_ Cardinality Between(int min, int max); 00136 00137 // Creates a cardinality that allows exactly n calls. 00138 GTEST_API_ Cardinality Exactly(int n); 00139 00140 // Creates a cardinality from its implementation. 00141 inline Cardinality MakeCardinality(const CardinalityInterface* c) { 00142 return Cardinality(c); 00143 } 00144 00145 } // namespace testing 00146 00147 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_