Unverified Commit 82febb8e by Gennadiy Civil Committed by GitHub

Merge pull request #1549 from gennadiycivil/master

Merging gMock, 2
parents d0905653 bee1d13f
$$ -*- mode: c++; -*- $$ -*- mode: c++; -*-
$$ This is a Pump source file. Please use Pump to convert it to $$ This is a Pump source file. Please use Pump to convert
$$ gmock-generated-function-mockers.h. $$ it to gmock-generated-function-mockers.h.
$$ $$
$var n = 10 $$ The maximum arity we support. $var n = 10 $$ The maximum arity we support.
// Copyright 2007, Google Inc. // Copyright 2007, Google Inc.
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <vector>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
...@@ -99,12 +100,19 @@ void ExpectationBase::RetireAllPreRequisites() ...@@ -99,12 +100,19 @@ void ExpectationBase::RetireAllPreRequisites()
return; return;
} }
for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); ::std::vector<ExpectationBase*> expectations(1, this);
it != immediate_prerequisites_.end(); ++it) { while (!expectations.empty()) {
ExpectationBase* const prerequisite = it->expectation_base().get(); ExpectationBase* exp = expectations.back();
if (!prerequisite->is_retired()) { expectations.pop_back();
prerequisite->RetireAllPreRequisites();
prerequisite->Retire(); for (ExpectationSet::const_iterator it =
exp->immediate_prerequisites_.begin();
it != exp->immediate_prerequisites_.end(); ++it) {
ExpectationBase* next = it->expectation_base().get();
if (!next->is_retired()) {
next->Retire();
expectations.push_back(next);
}
} }
} }
} }
...@@ -114,11 +122,18 @@ void ExpectationBase::RetireAllPreRequisites() ...@@ -114,11 +122,18 @@ void ExpectationBase::RetireAllPreRequisites()
bool ExpectationBase::AllPrerequisitesAreSatisfied() const bool ExpectationBase::AllPrerequisitesAreSatisfied() const
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
g_gmock_mutex.AssertHeld(); g_gmock_mutex.AssertHeld();
for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); ::std::vector<const ExpectationBase*> expectations(1, this);
it != immediate_prerequisites_.end(); ++it) { while (!expectations.empty()) {
if (!(it->expectation_base()->IsSatisfied()) || const ExpectationBase* exp = expectations.back();
!(it->expectation_base()->AllPrerequisitesAreSatisfied())) expectations.pop_back();
return false;
for (ExpectationSet::const_iterator it =
exp->immediate_prerequisites_.begin();
it != exp->immediate_prerequisites_.end(); ++it) {
const ExpectationBase* next = it->expectation_base().get();
if (!next->IsSatisfied()) return false;
expectations.push_back(next);
}
} }
return true; return true;
} }
...@@ -127,19 +142,28 @@ bool ExpectationBase::AllPrerequisitesAreSatisfied() const ...@@ -127,19 +142,28 @@ bool ExpectationBase::AllPrerequisitesAreSatisfied() const
void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result) const void ExpectationBase::FindUnsatisfiedPrerequisites(ExpectationSet* result) const
GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) { GTEST_EXCLUSIVE_LOCK_REQUIRED_(g_gmock_mutex) {
g_gmock_mutex.AssertHeld(); g_gmock_mutex.AssertHeld();
for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); ::std::vector<const ExpectationBase*> expectations(1, this);
it != immediate_prerequisites_.end(); ++it) { while (!expectations.empty()) {
if (it->expectation_base()->IsSatisfied()) { const ExpectationBase* exp = expectations.back();
// If *it is satisfied and has a call count of 0, some of its expectations.pop_back();
// pre-requisites may not be satisfied yet.
if (it->expectation_base()->call_count_ == 0) { for (ExpectationSet::const_iterator it =
it->expectation_base()->FindUnsatisfiedPrerequisites(result); exp->immediate_prerequisites_.begin();
it != exp->immediate_prerequisites_.end(); ++it) {
const ExpectationBase* next = it->expectation_base().get();
if (next->IsSatisfied()) {
// If *it is satisfied and has a call count of 0, some of its
// pre-requisites may not be satisfied yet.
if (next->call_count_ == 0) {
expectations.push_back(next);
}
} else {
// Now that we know next is unsatisfied, we are not so interested
// in whether its pre-requisites are satisfied. Therefore we
// don't iterate into it here.
*result += *it;
} }
} else {
// Now that we know *it is unsatisfied, we are not so interested
// in whether its pre-requisites are satisfied. Therefore we
// don't recursively call FindUnsatisfiedPrerequisites() here.
*result += *it;
} }
} }
} }
...@@ -254,11 +278,13 @@ void ReportUninterestingCall(CallReaction reaction, const std::string& msg) { ...@@ -254,11 +278,13 @@ void ReportUninterestingCall(CallReaction reaction, const std::string& msg) {
case kWarn: case kWarn:
Log(kWarning, Log(kWarning,
msg + msg +
"\nNOTE: You can safely ignore the above warning unless this " "\nNOTE: You can safely ignore the above warning unless this "
"call should not happen. Do not suppress it by blindly adding " "call should not happen. Do not suppress it by blindly adding "
"an EXPECT_CALL() if you don't mean to enforce the call. " "an EXPECT_CALL() if you don't mean to enforce the call. "
"See https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#" "See "
"knowing-when-to-expect for details.\n", "https://github.com/google/googletest/blob/master/googlemock/"
"docs/CookBook.md#"
"knowing-when-to-expect for details.\n",
stack_frames_to_skip); stack_frames_to_skip);
break; break;
default: // FAIL default: // FAIL
...@@ -334,9 +360,10 @@ const char* UntypedFunctionMockerBase::Name() const ...@@ -334,9 +360,10 @@ const char* UntypedFunctionMockerBase::Name() const
// Calculates the result of invoking this mock function with the given // Calculates the result of invoking this mock function with the given
// arguments, prints it, and returns it. The caller is responsible // arguments, prints it, and returns it. The caller is responsible
// for deleting the result. // for deleting the result.
UntypedActionResultHolderBase* UntypedActionResultHolderBase* UntypedFunctionMockerBase::UntypedInvokeWith(
UntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args) void* const untyped_args) GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
GTEST_LOCK_EXCLUDED_(g_gmock_mutex) { // See the definition of untyped_expectations_ for why access to it
// is unprotected here.
if (untyped_expectations_.size() == 0) { if (untyped_expectations_.size() == 0) {
// No expectation is set on this mock method - we have an // No expectation is set on this mock method - we have an
// uninteresting call. // uninteresting call.
...@@ -355,16 +382,19 @@ UntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args) ...@@ -355,16 +382,19 @@ UntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args)
// If the user allows this uninteresting call, we print it // If the user allows this uninteresting call, we print it
// only when they want informational messages. // only when they want informational messages.
reaction == kAllow ? LogIsVisible(kInfo) : reaction == kAllow ? LogIsVisible(kInfo) :
// If the user wants this to be a warning, we print it only // If the user wants this to be a warning, we print
// when they want to see warnings. // it only when they want to see warnings.
reaction == kWarn ? LogIsVisible(kWarning) : reaction == kWarn
// Otherwise, the user wants this to be an error, and we ? LogIsVisible(kWarning)
// should always print detailed information in the error. :
true; // Otherwise, the user wants this to be an error, and we
// should always print detailed information in the error.
true;
if (!need_to_report_uninteresting_call) { if (!need_to_report_uninteresting_call) {
// Perform the action without printing the call information. // Perform the action without printing the call information.
return this->UntypedPerformDefaultAction(untyped_args, "Function call: " + std::string(Name())); return this->UntypedPerformDefaultAction(
untyped_args, "Function call: " + std::string(Name()));
} }
// Warns about the uninteresting call. // Warns about the uninteresting call.
...@@ -446,6 +476,8 @@ UntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args) ...@@ -446,6 +476,8 @@ UntypedFunctionMockerBase::UntypedInvokeWith(const void* const untyped_args)
// Returns an Expectation object that references and co-owns exp, // Returns an Expectation object that references and co-owns exp,
// which must be an expectation on this mock function. // which must be an expectation on this mock function.
Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) { Expectation UntypedFunctionMockerBase::GetHandleOf(ExpectationBase* exp) {
// See the definition of untyped_expectations_ for why access to it
// is unprotected here.
for (UntypedExpectations::const_iterator it = for (UntypedExpectations::const_iterator it =
untyped_expectations_.begin(); untyped_expectations_.begin();
it != untyped_expectations_.end(); ++it) { it != untyped_expectations_.end(); ++it) {
...@@ -508,7 +540,7 @@ bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked() ...@@ -508,7 +540,7 @@ bool UntypedFunctionMockerBase::VerifyAndClearExpectationsLocked()
return expectations_met; return expectations_met;
} }
static CallReaction intToCallReaction(int mock_behavior) { CallReaction intToCallReaction(int mock_behavior) {
if (mock_behavior >= kAllow && mock_behavior <= kFail) { if (mock_behavior >= kAllow && mock_behavior <= kFail) {
return static_cast<internal::CallReaction>(mock_behavior); return static_cast<internal::CallReaction>(mock_behavior);
} }
...@@ -582,9 +614,15 @@ class MockObjectRegistry { ...@@ -582,9 +614,15 @@ class MockObjectRegistry {
leaked_count++; leaked_count++;
} }
if (leaked_count > 0) { if (leaked_count > 0) {
std::cout << "\nERROR: " << leaked_count std::cout << "\nERROR: " << leaked_count << " leaked mock "
<< " leaked mock " << (leaked_count == 1 ? "object" : "objects") << (leaked_count == 1 ? "object" : "objects")
<< " found at program exit.\n"; << " found at program exit. Expectations on a mock object is "
"verified when the object is destructed. Leaking a mock "
"means that its expectations aren't verified, which is "
"usually a test bug. If you really intend to leak a mock, "
"you can suppress this error using "
"testing::Mock::AllowLeak(mock_object), or you may use a "
"fake or stub instead of a mock.\n";
std::cout.flush(); std::cout.flush();
::std::cerr.flush(); ::std::cerr.flush();
// RUN_ALL_TESTS() has already returned when this destructor is // RUN_ALL_TESTS() has already returned when this destructor is
......
...@@ -704,6 +704,7 @@ class MockClass { ...@@ -704,6 +704,7 @@ class MockClass {
MOCK_METHOD0(MakeUnique, std::unique_ptr<int>()); MOCK_METHOD0(MakeUnique, std::unique_ptr<int>());
MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>()); MOCK_METHOD0(MakeUniqueBase, std::unique_ptr<Base>());
MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>()); MOCK_METHOD0(MakeVectorUnique, std::vector<std::unique_ptr<int>>());
MOCK_METHOD1(TakeUnique, int(std::unique_ptr<int>));
#endif #endif
private: private:
......
...@@ -748,7 +748,6 @@ TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) { ...@@ -748,7 +748,6 @@ TEST(ExpectCallSyntaxTest, WarningIsErrorWithFlag) {
testing::GMOCK_FLAG(default_mock_behavior) = original_behavior; testing::GMOCK_FLAG(default_mock_behavior) = original_behavior;
} }
#endif // GTEST_HAS_STREAM_REDIRECTION #endif // GTEST_HAS_STREAM_REDIRECTION
// Tests the semantics of ON_CALL(). // Tests the semantics of ON_CALL().
...@@ -2691,7 +2690,6 @@ int gmock_main(int argc, char **argv) { ...@@ -2691,7 +2690,6 @@ int gmock_main(int argc, char **argv) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
#endif // GMOCK_RENAME_MAIN #endif // GMOCK_RENAME_MAIN
testing::InitGoogleMock(&argc, argv); testing::InitGoogleMock(&argc, argv);
// Ensures that the tests pass no matter what value of // Ensures that the tests pass no matter what value of
// --gmock_catch_leaked_mocks and --gmock_verbose the user specifies. // --gmock_catch_leaked_mocks and --gmock_verbose the user specifies.
testing::GMOCK_FLAG(catch_leaked_mocks) = true; testing::GMOCK_FLAG(catch_leaked_mocks) = true;
......
...@@ -273,6 +273,11 @@ MATCHER_P2(IsPair, first, second, "") { ...@@ -273,6 +273,11 @@ MATCHER_P2(IsPair, first, second, "") {
return Value(arg.first, first) && Value(arg.second, second); return Value(arg.first, first) && Value(arg.second, second);
} }
TEST_F(GMockOutputTest, PrintsMatcher) {
const testing::Matcher<int> m1 = Ge(48);
EXPECT_THAT((std::pair<int, bool>(42, true)), IsPair(m1, true));
}
void TestCatchesLeakedMocksInAdHocTests() { void TestCatchesLeakedMocksInAdHocTests() {
MockFoo* foo = new MockFoo; MockFoo* foo = new MockFoo;
......
...@@ -288,6 +288,12 @@ Stack trace: ...@@ -288,6 +288,12 @@ Stack trace:
[ OK ] GMockOutputTest.ExplicitActionsRunOutWithDefaultAction [ OK ] GMockOutputTest.ExplicitActionsRunOutWithDefaultAction
[ RUN ] GMockOutputTest.CatchesLeakedMocks [ RUN ] GMockOutputTest.CatchesLeakedMocks
[ OK ] GMockOutputTest.CatchesLeakedMocks [ OK ] GMockOutputTest.CatchesLeakedMocks
[ RUN ] GMockOutputTest.PrintsMatcher
FILE:#: Failure
Value of: (std::pair<int, bool>(42, true))
Expected: is pair (is >= 48, true)
Actual: (42, true) (of type std::pair<int, bool>)
[ FAILED ] GMockOutputTest.PrintsMatcher
[ FAILED ] GMockOutputTest.UnexpectedCall [ FAILED ] GMockOutputTest.UnexpectedCall
[ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction [ FAILED ] GMockOutputTest.UnexpectedCallToVoidFunction
[ FAILED ] GMockOutputTest.ExcessiveCall [ FAILED ] GMockOutputTest.ExcessiveCall
...@@ -302,9 +308,10 @@ Stack trace: ...@@ -302,9 +308,10 @@ Stack trace:
[ FAILED ] GMockOutputTest.MismatchArgumentsAndWith [ FAILED ] GMockOutputTest.MismatchArgumentsAndWith
[ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction [ FAILED ] GMockOutputTest.UnexpectedCallWithDefaultAction
[ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction [ FAILED ] GMockOutputTest.ExcessiveCallWithDefaultAction
[ FAILED ] GMockOutputTest.PrintsMatcher
FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#. FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#.
FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#. FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#.
FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#. FILE:#: ERROR: this mock object should be deleted but never is. Its address is @0x#.
ERROR: 3 leaked mock objects found at program exit. ERROR: 3 leaked mock objects found at program exit. Expectations on a mock object is verified when the object is destructed. Leaking a mock means that its expectations aren't verified, which is usually a test bug. If you really intend to leak a mock, you can suppress this error using testing::Mock::AllowLeak(mock_object), or you may use a fake or stub instead of a mock.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment