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