// Copyright 2007, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Google Mock - a framework for writing C++ mock classes. // // This file implements the spec builder syntax (ON_CALL and // EXPECT_CALL). #include "gmock/gmock-spec-builders.h" #include <stdlib.h> #include <iostream> // NOLINT #include <map> #include <memory> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <vector> #include "gmock/gmock.h" #include "gtest/gtest.h" #include "gtest/internal/gtest-port.h" #if defined(GTEST_OS_CYGWIN) || defined(GTEST_OS_LINUX) || defined(GTEST_OS_MAC) #include <unistd.h> // NOLINT #endif #ifdef GTEST_OS_QURT #include <qurt_event.h> #endif // Silence C4800 (C4800: 'int *const ': forcing value // to bool 'true' or 'false') for MSVC 15 #if defined(_MSC_VER) && (_MSC_VER == 1900) GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800) #endif namespace testing { namespace internal { // Protects the mock object registry (in class Mock), all function // mockers, and all expectations. GTEST_API_ GTEST_DEFINE_STATIC_MUTEX_(g_gmock_mutex); // Logs a message including file and line number information. GTEST_API_ void LogWithLocation(testing::internal::LogSeverity severity, const char* file, int line, const std::string& message) { … } // Constructs an ExpectationBase object. ExpectationBase::ExpectationBase(const char* a_file, int a_line, const std::string& a_source_text) : … { … } // Destructs an ExpectationBase object. ExpectationBase::~ExpectationBase() = default; // Explicitly specifies the cardinality of this expectation. Used by // the subclasses to implement the .Times() clause. void ExpectationBase::SpecifyCardinality(const Cardinality& a_cardinality) { … } // Retires all pre-requisites of this expectation. void ExpectationBase::RetireAllPreRequisites() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { … } // Returns true if and only if all pre-requisites of this expectation // have been satisfied. bool ExpectationBase::AllPrerequisitesAreSatisfied() const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { … } // Adds unsatisfied pre-requisites of this expectation to 'result'. void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { … } // Describes how many times a function call matching this // expectation has occurred. void ExpectationBase::DescribeCallCountTo(::std::ostream* os) const GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { … } // Checks the action count (i.e. the number of WillOnce() and // WillRepeatedly() clauses) against the cardinality if this hasn't // been done before. Prints a warning if there are too many or too // few actions. void ExpectationBase::CheckActionCountIfNotDone() const GTEST_LOCK_EXCLUDED_(mutex_) { … } // Implements the .Times() clause. void ExpectationBase::UntypedTimes(const Cardinality& a_cardinality) { … } // Points to the implicit sequence introduced by a living InSequence // object (if any) in the current thread or NULL. GTEST_API_ ThreadLocal<Sequence*> g_gmock_implicit_sequence; // Reports an uninteresting call (whose description is in msg) in the // manner specified by 'reaction'. void ReportUninterestingCall(CallReaction reaction, const std::string& msg) { … } UntypedFunctionMockerBase::UntypedFunctionMockerBase() : … { … } UntypedFunctionMockerBase::~UntypedFunctionMockerBase() = default; // Sets the mock object this mock method belongs to, and registers // this information in the global mock registry. Will be called // whenever an EXPECT_CALL() or ON_CALL() is executed on this mock // method. void UntypedFunctionMockerBase::RegisterOwner(const void* mock_obj) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { … } // Sets the mock object this mock method belongs to, and sets the name // of the mock function. Will be called upon each invocation of this // mock function. void UntypedFunctionMockerBase::SetOwnerAndName(const void* mock_obj, const char* name) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { … } // Returns the name of the function being mocked. Must be called // after RegisterOwner() or SetOwnerAndName() has been called. const void* UntypedFunctionMockerBase::MockObject() const GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { … } // Returns the name of this mock method. Must be called after // SetOwnerAndName() has been called. const char* UntypedFunctionMockerBase::Name() const GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { … } // Returns an Expectation object that references and co-owns exp, // which must be an expectation on this mock function. Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { … } // Verifies that all expectations on this mock function have been // satisfied. Reports one or more Google Test non-fatal failures // and returns false if not. bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { … } static CallReaction intToCallReaction(int mock_behavior) { … } } // namespace internal // Class Mock. namespace { FunctionMockers; // The current state of a mock object. Such information is needed for // detecting leaked mock objects and explicitly verifying a mock's // expectations. struct MockObjectState { … }; // A global registry holding the state of all mock objects that are // alive. A mock object is added to this registry the first time // Mock::AllowLeak(), ON_CALL(), or EXPECT_CALL() is called on it. It // is removed from the registry in the mock object's destructor. class MockObjectRegistry { … }; // Protected by g_gmock_mutex. MockObjectRegistry g_mock_object_registry; // Maps a mock object to the reaction Google Mock should have when an // uninteresting method is called. Protected by g_gmock_mutex. std::unordered_map<uintptr_t, internal::CallReaction>& UninterestingCallReactionMap() { … } // Sets the reaction Google Mock should have when an uninteresting // method of the given mock object is called. void SetReactionOnUninterestingCalls(uintptr_t mock_obj, internal::CallReaction reaction) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } } // namespace // Tells Google Mock to allow uninteresting calls on the given mock // object. void Mock::AllowUninterestingCalls(uintptr_t mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Tells Google Mock to warn the user about uninteresting calls on the // given mock object. void Mock::WarnUninterestingCalls(uintptr_t mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Tells Google Mock to fail uninteresting calls on the given mock // object. void Mock::FailUninterestingCalls(uintptr_t mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Tells Google Mock the given mock object is being destroyed and its // entry in the call-reaction table should be removed. void Mock::UnregisterCallReaction(uintptr_t mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Returns the reaction Google Mock will have on uninteresting calls // made on the given mock object. internal::CallReaction Mock::GetReactionOnUninterestingCalls( const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Tells Google Mock to ignore mock_obj when checking for leaked mock // objects. void Mock::AllowLeak(const void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Verifies and clears all expectations on the given mock object. If // the expectations aren't satisfied, generates one or more Google // Test non-fatal failures and returns false. bool Mock::VerifyAndClearExpectations(void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Verifies all expectations on the given mock object and clears its // default actions and expectations. Returns true if and only if the // verification was successful. bool Mock::VerifyAndClear(void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Verifies and clears all expectations on the given mock object. If // the expectations aren't satisfied, generates one or more Google // Test non-fatal failures and returns false. bool Mock::VerifyAndClearExpectationsLocked(void* mock_obj) GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { … } bool Mock::IsNaggy(void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } bool Mock::IsNice(void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } bool Mock::IsStrict(void* mock_obj) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Registers a mock object and a mock method it owns. void Mock::Register(const void* mock_obj, internal::UntypedFunctionMockerBase* mocker) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Tells Google Mock where in the source code mock_obj is used in an // ON_CALL or EXPECT_CALL. In case mock_obj is leaked, this // information helps the user identify which object it is. void Mock::RegisterUseByOnCallOrExpectCall(const void* mock_obj, const char* file, int line) GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) { … } // Unregisters a mock method; removes the owning mock object from the // registry when the last mock method associated with it has been // unregistered. This is called only in the destructor of // FunctionMockerBase. void Mock::UnregisterLocked(internal::UntypedFunctionMockerBase* mocker) GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { … } // Clears all ON_CALL()s set on the given mock object. void Mock::ClearDefaultActionsLocked(void* mock_obj) GTEST_EXCLUSIVE_LOCK_REQUIRED_(internal::g_gmock_mutex) { … } Expectation::Expectation() = default; Expectation::Expectation( const std::shared_ptr<internal::ExpectationBase>& an_expectation_base) : … { … } Expectation::~Expectation() = default; // Adds an expectation to a sequence. void Sequence::AddExpectation(const Expectation& expectation) const { … } // Creates the implicit sequence if there isn't one. InSequence::InSequence() { … } // Deletes the implicit sequence if it was created by the constructor // of this object. InSequence::~InSequence() { … } } // namespace testing #if defined(_MSC_VER) && (_MSC_VER == 1900) GTEST_DISABLE_MSC_WARNINGS_POP_() // 4800 #endif