blob: 759dcf8c9048d5532a609dad01ef1bb4d6f2e4da [file] [log] [blame]
shiqiane35fdd92008-12-10 05:08:54 +00001// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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
13// distribution.
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.
17//
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.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
32// Google Mock - a framework for writing C++ mock classes.
33//
34// This file implements some commonly used cardinalities. More
35// cardinalities can be defined by the user implementing the
36// CardinalityInterface interface if necessary.
37
38#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
39#define GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_
40
41#include <limits.h>
42#include <ostream> // NOLINT
zhanyong.wan53e08c42010-09-14 05:38:21 +000043#include "gmock/internal/gmock-port.h"
44#include "gtest/gtest.h"
shiqiane35fdd92008-12-10 05:08:54 +000045
46namespace testing {
47
48// To implement a cardinality Foo, define:
49// 1. a class FooCardinality that implements the
50// CardinalityInterface interface, and
51// 2. a factory function that creates a Cardinality object from a
52// const FooCardinality*.
53//
54// The two-level delegation design follows that of Matcher, providing
55// consistency for extension developers. It also eases ownership
56// management as Cardinality objects can now be copied like plain values.
57
58// The implementation of a cardinality.
59class CardinalityInterface {
60 public:
61 virtual ~CardinalityInterface() {}
62
63 // Conservative estimate on the lower/upper bound of the number of
64 // calls allowed.
65 virtual int ConservativeLowerBound() const { return 0; }
66 virtual int ConservativeUpperBound() const { return INT_MAX; }
67
68 // Returns true iff call_count calls will satisfy this cardinality.
69 virtual bool IsSatisfiedByCallCount(int call_count) const = 0;
70
71 // Returns true iff call_count calls will saturate this cardinality.
72 virtual bool IsSaturatedByCallCount(int call_count) const = 0;
73
74 // Describes self to an ostream.
75 virtual void DescribeTo(::std::ostream* os) const = 0;
76};
77
78// A Cardinality is a copyable and IMMUTABLE (except by assignment)
79// object that specifies how many times a mock function is expected to
80// be called. The implementation of Cardinality is just a linked_ptr
81// to const CardinalityInterface, so copying is fairly cheap.
82// Don't inherit from Cardinality!
vladlosev587c1b32011-05-20 00:42:22 +000083class GTEST_API_ Cardinality {
shiqiane35fdd92008-12-10 05:08:54 +000084 public:
85 // Constructs a null cardinality. Needed for storing Cardinality
86 // objects in STL containers.
87 Cardinality() {}
88
89 // Constructs a Cardinality from its implementation.
90 explicit Cardinality(const CardinalityInterface* impl) : impl_(impl) {}
91
92 // Conservative estimate on the lower/upper bound of the number of
93 // calls allowed.
94 int ConservativeLowerBound() const { return impl_->ConservativeLowerBound(); }
95 int ConservativeUpperBound() const { return impl_->ConservativeUpperBound(); }
96
97 // Returns true iff call_count calls will satisfy this cardinality.
98 bool IsSatisfiedByCallCount(int call_count) const {
99 return impl_->IsSatisfiedByCallCount(call_count);
100 }
101
102 // Returns true iff call_count calls will saturate this cardinality.
103 bool IsSaturatedByCallCount(int call_count) const {
104 return impl_->IsSaturatedByCallCount(call_count);
105 }
106
107 // Returns true iff call_count calls will over-saturate this
108 // cardinality, i.e. exceed the maximum number of allowed calls.
109 bool IsOverSaturatedByCallCount(int call_count) const {
110 return impl_->IsSaturatedByCallCount(call_count) &&
111 !impl_->IsSatisfiedByCallCount(call_count);
112 }
113
114 // Describes self to an ostream
115 void DescribeTo(::std::ostream* os) const { impl_->DescribeTo(os); }
116
117 // Describes the given actual call count to an ostream.
118 static void DescribeActualCallCountTo(int actual_call_count,
119 ::std::ostream* os);
120 private:
121 internal::linked_ptr<const CardinalityInterface> impl_;
122};
123
124// Creates a cardinality that allows at least n calls.
vladlosev587c1b32011-05-20 00:42:22 +0000125GTEST_API_ Cardinality AtLeast(int n);
shiqiane35fdd92008-12-10 05:08:54 +0000126
127// Creates a cardinality that allows at most n calls.
vladlosev587c1b32011-05-20 00:42:22 +0000128GTEST_API_ Cardinality AtMost(int n);
shiqiane35fdd92008-12-10 05:08:54 +0000129
130// Creates a cardinality that allows any number of calls.
vladlosev587c1b32011-05-20 00:42:22 +0000131GTEST_API_ Cardinality AnyNumber();
shiqiane35fdd92008-12-10 05:08:54 +0000132
133// Creates a cardinality that allows between min and max calls.
vladlosev587c1b32011-05-20 00:42:22 +0000134GTEST_API_ Cardinality Between(int min, int max);
shiqiane35fdd92008-12-10 05:08:54 +0000135
136// Creates a cardinality that allows exactly n calls.
vladlosev587c1b32011-05-20 00:42:22 +0000137GTEST_API_ Cardinality Exactly(int n);
shiqiane35fdd92008-12-10 05:08:54 +0000138
139// Creates a cardinality from its implementation.
140inline Cardinality MakeCardinality(const CardinalityInterface* c) {
141 return Cardinality(c);
142}
143
144} // namespace testing
145
146#endif // GMOCK_INCLUDE_GMOCK_GMOCK_CARDINALITIES_H_