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