1e35fdd936d133bf8a48de140a3c666897588a05shiqian// Copyright 2007, Google Inc. 2e35fdd936d133bf8a48de140a3c666897588a05shiqian// All rights reserved. 3e35fdd936d133bf8a48de140a3c666897588a05shiqian// 4e35fdd936d133bf8a48de140a3c666897588a05shiqian// Redistribution and use in source and binary forms, with or without 5e35fdd936d133bf8a48de140a3c666897588a05shiqian// modification, are permitted provided that the following conditions are 6e35fdd936d133bf8a48de140a3c666897588a05shiqian// met: 7e35fdd936d133bf8a48de140a3c666897588a05shiqian// 8e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions of source code must retain the above copyright 9e35fdd936d133bf8a48de140a3c666897588a05shiqian// notice, this list of conditions and the following disclaimer. 10e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Redistributions in binary form must reproduce the above 11e35fdd936d133bf8a48de140a3c666897588a05shiqian// copyright notice, this list of conditions and the following disclaimer 12e35fdd936d133bf8a48de140a3c666897588a05shiqian// in the documentation and/or other materials provided with the 13e35fdd936d133bf8a48de140a3c666897588a05shiqian// distribution. 14e35fdd936d133bf8a48de140a3c666897588a05shiqian// * Neither the name of Google Inc. nor the names of its 15e35fdd936d133bf8a48de140a3c666897588a05shiqian// contributors may be used to endorse or promote products derived from 16e35fdd936d133bf8a48de140a3c666897588a05shiqian// this software without specific prior written permission. 17e35fdd936d133bf8a48de140a3c666897588a05shiqian// 18e35fdd936d133bf8a48de140a3c666897588a05shiqian// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19e35fdd936d133bf8a48de140a3c666897588a05shiqian// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21e35fdd936d133bf8a48de140a3c666897588a05shiqian// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22e35fdd936d133bf8a48de140a3c666897588a05shiqian// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23e35fdd936d133bf8a48de140a3c666897588a05shiqian// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24e35fdd936d133bf8a48de140a3c666897588a05shiqian// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25e35fdd936d133bf8a48de140a3c666897588a05shiqian// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26e35fdd936d133bf8a48de140a3c666897588a05shiqian// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27e35fdd936d133bf8a48de140a3c666897588a05shiqian// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28e35fdd936d133bf8a48de140a3c666897588a05shiqian// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29e35fdd936d133bf8a48de140a3c666897588a05shiqian// 30e35fdd936d133bf8a48de140a3c666897588a05shiqian// Author: wan@google.com (Zhanyong Wan) 31e35fdd936d133bf8a48de140a3c666897588a05shiqian 32e35fdd936d133bf8a48de140a3c666897588a05shiqian// Google Mock - a framework for writing C++ mock classes. 33e35fdd936d133bf8a48de140a3c666897588a05shiqian// 34e35fdd936d133bf8a48de140a3c666897588a05shiqian// This file implements the spec builder syntax (ON_CALL and 35e35fdd936d133bf8a48de140a3c666897588a05shiqian// EXPECT_CALL). 36e35fdd936d133bf8a48de140a3c666897588a05shiqian 3753e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/gmock-spec-builders.h" 38e35fdd936d133bf8a48de140a3c666897588a05shiqian 39df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan#include <stdlib.h> 40df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan#include <iostream> // NOLINT 41df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan#include <map> 42e35fdd936d133bf8a48de140a3c666897588a05shiqian#include <set> 436c54a5e1f91e033fa937be5d647ce43dee597ad8vladlosev#include <string> 4453e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gmock/gmock.h" 4553e08c44dd34857ba57581d7c5774d6c96a8d0e1zhanyong.wan#include "gtest/gtest.h" 46e35fdd936d133bf8a48de140a3c666897588a05shiqian 47df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan#if GTEST_OS_CYGWIN || GTEST_OS_LINUX || GTEST_OS_MAC 48658ac0b71a350cc833ee4520536b6c4964c7b944zhanyong.wan# include <unistd.h> // NOLINT 49df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan#endif 50df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 51e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace testing { 52e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace internal { 53e35fdd936d133bf8a48de140a3c666897588a05shiqian 54e35fdd936d133bf8a48de140a3c666897588a05shiqian// Protects the mock object registry (in class Mock), all function 55e35fdd936d133bf8a48de140a3c666897588a05shiqian// mockers, and all expectations. 56587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_gmock_mutex); 57e35fdd936d133bf8a48de140a3c666897588a05shiqian 58ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Logs a message including file and line number information. 59587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, 60587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev const char* file, int line, 61587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosev const string& message) { 62ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ::std::ostringstream s; 63ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan s << file << ":" << line << ": " << message << ::std::endl; 64ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Log(severity, s.str(), 0); 65ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 66ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 67e35fdd936d133bf8a48de140a3c666897588a05shiqian// Constructs an ExpectationBase object. 6832de5f53763125925e078498250f7e73a88de9edzhanyong.wanExpectationBase::ExpectationBase(const char* a_file, 6932de5f53763125925e078498250f7e73a88de9edzhanyong.wan int a_line, 7032de5f53763125925e078498250f7e73a88de9edzhanyong.wan const string& a_source_text) 7132de5f53763125925e078498250f7e73a88de9edzhanyong.wan : file_(a_file), 7232de5f53763125925e078498250f7e73a88de9edzhanyong.wan line_(a_line), 7332de5f53763125925e078498250f7e73a88de9edzhanyong.wan source_text_(a_source_text), 74e35fdd936d133bf8a48de140a3c666897588a05shiqian cardinality_specified_(false), 75e35fdd936d133bf8a48de140a3c666897588a05shiqian cardinality_(Exactly(1)), 76e35fdd936d133bf8a48de140a3c666897588a05shiqian call_count_(0), 77ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan retired_(false), 78ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan extra_matcher_specified_(false), 79ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan repeated_action_specified_(false), 80ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan retires_on_saturation_(false), 81ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan last_clause_(kNone), 82ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan action_count_checked_(false) {} 83e35fdd936d133bf8a48de140a3c666897588a05shiqian 84e35fdd936d133bf8a48de140a3c666897588a05shiqian// Destructs an ExpectationBase object. 85e35fdd936d133bf8a48de140a3c666897588a05shiqianExpectationBase::~ExpectationBase() {} 86e35fdd936d133bf8a48de140a3c666897588a05shiqian 87e35fdd936d133bf8a48de140a3c666897588a05shiqian// Explicitly specifies the cardinality of this expectation. Used by 88e35fdd936d133bf8a48de140a3c666897588a05shiqian// the subclasses to implement the .Times() clause. 8932de5f53763125925e078498250f7e73a88de9edzhanyong.wanvoid ExpectationBase::SpecifyCardinality(const Cardinality& a_cardinality) { 90e35fdd936d133bf8a48de140a3c666897588a05shiqian cardinality_specified_ = true; 9132de5f53763125925e078498250f7e73a88de9edzhanyong.wan cardinality_ = a_cardinality; 92e35fdd936d133bf8a48de140a3c666897588a05shiqian} 93e35fdd936d133bf8a48de140a3c666897588a05shiqian 94e35fdd936d133bf8a48de140a3c666897588a05shiqian// Retires all pre-requisites of this expectation. 954d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid ExpectationBase::RetireAllPreRequisites() 964d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 97e35fdd936d133bf8a48de140a3c666897588a05shiqian if (is_retired()) { 98e35fdd936d133bf8a48de140a3c666897588a05shiqian // We can take this short-cut as we never retire an expectation 99e35fdd936d133bf8a48de140a3c666897588a05shiqian // until we have retired all its pre-requisites. 100e35fdd936d133bf8a48de140a3c666897588a05shiqian return; 101e35fdd936d133bf8a48de140a3c666897588a05shiqian } 102e35fdd936d133bf8a48de140a3c666897588a05shiqian 10341b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); 10441b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan it != immediate_prerequisites_.end(); ++it) { 10541b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan ExpectationBase* const prerequisite = it->expectation_base().get(); 106e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!prerequisite->is_retired()) { 107e35fdd936d133bf8a48de140a3c666897588a05shiqian prerequisite->RetireAllPreRequisites(); 108e35fdd936d133bf8a48de140a3c666897588a05shiqian prerequisite->Retire(); 109e35fdd936d133bf8a48de140a3c666897588a05shiqian } 110e35fdd936d133bf8a48de140a3c666897588a05shiqian } 111e35fdd936d133bf8a48de140a3c666897588a05shiqian} 112e35fdd936d133bf8a48de140a3c666897588a05shiqian 113e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns true iff all pre-requisites of this expectation have been 114e35fdd936d133bf8a48de140a3c666897588a05shiqian// satisfied. 1154d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevbool ExpectationBase::AllPrerequisitesAreSatisfied() const 1164d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 117e35fdd936d133bf8a48de140a3c666897588a05shiqian g_gmock_mutex.AssertHeld(); 11841b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); 119e35fdd936d133bf8a48de140a3c666897588a05shiqian it != immediate_prerequisites_.end(); ++it) { 12041b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan if (!(it->expectation_base()->IsSatisfied()) || 12141b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan !(it->expectation_base()->AllPrerequisitesAreSatisfied())) 122e35fdd936d133bf8a48de140a3c666897588a05shiqian return false; 123e35fdd936d133bf8a48de140a3c666897588a05shiqian } 124e35fdd936d133bf8a48de140a3c666897588a05shiqian return true; 125e35fdd936d133bf8a48de140a3c666897588a05shiqian} 126e35fdd936d133bf8a48de140a3c666897588a05shiqian 127e35fdd936d133bf8a48de140a3c666897588a05shiqian// Adds unsatisfied pre-requisites of this expectation to 'result'. 1284d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result) const 1294d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 130e35fdd936d133bf8a48de140a3c666897588a05shiqian g_gmock_mutex.AssertHeld(); 13141b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); 132e35fdd936d133bf8a48de140a3c666897588a05shiqian it != immediate_prerequisites_.end(); ++it) { 13341b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan if (it->expectation_base()->IsSatisfied()) { 134e35fdd936d133bf8a48de140a3c666897588a05shiqian // If *it is satisfied and has a call count of 0, some of its 135e35fdd936d133bf8a48de140a3c666897588a05shiqian // pre-requisites may not be satisfied yet. 13641b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan if (it->expectation_base()->call_count_ == 0) { 13741b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan it->expectation_base()->FindUnsatisfiedPrerequisites(result); 138e35fdd936d133bf8a48de140a3c666897588a05shiqian } 139e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 140e35fdd936d133bf8a48de140a3c666897588a05shiqian // Now that we know *it is unsatisfied, we are not so interested 141e35fdd936d133bf8a48de140a3c666897588a05shiqian // in whether its pre-requisites are satisfied. Therefore we 142e35fdd936d133bf8a48de140a3c666897588a05shiqian // don't recursively call FindUnsatisfiedPrerequisites() here. 14341b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan *result += *it; 144e35fdd936d133bf8a48de140a3c666897588a05shiqian } 145e35fdd936d133bf8a48de140a3c666897588a05shiqian } 146e35fdd936d133bf8a48de140a3c666897588a05shiqian} 147e35fdd936d133bf8a48de140a3c666897588a05shiqian 148ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Describes how many times a function call matching this 149ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// expectation has occurred. 1504d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid ExpectationBase::DescribeCallCountTo(::std::ostream* os) const 1514d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 152ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan g_gmock_mutex.AssertHeld(); 153ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 154ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Describes how many times the function is expected to be called. 155ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan *os << " Expected: to be "; 156ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan cardinality().DescribeTo(os); 157ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan *os << "\n Actual: "; 158ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Cardinality::DescribeActualCallCountTo(call_count(), os); 159ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 160ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Describes the state of the expectation (e.g. is it satisfied? 161ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // is it active?). 162ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan *os << " - " << (IsOverSaturated() ? "over-saturated" : 163ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan IsSaturated() ? "saturated" : 164ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan IsSatisfied() ? "satisfied" : "unsatisfied") 165ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan << " and " 166ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan << (is_retired() ? "retired" : "active"); 167ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 168ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 169ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Checks the action count (i.e. the number of WillOnce() and 170ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// WillRepeatedly() clauses) against the cardinality if this hasn't 171ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// been done before. Prints a warning if there are too many or too 172ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// few actions. 1734d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid ExpectationBase::CheckActionCountIfNotDone() const 1744d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(mutex_) { 175ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan bool should_check = false; 176ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan { 177ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan MutexLock l(&mutex_); 178ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (!action_count_checked_) { 179ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan action_count_checked_ = true; 180ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan should_check = true; 181ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 182ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 183ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 184ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (should_check) { 185ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (!cardinality_specified_) { 186ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // The cardinality was inferred - no need to check the action 187ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // count against it. 188ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return; 189ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 190ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 191ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // The cardinality was explicitly specified. 192ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const int action_count = static_cast<int>(untyped_actions_.size()); 193ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const int upper_bound = cardinality().ConservativeUpperBound(); 194ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const int lower_bound = cardinality().ConservativeLowerBound(); 195ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan bool too_many; // True if there are too many actions, or false 196ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // if there are too few. 197ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (action_count > upper_bound || 198ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan (action_count == upper_bound && repeated_action_specified_)) { 199ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan too_many = true; 200ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } else if (0 < action_count && action_count < lower_bound && 201ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan !repeated_action_specified_) { 202ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan too_many = false; 203ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } else { 204ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return; 205ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 206ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 207ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ::std::stringstream ss; 208ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan DescribeLocationTo(&ss); 209ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ss << "Too " << (too_many ? "many" : "few") 210ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan << " actions specified in " << source_text() << "...\n" 211ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan << "Expected to be "; 212ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan cardinality().DescribeTo(&ss); 213ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ss << ", but has " << (too_many ? "" : "only ") 214ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan << action_count << " WillOnce()" 215ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan << (action_count == 1 ? "" : "s"); 216ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (repeated_action_specified_) { 217ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ss << " and a WillRepeatedly()"; 218ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 219ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ss << "."; 2202fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan Log(kWarning, ss.str(), -1); // -1 means "don't print stack trace". 221ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 222ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 223ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 224ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Implements the .Times() clause. 225ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wanvoid ExpectationBase::UntypedTimes(const Cardinality& a_cardinality) { 226ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (last_clause_ == kTimes) { 227ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ExpectSpecProperty(false, 228ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ".Times() cannot appear " 229ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan "more than once in an EXPECT_CALL()."); 230ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } else { 231ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ExpectSpecProperty(last_clause_ < kTimes, 232ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ".Times() cannot appear after " 233ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ".InSequence(), .WillOnce(), .WillRepeatedly(), " 234ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan "or .RetiresOnSaturation()."); 235ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 236ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan last_clause_ = kTimes; 237ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 238ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan SpecifyCardinality(a_cardinality); 239ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 240ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 241e35fdd936d133bf8a48de140a3c666897588a05shiqian// Points to the implicit sequence introduced by a living InSequence 242e35fdd936d133bf8a48de140a3c666897588a05shiqian// object (if any) in the current thread or NULL. 243587c1b37c2f0b6d430fb13ce09326db0135b557cvladlosevGTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence; 244e35fdd936d133bf8a48de140a3c666897588a05shiqian 245e35fdd936d133bf8a48de140a3c666897588a05shiqian// Reports an uninteresting call (whose description is in msg) in the 246e35fdd936d133bf8a48de140a3c666897588a05shiqian// manner specified by 'reaction'. 247e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid ReportUninterestingCall(CallReaction reaction, const string& msg) { 2485625dd333a621932c469e2261fd9f0c8687378f8kosak // Include a stack trace only if --gmock_verbose=info is specified. 2495625dd333a621932c469e2261fd9f0c8687378f8kosak const int stack_frames_to_skip = 2505625dd333a621932c469e2261fd9f0c8687378f8kosak GMOCK_FLAG(verbose) == kInfoVerbosity ? 3 : -1; 251e35fdd936d133bf8a48de140a3c666897588a05shiqian switch (reaction) { 2522fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan case kAllow: 2535625dd333a621932c469e2261fd9f0c8687378f8kosak Log(kInfo, msg, stack_frames_to_skip); 254e35fdd936d133bf8a48de140a3c666897588a05shiqian break; 2552fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan case kWarn: 25604ce8521f481d857db1aa00a206278759b9b0381kosak Log(kWarning, 25704ce8521f481d857db1aa00a206278759b9b0381kosak msg + 25804ce8521f481d857db1aa00a206278759b9b0381kosak "\nNOTE: You can safely ignore the above warning unless this " 25904ce8521f481d857db1aa00a206278759b9b0381kosak "call should not happen. Do not suppress it by blindly adding " 26004ce8521f481d857db1aa00a206278759b9b0381kosak "an EXPECT_CALL() if you don't mean to enforce the call. " 26104ce8521f481d857db1aa00a206278759b9b0381kosak "See http://code.google.com/p/googlemock/wiki/CookBook#" 2625625dd333a621932c469e2261fd9f0c8687378f8kosak "Knowing_When_to_Expect for details.\n", 2635625dd333a621932c469e2261fd9f0c8687378f8kosak stack_frames_to_skip); 264e35fdd936d133bf8a48de140a3c666897588a05shiqian break; 265e35fdd936d133bf8a48de140a3c666897588a05shiqian default: // FAIL 266e35fdd936d133bf8a48de140a3c666897588a05shiqian Expect(false, NULL, -1, msg); 267e35fdd936d133bf8a48de140a3c666897588a05shiqian } 268e35fdd936d133bf8a48de140a3c666897588a05shiqian} 269e35fdd936d133bf8a48de140a3c666897588a05shiqian 270ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wanUntypedFunctionMockerBase::UntypedFunctionMockerBase() 271ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan : mock_obj_(NULL), name_("") {} 272ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 273ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wanUntypedFunctionMockerBase::~UntypedFunctionMockerBase() {} 274ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 275ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Sets the mock object this mock method belongs to, and registers 276ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// this information in the global mock registry. Will be called 277ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// whenever an EXPECT_CALL() or ON_CALL() is executed on this mock 278ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// method. 2794d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj) 2804d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 281ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan { 282ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan MutexLock l(&g_gmock_mutex); 283ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan mock_obj_ = mock_obj; 284ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 285ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Mock::Register(mock_obj, this); 286ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 287ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 288ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Sets the mock object this mock method belongs to, and sets the name 289ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// of the mock function. Will be called upon each invocation of this 290ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// mock function. 2914d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj, 2924d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev const char* name) 2934d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 294ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // We protect name_ under g_gmock_mutex in case this mock function 295ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // is called from two threads concurrently. 296ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan MutexLock l(&g_gmock_mutex); 297ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan mock_obj_ = mock_obj; 298ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan name_ = name; 299ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 300ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 301ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Returns the name of the function being mocked. Must be called 302ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// after RegisterOwner() or SetOwnerAndName() has been called. 3034d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevconst void* UntypedFunctionMockerBase::MockObject() const 3044d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 305ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const void* mock_obj; 306ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan { 307ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // We protect mock_obj_ under g_gmock_mutex in case this mock 308ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // function is called from two threads concurrently. 309ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan MutexLock l(&g_gmock_mutex); 310ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Assert(mock_obj_ != NULL, __FILE__, __LINE__, 311ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan "MockObject() must not be called before RegisterOwner() or " 312ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan "SetOwnerAndName() has been called."); 313ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan mock_obj = mock_obj_; 314ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 315ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return mock_obj; 316ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 317ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 318ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Returns the name of this mock method. Must be called after 319ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// SetOwnerAndName() has been called. 3204d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevconst char* UntypedFunctionMockerBase::Name() const 3214d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 322ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const char* name; 323ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan { 324ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // We protect name_ under g_gmock_mutex in case this mock 325ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // function is called from two threads concurrently. 326ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan MutexLock l(&g_gmock_mutex); 327ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Assert(name_ != NULL, __FILE__, __LINE__, 328ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan "Name() must not be called before SetOwnerAndName() has " 329ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan "been called."); 330ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan name = name_; 331ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 332ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return name; 333ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 334ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 335ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Calculates the result of invoking this mock function with the given 336ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// arguments, prints it, and returns it. The caller is responsible 337ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// for deleting the result. 338b5c81098a8ccc25e313ffca56c911200b3591ea0kosakUntypedActionResultHolderBase* 3394d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevUntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args) 3404d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { 341ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (untyped_expectations_.size() == 0) { 342ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // No expectation is set on this mock method - we have an 343ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // uninteresting call. 344ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 345ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // We must get Google Mock's reaction on uninteresting calls 346ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // made on this mock object BEFORE performing the action, 347ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // because the action may DELETE the mock object and make the 348ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // following expression meaningless. 349ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const CallReaction reaction = 350ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Mock::GetReactionOnUninterestingCalls(MockObject()); 351ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 352ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // True iff we need to print this call's arguments and return 353ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // value. This definition must be kept in sync with 354ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // the behavior of ReportUninterestingCall(). 355ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const bool need_to_report_uninteresting_call = 356ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // If the user allows this uninteresting call, we print it 357ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // only when he wants informational messages. 3582fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan reaction == kAllow ? LogIsVisible(kInfo) : 359ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // If the user wants this to be a warning, we print it only 360ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // when he wants to see warnings. 3612fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan reaction == kWarn ? LogIsVisible(kWarning) : 362ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Otherwise, the user wants this to be an error, and we 363ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // should always print detailed information in the error. 364ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan true; 365ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 366ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (!need_to_report_uninteresting_call) { 367ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Perform the action without printing the call information. 368ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return this->UntypedPerformDefaultAction(untyped_args, ""); 369ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 370ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 371ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Warns about the uninteresting call. 372ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ::std::stringstream ss; 373ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedDescribeUninterestingCall(untyped_args, &ss); 374ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 375ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Calculates the function result. 376b5c81098a8ccc25e313ffca56c911200b3591ea0kosak UntypedActionResultHolderBase* const result = 377ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedPerformDefaultAction(untyped_args, ss.str()); 378ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 379ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Prints the function result. 380ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (result != NULL) 381ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan result->PrintAsActionResult(&ss); 382ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 383ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ReportUninterestingCall(reaction, ss.str()); 384ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return result; 385ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 386ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 387ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan bool is_excessive = false; 388ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ::std::stringstream ss; 389ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ::std::stringstream why; 390ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ::std::stringstream loc; 391ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const void* untyped_action = NULL; 392ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 393ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // The UntypedFindMatchingExpectation() function acquires and 394ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // releases g_gmock_mutex. 395ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const ExpectationBase* const untyped_expectation = 396ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedFindMatchingExpectation( 397ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_args, &untyped_action, &is_excessive, 398ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan &ss, &why); 399ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan const bool found = untyped_expectation != NULL; 400ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 401ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // True iff we need to print the call's arguments and return value. 402ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // This definition must be kept in sync with the uses of Expect() 403ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // and Log() in this function. 4042fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan const bool need_to_report_call = 4052fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan !found || is_excessive || LogIsVisible(kInfo); 406ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (!need_to_report_call) { 407ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // Perform the action without printing the call information. 408ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return 409ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_action == NULL ? 410ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedPerformDefaultAction(untyped_args, "") : 411ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedPerformAction(untyped_action, untyped_args); 412ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 413ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 414ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ss << " Function call: " << Name(); 415ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedPrintArgs(untyped_args, &ss); 416ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 417ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // In case the action deletes a piece of the expectation, we 418ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // generate the message beforehand. 419ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (found && !is_excessive) { 420ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_expectation->DescribeLocationTo(&loc); 421ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 422ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 423b5c81098a8ccc25e313ffca56c911200b3591ea0kosak UntypedActionResultHolderBase* const result = 424ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_action == NULL ? 425ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedPerformDefaultAction(untyped_args, ss.str()) : 426ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan this->UntypedPerformAction(untyped_action, untyped_args); 427ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (result != NULL) 428ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan result->PrintAsActionResult(&ss); 429ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ss << "\n" << why.str(); 430ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 431ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (!found) { 432ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // No expectation matches this call - reports a failure. 433ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Expect(false, NULL, -1, ss.str()); 434ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } else if (is_excessive) { 435ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // We had an upper-bound violation and the failure message is in ss. 436ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Expect(false, untyped_expectation->file(), 437ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_expectation->line(), ss.str()); 438ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } else { 439ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // We had an expected call and the matching expectation is 440ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // described in ss. 4412fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan Log(kInfo, loc.str() + ss.str(), 2); 442ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 443ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 444ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return result; 445ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 446ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 447ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Returns an Expectation object that references and co-owns exp, 448ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// which must be an expectation on this mock function. 449ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wanExpectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { 450ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan for (UntypedExpectations::const_iterator it = 451ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_expectations_.begin(); 452ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan it != untyped_expectations_.end(); ++it) { 453ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (it->get() == exp) { 454ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return Expectation(*it); 455ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 456ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 457ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 458ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Assert(false, __FILE__, __LINE__, "Cannot find expectation."); 459ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return Expectation(); 460ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // The above statement is just to make the code compile, and will 461ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // never be executed. 462ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 463ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 464ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// Verifies that all expectations on this mock function have been 465ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// satisfied. Reports one or more Google Test non-fatal failures 466ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan// and returns false if not. 4674d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevbool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() 4684d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { 469ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan g_gmock_mutex.AssertHeld(); 470ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan bool expectations_met = true; 471ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan for (UntypedExpectations::const_iterator it = 472ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_expectations_.begin(); 473ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan it != untyped_expectations_.end(); ++it) { 474ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ExpectationBase* const untyped_expectation = it->get(); 475ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan if (untyped_expectation->IsOverSaturated()) { 476ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // There was an upper-bound violation. Since the error was 477ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // already reported when it occurred, there is no need to do 478ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // anything here. 479ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan expectations_met = false; 480ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } else if (!untyped_expectation->IsSatisfied()) { 481ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan expectations_met = false; 482ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ::std::stringstream ss; 483ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan ss << "Actual function call count doesn't match " 484ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan << untyped_expectation->source_text() << "...\n"; 485ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // No need to show the source file location of the expectation 486ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // in the description, as the Expect() call that follows already 487ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan // takes care of it. 488ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_expectation->MaybeDescribeExtraMatcherTo(&ss); 489ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_expectation->DescribeCallCountTo(&ss); 490ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan Expect(false, untyped_expectation->file(), 491ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan untyped_expectation->line(), ss.str()); 492ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 493ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan } 4949bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev 4959bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev // Deleting our expectations may trigger other mock objects to be deleted, for 4969bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev // example if an action contains a reference counted smart pointer to that 4979bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev // mock object, and that is the last reference. So if we delete our 4989bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev // expectations within the context of the global mutex we may deadlock when 4999bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev // this method is called again. Instead, make a copy of the set of 5009bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev // expectations to delete, clear our set within the mutex, and then clear the 5019bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev // copied set outside of it. 5029bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev UntypedExpectations expectations_to_delete; 5039bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev untyped_expectations_.swap(expectations_to_delete); 5049bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev 5059bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev g_gmock_mutex.Unlock(); 5069bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev expectations_to_delete.clear(); 5079bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev g_gmock_mutex.Lock(); 5089bcb5f9146db42bc38b6bb744fb0cf518a0205bevladlosev 509ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan return expectations_met; 510ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan} 511ed6c9277bb12f2808bb812ae8f91492dac9517b4zhanyong.wan 512e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace internal 513e35fdd936d133bf8a48de140a3c666897588a05shiqian 514e35fdd936d133bf8a48de140a3c666897588a05shiqian// Class Mock. 515e35fdd936d133bf8a48de140a3c666897588a05shiqian 516e35fdd936d133bf8a48de140a3c666897588a05shiqiannamespace { 517e35fdd936d133bf8a48de140a3c666897588a05shiqian 518e35fdd936d133bf8a48de140a3c666897588a05shiqiantypedef std::set<internal::UntypedFunctionMockerBase*> FunctionMockers; 519e35fdd936d133bf8a48de140a3c666897588a05shiqian 520df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// The current state of a mock object. Such information is needed for 521df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// detecting leaked mock objects and explicitly verifying a mock's 522df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// expectations. 523df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wanstruct MockObjectState { 524df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan MockObjectState() 525df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan : first_used_file(NULL), first_used_line(-1), leakable(false) {} 526df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 527df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // Where in the source file an ON_CALL or EXPECT_CALL is first 528df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // invoked on this mock object. 529df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan const char* first_used_file; 530df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan int first_used_line; 531e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan ::std::string first_used_test_case; 532e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan ::std::string first_used_test; 533df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan bool leakable; // true iff it's OK to leak the object. 534df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan FunctionMockers function_mockers; // All registered methods of the object. 535df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan}; 536df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 537df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// A global registry holding the state of all mock objects that are 538df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// alive. A mock object is added to this registry the first time 539df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// Mock::AllowLeak(), ON_CALL(), or EXPECT_CALL() is called on it. It 540df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// is removed from the registry in the mock object's destructor. 541df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wanclass MockObjectRegistry { 542df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan public: 543df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // Maps a mock object (identified by its address) to its state. 544df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan typedef std::map<const void*, MockObjectState> StateMap; 545df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 546df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // This destructor will be called when a program exits, after all 547df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // tests in it have been run. By then, there should be no mock 548df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // object alive. Therefore we report any living object as test 549df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // failure, unless the user explicitly asked us to ignore it. 550df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan ~MockObjectRegistry() { 5519571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is 5529571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan // a macro. 553df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 554df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan if (!GMOCK_FLAG(catch_leaked_mocks)) 555df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan return; 556df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 557df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan int leaked_count = 0; 558df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan for (StateMap::const_iterator it = states_.begin(); it != states_.end(); 559df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan ++it) { 560df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan if (it->second.leakable) // The user said it's fine to leak this object. 561df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan continue; 562df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 563df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // TODO(wan@google.com): Print the type of the leaked object. 564df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // This can help the user identify the leaked object. 5659571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan std::cout << "\n"; 566df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan const MockObjectState& state = it->second; 567f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan std::cout << internal::FormatFileLocation(state.first_used_file, 568f5e1ce5b9237edbc2e524ae9ebcb2452dc842937zhanyong.wan state.first_used_line); 5699571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan std::cout << " ERROR: this mock object"; 570e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan if (state.first_used_test != "") { 5719571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan std::cout << " (used in test " << state.first_used_test_case << "." 572e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan << state.first_used_test << ")"; 573e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan } 5749571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan std::cout << " should be deleted but never is. Its address is @" 575e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan << it->first << "."; 576df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan leaked_count++; 577df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan } 578df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan if (leaked_count > 0) { 5799571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan std::cout << "\nERROR: " << leaked_count 580df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan << " leaked mock " << (leaked_count == 1 ? "object" : "objects") 581df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan << " found at program exit.\n"; 5829571b28675a5a602ed3c8a7acf270d03aca69c96zhanyong.wan std::cout.flush(); 583df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan ::std::cerr.flush(); 584df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // RUN_ALL_TESTS() has already returned when this destructor is 585df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // called. Therefore we cannot use the normal Google Test 586df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // failure reporting mechanism. 587df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan _exit(1); // We cannot call exit() as it is not reentrant and 588df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan // may already have been called. 589df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan } 590df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan } 591df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 592df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan StateMap& states() { return states_; } 59379a367eb217fcd47e2beaf8c0f87fe6d5926f739jgm 594df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan private: 595df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan StateMap states_; 596df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan}; 597df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 598df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// Protected by g_gmock_mutex. 599e35fdd936d133bf8a48de140a3c666897588a05shiqianMockObjectRegistry g_mock_object_registry; 600e35fdd936d133bf8a48de140a3c666897588a05shiqian 601e35fdd936d133bf8a48de140a3c666897588a05shiqian// Maps a mock object to the reaction Google Mock should have when an 602e35fdd936d133bf8a48de140a3c666897588a05shiqian// uninteresting method is called. Protected by g_gmock_mutex. 603e35fdd936d133bf8a48de140a3c666897588a05shiqianstd::map<const void*, internal::CallReaction> g_uninteresting_call_reaction; 604e35fdd936d133bf8a48de140a3c666897588a05shiqian 605e35fdd936d133bf8a48de140a3c666897588a05shiqian// Sets the reaction Google Mock should have when an uninteresting 606e35fdd936d133bf8a48de140a3c666897588a05shiqian// method of the given mock object is called. 607e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid SetReactionOnUninterestingCalls(const void* mock_obj, 6084d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev internal::CallReaction reaction) 6094d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 610e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::MutexLock l(&internal::g_gmock_mutex); 611e35fdd936d133bf8a48de140a3c666897588a05shiqian g_uninteresting_call_reaction[mock_obj] = reaction; 612e35fdd936d133bf8a48de140a3c666897588a05shiqian} 613e35fdd936d133bf8a48de140a3c666897588a05shiqian 614e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace 615e35fdd936d133bf8a48de140a3c666897588a05shiqian 616e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tells Google Mock to allow uninteresting calls on the given mock 617e35fdd936d133bf8a48de140a3c666897588a05shiqian// object. 6184d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::AllowUninterestingCalls(const void* mock_obj) 6194d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 6202fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan SetReactionOnUninterestingCalls(mock_obj, internal::kAllow); 621e35fdd936d133bf8a48de140a3c666897588a05shiqian} 622e35fdd936d133bf8a48de140a3c666897588a05shiqian 623e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tells Google Mock to warn the user about uninteresting calls on the 624e35fdd936d133bf8a48de140a3c666897588a05shiqian// given mock object. 6254d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::WarnUninterestingCalls(const void* mock_obj) 6264d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 6272fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan SetReactionOnUninterestingCalls(mock_obj, internal::kWarn); 628e35fdd936d133bf8a48de140a3c666897588a05shiqian} 629e35fdd936d133bf8a48de140a3c666897588a05shiqian 630e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tells Google Mock to fail uninteresting calls on the given mock 631e35fdd936d133bf8a48de140a3c666897588a05shiqian// object. 6324d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::FailUninterestingCalls(const void* mock_obj) 6334d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 6342fd619edd3d1ec053f6276debdb513f1122ebcf3zhanyong.wan SetReactionOnUninterestingCalls(mock_obj, internal::kFail); 635e35fdd936d133bf8a48de140a3c666897588a05shiqian} 636e35fdd936d133bf8a48de140a3c666897588a05shiqian 637e35fdd936d133bf8a48de140a3c666897588a05shiqian// Tells Google Mock the given mock object is being destroyed and its 638e35fdd936d133bf8a48de140a3c666897588a05shiqian// entry in the call-reaction table should be removed. 6394d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::UnregisterCallReaction(const void* mock_obj) 6404d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 641e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::MutexLock l(&internal::g_gmock_mutex); 642e35fdd936d133bf8a48de140a3c666897588a05shiqian g_uninteresting_call_reaction.erase(mock_obj); 643e35fdd936d133bf8a48de140a3c666897588a05shiqian} 644e35fdd936d133bf8a48de140a3c666897588a05shiqian 645e35fdd936d133bf8a48de140a3c666897588a05shiqian// Returns the reaction Google Mock will have on uninteresting calls 646e35fdd936d133bf8a48de140a3c666897588a05shiqian// made on the given mock object. 647e35fdd936d133bf8a48de140a3c666897588a05shiqianinternal::CallReaction Mock::GetReactionOnUninterestingCalls( 6484d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev const void* mock_obj) 6494d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 650e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::MutexLock l(&internal::g_gmock_mutex); 651e35fdd936d133bf8a48de140a3c666897588a05shiqian return (g_uninteresting_call_reaction.count(mock_obj) == 0) ? 652c896504e4175f08cd229ea151861558ba9380f50zhanyong.wan internal::kDefault : g_uninteresting_call_reaction[mock_obj]; 653e35fdd936d133bf8a48de140a3c666897588a05shiqian} 654e35fdd936d133bf8a48de140a3c666897588a05shiqian 655df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// Tells Google Mock to ignore mock_obj when checking for leaked mock 656df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// objects. 6574d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::AllowLeak(const void* mock_obj) 6584d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 659df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan internal::MutexLock l(&internal::g_gmock_mutex); 660df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan g_mock_object_registry.states()[mock_obj].leakable = true; 661df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan} 662df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 663e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies and clears all expectations on the given mock object. If 664e35fdd936d133bf8a48de140a3c666897588a05shiqian// the expectations aren't satisfied, generates one or more Google 665e35fdd936d133bf8a48de140a3c666897588a05shiqian// Test non-fatal failures and returns false. 6664d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevbool Mock::VerifyAndClearExpectations(void* mock_obj) 6674d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 668e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::MutexLock l(&internal::g_gmock_mutex); 669e35fdd936d133bf8a48de140a3c666897588a05shiqian return VerifyAndClearExpectationsLocked(mock_obj); 670e35fdd936d133bf8a48de140a3c666897588a05shiqian} 671e35fdd936d133bf8a48de140a3c666897588a05shiqian 672e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies all expectations on the given mock object and clears its 673e35fdd936d133bf8a48de140a3c666897588a05shiqian// default actions and expectations. Returns true iff the 674e35fdd936d133bf8a48de140a3c666897588a05shiqian// verification was successful. 6754d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevbool Mock::VerifyAndClear(void* mock_obj) 6764d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 677e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::MutexLock l(&internal::g_gmock_mutex); 678e35fdd936d133bf8a48de140a3c666897588a05shiqian ClearDefaultActionsLocked(mock_obj); 679e35fdd936d133bf8a48de140a3c666897588a05shiqian return VerifyAndClearExpectationsLocked(mock_obj); 680e35fdd936d133bf8a48de140a3c666897588a05shiqian} 681e35fdd936d133bf8a48de140a3c666897588a05shiqian 682e35fdd936d133bf8a48de140a3c666897588a05shiqian// Verifies and clears all expectations on the given mock object. If 683e35fdd936d133bf8a48de140a3c666897588a05shiqian// the expectations aren't satisfied, generates one or more Google 684e35fdd936d133bf8a48de140a3c666897588a05shiqian// Test non-fatal failures and returns false. 6854d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevbool Mock::VerifyAndClearExpectationsLocked(void* mock_obj) 6864d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { 687e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::g_gmock_mutex.AssertHeld(); 688df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan if (g_mock_object_registry.states().count(mock_obj) == 0) { 689e35fdd936d133bf8a48de140a3c666897588a05shiqian // No EXPECT_CALL() was set on the given mock object. 690e35fdd936d133bf8a48de140a3c666897588a05shiqian return true; 691e35fdd936d133bf8a48de140a3c666897588a05shiqian } 692e35fdd936d133bf8a48de140a3c666897588a05shiqian 693e35fdd936d133bf8a48de140a3c666897588a05shiqian // Verifies and clears the expectations on each mock method in the 694e35fdd936d133bf8a48de140a3c666897588a05shiqian // given mock object. 695e35fdd936d133bf8a48de140a3c666897588a05shiqian bool expectations_met = true; 696df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan FunctionMockers& mockers = 697df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan g_mock_object_registry.states()[mock_obj].function_mockers; 698e35fdd936d133bf8a48de140a3c666897588a05shiqian for (FunctionMockers::const_iterator it = mockers.begin(); 699e35fdd936d133bf8a48de140a3c666897588a05shiqian it != mockers.end(); ++it) { 700e35fdd936d133bf8a48de140a3c666897588a05shiqian if (!(*it)->VerifyAndClearExpectationsLocked()) { 701e35fdd936d133bf8a48de140a3c666897588a05shiqian expectations_met = false; 702e35fdd936d133bf8a48de140a3c666897588a05shiqian } 703e35fdd936d133bf8a48de140a3c666897588a05shiqian } 704e35fdd936d133bf8a48de140a3c666897588a05shiqian 705e35fdd936d133bf8a48de140a3c666897588a05shiqian // We don't clear the content of mockers, as they may still be 706e35fdd936d133bf8a48de140a3c666897588a05shiqian // needed by ClearDefaultActionsLocked(). 707e35fdd936d133bf8a48de140a3c666897588a05shiqian return expectations_met; 708e35fdd936d133bf8a48de140a3c666897588a05shiqian} 709e35fdd936d133bf8a48de140a3c666897588a05shiqian 710e35fdd936d133bf8a48de140a3c666897588a05shiqian// Registers a mock object and a mock method it owns. 711e35fdd936d133bf8a48de140a3c666897588a05shiqianvoid Mock::Register(const void* mock_obj, 7124d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev internal::UntypedFunctionMockerBase* mocker) 7134d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 714e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::MutexLock l(&internal::g_gmock_mutex); 715df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan g_mock_object_registry.states()[mock_obj].function_mockers.insert(mocker); 716df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan} 717df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan 718df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// Tells Google Mock where in the source code mock_obj is used in an 719df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this 720df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan// information helps the user identify which object it is. 7214d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj, 7224d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev const char* file, int line) 7234d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { 724df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan internal::MutexLock l(&internal::g_gmock_mutex); 725df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan MockObjectState& state = g_mock_object_registry.states()[mock_obj]; 726df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan if (state.first_used_file == NULL) { 727df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan state.first_used_file = file; 728df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan state.first_used_line = line; 729e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan const TestInfo* const test_info = 730e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan UnitTest::GetInstance()->current_test_info(); 731e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan if (test_info != NULL) { 732e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan // TODO(wan@google.com): record the test case name when the 733e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan // ON_CALL or EXPECT_CALL is invoked from SetUpTestCase() or 734e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan // TearDownTestCase(). 735e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan state.first_used_test_case = test_info->test_case_name(); 736e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan state.first_used_test = test_info->name(); 737e7bb5ededa4df6ec430c1e84154bc01bf84d4ecczhanyong.wan } 738df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan } 739e35fdd936d133bf8a48de140a3c666897588a05shiqian} 740e35fdd936d133bf8a48de140a3c666897588a05shiqian 741e35fdd936d133bf8a48de140a3c666897588a05shiqian// Unregisters a mock method; removes the owning mock object from the 742e35fdd936d133bf8a48de140a3c666897588a05shiqian// registry when the last mock method associated with it has been 743e35fdd936d133bf8a48de140a3c666897588a05shiqian// unregistered. This is called only in the destructor of 744e35fdd936d133bf8a48de140a3c666897588a05shiqian// FunctionMockerBase. 7454d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker) 7464d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { 747e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::g_gmock_mutex.AssertHeld(); 748df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan for (MockObjectRegistry::StateMap::iterator it = 749df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan g_mock_object_registry.states().begin(); 750df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan it != g_mock_object_registry.states().end(); ++it) { 751df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan FunctionMockers& mockers = it->second.function_mockers; 752e35fdd936d133bf8a48de140a3c666897588a05shiqian if (mockers.erase(mocker) > 0) { 753e35fdd936d133bf8a48de140a3c666897588a05shiqian // mocker was in mockers and has been just removed. 754e35fdd936d133bf8a48de140a3c666897588a05shiqian if (mockers.empty()) { 755df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan g_mock_object_registry.states().erase(it); 756e35fdd936d133bf8a48de140a3c666897588a05shiqian } 757e35fdd936d133bf8a48de140a3c666897588a05shiqian return; 758e35fdd936d133bf8a48de140a3c666897588a05shiqian } 759e35fdd936d133bf8a48de140a3c666897588a05shiqian } 760e35fdd936d133bf8a48de140a3c666897588a05shiqian} 761e35fdd936d133bf8a48de140a3c666897588a05shiqian 762e35fdd936d133bf8a48de140a3c666897588a05shiqian// Clears all ON_CALL()s set on the given mock object. 7634d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosevvoid Mock::ClearDefaultActionsLocked(void* mock_obj) 7644d60a596b4135c5a7e21ef7b4fe24a5c90329e0fvladlosev GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { 765e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::g_gmock_mutex.AssertHeld(); 766e35fdd936d133bf8a48de140a3c666897588a05shiqian 767df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan if (g_mock_object_registry.states().count(mock_obj) == 0) { 768e35fdd936d133bf8a48de140a3c666897588a05shiqian // No ON_CALL() was set on the given mock object. 769e35fdd936d133bf8a48de140a3c666897588a05shiqian return; 770e35fdd936d133bf8a48de140a3c666897588a05shiqian } 771e35fdd936d133bf8a48de140a3c666897588a05shiqian 772e35fdd936d133bf8a48de140a3c666897588a05shiqian // Clears the default actions for each mock method in the given mock 773e35fdd936d133bf8a48de140a3c666897588a05shiqian // object. 774df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan FunctionMockers& mockers = 775df35a763b9d98d7040a00fc1e5cffe91a80ba9e0zhanyong.wan g_mock_object_registry.states()[mock_obj].function_mockers; 776e35fdd936d133bf8a48de140a3c666897588a05shiqian for (FunctionMockers::const_iterator it = mockers.begin(); 777e35fdd936d133bf8a48de140a3c666897588a05shiqian it != mockers.end(); ++it) { 778e35fdd936d133bf8a48de140a3c666897588a05shiqian (*it)->ClearDefaultActionsLocked(); 779e35fdd936d133bf8a48de140a3c666897588a05shiqian } 780e35fdd936d133bf8a48de140a3c666897588a05shiqian 781e35fdd936d133bf8a48de140a3c666897588a05shiqian // We don't clear the content of mockers, as they may still be 782e35fdd936d133bf8a48de140a3c666897588a05shiqian // needed by VerifyAndClearExpectationsLocked(). 783e35fdd936d133bf8a48de140a3c666897588a05shiqian} 784e35fdd936d133bf8a48de140a3c666897588a05shiqian 7857c95d8346e9c62e9d6d7e8202fbcfb34c4404db9zhanyong.wanExpectation::Expectation() {} 7867c95d8346e9c62e9d6d7e8202fbcfb34c4404db9zhanyong.wan 7877c95d8346e9c62e9d6d7e8202fbcfb34c4404db9zhanyong.wanExpectation::Expectation( 78832de5f53763125925e078498250f7e73a88de9edzhanyong.wan const internal::linked_ptr<internal::ExpectationBase>& an_expectation_base) 78932de5f53763125925e078498250f7e73a88de9edzhanyong.wan : expectation_base_(an_expectation_base) {} 7907c95d8346e9c62e9d6d7e8202fbcfb34c4404db9zhanyong.wan 7917c95d8346e9c62e9d6d7e8202fbcfb34c4404db9zhanyong.wanExpectation::~Expectation() {} 7927c95d8346e9c62e9d6d7e8202fbcfb34c4404db9zhanyong.wan 793e35fdd936d133bf8a48de140a3c666897588a05shiqian// Adds an expectation to a sequence. 79441b9b0b5614588d252d565646ae43e9607d46502zhanyong.wanvoid Sequence::AddExpectation(const Expectation& expectation) const { 795e35fdd936d133bf8a48de140a3c666897588a05shiqian if (*last_expectation_ != expectation) { 79641b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan if (last_expectation_->expectation_base() != NULL) { 79741b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan expectation.expectation_base()->immediate_prerequisites_ 79841b9b0b5614588d252d565646ae43e9607d46502zhanyong.wan += *last_expectation_; 799e35fdd936d133bf8a48de140a3c666897588a05shiqian } 800e35fdd936d133bf8a48de140a3c666897588a05shiqian *last_expectation_ = expectation; 801e35fdd936d133bf8a48de140a3c666897588a05shiqian } 802e35fdd936d133bf8a48de140a3c666897588a05shiqian} 803e35fdd936d133bf8a48de140a3c666897588a05shiqian 804e35fdd936d133bf8a48de140a3c666897588a05shiqian// Creates the implicit sequence if there isn't one. 805e35fdd936d133bf8a48de140a3c666897588a05shiqianInSequence::InSequence() { 806e35fdd936d133bf8a48de140a3c666897588a05shiqian if (internal::g_gmock_implicit_sequence.get() == NULL) { 807e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::g_gmock_implicit_sequence.set(new Sequence); 808e35fdd936d133bf8a48de140a3c666897588a05shiqian sequence_created_ = true; 809e35fdd936d133bf8a48de140a3c666897588a05shiqian } else { 810e35fdd936d133bf8a48de140a3c666897588a05shiqian sequence_created_ = false; 811e35fdd936d133bf8a48de140a3c666897588a05shiqian } 812e35fdd936d133bf8a48de140a3c666897588a05shiqian} 813e35fdd936d133bf8a48de140a3c666897588a05shiqian 814e35fdd936d133bf8a48de140a3c666897588a05shiqian// Deletes the implicit sequence if it was created by the constructor 815e35fdd936d133bf8a48de140a3c666897588a05shiqian// of this object. 816e35fdd936d133bf8a48de140a3c666897588a05shiqianInSequence::~InSequence() { 817e35fdd936d133bf8a48de140a3c666897588a05shiqian if (sequence_created_) { 818e35fdd936d133bf8a48de140a3c666897588a05shiqian delete internal::g_gmock_implicit_sequence.get(); 819e35fdd936d133bf8a48de140a3c666897588a05shiqian internal::g_gmock_implicit_sequence.set(NULL); 820e35fdd936d133bf8a48de140a3c666897588a05shiqian } 821e35fdd936d133bf8a48de140a3c666897588a05shiqian} 822e35fdd936d133bf8a48de140a3c666897588a05shiqian 823e35fdd936d133bf8a48de140a3c666897588a05shiqian} // namespace testing 824