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