shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // 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 the ON_CALL() and EXPECT_CALL() macros. |
| 35 | // |
| 36 | // A user can use the ON_CALL() macro to specify the default action of |
| 37 | // a mock method. The syntax is: |
| 38 | // |
| 39 | // ON_CALL(mock_object, Method(argument-matchers)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 40 | // .With(multi-argument-matcher) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 41 | // .WillByDefault(action); |
| 42 | // |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 43 | // where the .With() clause is optional. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 44 | // |
| 45 | // A user can use the EXPECT_CALL() macro to specify an expectation on |
| 46 | // a mock method. The syntax is: |
| 47 | // |
| 48 | // EXPECT_CALL(mock_object, Method(argument-matchers)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 49 | // .With(multi-argument-matchers) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 50 | // .Times(cardinality) |
| 51 | // .InSequence(sequences) |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 52 | // .After(expectations) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 53 | // .WillOnce(action) |
| 54 | // .WillRepeatedly(action) |
| 55 | // .RetiresOnSaturation(); |
| 56 | // |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 57 | // where all clauses are optional, and .InSequence()/.After()/ |
| 58 | // .WillOnce() can appear any number of times. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 59 | |
| 60 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ |
| 61 | #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ |
| 62 | |
| 63 | #include <map> |
| 64 | #include <set> |
| 65 | #include <sstream> |
| 66 | #include <string> |
| 67 | #include <vector> |
| 68 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 69 | #include "gmock/gmock-actions.h" |
| 70 | #include "gmock/gmock-cardinalities.h" |
| 71 | #include "gmock/gmock-matchers.h" |
| 72 | #include "gmock/internal/gmock-internal-utils.h" |
| 73 | #include "gmock/internal/gmock-port.h" |
| 74 | #include "gtest/gtest.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 75 | |
| 76 | namespace testing { |
| 77 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 78 | // An abstract handle of an expectation. |
| 79 | class Expectation; |
| 80 | |
| 81 | // A set of expectation handles. |
| 82 | class ExpectationSet; |
| 83 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 84 | // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION |
| 85 | // and MUST NOT BE USED IN USER CODE!!! |
| 86 | namespace internal { |
| 87 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 88 | // Implements a mock function. |
| 89 | template <typename F> class FunctionMocker; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 90 | |
| 91 | // Base class for expectations. |
| 92 | class ExpectationBase; |
| 93 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 94 | // Implements an expectation. |
| 95 | template <typename F> class TypedExpectation; |
| 96 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 97 | // Helper class for testing the Expectation class template. |
| 98 | class ExpectationTester; |
| 99 | |
| 100 | // Base class for function mockers. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 101 | template <typename F> class FunctionMockerBase; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 102 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 103 | // Protects the mock object registry (in class Mock), all function |
| 104 | // mockers, and all expectations. |
| 105 | // |
| 106 | // The reason we don't use more fine-grained protection is: when a |
| 107 | // mock function Foo() is called, it needs to consult its expectations |
| 108 | // to see which one should be picked. If another thread is allowed to |
| 109 | // call a mock function (either Foo() or a different one) at the same |
| 110 | // time, it could affect the "retired" attributes of Foo()'s |
| 111 | // expectations when InSequence() is used, and thus affect which |
| 112 | // expectation gets picked. Therefore, we sequence all mock function |
| 113 | // calls to ensure the integrity of the mock objects' states. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 114 | GTEST_API_ GTEST_DECLARE_STATIC_MUTEX_(g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 115 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 116 | // Untyped base class for ActionResultHolder<R>. |
| 117 | class UntypedActionResultHolderBase; |
| 118 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 119 | // Abstract base class of FunctionMockerBase. This is the |
| 120 | // type-agnostic part of the function mocker interface. Its pure |
| 121 | // virtual methods are implemented by FunctionMockerBase. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 122 | class GTEST_API_ UntypedFunctionMockerBase { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 123 | public: |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 124 | UntypedFunctionMockerBase(); |
| 125 | virtual ~UntypedFunctionMockerBase(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 126 | |
| 127 | // Verifies that all expectations on this mock function have been |
| 128 | // satisfied. Reports one or more Google Test non-fatal failures |
| 129 | // and returns false if not. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 130 | bool VerifyAndClearExpectationsLocked() |
| 131 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 132 | |
| 133 | // Clears the ON_CALL()s set on this mock function. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 134 | virtual void ClearDefaultActionsLocked() |
| 135 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) = 0; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 136 | |
| 137 | // In all of the following Untyped* functions, it's the caller's |
| 138 | // responsibility to guarantee the correctness of the arguments' |
| 139 | // types. |
| 140 | |
| 141 | // Performs the default action with the given arguments and returns |
| 142 | // the action's result. The call description string will be used in |
| 143 | // the error message to describe the call in the case the default |
| 144 | // action fails. |
| 145 | // L = * |
| 146 | virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction( |
| 147 | const void* untyped_args, |
| 148 | const string& call_description) const = 0; |
| 149 | |
| 150 | // Performs the given action with the given arguments and returns |
| 151 | // the action's result. |
| 152 | // L = * |
| 153 | virtual UntypedActionResultHolderBase* UntypedPerformAction( |
| 154 | const void* untyped_action, |
| 155 | const void* untyped_args) const = 0; |
| 156 | |
| 157 | // Writes a message that the call is uninteresting (i.e. neither |
| 158 | // explicitly expected nor explicitly unexpected) to the given |
| 159 | // ostream. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 160 | virtual void UntypedDescribeUninterestingCall( |
| 161 | const void* untyped_args, |
| 162 | ::std::ostream* os) const |
| 163 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 164 | |
| 165 | // Returns the expectation that matches the given function arguments |
| 166 | // (or NULL is there's no match); when a match is found, |
| 167 | // untyped_action is set to point to the action that should be |
| 168 | // performed (or NULL if the action is "do default"), and |
| 169 | // is_excessive is modified to indicate whether the call exceeds the |
| 170 | // expected number. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 171 | virtual const ExpectationBase* UntypedFindMatchingExpectation( |
| 172 | const void* untyped_args, |
| 173 | const void** untyped_action, bool* is_excessive, |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 174 | ::std::ostream* what, ::std::ostream* why) |
| 175 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) = 0; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 176 | |
| 177 | // Prints the given function arguments to the ostream. |
| 178 | virtual void UntypedPrintArgs(const void* untyped_args, |
| 179 | ::std::ostream* os) const = 0; |
| 180 | |
| 181 | // Sets the mock object this mock method belongs to, and registers |
| 182 | // this information in the global mock registry. Will be called |
| 183 | // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock |
| 184 | // method. |
| 185 | // TODO(wan@google.com): rename to SetAndRegisterOwner(). |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 186 | void RegisterOwner(const void* mock_obj) |
| 187 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 188 | |
| 189 | // Sets the mock object this mock method belongs to, and sets the |
| 190 | // name of the mock function. Will be called upon each invocation |
| 191 | // of this mock function. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 192 | void SetOwnerAndName(const void* mock_obj, const char* name) |
| 193 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 194 | |
| 195 | // Returns the mock object this mock method belongs to. Must be |
| 196 | // called after RegisterOwner() or SetOwnerAndName() has been |
| 197 | // called. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 198 | const void* MockObject() const |
| 199 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 200 | |
| 201 | // Returns the name of this mock method. Must be called after |
| 202 | // SetOwnerAndName() has been called. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 203 | const char* Name() const |
| 204 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 205 | |
| 206 | // Returns the result of invoking this mock function with the given |
| 207 | // arguments. This function can be safely called from multiple |
| 208 | // threads concurrently. The caller is responsible for deleting the |
| 209 | // result. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 210 | const UntypedActionResultHolderBase* UntypedInvokeWith( |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 211 | const void* untyped_args) |
| 212 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 213 | |
| 214 | protected: |
| 215 | typedef std::vector<const void*> UntypedOnCallSpecs; |
| 216 | |
| 217 | typedef std::vector<internal::linked_ptr<ExpectationBase> > |
| 218 | UntypedExpectations; |
| 219 | |
| 220 | // Returns an Expectation object that references and co-owns exp, |
| 221 | // which must be an expectation on this mock function. |
| 222 | Expectation GetHandleOf(ExpectationBase* exp); |
| 223 | |
| 224 | // Address of the mock object this mock method belongs to. Only |
| 225 | // valid after this mock method has been called or |
| 226 | // ON_CALL/EXPECT_CALL has been invoked on it. |
| 227 | const void* mock_obj_; // Protected by g_gmock_mutex. |
| 228 | |
| 229 | // Name of the function being mocked. Only valid after this mock |
| 230 | // method has been called. |
| 231 | const char* name_; // Protected by g_gmock_mutex. |
| 232 | |
| 233 | // All default action specs for this function mocker. |
| 234 | UntypedOnCallSpecs untyped_on_call_specs_; |
| 235 | |
| 236 | // All expectations for this function mocker. |
| 237 | UntypedExpectations untyped_expectations_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 238 | }; // class UntypedFunctionMockerBase |
| 239 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 240 | // Untyped base class for OnCallSpec<F>. |
| 241 | class UntypedOnCallSpecBase { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 242 | public: |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 243 | // The arguments are the location of the ON_CALL() statement. |
| 244 | UntypedOnCallSpecBase(const char* a_file, int a_line) |
| 245 | : file_(a_file), line_(a_line), last_clause_(kNone) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 246 | |
| 247 | // Where in the source file was the default action spec defined? |
| 248 | const char* file() const { return file_; } |
| 249 | int line() const { return line_; } |
| 250 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 251 | protected: |
| 252 | // Gives each clause in the ON_CALL() statement a name. |
| 253 | enum Clause { |
| 254 | // Do not change the order of the enum members! The run-time |
| 255 | // syntax checking relies on it. |
| 256 | kNone, |
| 257 | kWith, |
vladlosev | ab29bb6 | 2011-04-08 01:32:32 +0000 | [diff] [blame] | 258 | kWillByDefault |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | // Asserts that the ON_CALL() statement has a certain property. |
| 262 | void AssertSpecProperty(bool property, const string& failure_message) const { |
| 263 | Assert(property, file_, line_, failure_message); |
| 264 | } |
| 265 | |
| 266 | // Expects that the ON_CALL() statement has a certain property. |
| 267 | void ExpectSpecProperty(bool property, const string& failure_message) const { |
| 268 | Expect(property, file_, line_, failure_message); |
| 269 | } |
| 270 | |
| 271 | const char* file_; |
| 272 | int line_; |
| 273 | |
| 274 | // The last clause in the ON_CALL() statement as seen so far. |
| 275 | // Initially kNone and changes as the statement is parsed. |
| 276 | Clause last_clause_; |
| 277 | }; // class UntypedOnCallSpecBase |
| 278 | |
| 279 | // This template class implements an ON_CALL spec. |
| 280 | template <typename F> |
| 281 | class OnCallSpec : public UntypedOnCallSpecBase { |
| 282 | public: |
| 283 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 284 | typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; |
| 285 | |
| 286 | // Constructs an OnCallSpec object from the information inside |
| 287 | // the parenthesis of an ON_CALL() statement. |
| 288 | OnCallSpec(const char* a_file, int a_line, |
| 289 | const ArgumentMatcherTuple& matchers) |
| 290 | : UntypedOnCallSpecBase(a_file, a_line), |
| 291 | matchers_(matchers), |
| 292 | // By default, extra_matcher_ should match anything. However, |
| 293 | // we cannot initialize it with _ as that triggers a compiler |
| 294 | // bug in Symbian's C++ compiler (cannot decide between two |
| 295 | // overloaded constructors of Matcher<const ArgumentTuple&>). |
| 296 | extra_matcher_(A<const ArgumentTuple&>()) { |
| 297 | } |
| 298 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 299 | // Implements the .With() clause. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 300 | OnCallSpec& With(const Matcher<const ArgumentTuple&>& m) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 301 | // Makes sure this is called at most once. |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 302 | ExpectSpecProperty(last_clause_ < kWith, |
| 303 | ".With() cannot appear " |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 304 | "more than once in an ON_CALL()."); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 305 | last_clause_ = kWith; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 306 | |
| 307 | extra_matcher_ = m; |
| 308 | return *this; |
| 309 | } |
| 310 | |
| 311 | // Implements the .WillByDefault() clause. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 312 | OnCallSpec& WillByDefault(const Action<F>& action) { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 313 | ExpectSpecProperty(last_clause_ < kWillByDefault, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 314 | ".WillByDefault() must appear " |
| 315 | "exactly once in an ON_CALL()."); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 316 | last_clause_ = kWillByDefault; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 317 | |
| 318 | ExpectSpecProperty(!action.IsDoDefault(), |
| 319 | "DoDefault() cannot be used in ON_CALL()."); |
| 320 | action_ = action; |
| 321 | return *this; |
| 322 | } |
| 323 | |
| 324 | // Returns true iff the given arguments match the matchers. |
| 325 | bool Matches(const ArgumentTuple& args) const { |
| 326 | return TupleMatches(matchers_, args) && extra_matcher_.Matches(args); |
| 327 | } |
| 328 | |
| 329 | // Returns the action specified by the user. |
| 330 | const Action<F>& GetAction() const { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 331 | AssertSpecProperty(last_clause_ == kWillByDefault, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 332 | ".WillByDefault() must appear exactly " |
| 333 | "once in an ON_CALL()."); |
| 334 | return action_; |
| 335 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 336 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 337 | private: |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 338 | // The information in statement |
| 339 | // |
| 340 | // ON_CALL(mock_object, Method(matchers)) |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 341 | // .With(multi-argument-matcher) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 342 | // .WillByDefault(action); |
| 343 | // |
| 344 | // is recorded in the data members like this: |
| 345 | // |
| 346 | // source file that contains the statement => file_ |
| 347 | // line number of the statement => line_ |
| 348 | // matchers => matchers_ |
| 349 | // multi-argument-matcher => extra_matcher_ |
| 350 | // action => action_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 351 | ArgumentMatcherTuple matchers_; |
| 352 | Matcher<const ArgumentTuple&> extra_matcher_; |
| 353 | Action<F> action_; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 354 | }; // class OnCallSpec |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 355 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 356 | // Possible reactions on uninteresting calls. TODO(wan@google.com): |
| 357 | // rename the enum values to the kFoo style. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 358 | enum CallReaction { |
| 359 | ALLOW, |
| 360 | WARN, |
vladlosev | ab29bb6 | 2011-04-08 01:32:32 +0000 | [diff] [blame] | 361 | FAIL |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | } // namespace internal |
| 365 | |
| 366 | // Utilities for manipulating mock objects. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 367 | class GTEST_API_ Mock { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 368 | public: |
| 369 | // The following public methods can be called concurrently. |
| 370 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 371 | // Tells Google Mock to ignore mock_obj when checking for leaked |
| 372 | // mock objects. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 373 | static void AllowLeak(const void* mock_obj) |
| 374 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 375 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 376 | // Verifies and clears all expectations on the given mock object. |
| 377 | // If the expectations aren't satisfied, generates one or more |
| 378 | // Google Test non-fatal failures and returns false. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 379 | static bool VerifyAndClearExpectations(void* mock_obj) |
| 380 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 381 | |
| 382 | // Verifies all expectations on the given mock object and clears its |
| 383 | // default actions and expectations. Returns true iff the |
| 384 | // verification was successful. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 385 | static bool VerifyAndClear(void* mock_obj) |
| 386 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 387 | private: |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 388 | friend class internal::UntypedFunctionMockerBase; |
| 389 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 390 | // Needed for a function mocker to register itself (so that we know |
| 391 | // how to clear a mock object). |
| 392 | template <typename F> |
| 393 | friend class internal::FunctionMockerBase; |
| 394 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 395 | template <typename M> |
| 396 | friend class NiceMock; |
| 397 | |
| 398 | template <typename M> |
| 399 | friend class StrictMock; |
| 400 | |
| 401 | // Tells Google Mock to allow uninteresting calls on the given mock |
| 402 | // object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 403 | static void AllowUninterestingCalls(const void* mock_obj) |
| 404 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 405 | |
| 406 | // Tells Google Mock to warn the user about uninteresting calls on |
| 407 | // the given mock object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 408 | static void WarnUninterestingCalls(const void* mock_obj) |
| 409 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 410 | |
| 411 | // Tells Google Mock to fail uninteresting calls on the given mock |
| 412 | // object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 413 | static void FailUninterestingCalls(const void* mock_obj) |
| 414 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 415 | |
| 416 | // Tells Google Mock the given mock object is being destroyed and |
| 417 | // its entry in the call-reaction table should be removed. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 418 | static void UnregisterCallReaction(const void* mock_obj) |
| 419 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 420 | |
| 421 | // Returns the reaction Google Mock will have on uninteresting calls |
| 422 | // made on the given mock object. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 423 | static internal::CallReaction GetReactionOnUninterestingCalls( |
| 424 | const void* mock_obj); |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 425 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 426 | |
| 427 | // Verifies that all expectations on the given mock object have been |
| 428 | // satisfied. Reports one or more Google Test non-fatal failures |
| 429 | // and returns false if not. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 430 | static bool VerifyAndClearExpectationsLocked(void* mock_obj) |
| 431 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 432 | |
| 433 | // Clears all ON_CALL()s set on the given mock object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 434 | static void ClearDefaultActionsLocked(void* mock_obj) |
| 435 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 436 | |
| 437 | // Registers a mock object and a mock method it owns. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 438 | static void Register( |
| 439 | const void* mock_obj, |
| 440 | internal::UntypedFunctionMockerBase* mocker) |
| 441 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 442 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 443 | // Tells Google Mock where in the source code mock_obj is used in an |
| 444 | // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this |
| 445 | // information helps the user identify which object it is. |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 446 | static void RegisterUseByOnCallOrExpectCall( |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 447 | const void* mock_obj, const char* file, int line) |
| 448 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex); |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 449 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 450 | // Unregisters a mock method; removes the owning mock object from |
| 451 | // the registry when the last mock method associated with it has |
| 452 | // been unregistered. This is called only in the destructor of |
| 453 | // FunctionMockerBase. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 454 | static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker) |
| 455 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 456 | }; // class Mock |
| 457 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 458 | // An abstract handle of an expectation. Useful in the .After() |
| 459 | // clause of EXPECT_CALL() for setting the (partial) order of |
| 460 | // expectations. The syntax: |
| 461 | // |
| 462 | // Expectation e1 = EXPECT_CALL(...)...; |
| 463 | // EXPECT_CALL(...).After(e1)...; |
| 464 | // |
| 465 | // sets two expectations where the latter can only be matched after |
| 466 | // the former has been satisfied. |
| 467 | // |
| 468 | // Notes: |
| 469 | // - This class is copyable and has value semantics. |
| 470 | // - Constness is shallow: a const Expectation object itself cannot |
| 471 | // be modified, but the mutable methods of the ExpectationBase |
| 472 | // object it references can be called via expectation_base(). |
zhanyong.wan | 7c95d83 | 2009-10-01 21:56:16 +0000 | [diff] [blame] | 473 | // - The constructors and destructor are defined out-of-line because |
| 474 | // the Symbian WINSCW compiler wants to otherwise instantiate them |
| 475 | // when it sees this class definition, at which point it doesn't have |
| 476 | // ExpectationBase available yet, leading to incorrect destruction |
| 477 | // in the linked_ptr (or compilation errors if using a checking |
| 478 | // linked_ptr). |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 479 | class GTEST_API_ Expectation { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 480 | public: |
| 481 | // Constructs a null object that doesn't reference any expectation. |
zhanyong.wan | 7c95d83 | 2009-10-01 21:56:16 +0000 | [diff] [blame] | 482 | Expectation(); |
| 483 | |
| 484 | ~Expectation(); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 485 | |
| 486 | // This single-argument ctor must not be explicit, in order to support the |
| 487 | // Expectation e = EXPECT_CALL(...); |
| 488 | // syntax. |
| 489 | // |
| 490 | // A TypedExpectation object stores its pre-requisites as |
| 491 | // Expectation objects, and needs to call the non-const Retire() |
| 492 | // method on the ExpectationBase objects they reference. Therefore |
| 493 | // Expectation must receive a *non-const* reference to the |
| 494 | // ExpectationBase object. |
| 495 | Expectation(internal::ExpectationBase& exp); // NOLINT |
| 496 | |
| 497 | // The compiler-generated copy ctor and operator= work exactly as |
| 498 | // intended, so we don't need to define our own. |
| 499 | |
| 500 | // Returns true iff rhs references the same expectation as this object does. |
| 501 | bool operator==(const Expectation& rhs) const { |
| 502 | return expectation_base_ == rhs.expectation_base_; |
| 503 | } |
| 504 | |
| 505 | bool operator!=(const Expectation& rhs) const { return !(*this == rhs); } |
| 506 | |
| 507 | private: |
| 508 | friend class ExpectationSet; |
| 509 | friend class Sequence; |
| 510 | friend class ::testing::internal::ExpectationBase; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 511 | friend class ::testing::internal::UntypedFunctionMockerBase; |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 512 | |
| 513 | template <typename F> |
| 514 | friend class ::testing::internal::FunctionMockerBase; |
| 515 | |
| 516 | template <typename F> |
| 517 | friend class ::testing::internal::TypedExpectation; |
| 518 | |
| 519 | // This comparator is needed for putting Expectation objects into a set. |
| 520 | class Less { |
| 521 | public: |
| 522 | bool operator()(const Expectation& lhs, const Expectation& rhs) const { |
| 523 | return lhs.expectation_base_.get() < rhs.expectation_base_.get(); |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | typedef ::std::set<Expectation, Less> Set; |
| 528 | |
| 529 | Expectation( |
zhanyong.wan | 7c95d83 | 2009-10-01 21:56:16 +0000 | [diff] [blame] | 530 | const internal::linked_ptr<internal::ExpectationBase>& expectation_base); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 531 | |
| 532 | // Returns the expectation this object references. |
| 533 | const internal::linked_ptr<internal::ExpectationBase>& |
| 534 | expectation_base() const { |
| 535 | return expectation_base_; |
| 536 | } |
| 537 | |
| 538 | // A linked_ptr that co-owns the expectation this handle references. |
| 539 | internal::linked_ptr<internal::ExpectationBase> expectation_base_; |
| 540 | }; |
| 541 | |
| 542 | // A set of expectation handles. Useful in the .After() clause of |
| 543 | // EXPECT_CALL() for setting the (partial) order of expectations. The |
| 544 | // syntax: |
| 545 | // |
| 546 | // ExpectationSet es; |
| 547 | // es += EXPECT_CALL(...)...; |
| 548 | // es += EXPECT_CALL(...)...; |
| 549 | // EXPECT_CALL(...).After(es)...; |
| 550 | // |
| 551 | // sets three expectations where the last one can only be matched |
| 552 | // after the first two have both been satisfied. |
| 553 | // |
| 554 | // This class is copyable and has value semantics. |
| 555 | class ExpectationSet { |
| 556 | public: |
| 557 | // A bidirectional iterator that can read a const element in the set. |
| 558 | typedef Expectation::Set::const_iterator const_iterator; |
| 559 | |
| 560 | // An object stored in the set. This is an alias of Expectation. |
| 561 | typedef Expectation::Set::value_type value_type; |
| 562 | |
| 563 | // Constructs an empty set. |
| 564 | ExpectationSet() {} |
| 565 | |
| 566 | // This single-argument ctor must not be explicit, in order to support the |
| 567 | // ExpectationSet es = EXPECT_CALL(...); |
| 568 | // syntax. |
| 569 | ExpectationSet(internal::ExpectationBase& exp) { // NOLINT |
| 570 | *this += Expectation(exp); |
| 571 | } |
| 572 | |
| 573 | // This single-argument ctor implements implicit conversion from |
| 574 | // Expectation and thus must not be explicit. This allows either an |
| 575 | // Expectation or an ExpectationSet to be used in .After(). |
| 576 | ExpectationSet(const Expectation& e) { // NOLINT |
| 577 | *this += e; |
| 578 | } |
| 579 | |
| 580 | // The compiler-generator ctor and operator= works exactly as |
| 581 | // intended, so we don't need to define our own. |
| 582 | |
| 583 | // Returns true iff rhs contains the same set of Expectation objects |
| 584 | // as this does. |
| 585 | bool operator==(const ExpectationSet& rhs) const { |
| 586 | return expectations_ == rhs.expectations_; |
| 587 | } |
| 588 | |
| 589 | bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); } |
| 590 | |
| 591 | // Implements the syntax |
| 592 | // expectation_set += EXPECT_CALL(...); |
| 593 | ExpectationSet& operator+=(const Expectation& e) { |
| 594 | expectations_.insert(e); |
| 595 | return *this; |
| 596 | } |
| 597 | |
| 598 | int size() const { return static_cast<int>(expectations_.size()); } |
| 599 | |
| 600 | const_iterator begin() const { return expectations_.begin(); } |
| 601 | const_iterator end() const { return expectations_.end(); } |
| 602 | |
| 603 | private: |
| 604 | Expectation::Set expectations_; |
| 605 | }; |
| 606 | |
| 607 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 608 | // Sequence objects are used by a user to specify the relative order |
| 609 | // in which the expectations should match. They are copyable (we rely |
| 610 | // on the compiler-defined copy constructor and assignment operator). |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 611 | class GTEST_API_ Sequence { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 612 | public: |
| 613 | // Constructs an empty sequence. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 614 | Sequence() : last_expectation_(new Expectation) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 615 | |
| 616 | // Adds an expectation to this sequence. The caller must ensure |
| 617 | // that no other thread is accessing this Sequence object. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 618 | void AddExpectation(const Expectation& expectation) const; |
| 619 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 620 | private: |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 621 | // The last expectation in this sequence. We use a linked_ptr here |
| 622 | // because Sequence objects are copyable and we want the copies to |
| 623 | // be aliases. The linked_ptr allows the copies to co-own and share |
| 624 | // the same Expectation object. |
| 625 | internal::linked_ptr<Expectation> last_expectation_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 626 | }; // class Sequence |
| 627 | |
| 628 | // An object of this type causes all EXPECT_CALL() statements |
| 629 | // encountered in its scope to be put in an anonymous sequence. The |
| 630 | // work is done in the constructor and destructor. You should only |
| 631 | // create an InSequence object on the stack. |
| 632 | // |
| 633 | // The sole purpose for this class is to support easy definition of |
| 634 | // sequential expectations, e.g. |
| 635 | // |
| 636 | // { |
| 637 | // InSequence dummy; // The name of the object doesn't matter. |
| 638 | // |
| 639 | // // The following expectations must match in the order they appear. |
| 640 | // EXPECT_CALL(a, Bar())...; |
| 641 | // EXPECT_CALL(a, Baz())...; |
| 642 | // ... |
| 643 | // EXPECT_CALL(b, Xyz())...; |
| 644 | // } |
| 645 | // |
| 646 | // You can create InSequence objects in multiple threads, as long as |
| 647 | // they are used to affect different mock objects. The idea is that |
| 648 | // each thread can create and set up its own mocks as if it's the only |
| 649 | // thread. However, for clarity of your tests we recommend you to set |
| 650 | // up mocks in the main thread unless you have a good reason not to do |
| 651 | // so. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 652 | class GTEST_API_ InSequence { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 653 | public: |
| 654 | InSequence(); |
| 655 | ~InSequence(); |
| 656 | private: |
| 657 | bool sequence_created_; |
| 658 | |
| 659 | GTEST_DISALLOW_COPY_AND_ASSIGN_(InSequence); // NOLINT |
zhanyong.wan | ccedc1c | 2010-08-09 22:46:12 +0000 | [diff] [blame] | 660 | } GTEST_ATTRIBUTE_UNUSED_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 661 | |
| 662 | namespace internal { |
| 663 | |
| 664 | // Points to the implicit sequence introduced by a living InSequence |
| 665 | // object (if any) in the current thread or NULL. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 666 | GTEST_API_ extern ThreadLocal<Sequence*> g_gmock_implicit_sequence; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 667 | |
| 668 | // Base class for implementing expectations. |
| 669 | // |
| 670 | // There are two reasons for having a type-agnostic base class for |
| 671 | // Expectation: |
| 672 | // |
| 673 | // 1. We need to store collections of expectations of different |
| 674 | // types (e.g. all pre-requisites of a particular expectation, all |
| 675 | // expectations in a sequence). Therefore these expectation objects |
| 676 | // must share a common base class. |
| 677 | // |
| 678 | // 2. We can avoid binary code bloat by moving methods not depending |
| 679 | // on the template argument of Expectation to the base class. |
| 680 | // |
| 681 | // This class is internal and mustn't be used by user code directly. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 682 | class GTEST_API_ ExpectationBase { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 683 | public: |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 684 | // source_text is the EXPECT_CALL(...) source that created this Expectation. |
| 685 | ExpectationBase(const char* file, int line, const string& source_text); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 686 | |
| 687 | virtual ~ExpectationBase(); |
| 688 | |
| 689 | // Where in the source file was the expectation spec defined? |
| 690 | const char* file() const { return file_; } |
| 691 | int line() const { return line_; } |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 692 | const char* source_text() const { return source_text_.c_str(); } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 693 | // Returns the cardinality specified in the expectation spec. |
| 694 | const Cardinality& cardinality() const { return cardinality_; } |
| 695 | |
| 696 | // Describes the source file location of this expectation. |
| 697 | void DescribeLocationTo(::std::ostream* os) const { |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 698 | *os << FormatFileLocation(file(), line()) << " "; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | // Describes how many times a function call matching this |
| 702 | // expectation has occurred. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 703 | void DescribeCallCountTo(::std::ostream* os) const |
| 704 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 705 | |
| 706 | // If this mock method has an extra matcher (i.e. .With(matcher)), |
| 707 | // describes it to the ostream. |
| 708 | virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) = 0; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 709 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 710 | protected: |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 711 | friend class ::testing::Expectation; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 712 | friend class UntypedFunctionMockerBase; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 713 | |
| 714 | enum Clause { |
| 715 | // Don't change the order of the enum members! |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 716 | kNone, |
| 717 | kWith, |
| 718 | kTimes, |
| 719 | kInSequence, |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 720 | kAfter, |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 721 | kWillOnce, |
| 722 | kWillRepeatedly, |
vladlosev | ab29bb6 | 2011-04-08 01:32:32 +0000 | [diff] [blame] | 723 | kRetiresOnSaturation |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 724 | }; |
| 725 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 726 | typedef std::vector<const void*> UntypedActions; |
| 727 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 728 | // Returns an Expectation object that references and co-owns this |
| 729 | // expectation. |
| 730 | virtual Expectation GetHandle() = 0; |
| 731 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 732 | // Asserts that the EXPECT_CALL() statement has the given property. |
| 733 | void AssertSpecProperty(bool property, const string& failure_message) const { |
| 734 | Assert(property, file_, line_, failure_message); |
| 735 | } |
| 736 | |
| 737 | // Expects that the EXPECT_CALL() statement has the given property. |
| 738 | void ExpectSpecProperty(bool property, const string& failure_message) const { |
| 739 | Expect(property, file_, line_, failure_message); |
| 740 | } |
| 741 | |
| 742 | // Explicitly specifies the cardinality of this expectation. Used |
| 743 | // by the subclasses to implement the .Times() clause. |
| 744 | void SpecifyCardinality(const Cardinality& cardinality); |
| 745 | |
| 746 | // Returns true iff the user specified the cardinality explicitly |
| 747 | // using a .Times(). |
| 748 | bool cardinality_specified() const { return cardinality_specified_; } |
| 749 | |
| 750 | // Sets the cardinality of this expectation spec. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 751 | void set_cardinality(const Cardinality& a_cardinality) { |
| 752 | cardinality_ = a_cardinality; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // The following group of methods should only be called after the |
| 756 | // EXPECT_CALL() statement, and only when g_gmock_mutex is held by |
| 757 | // the current thread. |
| 758 | |
| 759 | // Retires all pre-requisites of this expectation. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 760 | void RetireAllPreRequisites() |
| 761 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 762 | |
| 763 | // Returns true iff this expectation is retired. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 764 | bool is_retired() const |
| 765 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 766 | g_gmock_mutex.AssertHeld(); |
| 767 | return retired_; |
| 768 | } |
| 769 | |
| 770 | // Retires this expectation. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 771 | void Retire() |
| 772 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 773 | g_gmock_mutex.AssertHeld(); |
| 774 | retired_ = true; |
| 775 | } |
| 776 | |
| 777 | // Returns true iff this expectation is satisfied. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 778 | bool IsSatisfied() const |
| 779 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 780 | g_gmock_mutex.AssertHeld(); |
| 781 | return cardinality().IsSatisfiedByCallCount(call_count_); |
| 782 | } |
| 783 | |
| 784 | // Returns true iff this expectation is saturated. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 785 | bool IsSaturated() const |
| 786 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 787 | g_gmock_mutex.AssertHeld(); |
| 788 | return cardinality().IsSaturatedByCallCount(call_count_); |
| 789 | } |
| 790 | |
| 791 | // Returns true iff this expectation is over-saturated. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 792 | bool IsOverSaturated() const |
| 793 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 794 | g_gmock_mutex.AssertHeld(); |
| 795 | return cardinality().IsOverSaturatedByCallCount(call_count_); |
| 796 | } |
| 797 | |
| 798 | // Returns true iff all pre-requisites of this expectation are satisfied. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 799 | bool AllPrerequisitesAreSatisfied() const |
| 800 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 801 | |
| 802 | // Adds unsatisfied pre-requisites of this expectation to 'result'. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 803 | void FindUnsatisfiedPrerequisites(ExpectationSet* result) const |
| 804 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 805 | |
| 806 | // Returns the number this expectation has been invoked. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 807 | int call_count() const |
| 808 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 809 | g_gmock_mutex.AssertHeld(); |
| 810 | return call_count_; |
| 811 | } |
| 812 | |
| 813 | // Increments the number this expectation has been invoked. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 814 | void IncrementCallCount() |
| 815 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 816 | g_gmock_mutex.AssertHeld(); |
| 817 | call_count_++; |
| 818 | } |
| 819 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 820 | // Checks the action count (i.e. the number of WillOnce() and |
| 821 | // WillRepeatedly() clauses) against the cardinality if this hasn't |
| 822 | // been done before. Prints a warning if there are too many or too |
| 823 | // few actions. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 824 | void CheckActionCountIfNotDone() const |
| 825 | GTEST_LOCK_EXCLUDED_(mutex_); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 826 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 827 | friend class ::testing::Sequence; |
| 828 | friend class ::testing::internal::ExpectationTester; |
| 829 | |
| 830 | template <typename Function> |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 831 | friend class TypedExpectation; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 832 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 833 | // Implements the .Times() clause. |
| 834 | void UntypedTimes(const Cardinality& a_cardinality); |
| 835 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 836 | // This group of fields are part of the spec and won't change after |
| 837 | // an EXPECT_CALL() statement finishes. |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 838 | const char* file_; // The file that contains the expectation. |
| 839 | int line_; // The line number of the expectation. |
| 840 | const string source_text_; // The EXPECT_CALL(...) source text. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 841 | // True iff the cardinality is specified explicitly. |
| 842 | bool cardinality_specified_; |
| 843 | Cardinality cardinality_; // The cardinality of the expectation. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 844 | // The immediate pre-requisites (i.e. expectations that must be |
| 845 | // satisfied before this expectation can be matched) of this |
| 846 | // expectation. We use linked_ptr in the set because we want an |
| 847 | // Expectation object to be co-owned by its FunctionMocker and its |
| 848 | // successors. This allows multiple mock objects to be deleted at |
| 849 | // different times. |
| 850 | ExpectationSet immediate_prerequisites_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 851 | |
| 852 | // This group of fields are the current state of the expectation, |
| 853 | // and can change as the mock function is called. |
| 854 | int call_count_; // How many times this expectation has been invoked. |
| 855 | bool retired_; // True iff this expectation has retired. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 856 | UntypedActions untyped_actions_; |
| 857 | bool extra_matcher_specified_; |
| 858 | bool repeated_action_specified_; // True if a WillRepeatedly() was specified. |
| 859 | bool retires_on_saturation_; |
| 860 | Clause last_clause_; |
| 861 | mutable bool action_count_checked_; // Under mutex_. |
| 862 | mutable Mutex mutex_; // Protects action_count_checked_. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 863 | |
| 864 | GTEST_DISALLOW_ASSIGN_(ExpectationBase); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 865 | }; // class ExpectationBase |
| 866 | |
| 867 | // Impements an expectation for the given function type. |
| 868 | template <typename F> |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 869 | class TypedExpectation : public ExpectationBase { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 870 | public: |
| 871 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 872 | typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; |
| 873 | typedef typename Function<F>::Result Result; |
| 874 | |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 875 | TypedExpectation(FunctionMockerBase<F>* owner, |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 876 | const char* a_file, int a_line, const string& a_source_text, |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 877 | const ArgumentMatcherTuple& m) |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 878 | : ExpectationBase(a_file, a_line, a_source_text), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 879 | owner_(owner), |
| 880 | matchers_(m), |
zhanyong.wan | 1849065 | 2009-05-11 18:54:08 +0000 | [diff] [blame] | 881 | // By default, extra_matcher_ should match anything. However, |
| 882 | // we cannot initialize it with _ as that triggers a compiler |
| 883 | // bug in Symbian's C++ compiler (cannot decide between two |
| 884 | // overloaded constructors of Matcher<const ArgumentTuple&>). |
| 885 | extra_matcher_(A<const ArgumentTuple&>()), |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 886 | repeated_action_(DoDefault()) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 887 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 888 | virtual ~TypedExpectation() { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 889 | // Check the validity of the action count if it hasn't been done |
| 890 | // yet (for example, if the expectation was never used). |
| 891 | CheckActionCountIfNotDone(); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 892 | for (UntypedActions::const_iterator it = untyped_actions_.begin(); |
| 893 | it != untyped_actions_.end(); ++it) { |
| 894 | delete static_cast<const Action<F>*>(*it); |
| 895 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 896 | } |
| 897 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 898 | // Implements the .With() clause. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 899 | TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 900 | if (last_clause_ == kWith) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 901 | ExpectSpecProperty(false, |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 902 | ".With() cannot appear " |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 903 | "more than once in an EXPECT_CALL()."); |
| 904 | } else { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 905 | ExpectSpecProperty(last_clause_ < kWith, |
| 906 | ".With() must be the first " |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 907 | "clause in an EXPECT_CALL()."); |
| 908 | } |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 909 | last_clause_ = kWith; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 910 | |
| 911 | extra_matcher_ = m; |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 912 | extra_matcher_specified_ = true; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 913 | return *this; |
| 914 | } |
| 915 | |
| 916 | // Implements the .Times() clause. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 917 | TypedExpectation& Times(const Cardinality& a_cardinality) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 918 | ExpectationBase::UntypedTimes(a_cardinality); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 919 | return *this; |
| 920 | } |
| 921 | |
| 922 | // Implements the .Times() clause. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 923 | TypedExpectation& Times(int n) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 924 | return Times(Exactly(n)); |
| 925 | } |
| 926 | |
| 927 | // Implements the .InSequence() clause. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 928 | TypedExpectation& InSequence(const Sequence& s) { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 929 | ExpectSpecProperty(last_clause_ <= kInSequence, |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 930 | ".InSequence() cannot appear after .After()," |
| 931 | " .WillOnce(), .WillRepeatedly(), or " |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 932 | ".RetiresOnSaturation()."); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 933 | last_clause_ = kInSequence; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 934 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 935 | s.AddExpectation(GetHandle()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 936 | return *this; |
| 937 | } |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 938 | TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 939 | return InSequence(s1).InSequence(s2); |
| 940 | } |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 941 | TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, |
| 942 | const Sequence& s3) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 943 | return InSequence(s1, s2).InSequence(s3); |
| 944 | } |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 945 | TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, |
| 946 | const Sequence& s3, const Sequence& s4) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 947 | return InSequence(s1, s2, s3).InSequence(s4); |
| 948 | } |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 949 | TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, |
| 950 | const Sequence& s3, const Sequence& s4, |
| 951 | const Sequence& s5) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 952 | return InSequence(s1, s2, s3, s4).InSequence(s5); |
| 953 | } |
| 954 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 955 | // Implements that .After() clause. |
| 956 | TypedExpectation& After(const ExpectationSet& s) { |
| 957 | ExpectSpecProperty(last_clause_ <= kAfter, |
| 958 | ".After() cannot appear after .WillOnce()," |
| 959 | " .WillRepeatedly(), or " |
| 960 | ".RetiresOnSaturation()."); |
| 961 | last_clause_ = kAfter; |
| 962 | |
| 963 | for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) { |
| 964 | immediate_prerequisites_ += *it; |
| 965 | } |
| 966 | return *this; |
| 967 | } |
| 968 | TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) { |
| 969 | return After(s1).After(s2); |
| 970 | } |
| 971 | TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, |
| 972 | const ExpectationSet& s3) { |
| 973 | return After(s1, s2).After(s3); |
| 974 | } |
| 975 | TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, |
| 976 | const ExpectationSet& s3, const ExpectationSet& s4) { |
| 977 | return After(s1, s2, s3).After(s4); |
| 978 | } |
| 979 | TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, |
| 980 | const ExpectationSet& s3, const ExpectationSet& s4, |
| 981 | const ExpectationSet& s5) { |
| 982 | return After(s1, s2, s3, s4).After(s5); |
| 983 | } |
| 984 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 985 | // Implements the .WillOnce() clause. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 986 | TypedExpectation& WillOnce(const Action<F>& action) { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 987 | ExpectSpecProperty(last_clause_ <= kWillOnce, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 988 | ".WillOnce() cannot appear after " |
| 989 | ".WillRepeatedly() or .RetiresOnSaturation()."); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 990 | last_clause_ = kWillOnce; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 991 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 992 | untyped_actions_.push_back(new Action<F>(action)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 993 | if (!cardinality_specified()) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 994 | set_cardinality(Exactly(static_cast<int>(untyped_actions_.size()))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 995 | } |
| 996 | return *this; |
| 997 | } |
| 998 | |
| 999 | // Implements the .WillRepeatedly() clause. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1000 | TypedExpectation& WillRepeatedly(const Action<F>& action) { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 1001 | if (last_clause_ == kWillRepeatedly) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1002 | ExpectSpecProperty(false, |
| 1003 | ".WillRepeatedly() cannot appear " |
| 1004 | "more than once in an EXPECT_CALL()."); |
| 1005 | } else { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 1006 | ExpectSpecProperty(last_clause_ < kWillRepeatedly, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1007 | ".WillRepeatedly() cannot appear " |
| 1008 | "after .RetiresOnSaturation()."); |
| 1009 | } |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 1010 | last_clause_ = kWillRepeatedly; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1011 | repeated_action_specified_ = true; |
| 1012 | |
| 1013 | repeated_action_ = action; |
| 1014 | if (!cardinality_specified()) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1015 | set_cardinality(AtLeast(static_cast<int>(untyped_actions_.size()))); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Now that no more action clauses can be specified, we check |
| 1019 | // whether their count makes sense. |
| 1020 | CheckActionCountIfNotDone(); |
| 1021 | return *this; |
| 1022 | } |
| 1023 | |
| 1024 | // Implements the .RetiresOnSaturation() clause. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1025 | TypedExpectation& RetiresOnSaturation() { |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 1026 | ExpectSpecProperty(last_clause_ < kRetiresOnSaturation, |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1027 | ".RetiresOnSaturation() cannot appear " |
| 1028 | "more than once."); |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 1029 | last_clause_ = kRetiresOnSaturation; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1030 | retires_on_saturation_ = true; |
| 1031 | |
| 1032 | // Now that no more action clauses can be specified, we check |
| 1033 | // whether their count makes sense. |
| 1034 | CheckActionCountIfNotDone(); |
| 1035 | return *this; |
| 1036 | } |
| 1037 | |
| 1038 | // Returns the matchers for the arguments as specified inside the |
| 1039 | // EXPECT_CALL() macro. |
| 1040 | const ArgumentMatcherTuple& matchers() const { |
| 1041 | return matchers_; |
| 1042 | } |
| 1043 | |
zhanyong.wan | bf55085 | 2009-06-09 06:09:53 +0000 | [diff] [blame] | 1044 | // Returns the matcher specified by the .With() clause. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1045 | const Matcher<const ArgumentTuple&>& extra_matcher() const { |
| 1046 | return extra_matcher_; |
| 1047 | } |
| 1048 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1049 | // Returns the action specified by the .WillRepeatedly() clause. |
| 1050 | const Action<F>& repeated_action() const { return repeated_action_; } |
| 1051 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1052 | // If this mock method has an extra matcher (i.e. .With(matcher)), |
| 1053 | // describes it to the ostream. |
| 1054 | virtual void MaybeDescribeExtraMatcherTo(::std::ostream* os) { |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 1055 | if (extra_matcher_specified_) { |
| 1056 | *os << " Expected args: "; |
| 1057 | extra_matcher_.DescribeTo(os); |
| 1058 | *os << "\n"; |
| 1059 | } |
| 1060 | } |
| 1061 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1062 | private: |
| 1063 | template <typename Function> |
| 1064 | friend class FunctionMockerBase; |
| 1065 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1066 | // Returns an Expectation object that references and co-owns this |
| 1067 | // expectation. |
| 1068 | virtual Expectation GetHandle() { |
| 1069 | return owner_->GetHandleOf(this); |
| 1070 | } |
| 1071 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1072 | // The following methods will be called only after the EXPECT_CALL() |
| 1073 | // statement finishes and when the current thread holds |
| 1074 | // g_gmock_mutex. |
| 1075 | |
| 1076 | // Returns true iff this expectation matches the given arguments. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1077 | bool Matches(const ArgumentTuple& args) const |
| 1078 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1079 | g_gmock_mutex.AssertHeld(); |
| 1080 | return TupleMatches(matchers_, args) && extra_matcher_.Matches(args); |
| 1081 | } |
| 1082 | |
| 1083 | // Returns true iff this expectation should handle the given arguments. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1084 | bool ShouldHandleArguments(const ArgumentTuple& args) const |
| 1085 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1086 | g_gmock_mutex.AssertHeld(); |
| 1087 | |
| 1088 | // In case the action count wasn't checked when the expectation |
| 1089 | // was defined (e.g. if this expectation has no WillRepeatedly() |
| 1090 | // or RetiresOnSaturation() clause), we check it when the |
| 1091 | // expectation is used for the first time. |
| 1092 | CheckActionCountIfNotDone(); |
| 1093 | return !is_retired() && AllPrerequisitesAreSatisfied() && Matches(args); |
| 1094 | } |
| 1095 | |
| 1096 | // Describes the result of matching the arguments against this |
| 1097 | // expectation to the given ostream. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1098 | void ExplainMatchResultTo( |
| 1099 | const ArgumentTuple& args, |
| 1100 | ::std::ostream* os) const |
| 1101 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1102 | g_gmock_mutex.AssertHeld(); |
| 1103 | |
| 1104 | if (is_retired()) { |
| 1105 | *os << " Expected: the expectation is active\n" |
| 1106 | << " Actual: it is retired\n"; |
| 1107 | } else if (!Matches(args)) { |
| 1108 | if (!TupleMatches(matchers_, args)) { |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1109 | ExplainMatchFailureTupleTo(matchers_, args, os); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1110 | } |
zhanyong.wan | 8211331 | 2010-01-08 21:55:40 +0000 | [diff] [blame] | 1111 | StringMatchResultListener listener; |
| 1112 | if (!extra_matcher_.MatchAndExplain(args, &listener)) { |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 1113 | *os << " Expected args: "; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1114 | extra_matcher_.DescribeTo(os); |
zhanyong.wan | 2661c68 | 2009-06-09 05:42:12 +0000 | [diff] [blame] | 1115 | *os << "\n Actual: don't match"; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1116 | |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1117 | internal::PrintIfNotEmpty(listener.str(), os); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1118 | *os << "\n"; |
| 1119 | } |
| 1120 | } else if (!AllPrerequisitesAreSatisfied()) { |
| 1121 | *os << " Expected: all pre-requisites are satisfied\n" |
| 1122 | << " Actual: the following immediate pre-requisites " |
| 1123 | << "are not satisfied:\n"; |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1124 | ExpectationSet unsatisfied_prereqs; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1125 | FindUnsatisfiedPrerequisites(&unsatisfied_prereqs); |
| 1126 | int i = 0; |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1127 | for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1128 | it != unsatisfied_prereqs.end(); ++it) { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1129 | it->expectation_base()->DescribeLocationTo(os); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1130 | *os << "pre-requisite #" << i++ << "\n"; |
| 1131 | } |
| 1132 | *os << " (end of pre-requisites)\n"; |
| 1133 | } else { |
| 1134 | // This line is here just for completeness' sake. It will never |
zhanyong.wan | b1c7f93 | 2010-03-24 17:35:11 +0000 | [diff] [blame] | 1135 | // be executed as currently the ExplainMatchResultTo() function |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1136 | // is called only when the mock function call does NOT match the |
| 1137 | // expectation. |
| 1138 | *os << "The call matches the expectation.\n"; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | // Returns the action that should be taken for the current invocation. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1143 | const Action<F>& GetCurrentAction( |
| 1144 | const FunctionMockerBase<F>* mocker, |
| 1145 | const ArgumentTuple& args) const |
| 1146 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1147 | g_gmock_mutex.AssertHeld(); |
| 1148 | const int count = call_count(); |
| 1149 | Assert(count >= 1, __FILE__, __LINE__, |
| 1150 | "call_count() is <= 0 when GetCurrentAction() is " |
| 1151 | "called - this should never happen."); |
| 1152 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1153 | const int action_count = static_cast<int>(untyped_actions_.size()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1154 | if (action_count > 0 && !repeated_action_specified_ && |
| 1155 | count > action_count) { |
| 1156 | // If there is at least one WillOnce() and no WillRepeatedly(), |
| 1157 | // we warn the user when the WillOnce() clauses ran out. |
| 1158 | ::std::stringstream ss; |
| 1159 | DescribeLocationTo(&ss); |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 1160 | ss << "Actions ran out in " << source_text() << "...\n" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1161 | << "Called " << count << " times, but only " |
| 1162 | << action_count << " WillOnce()" |
| 1163 | << (action_count == 1 ? " is" : "s are") << " specified - "; |
| 1164 | mocker->DescribeDefaultActionTo(args, &ss); |
| 1165 | Log(WARNING, ss.str(), 1); |
| 1166 | } |
| 1167 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1168 | return count <= action_count ? |
| 1169 | *static_cast<const Action<F>*>(untyped_actions_[count - 1]) : |
| 1170 | repeated_action(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | // Given the arguments of a mock function call, if the call will |
| 1174 | // over-saturate this expectation, returns the default action; |
| 1175 | // otherwise, returns the next action in this expectation. Also |
| 1176 | // describes *what* happened to 'what', and explains *why* Google |
| 1177 | // Mock does it to 'why'. This method is not const as it calls |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1178 | // IncrementCallCount(). A return value of NULL means the default |
| 1179 | // action. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1180 | const Action<F>* GetActionForArguments( |
| 1181 | const FunctionMockerBase<F>* mocker, |
| 1182 | const ArgumentTuple& args, |
| 1183 | ::std::ostream* what, |
| 1184 | ::std::ostream* why) |
| 1185 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1186 | g_gmock_mutex.AssertHeld(); |
| 1187 | if (IsSaturated()) { |
| 1188 | // We have an excessive call. |
| 1189 | IncrementCallCount(); |
| 1190 | *what << "Mock function called more times than expected - "; |
| 1191 | mocker->DescribeDefaultActionTo(args, what); |
| 1192 | DescribeCallCountTo(why); |
| 1193 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1194 | // TODO(wan@google.com): allow the user to control whether |
| 1195 | // unexpected calls should fail immediately or continue using a |
| 1196 | // flag --gmock_unexpected_calls_are_fatal. |
| 1197 | return NULL; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | IncrementCallCount(); |
| 1201 | RetireAllPreRequisites(); |
| 1202 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1203 | if (retires_on_saturation_ && IsSaturated()) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1204 | Retire(); |
| 1205 | } |
| 1206 | |
| 1207 | // Must be done after IncrementCount()! |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 1208 | *what << "Mock function call matches " << source_text() <<"...\n"; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1209 | return &(GetCurrentAction(mocker, args)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | // All the fields below won't change once the EXPECT_CALL() |
| 1213 | // statement finishes. |
| 1214 | FunctionMockerBase<F>* const owner_; |
| 1215 | ArgumentMatcherTuple matchers_; |
| 1216 | Matcher<const ArgumentTuple&> extra_matcher_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1217 | Action<F> repeated_action_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1218 | |
| 1219 | GTEST_DISALLOW_COPY_AND_ASSIGN_(TypedExpectation); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1220 | }; // class TypedExpectation |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1221 | |
| 1222 | // A MockSpec object is used by ON_CALL() or EXPECT_CALL() for |
| 1223 | // specifying the default behavior of, or expectation on, a mock |
| 1224 | // function. |
| 1225 | |
| 1226 | // Note: class MockSpec really belongs to the ::testing namespace. |
| 1227 | // However if we define it in ::testing, MSVC will complain when |
| 1228 | // classes in ::testing::internal declare it as a friend class |
| 1229 | // template. To workaround this compiler bug, we define MockSpec in |
| 1230 | // ::testing::internal and import it into ::testing. |
| 1231 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1232 | // Logs a message including file and line number information. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 1233 | GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, |
| 1234 | const char* file, int line, |
| 1235 | const string& message); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1236 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1237 | template <typename F> |
| 1238 | class MockSpec { |
| 1239 | public: |
| 1240 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 1241 | typedef typename internal::Function<F>::ArgumentMatcherTuple |
| 1242 | ArgumentMatcherTuple; |
| 1243 | |
| 1244 | // Constructs a MockSpec object, given the function mocker object |
| 1245 | // that the spec is associated with. |
| 1246 | explicit MockSpec(internal::FunctionMockerBase<F>* function_mocker) |
| 1247 | : function_mocker_(function_mocker) {} |
| 1248 | |
| 1249 | // Adds a new default action spec to the function mocker and returns |
| 1250 | // the newly created spec. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1251 | internal::OnCallSpec<F>& InternalDefaultActionSetAt( |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1252 | const char* file, int line, const char* obj, const char* call) { |
| 1253 | LogWithLocation(internal::INFO, file, line, |
| 1254 | string("ON_CALL(") + obj + ", " + call + ") invoked"); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1255 | return function_mocker_->AddNewOnCallSpec(file, line, matchers_); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | // Adds a new expectation spec to the function mocker and returns |
| 1259 | // the newly created spec. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1260 | internal::TypedExpectation<F>& InternalExpectedAt( |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1261 | const char* file, int line, const char* obj, const char* call) { |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 1262 | const string source_text(string("EXPECT_CALL(") + obj + ", " + call + ")"); |
| 1263 | LogWithLocation(internal::INFO, file, line, source_text + " invoked"); |
| 1264 | return function_mocker_->AddNewExpectation( |
| 1265 | file, line, source_text, matchers_); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | private: |
| 1269 | template <typename Function> |
| 1270 | friend class internal::FunctionMocker; |
| 1271 | |
| 1272 | void SetMatchers(const ArgumentMatcherTuple& matchers) { |
| 1273 | matchers_ = matchers; |
| 1274 | } |
| 1275 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1276 | // The function mocker that owns this spec. |
| 1277 | internal::FunctionMockerBase<F>* const function_mocker_; |
| 1278 | // The argument matchers specified in the spec. |
| 1279 | ArgumentMatcherTuple matchers_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1280 | |
| 1281 | GTEST_DISALLOW_ASSIGN_(MockSpec); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1282 | }; // class MockSpec |
| 1283 | |
| 1284 | // MSVC warns about using 'this' in base member initializer list, so |
| 1285 | // we need to temporarily disable the warning. We have to do it for |
| 1286 | // the entire class to suppress the warning, even though it's about |
| 1287 | // the constructor only. |
| 1288 | |
| 1289 | #ifdef _MSC_VER |
zhanyong.wan | 658ac0b | 2011-02-24 07:29:13 +0000 | [diff] [blame] | 1290 | # pragma warning(push) // Saves the current warning state. |
| 1291 | # pragma warning(disable:4355) // Temporarily disables warning 4355. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1292 | #endif // _MSV_VER |
| 1293 | |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1294 | // C++ treats the void type specially. For example, you cannot define |
| 1295 | // a void-typed variable or pass a void value to a function. |
| 1296 | // ActionResultHolder<T> holds a value of type T, where T must be a |
| 1297 | // copyable type or void (T doesn't need to be default-constructable). |
| 1298 | // It hides the syntactic difference between void and other types, and |
| 1299 | // is used to unify the code for invoking both void-returning and |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1300 | // non-void-returning mock functions. |
| 1301 | |
| 1302 | // Untyped base class for ActionResultHolder<T>. |
| 1303 | class UntypedActionResultHolderBase { |
| 1304 | public: |
| 1305 | virtual ~UntypedActionResultHolderBase() {} |
| 1306 | |
| 1307 | // Prints the held value as an action's result to os. |
| 1308 | virtual void PrintAsActionResult(::std::ostream* os) const = 0; |
| 1309 | }; |
| 1310 | |
| 1311 | // This generic definition is used when T is not void. |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1312 | template <typename T> |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1313 | class ActionResultHolder : public UntypedActionResultHolderBase { |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1314 | public: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1315 | explicit ActionResultHolder(T a_value) : value_(a_value) {} |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1316 | |
| 1317 | // The compiler-generated copy constructor and assignment operator |
| 1318 | // are exactly what we need, so we don't need to define them. |
| 1319 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1320 | // Returns the held value and deletes this object. |
| 1321 | T GetValueAndDelete() const { |
| 1322 | T retval(value_); |
| 1323 | delete this; |
| 1324 | return retval; |
| 1325 | } |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Prints the held value as an action's result to os. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1328 | virtual void PrintAsActionResult(::std::ostream* os) const { |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1329 | *os << "\n Returns: "; |
vladlosev | e2e8ba4 | 2010-05-13 18:16:03 +0000 | [diff] [blame] | 1330 | // T may be a reference type, so we don't use UniversalPrint(). |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1331 | UniversalPrinter<T>::Print(value_, os); |
| 1332 | } |
| 1333 | |
| 1334 | // Performs the given mock function's default action and returns the |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1335 | // result in a new-ed ActionResultHolder. |
| 1336 | template <typename F> |
| 1337 | static ActionResultHolder* PerformDefaultAction( |
| 1338 | const FunctionMockerBase<F>* func_mocker, |
| 1339 | const typename Function<F>::ArgumentTuple& args, |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1340 | const string& call_description) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1341 | return new ActionResultHolder( |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1342 | func_mocker->PerformDefaultAction(args, call_description)); |
| 1343 | } |
| 1344 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1345 | // Performs the given action and returns the result in a new-ed |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1346 | // ActionResultHolder. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1347 | template <typename F> |
| 1348 | static ActionResultHolder* |
| 1349 | PerformAction(const Action<F>& action, |
| 1350 | const typename Function<F>::ArgumentTuple& args) { |
| 1351 | return new ActionResultHolder(action.Perform(args)); |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | private: |
| 1355 | T value_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1356 | |
| 1357 | // T could be a reference type, so = isn't supported. |
| 1358 | GTEST_DISALLOW_ASSIGN_(ActionResultHolder); |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1359 | }; |
| 1360 | |
| 1361 | // Specialization for T = void. |
| 1362 | template <> |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1363 | class ActionResultHolder<void> : public UntypedActionResultHolderBase { |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1364 | public: |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1365 | void GetValueAndDelete() const { delete this; } |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1366 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1367 | virtual void PrintAsActionResult(::std::ostream* /* os */) const {} |
| 1368 | |
| 1369 | // Performs the given mock function's default action and returns NULL; |
| 1370 | template <typename F> |
| 1371 | static ActionResultHolder* PerformDefaultAction( |
| 1372 | const FunctionMockerBase<F>* func_mocker, |
| 1373 | const typename Function<F>::ArgumentTuple& args, |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1374 | const string& call_description) { |
| 1375 | func_mocker->PerformDefaultAction(args, call_description); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1376 | return NULL; |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1379 | // Performs the given action and returns NULL. |
| 1380 | template <typename F> |
| 1381 | static ActionResultHolder* PerformAction( |
| 1382 | const Action<F>& action, |
| 1383 | const typename Function<F>::ArgumentTuple& args) { |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1384 | action.Perform(args); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1385 | return NULL; |
zhanyong.wan | 9413f2f | 2009-05-29 19:50:06 +0000 | [diff] [blame] | 1386 | } |
| 1387 | }; |
| 1388 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1389 | // The base of the function mocker class for the given function type. |
| 1390 | // We put the methods in this class instead of its child to avoid code |
| 1391 | // bloat. |
| 1392 | template <typename F> |
| 1393 | class FunctionMockerBase : public UntypedFunctionMockerBase { |
| 1394 | public: |
| 1395 | typedef typename Function<F>::Result Result; |
| 1396 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 1397 | typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; |
| 1398 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1399 | FunctionMockerBase() : current_spec_(this) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1400 | |
| 1401 | // The destructor verifies that all expectations on this mock |
| 1402 | // function have been satisfied. If not, it will report Google Test |
| 1403 | // non-fatal failures for the violations. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1404 | virtual ~FunctionMockerBase() |
| 1405 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1406 | MutexLock l(&g_gmock_mutex); |
| 1407 | VerifyAndClearExpectationsLocked(); |
| 1408 | Mock::UnregisterLocked(this); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1409 | ClearDefaultActionsLocked(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | // Returns the ON_CALL spec that matches this mock function with the |
| 1413 | // given arguments; returns NULL if no matching ON_CALL is found. |
| 1414 | // L = * |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1415 | const OnCallSpec<F>* FindOnCallSpec( |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1416 | const ArgumentTuple& args) const { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1417 | for (UntypedOnCallSpecs::const_reverse_iterator it |
| 1418 | = untyped_on_call_specs_.rbegin(); |
| 1419 | it != untyped_on_call_specs_.rend(); ++it) { |
| 1420 | const OnCallSpec<F>* spec = static_cast<const OnCallSpec<F>*>(*it); |
| 1421 | if (spec->Matches(args)) |
| 1422 | return spec; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | return NULL; |
| 1426 | } |
| 1427 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 1428 | // Performs the default action of this mock function on the given arguments |
| 1429 | // and returns the result. Asserts with a helpful call descrption if there is |
| 1430 | // no valid return value. This method doesn't depend on the mutable state of |
| 1431 | // this object, and thus can be called concurrently without locking. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1432 | // L = * |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 1433 | Result PerformDefaultAction(const ArgumentTuple& args, |
| 1434 | const string& call_description) const { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1435 | const OnCallSpec<F>* const spec = |
| 1436 | this->FindOnCallSpec(args); |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 1437 | if (spec != NULL) { |
| 1438 | return spec->GetAction().Perform(args); |
| 1439 | } |
| 1440 | Assert(DefaultValue<Result>::Exists(), "", -1, |
| 1441 | call_description + "\n The mock function has no default action " |
| 1442 | "set, and its return type has no default value set."); |
| 1443 | return DefaultValue<Result>::Get(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1446 | // Performs the default action with the given arguments and returns |
| 1447 | // the action's result. The call description string will be used in |
| 1448 | // the error message to describe the call in the case the default |
| 1449 | // action fails. The caller is responsible for deleting the result. |
| 1450 | // L = * |
| 1451 | virtual UntypedActionResultHolderBase* UntypedPerformDefaultAction( |
| 1452 | const void* untyped_args, // must point to an ArgumentTuple |
| 1453 | const string& call_description) const { |
| 1454 | const ArgumentTuple& args = |
| 1455 | *static_cast<const ArgumentTuple*>(untyped_args); |
| 1456 | return ResultHolder::PerformDefaultAction(this, args, call_description); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1459 | // Performs the given action with the given arguments and returns |
| 1460 | // the action's result. The caller is responsible for deleting the |
| 1461 | // result. |
| 1462 | // L = * |
| 1463 | virtual UntypedActionResultHolderBase* UntypedPerformAction( |
| 1464 | const void* untyped_action, const void* untyped_args) const { |
| 1465 | // Make a copy of the action before performing it, in case the |
| 1466 | // action deletes the mock object (and thus deletes itself). |
| 1467 | const Action<F> action = *static_cast<const Action<F>*>(untyped_action); |
| 1468 | const ArgumentTuple& args = |
| 1469 | *static_cast<const ArgumentTuple*>(untyped_args); |
| 1470 | return ResultHolder::PerformAction(action, args); |
| 1471 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1472 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1473 | // Implements UntypedFunctionMockerBase::ClearDefaultActionsLocked(): |
| 1474 | // clears the ON_CALL()s set on this mock function. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1475 | virtual void ClearDefaultActionsLocked() |
| 1476 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1477 | g_gmock_mutex.AssertHeld(); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1478 | for (UntypedOnCallSpecs::const_iterator it = |
| 1479 | untyped_on_call_specs_.begin(); |
| 1480 | it != untyped_on_call_specs_.end(); ++it) { |
| 1481 | delete static_cast<const OnCallSpec<F>*>(*it); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1482 | } |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1483 | untyped_on_call_specs_.clear(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1484 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1485 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1486 | protected: |
| 1487 | template <typename Function> |
| 1488 | friend class MockSpec; |
| 1489 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1490 | typedef ActionResultHolder<Result> ResultHolder; |
| 1491 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1492 | // Returns the result of invoking this mock function with the given |
| 1493 | // arguments. This function can be safely called from multiple |
| 1494 | // threads concurrently. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1495 | Result InvokeWith(const ArgumentTuple& args) |
| 1496 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1497 | return static_cast<const ResultHolder*>( |
| 1498 | this->UntypedInvokeWith(&args))->GetValueAndDelete(); |
| 1499 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1500 | |
| 1501 | // Adds and returns a default action spec for this mock function. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1502 | OnCallSpec<F>& AddNewOnCallSpec( |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1503 | const char* file, int line, |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1504 | const ArgumentMatcherTuple& m) |
| 1505 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 1506 | Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1507 | OnCallSpec<F>* const on_call_spec = new OnCallSpec<F>(file, line, m); |
| 1508 | untyped_on_call_specs_.push_back(on_call_spec); |
| 1509 | return *on_call_spec; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | // Adds and returns an expectation spec for this mock function. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1513 | TypedExpectation<F>& AddNewExpectation( |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 1514 | const char* file, |
| 1515 | int line, |
| 1516 | const string& source_text, |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1517 | const ArgumentMatcherTuple& m) |
| 1518 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 1519 | Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1520 | TypedExpectation<F>* const expectation = |
| 1521 | new TypedExpectation<F>(this, file, line, source_text, m); |
| 1522 | const linked_ptr<ExpectationBase> untyped_expectation(expectation); |
| 1523 | untyped_expectations_.push_back(untyped_expectation); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1524 | |
| 1525 | // Adds this expectation into the implicit sequence if there is one. |
| 1526 | Sequence* const implicit_sequence = g_gmock_implicit_sequence.get(); |
| 1527 | if (implicit_sequence != NULL) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1528 | implicit_sequence->AddExpectation(Expectation(untyped_expectation)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | return *expectation; |
| 1532 | } |
| 1533 | |
| 1534 | // The current spec (either default action spec or expectation spec) |
| 1535 | // being described on this function mocker. |
| 1536 | MockSpec<F>& current_spec() { return current_spec_; } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 1537 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1538 | private: |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1539 | template <typename Func> friend class TypedExpectation; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1540 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1541 | // Some utilities needed for implementing UntypedInvokeWith(). |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Describes what default action will be performed for the given |
| 1544 | // arguments. |
| 1545 | // L = * |
| 1546 | void DescribeDefaultActionTo(const ArgumentTuple& args, |
| 1547 | ::std::ostream* os) const { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1548 | const OnCallSpec<F>* const spec = FindOnCallSpec(args); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1549 | |
| 1550 | if (spec == NULL) { |
| 1551 | *os << (internal::type_equals<Result, void>::value ? |
| 1552 | "returning directly.\n" : |
| 1553 | "returning default value.\n"); |
| 1554 | } else { |
| 1555 | *os << "taking default action specified at:\n" |
vladlosev | e5121b5 | 2011-02-11 23:50:38 +0000 | [diff] [blame] | 1556 | << FormatFileLocation(spec->file(), spec->line()) << "\n"; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | // Writes a message that the call is uninteresting (i.e. neither |
| 1561 | // explicitly expected nor explicitly unexpected) to the given |
| 1562 | // ostream. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1563 | virtual void UntypedDescribeUninterestingCall( |
| 1564 | const void* untyped_args, |
| 1565 | ::std::ostream* os) const |
| 1566 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1567 | const ArgumentTuple& args = |
| 1568 | *static_cast<const ArgumentTuple*>(untyped_args); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1569 | *os << "Uninteresting mock function call - "; |
| 1570 | DescribeDefaultActionTo(args, os); |
| 1571 | *os << " Function call: " << Name(); |
vladlosev | e2e8ba4 | 2010-05-13 18:16:03 +0000 | [diff] [blame] | 1572 | UniversalPrint(args, os); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1575 | // Returns the expectation that matches the given function arguments |
| 1576 | // (or NULL is there's no match); when a match is found, |
| 1577 | // untyped_action is set to point to the action that should be |
| 1578 | // performed (or NULL if the action is "do default"), and |
| 1579 | // is_excessive is modified to indicate whether the call exceeds the |
| 1580 | // expected number. |
| 1581 | // |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1582 | // Critical section: We must find the matching expectation and the |
| 1583 | // corresponding action that needs to be taken in an ATOMIC |
| 1584 | // transaction. Otherwise another thread may call this mock |
| 1585 | // method in the middle and mess up the state. |
| 1586 | // |
| 1587 | // However, performing the action has to be left out of the critical |
| 1588 | // section. The reason is that we have no control on what the |
| 1589 | // action does (it can invoke an arbitrary user function or even a |
| 1590 | // mock function) and excessive locking could cause a dead lock. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1591 | virtual const ExpectationBase* UntypedFindMatchingExpectation( |
| 1592 | const void* untyped_args, |
| 1593 | const void** untyped_action, bool* is_excessive, |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1594 | ::std::ostream* what, ::std::ostream* why) |
| 1595 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1596 | const ArgumentTuple& args = |
| 1597 | *static_cast<const ArgumentTuple*>(untyped_args); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1598 | MutexLock l(&g_gmock_mutex); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1599 | TypedExpectation<F>* exp = this->FindMatchingExpectationLocked(args); |
| 1600 | if (exp == NULL) { // A match wasn't found. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1601 | this->FormatUnexpectedCallMessageLocked(args, what, why); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1602 | return NULL; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // This line must be done before calling GetActionForArguments(), |
| 1606 | // which will increment the call count for *exp and thus affect |
| 1607 | // its saturation status. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1608 | *is_excessive = exp->IsSaturated(); |
| 1609 | const Action<F>* action = exp->GetActionForArguments(this, args, what, why); |
| 1610 | if (action != NULL && action->IsDoDefault()) |
| 1611 | action = NULL; // Normalize "do default" to NULL. |
| 1612 | *untyped_action = action; |
| 1613 | return exp; |
| 1614 | } |
| 1615 | |
| 1616 | // Prints the given function arguments to the ostream. |
| 1617 | virtual void UntypedPrintArgs(const void* untyped_args, |
| 1618 | ::std::ostream* os) const { |
| 1619 | const ArgumentTuple& args = |
| 1620 | *static_cast<const ArgumentTuple*>(untyped_args); |
| 1621 | UniversalPrint(args, os); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | // Returns the expectation that matches the arguments, or NULL if no |
| 1625 | // expectation matches them. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1626 | TypedExpectation<F>* FindMatchingExpectationLocked( |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1627 | const ArgumentTuple& args) const |
| 1628 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1629 | g_gmock_mutex.AssertHeld(); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1630 | for (typename UntypedExpectations::const_reverse_iterator it = |
| 1631 | untyped_expectations_.rbegin(); |
| 1632 | it != untyped_expectations_.rend(); ++it) { |
| 1633 | TypedExpectation<F>* const exp = |
| 1634 | static_cast<TypedExpectation<F>*>(it->get()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1635 | if (exp->ShouldHandleArguments(args)) { |
| 1636 | return exp; |
| 1637 | } |
| 1638 | } |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | |
| 1642 | // Returns a message that the arguments don't match any expectation. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1643 | void FormatUnexpectedCallMessageLocked( |
| 1644 | const ArgumentTuple& args, |
| 1645 | ::std::ostream* os, |
| 1646 | ::std::ostream* why) const |
| 1647 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1648 | g_gmock_mutex.AssertHeld(); |
| 1649 | *os << "\nUnexpected mock function call - "; |
| 1650 | DescribeDefaultActionTo(args, os); |
| 1651 | PrintTriedExpectationsLocked(args, why); |
| 1652 | } |
| 1653 | |
| 1654 | // Prints a list of expectations that have been tried against the |
| 1655 | // current mock function call. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame^] | 1656 | void PrintTriedExpectationsLocked( |
| 1657 | const ArgumentTuple& args, |
| 1658 | ::std::ostream* why) const |
| 1659 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1660 | g_gmock_mutex.AssertHeld(); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1661 | const int count = static_cast<int>(untyped_expectations_.size()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1662 | *why << "Google Mock tried the following " << count << " " |
| 1663 | << (count == 1 ? "expectation, but it didn't match" : |
| 1664 | "expectations, but none matched") |
| 1665 | << ":\n"; |
| 1666 | for (int i = 0; i < count; i++) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1667 | TypedExpectation<F>* const expectation = |
| 1668 | static_cast<TypedExpectation<F>*>(untyped_expectations_[i].get()); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1669 | *why << "\n"; |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1670 | expectation->DescribeLocationTo(why); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1671 | if (count > 1) { |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 1672 | *why << "tried expectation #" << i << ": "; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1673 | } |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 1674 | *why << expectation->source_text() << "...\n"; |
| 1675 | expectation->ExplainMatchResultTo(args, why); |
| 1676 | expectation->DescribeCallCountTo(why); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1677 | } |
| 1678 | } |
| 1679 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1680 | // The current spec (either default action spec or expectation spec) |
| 1681 | // being described on this function mocker. |
| 1682 | MockSpec<F> current_spec_; |
| 1683 | |
zhanyong.wan | 16cf473 | 2009-05-14 20:55:30 +0000 | [diff] [blame] | 1684 | // There is no generally useful and implementable semantics of |
| 1685 | // copying a mock object, so copying a mock is usually a user error. |
| 1686 | // Thus we disallow copying function mockers. If the user really |
| 1687 | // wants to copy a mock object, he should implement his own copy |
| 1688 | // operation, for example: |
| 1689 | // |
| 1690 | // class MockFoo : public Foo { |
| 1691 | // public: |
| 1692 | // // Defines a copy constructor explicitly. |
| 1693 | // MockFoo(const MockFoo& src) {} |
| 1694 | // ... |
| 1695 | // }; |
| 1696 | GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1697 | }; // class FunctionMockerBase |
| 1698 | |
| 1699 | #ifdef _MSC_VER |
zhanyong.wan | 658ac0b | 2011-02-24 07:29:13 +0000 | [diff] [blame] | 1700 | # pragma warning(pop) // Restores the warning state. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1701 | #endif // _MSV_VER |
| 1702 | |
| 1703 | // Implements methods of FunctionMockerBase. |
| 1704 | |
| 1705 | // Verifies that all expectations on this mock function have been |
| 1706 | // satisfied. Reports one or more Google Test non-fatal failures and |
| 1707 | // returns false if not. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1708 | |
| 1709 | // Reports an uninteresting call (whose description is in msg) in the |
| 1710 | // manner specified by 'reaction'. |
| 1711 | void ReportUninterestingCall(CallReaction reaction, const string& msg); |
| 1712 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1713 | } // namespace internal |
| 1714 | |
| 1715 | // The style guide prohibits "using" statements in a namespace scope |
| 1716 | // inside a header file. However, the MockSpec class template is |
| 1717 | // meant to be defined in the ::testing namespace. The following line |
| 1718 | // is just a trick for working around a bug in MSVC 8.0, which cannot |
| 1719 | // handle it if we define MockSpec in ::testing. |
| 1720 | using internal::MockSpec; |
| 1721 | |
| 1722 | // Const(x) is a convenient function for obtaining a const reference |
| 1723 | // to x. This is useful for setting expectations on an overloaded |
| 1724 | // const mock method, e.g. |
| 1725 | // |
| 1726 | // class MockFoo : public FooInterface { |
| 1727 | // public: |
| 1728 | // MOCK_METHOD0(Bar, int()); |
| 1729 | // MOCK_CONST_METHOD0(Bar, int&()); |
| 1730 | // }; |
| 1731 | // |
| 1732 | // MockFoo foo; |
| 1733 | // // Expects a call to non-const MockFoo::Bar(). |
| 1734 | // EXPECT_CALL(foo, Bar()); |
| 1735 | // // Expects a call to const MockFoo::Bar(). |
| 1736 | // EXPECT_CALL(Const(foo), Bar()); |
| 1737 | template <typename T> |
| 1738 | inline const T& Const(const T& x) { return x; } |
| 1739 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 1740 | // Constructs an Expectation object that references and co-owns exp. |
| 1741 | inline Expectation::Expectation(internal::ExpectationBase& exp) // NOLINT |
| 1742 | : expectation_base_(exp.GetHandle().expectation_base()) {} |
| 1743 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1744 | } // namespace testing |
| 1745 | |
| 1746 | // A separate macro is required to avoid compile errors when the name |
| 1747 | // of the method used in call is a result of macro expansion. |
| 1748 | // See CompilesWithMethodNameExpandedFromMacro tests in |
| 1749 | // internal/gmock-spec-builders_test.cc for more details. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 1750 | #define GMOCK_ON_CALL_IMPL_(obj, call) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1751 | ((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \ |
| 1752 | #obj, #call) |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 1753 | #define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1754 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 1755 | #define GMOCK_EXPECT_CALL_IMPL_(obj, call) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1756 | ((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call) |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 1757 | #define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1758 | |
| 1759 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ |