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