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