shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file implements the spec builder syntax (ON_CALL and |
| 35 | // EXPECT_CALL). |
| 36 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 37 | #include "gmock/gmock-spec-builders.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 38 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <iostream> // NOLINT |
| 41 | #include <map> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 42 | #include <set> |
vladlosev | 6c54a5e | 2009-10-21 06:15:34 +0000 | [diff] [blame] | 43 | #include <string> |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 44 | #include "gmock/gmock.h" |
| 45 | #include "gtest/gtest.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 46 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 47 | #if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC |
zhanyong.wan | 658ac0b | 2011-02-24 07:29:13 +0000 | [diff] [blame] | 48 | # include <unistd.h> // NOLINT |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 51 | namespace testing { |
| 52 | namespace internal { |
| 53 | |
| 54 | // Protects the mock object registry (in class Mock), all function |
| 55 | // mockers, and all expectations. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 56 | GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_gmock_mutex); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 57 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 58 | // Logs a message including file and line number information. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 59 | GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, |
| 60 | const char* file, int line, |
| 61 | const string& message) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 62 | ::std::ostringstream s; |
| 63 | s << file << ":" << line << ": " << message << ::std::endl; |
| 64 | Log(severity, s.str(), 0); |
| 65 | } |
| 66 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 67 | // Constructs an ExpectationBase object. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 68 | ExpectationBase::ExpectationBase(const char* a_file, |
| 69 | int a_line, |
| 70 | const string& a_source_text) |
| 71 | : file_(a_file), |
| 72 | line_(a_line), |
| 73 | source_text_(a_source_text), |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 74 | cardinality_specified_(false), |
| 75 | cardinality_(Exactly(1)), |
| 76 | call_count_(0), |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 77 | retired_(false), |
| 78 | extra_matcher_specified_(false), |
| 79 | repeated_action_specified_(false), |
| 80 | retires_on_saturation_(false), |
| 81 | last_clause_(kNone), |
| 82 | action_count_checked_(false) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 83 | |
| 84 | // Destructs an ExpectationBase object. |
| 85 | ExpectationBase::~ExpectationBase() {} |
| 86 | |
| 87 | // Explicitly specifies the cardinality of this expectation. Used by |
| 88 | // the subclasses to implement the .Times() clause. |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 89 | void ExpectationBase::SpecifyCardinality(const Cardinality& a_cardinality) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 90 | cardinality_specified_ = true; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 91 | cardinality_ = a_cardinality; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Retires all pre-requisites of this expectation. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 95 | void ExpectationBase::RetireAllPreRequisites() |
| 96 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 97 | if (is_retired()) { |
| 98 | // We can take this short-cut as we never retire an expectation |
| 99 | // until we have retired all its pre-requisites. |
| 100 | return; |
| 101 | } |
| 102 | |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 103 | for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); |
| 104 | it != immediate_prerequisites_.end(); ++it) { |
| 105 | ExpectationBase* const prerequisite = it->expectation_base().get(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 106 | if (!prerequisite->is_retired()) { |
| 107 | prerequisite->RetireAllPreRequisites(); |
| 108 | prerequisite->Retire(); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Returns true iff all pre-requisites of this expectation have been |
| 114 | // satisfied. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 115 | bool ExpectationBase::AllPrerequisitesAreSatisfied() const |
| 116 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 117 | g_gmock_mutex.AssertHeld(); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 118 | for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 119 | it != immediate_prerequisites_.end(); ++it) { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 120 | if (!(it->expectation_base()->IsSatisfied()) || |
| 121 | !(it->expectation_base()->AllPrerequisitesAreSatisfied())) |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | // Adds unsatisfied pre-requisites of this expectation to 'result'. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 128 | void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result) const |
| 129 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 130 | g_gmock_mutex.AssertHeld(); |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 131 | for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 132 | it != immediate_prerequisites_.end(); ++it) { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 133 | if (it->expectation_base()->IsSatisfied()) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 134 | // If *it is satisfied and has a call count of 0, some of its |
| 135 | // pre-requisites may not be satisfied yet. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 136 | if (it->expectation_base()->call_count_ == 0) { |
| 137 | it->expectation_base()->FindUnsatisfiedPrerequisites(result); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 138 | } |
| 139 | } else { |
| 140 | // Now that we know *it is unsatisfied, we are not so interested |
| 141 | // in whether its pre-requisites are satisfied. Therefore we |
| 142 | // don't recursively call FindUnsatisfiedPrerequisites() here. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 143 | *result += *it; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 148 | // Describes how many times a function call matching this |
| 149 | // expectation has occurred. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 150 | void ExpectationBase::DescribeCallCountTo(::std::ostream* os) const |
| 151 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 152 | g_gmock_mutex.AssertHeld(); |
| 153 | |
| 154 | // Describes how many times the function is expected to be called. |
| 155 | *os << " Expected: to be "; |
| 156 | cardinality().DescribeTo(os); |
| 157 | *os << "\n Actual: "; |
| 158 | Cardinality::DescribeActualCallCountTo(call_count(), os); |
| 159 | |
| 160 | // Describes the state of the expectation (e.g. is it satisfied? |
| 161 | // is it active?). |
| 162 | *os << " - " << (IsOverSaturated() ? "over-saturated" : |
| 163 | IsSaturated() ? "saturated" : |
| 164 | IsSatisfied() ? "satisfied" : "unsatisfied") |
| 165 | << " and " |
| 166 | << (is_retired() ? "retired" : "active"); |
| 167 | } |
| 168 | |
| 169 | // Checks the action count (i.e. the number of WillOnce() and |
| 170 | // WillRepeatedly() clauses) against the cardinality if this hasn't |
| 171 | // been done before. Prints a warning if there are too many or too |
| 172 | // few actions. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 173 | void ExpectationBase::CheckActionCountIfNotDone() const |
| 174 | GTEST_LOCK_EXCLUDED_(mutex_) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 175 | bool should_check = false; |
| 176 | { |
| 177 | MutexLock l(&mutex_); |
| 178 | if (!action_count_checked_) { |
| 179 | action_count_checked_ = true; |
| 180 | should_check = true; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (should_check) { |
| 185 | if (!cardinality_specified_) { |
| 186 | // The cardinality was inferred - no need to check the action |
| 187 | // count against it. |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | // The cardinality was explicitly specified. |
| 192 | const int action_count = static_cast<int>(untyped_actions_.size()); |
| 193 | const int upper_bound = cardinality().ConservativeUpperBound(); |
| 194 | const int lower_bound = cardinality().ConservativeLowerBound(); |
| 195 | bool too_many; // True if there are too many actions, or false |
| 196 | // if there are too few. |
| 197 | if (action_count > upper_bound || |
| 198 | (action_count == upper_bound && repeated_action_specified_)) { |
| 199 | too_many = true; |
| 200 | } else if (0 < action_count && action_count < lower_bound && |
| 201 | !repeated_action_specified_) { |
| 202 | too_many = false; |
| 203 | } else { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | ::std::stringstream ss; |
| 208 | DescribeLocationTo(&ss); |
| 209 | ss << "Too " << (too_many ? "many" : "few") |
| 210 | << " actions specified in " << source_text() << "...\n" |
| 211 | << "Expected to be "; |
| 212 | cardinality().DescribeTo(&ss); |
| 213 | ss << ", but has " << (too_many ? "" : "only ") |
| 214 | << action_count << " WillOnce()" |
| 215 | << (action_count == 1 ? "" : "s"); |
| 216 | if (repeated_action_specified_) { |
| 217 | ss << " and a WillRepeatedly()"; |
| 218 | } |
| 219 | ss << "."; |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 220 | Log(kWarning, ss.str(), -1); // -1 means "don't print stack trace". |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | // Implements the .Times() clause. |
| 225 | void ExpectationBase::UntypedTimes(const Cardinality& a_cardinality) { |
| 226 | if (last_clause_ == kTimes) { |
| 227 | ExpectSpecProperty(false, |
| 228 | ".Times() cannot appear " |
| 229 | "more than once in an EXPECT_CALL()."); |
| 230 | } else { |
| 231 | ExpectSpecProperty(last_clause_ < kTimes, |
| 232 | ".Times() cannot appear after " |
| 233 | ".InSequence(), .WillOnce(), .WillRepeatedly(), " |
| 234 | "or .RetiresOnSaturation()."); |
| 235 | } |
| 236 | last_clause_ = kTimes; |
| 237 | |
| 238 | SpecifyCardinality(a_cardinality); |
| 239 | } |
| 240 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 241 | // Points to the implicit sequence introduced by a living InSequence |
| 242 | // object (if any) in the current thread or NULL. |
vladlosev | 587c1b3 | 2011-05-20 00:42:22 +0000 | [diff] [blame] | 243 | GTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 244 | |
| 245 | // Reports an uninteresting call (whose description is in msg) in the |
| 246 | // manner specified by 'reaction'. |
| 247 | void ReportUninterestingCall(CallReaction reaction, const string& msg) { |
| 248 | switch (reaction) { |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 249 | case kAllow: |
| 250 | Log(kInfo, msg, 3); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 251 | break; |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 252 | case kWarn: |
kosak | 04ce852 | 2014-01-12 23:42:34 +0000 | [diff] [blame^] | 253 | Log(kWarning, |
| 254 | msg + |
| 255 | "\nNOTE: You can safely ignore the above warning unless this " |
| 256 | "call should not happen. Do not suppress it by blindly adding " |
| 257 | "an EXPECT_CALL() if you don't mean to enforce the call. " |
| 258 | "See http://code.google.com/p/googlemock/wiki/CookBook#" |
| 259 | "Knowing_When_to_Expect for details.", |
| 260 | 3); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 261 | break; |
| 262 | default: // FAIL |
| 263 | Expect(false, NULL, -1, msg); |
| 264 | } |
| 265 | } |
| 266 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 267 | UntypedFunctionMockerBase::UntypedFunctionMockerBase() |
| 268 | : mock_obj_(NULL), name_("") {} |
| 269 | |
| 270 | UntypedFunctionMockerBase::~UntypedFunctionMockerBase() {} |
| 271 | |
| 272 | // Sets the mock object this mock method belongs to, and registers |
| 273 | // this information in the global mock registry. Will be called |
| 274 | // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock |
| 275 | // method. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 276 | void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj) |
| 277 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 278 | { |
| 279 | MutexLock l(&g_gmock_mutex); |
| 280 | mock_obj_ = mock_obj; |
| 281 | } |
| 282 | Mock::Register(mock_obj, this); |
| 283 | } |
| 284 | |
| 285 | // Sets the mock object this mock method belongs to, and sets the name |
| 286 | // of the mock function. Will be called upon each invocation of this |
| 287 | // mock function. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 288 | void UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj, |
| 289 | const char* name) |
| 290 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 291 | // We protect name_ under g_gmock_mutex in case this mock function |
| 292 | // is called from two threads concurrently. |
| 293 | MutexLock l(&g_gmock_mutex); |
| 294 | mock_obj_ = mock_obj; |
| 295 | name_ = name; |
| 296 | } |
| 297 | |
| 298 | // Returns the name of the function being mocked. Must be called |
| 299 | // after RegisterOwner() or SetOwnerAndName() has been called. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 300 | const void* UntypedFunctionMockerBase::MockObject() const |
| 301 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 302 | const void* mock_obj; |
| 303 | { |
| 304 | // We protect mock_obj_ under g_gmock_mutex in case this mock |
| 305 | // function is called from two threads concurrently. |
| 306 | MutexLock l(&g_gmock_mutex); |
| 307 | Assert(mock_obj_ != NULL, __FILE__, __LINE__, |
| 308 | "MockObject() must not be called before RegisterOwner() or " |
| 309 | "SetOwnerAndName() has been called."); |
| 310 | mock_obj = mock_obj_; |
| 311 | } |
| 312 | return mock_obj; |
| 313 | } |
| 314 | |
| 315 | // Returns the name of this mock method. Must be called after |
| 316 | // SetOwnerAndName() has been called. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 317 | const char* UntypedFunctionMockerBase::Name() const |
| 318 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 319 | const char* name; |
| 320 | { |
| 321 | // We protect name_ under g_gmock_mutex in case this mock |
| 322 | // function is called from two threads concurrently. |
| 323 | MutexLock l(&g_gmock_mutex); |
| 324 | Assert(name_ != NULL, __FILE__, __LINE__, |
| 325 | "Name() must not be called before SetOwnerAndName() has " |
| 326 | "been called."); |
| 327 | name = name_; |
| 328 | } |
| 329 | return name; |
| 330 | } |
| 331 | |
| 332 | // Calculates the result of invoking this mock function with the given |
| 333 | // arguments, prints it, and returns it. The caller is responsible |
| 334 | // for deleting the result. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 335 | const UntypedActionResultHolderBase* |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 336 | UntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args) |
| 337 | GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 338 | if (untyped_expectations_.size() == 0) { |
| 339 | // No expectation is set on this mock method - we have an |
| 340 | // uninteresting call. |
| 341 | |
| 342 | // We must get Google Mock's reaction on uninteresting calls |
| 343 | // made on this mock object BEFORE performing the action, |
| 344 | // because the action may DELETE the mock object and make the |
| 345 | // following expression meaningless. |
| 346 | const CallReaction reaction = |
| 347 | Mock::GetReactionOnUninterestingCalls(MockObject()); |
| 348 | |
| 349 | // True iff we need to print this call's arguments and return |
| 350 | // value. This definition must be kept in sync with |
| 351 | // the behavior of ReportUninterestingCall(). |
| 352 | const bool need_to_report_uninteresting_call = |
| 353 | // If the user allows this uninteresting call, we print it |
| 354 | // only when he wants informational messages. |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 355 | reaction == kAllow ? LogIsVisible(kInfo) : |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 356 | // If the user wants this to be a warning, we print it only |
| 357 | // when he wants to see warnings. |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 358 | reaction == kWarn ? LogIsVisible(kWarning) : |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 359 | // Otherwise, the user wants this to be an error, and we |
| 360 | // should always print detailed information in the error. |
| 361 | true; |
| 362 | |
| 363 | if (!need_to_report_uninteresting_call) { |
| 364 | // Perform the action without printing the call information. |
| 365 | return this->UntypedPerformDefaultAction(untyped_args, ""); |
| 366 | } |
| 367 | |
| 368 | // Warns about the uninteresting call. |
| 369 | ::std::stringstream ss; |
| 370 | this->UntypedDescribeUninterestingCall(untyped_args, &ss); |
| 371 | |
| 372 | // Calculates the function result. |
| 373 | const UntypedActionResultHolderBase* const result = |
| 374 | this->UntypedPerformDefaultAction(untyped_args, ss.str()); |
| 375 | |
| 376 | // Prints the function result. |
| 377 | if (result != NULL) |
| 378 | result->PrintAsActionResult(&ss); |
| 379 | |
| 380 | ReportUninterestingCall(reaction, ss.str()); |
| 381 | return result; |
| 382 | } |
| 383 | |
| 384 | bool is_excessive = false; |
| 385 | ::std::stringstream ss; |
| 386 | ::std::stringstream why; |
| 387 | ::std::stringstream loc; |
| 388 | const void* untyped_action = NULL; |
| 389 | |
| 390 | // The UntypedFindMatchingExpectation() function acquires and |
| 391 | // releases g_gmock_mutex. |
| 392 | const ExpectationBase* const untyped_expectation = |
| 393 | this->UntypedFindMatchingExpectation( |
| 394 | untyped_args, &untyped_action, &is_excessive, |
| 395 | &ss, &why); |
| 396 | const bool found = untyped_expectation != NULL; |
| 397 | |
| 398 | // True iff we need to print the call's arguments and return value. |
| 399 | // This definition must be kept in sync with the uses of Expect() |
| 400 | // and Log() in this function. |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 401 | const bool need_to_report_call = |
| 402 | !found || is_excessive || LogIsVisible(kInfo); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 403 | if (!need_to_report_call) { |
| 404 | // Perform the action without printing the call information. |
| 405 | return |
| 406 | untyped_action == NULL ? |
| 407 | this->UntypedPerformDefaultAction(untyped_args, "") : |
| 408 | this->UntypedPerformAction(untyped_action, untyped_args); |
| 409 | } |
| 410 | |
| 411 | ss << " Function call: " << Name(); |
| 412 | this->UntypedPrintArgs(untyped_args, &ss); |
| 413 | |
| 414 | // In case the action deletes a piece of the expectation, we |
| 415 | // generate the message beforehand. |
| 416 | if (found && !is_excessive) { |
| 417 | untyped_expectation->DescribeLocationTo(&loc); |
| 418 | } |
| 419 | |
| 420 | const UntypedActionResultHolderBase* const result = |
| 421 | untyped_action == NULL ? |
| 422 | this->UntypedPerformDefaultAction(untyped_args, ss.str()) : |
| 423 | this->UntypedPerformAction(untyped_action, untyped_args); |
| 424 | if (result != NULL) |
| 425 | result->PrintAsActionResult(&ss); |
| 426 | ss << "\n" << why.str(); |
| 427 | |
| 428 | if (!found) { |
| 429 | // No expectation matches this call - reports a failure. |
| 430 | Expect(false, NULL, -1, ss.str()); |
| 431 | } else if (is_excessive) { |
| 432 | // We had an upper-bound violation and the failure message is in ss. |
| 433 | Expect(false, untyped_expectation->file(), |
| 434 | untyped_expectation->line(), ss.str()); |
| 435 | } else { |
| 436 | // We had an expected call and the matching expectation is |
| 437 | // described in ss. |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 438 | Log(kInfo, loc.str() + ss.str(), 2); |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | return result; |
| 442 | } |
| 443 | |
| 444 | // Returns an Expectation object that references and co-owns exp, |
| 445 | // which must be an expectation on this mock function. |
| 446 | Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { |
| 447 | for (UntypedExpectations::const_iterator it = |
| 448 | untyped_expectations_.begin(); |
| 449 | it != untyped_expectations_.end(); ++it) { |
| 450 | if (it->get() == exp) { |
| 451 | return Expectation(*it); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | Assert(false, __FILE__, __LINE__, "Cannot find expectation."); |
| 456 | return Expectation(); |
| 457 | // The above statement is just to make the code compile, and will |
| 458 | // never be executed. |
| 459 | } |
| 460 | |
| 461 | // Verifies that all expectations on this mock function have been |
| 462 | // satisfied. Reports one or more Google Test non-fatal failures |
| 463 | // and returns false if not. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 464 | bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() |
| 465 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 466 | g_gmock_mutex.AssertHeld(); |
| 467 | bool expectations_met = true; |
| 468 | for (UntypedExpectations::const_iterator it = |
| 469 | untyped_expectations_.begin(); |
| 470 | it != untyped_expectations_.end(); ++it) { |
| 471 | ExpectationBase* const untyped_expectation = it->get(); |
| 472 | if (untyped_expectation->IsOverSaturated()) { |
| 473 | // There was an upper-bound violation. Since the error was |
| 474 | // already reported when it occurred, there is no need to do |
| 475 | // anything here. |
| 476 | expectations_met = false; |
| 477 | } else if (!untyped_expectation->IsSatisfied()) { |
| 478 | expectations_met = false; |
| 479 | ::std::stringstream ss; |
| 480 | ss << "Actual function call count doesn't match " |
| 481 | << untyped_expectation->source_text() << "...\n"; |
| 482 | // No need to show the source file location of the expectation |
| 483 | // in the description, as the Expect() call that follows already |
| 484 | // takes care of it. |
| 485 | untyped_expectation->MaybeDescribeExtraMatcherTo(&ss); |
| 486 | untyped_expectation->DescribeCallCountTo(&ss); |
| 487 | Expect(false, untyped_expectation->file(), |
| 488 | untyped_expectation->line(), ss.str()); |
| 489 | } |
| 490 | } |
vladlosev | 9bcb5f9 | 2011-10-24 23:41:07 +0000 | [diff] [blame] | 491 | |
| 492 | // Deleting our expectations may trigger other mock objects to be deleted, for |
| 493 | // example if an action contains a reference counted smart pointer to that |
| 494 | // mock object, and that is the last reference. So if we delete our |
| 495 | // expectations within the context of the global mutex we may deadlock when |
| 496 | // this method is called again. Instead, make a copy of the set of |
| 497 | // expectations to delete, clear our set within the mutex, and then clear the |
| 498 | // copied set outside of it. |
| 499 | UntypedExpectations expectations_to_delete; |
| 500 | untyped_expectations_.swap(expectations_to_delete); |
| 501 | |
| 502 | g_gmock_mutex.Unlock(); |
| 503 | expectations_to_delete.clear(); |
| 504 | g_gmock_mutex.Lock(); |
| 505 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 506 | return expectations_met; |
| 507 | } |
| 508 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 509 | } // namespace internal |
| 510 | |
| 511 | // Class Mock. |
| 512 | |
| 513 | namespace { |
| 514 | |
| 515 | typedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 516 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 517 | // The current state of a mock object. Such information is needed for |
| 518 | // detecting leaked mock objects and explicitly verifying a mock's |
| 519 | // expectations. |
| 520 | struct MockObjectState { |
| 521 | MockObjectState() |
| 522 | : first_used_file(NULL), first_used_line(-1), leakable(false) {} |
| 523 | |
| 524 | // Where in the source file an ON_CALL or EXPECT_CALL is first |
| 525 | // invoked on this mock object. |
| 526 | const char* first_used_file; |
| 527 | int first_used_line; |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 528 | ::std::string first_used_test_case; |
| 529 | ::std::string first_used_test; |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 530 | bool leakable; // true iff it's OK to leak the object. |
| 531 | FunctionMockers function_mockers; // All registered methods of the object. |
| 532 | }; |
| 533 | |
| 534 | // A global registry holding the state of all mock objects that are |
| 535 | // alive. A mock object is added to this registry the first time |
| 536 | // Mock::AllowLeak(), ON_CALL(), or EXPECT_CALL() is called on it. It |
| 537 | // is removed from the registry in the mock object's destructor. |
| 538 | class MockObjectRegistry { |
| 539 | public: |
| 540 | // Maps a mock object (identified by its address) to its state. |
| 541 | typedef std::map<const void*, MockObjectState> StateMap; |
| 542 | |
| 543 | // This destructor will be called when a program exits, after all |
| 544 | // tests in it have been run. By then, there should be no mock |
| 545 | // object alive. Therefore we report any living object as test |
| 546 | // failure, unless the user explicitly asked us to ignore it. |
| 547 | ~MockObjectRegistry() { |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 548 | // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is |
| 549 | // a macro. |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 550 | |
| 551 | if (!GMOCK_FLAG(catch_leaked_mocks)) |
| 552 | return; |
| 553 | |
| 554 | int leaked_count = 0; |
| 555 | for (StateMap::const_iterator it = states_.begin(); it != states_.end(); |
| 556 | ++it) { |
| 557 | if (it->second.leakable) // The user said it's fine to leak this object. |
| 558 | continue; |
| 559 | |
| 560 | // TODO(wan@google.com): Print the type of the leaked object. |
| 561 | // This can help the user identify the leaked object. |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 562 | std::cout << "\n"; |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 563 | const MockObjectState& state = it->second; |
zhanyong.wan | f5e1ce5 | 2009-09-16 07:02:02 +0000 | [diff] [blame] | 564 | std::cout << internal::FormatFileLocation(state.first_used_file, |
| 565 | state.first_used_line); |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 566 | std::cout << " ERROR: this mock object"; |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 567 | if (state.first_used_test != "") { |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 568 | std::cout << " (used in test " << state.first_used_test_case << "." |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 569 | << state.first_used_test << ")"; |
| 570 | } |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 571 | std::cout << " should be deleted but never is. Its address is @" |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 572 | << it->first << "."; |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 573 | leaked_count++; |
| 574 | } |
| 575 | if (leaked_count > 0) { |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 576 | std::cout << "\nERROR: " << leaked_count |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 577 | << " leaked mock " << (leaked_count == 1 ? "object" : "objects") |
| 578 | << " found at program exit.\n"; |
zhanyong.wan | 9571b28 | 2009-08-07 07:15:56 +0000 | [diff] [blame] | 579 | std::cout.flush(); |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 580 | ::std::cerr.flush(); |
| 581 | // RUN_ALL_TESTS() has already returned when this destructor is |
| 582 | // called. Therefore we cannot use the normal Google Test |
| 583 | // failure reporting mechanism. |
| 584 | _exit(1); // We cannot call exit() as it is not reentrant and |
| 585 | // may already have been called. |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | StateMap& states() { return states_; } |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 590 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 591 | private: |
| 592 | StateMap states_; |
| 593 | }; |
| 594 | |
| 595 | // Protected by g_gmock_mutex. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 596 | MockObjectRegistry g_mock_object_registry; |
| 597 | |
| 598 | // Maps a mock object to the reaction Google Mock should have when an |
| 599 | // uninteresting method is called. Protected by g_gmock_mutex. |
| 600 | std::map<const void*, internal::CallReaction> g_uninteresting_call_reaction; |
| 601 | |
| 602 | // Sets the reaction Google Mock should have when an uninteresting |
| 603 | // method of the given mock object is called. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 604 | void SetReactionOnUninterestingCalls(const void* mock_obj, |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 605 | internal::CallReaction reaction) |
| 606 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 607 | internal::MutexLock l(&internal::g_gmock_mutex); |
| 608 | g_uninteresting_call_reaction[mock_obj] = reaction; |
| 609 | } |
| 610 | |
| 611 | } // namespace |
| 612 | |
| 613 | // Tells Google Mock to allow uninteresting calls on the given mock |
| 614 | // object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 615 | void Mock::AllowUninterestingCalls(const void* mock_obj) |
| 616 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 617 | SetReactionOnUninterestingCalls(mock_obj, internal::kAllow); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Tells Google Mock to warn the user about uninteresting calls on the |
| 621 | // given mock object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 622 | void Mock::WarnUninterestingCalls(const void* mock_obj) |
| 623 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 624 | SetReactionOnUninterestingCalls(mock_obj, internal::kWarn); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // Tells Google Mock to fail uninteresting calls on the given mock |
| 628 | // object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 629 | void Mock::FailUninterestingCalls(const void* mock_obj) |
| 630 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
zhanyong.wan | 2fd619e | 2012-05-31 20:40:56 +0000 | [diff] [blame] | 631 | SetReactionOnUninterestingCalls(mock_obj, internal::kFail); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Tells Google Mock the given mock object is being destroyed and its |
| 635 | // entry in the call-reaction table should be removed. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 636 | void Mock::UnregisterCallReaction(const void* mock_obj) |
| 637 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 638 | internal::MutexLock l(&internal::g_gmock_mutex); |
| 639 | g_uninteresting_call_reaction.erase(mock_obj); |
| 640 | } |
| 641 | |
| 642 | // Returns the reaction Google Mock will have on uninteresting calls |
| 643 | // made on the given mock object. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 644 | internal::CallReaction Mock::GetReactionOnUninterestingCalls( |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 645 | const void* mock_obj) |
| 646 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 647 | internal::MutexLock l(&internal::g_gmock_mutex); |
| 648 | return (g_uninteresting_call_reaction.count(mock_obj) == 0) ? |
zhanyong.wan | c896504 | 2013-03-01 07:10:07 +0000 | [diff] [blame] | 649 | internal::kDefault : g_uninteresting_call_reaction[mock_obj]; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 650 | } |
| 651 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 652 | // Tells Google Mock to ignore mock_obj when checking for leaked mock |
| 653 | // objects. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 654 | void Mock::AllowLeak(const void* mock_obj) |
| 655 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 656 | internal::MutexLock l(&internal::g_gmock_mutex); |
| 657 | g_mock_object_registry.states()[mock_obj].leakable = true; |
| 658 | } |
| 659 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 660 | // Verifies and clears all expectations on the given mock object. If |
| 661 | // the expectations aren't satisfied, generates one or more Google |
| 662 | // Test non-fatal failures and returns false. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 663 | bool Mock::VerifyAndClearExpectations(void* mock_obj) |
| 664 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 665 | internal::MutexLock l(&internal::g_gmock_mutex); |
| 666 | return VerifyAndClearExpectationsLocked(mock_obj); |
| 667 | } |
| 668 | |
| 669 | // Verifies all expectations on the given mock object and clears its |
| 670 | // default actions and expectations. Returns true iff the |
| 671 | // verification was successful. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 672 | bool Mock::VerifyAndClear(void* mock_obj) |
| 673 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 674 | internal::MutexLock l(&internal::g_gmock_mutex); |
| 675 | ClearDefaultActionsLocked(mock_obj); |
| 676 | return VerifyAndClearExpectationsLocked(mock_obj); |
| 677 | } |
| 678 | |
| 679 | // Verifies and clears all expectations on the given mock object. If |
| 680 | // the expectations aren't satisfied, generates one or more Google |
| 681 | // Test non-fatal failures and returns false. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 682 | bool Mock::VerifyAndClearExpectationsLocked(void* mock_obj) |
| 683 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 684 | internal::g_gmock_mutex.AssertHeld(); |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 685 | if (g_mock_object_registry.states().count(mock_obj) == 0) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 686 | // No EXPECT_CALL() was set on the given mock object. |
| 687 | return true; |
| 688 | } |
| 689 | |
| 690 | // Verifies and clears the expectations on each mock method in the |
| 691 | // given mock object. |
| 692 | bool expectations_met = true; |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 693 | FunctionMockers& mockers = |
| 694 | g_mock_object_registry.states()[mock_obj].function_mockers; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 695 | for (FunctionMockers::const_iterator it = mockers.begin(); |
| 696 | it != mockers.end(); ++it) { |
| 697 | if (!(*it)->VerifyAndClearExpectationsLocked()) { |
| 698 | expectations_met = false; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // We don't clear the content of mockers, as they may still be |
| 703 | // needed by ClearDefaultActionsLocked(). |
| 704 | return expectations_met; |
| 705 | } |
| 706 | |
| 707 | // Registers a mock object and a mock method it owns. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 708 | void Mock::Register(const void* mock_obj, |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 709 | internal::UntypedFunctionMockerBase* mocker) |
| 710 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 711 | internal::MutexLock l(&internal::g_gmock_mutex); |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 712 | g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker); |
| 713 | } |
| 714 | |
| 715 | // Tells Google Mock where in the source code mock_obj is used in an |
| 716 | // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this |
| 717 | // information helps the user identify which object it is. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 718 | void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj, |
| 719 | const char* file, int line) |
| 720 | GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 721 | internal::MutexLock l(&internal::g_gmock_mutex); |
| 722 | MockObjectState& state = g_mock_object_registry.states()[mock_obj]; |
| 723 | if (state.first_used_file == NULL) { |
| 724 | state.first_used_file = file; |
| 725 | state.first_used_line = line; |
zhanyong.wan | e7bb5ed | 2009-05-05 23:14:47 +0000 | [diff] [blame] | 726 | const TestInfo* const test_info = |
| 727 | UnitTest::GetInstance()->current_test_info(); |
| 728 | if (test_info != NULL) { |
| 729 | // TODO(wan@google.com): record the test case name when the |
| 730 | // ON_CALL or EXPECT_CALL is invoked from SetUpTestCase() or |
| 731 | // TearDownTestCase(). |
| 732 | state.first_used_test_case = test_info->test_case_name(); |
| 733 | state.first_used_test = test_info->name(); |
| 734 | } |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 735 | } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | // Unregisters a mock method; removes the owning mock object from the |
| 739 | // registry when the last mock method associated with it has been |
| 740 | // unregistered. This is called only in the destructor of |
| 741 | // FunctionMockerBase. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 742 | void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker) |
| 743 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 744 | internal::g_gmock_mutex.AssertHeld(); |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 745 | for (MockObjectRegistry::StateMap::iterator it = |
| 746 | g_mock_object_registry.states().begin(); |
| 747 | it != g_mock_object_registry.states().end(); ++it) { |
| 748 | FunctionMockers& mockers = it->second.function_mockers; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 749 | if (mockers.erase(mocker) > 0) { |
| 750 | // mocker was in mockers and has been just removed. |
| 751 | if (mockers.empty()) { |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 752 | g_mock_object_registry.states().erase(it); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 753 | } |
| 754 | return; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Clears all ON_CALL()s set on the given mock object. |
vladlosev | 4d60a59 | 2011-10-24 21:16:22 +0000 | [diff] [blame] | 760 | void Mock::ClearDefaultActionsLocked(void* mock_obj) |
| 761 | GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 762 | internal::g_gmock_mutex.AssertHeld(); |
| 763 | |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 764 | if (g_mock_object_registry.states().count(mock_obj) == 0) { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 765 | // No ON_CALL() was set on the given mock object. |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | // Clears the default actions for each mock method in the given mock |
| 770 | // object. |
zhanyong.wan | df35a76 | 2009-04-22 22:25:31 +0000 | [diff] [blame] | 771 | FunctionMockers& mockers = |
| 772 | g_mock_object_registry.states()[mock_obj].function_mockers; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 773 | for (FunctionMockers::const_iterator it = mockers.begin(); |
| 774 | it != mockers.end(); ++it) { |
| 775 | (*it)->ClearDefaultActionsLocked(); |
| 776 | } |
| 777 | |
| 778 | // We don't clear the content of mockers, as they may still be |
| 779 | // needed by VerifyAndClearExpectationsLocked(). |
| 780 | } |
| 781 | |
zhanyong.wan | 7c95d83 | 2009-10-01 21:56:16 +0000 | [diff] [blame] | 782 | Expectation::Expectation() {} |
| 783 | |
| 784 | Expectation::Expectation( |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 785 | const internal::linked_ptr<internal::ExpectationBase>& an_expectation_base) |
| 786 | : expectation_base_(an_expectation_base) {} |
zhanyong.wan | 7c95d83 | 2009-10-01 21:56:16 +0000 | [diff] [blame] | 787 | |
| 788 | Expectation::~Expectation() {} |
| 789 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 790 | // Adds an expectation to a sequence. |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 791 | void Sequence::AddExpectation(const Expectation& expectation) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 792 | if (*last_expectation_ != expectation) { |
zhanyong.wan | 41b9b0b | 2009-07-01 19:04:51 +0000 | [diff] [blame] | 793 | if (last_expectation_->expectation_base() != NULL) { |
| 794 | expectation.expectation_base()->immediate_prerequisites_ |
| 795 | += *last_expectation_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 796 | } |
| 797 | *last_expectation_ = expectation; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | // Creates the implicit sequence if there isn't one. |
| 802 | InSequence::InSequence() { |
| 803 | if (internal::g_gmock_implicit_sequence.get() == NULL) { |
| 804 | internal::g_gmock_implicit_sequence.set(new Sequence); |
| 805 | sequence_created_ = true; |
| 806 | } else { |
| 807 | sequence_created_ = false; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | // Deletes the implicit sequence if it was created by the constructor |
| 812 | // of this object. |
| 813 | InSequence::~InSequence() { |
| 814 | if (sequence_created_) { |
| 815 | delete internal::g_gmock_implicit_sequence.get(); |
| 816 | internal::g_gmock_implicit_sequence.set(NULL); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | } // namespace testing |