Improves the tests for nice, naggy, and strict mocks.
This commit is contained in:
parent
20d1a235bc
commit
c896504e41
@ -361,7 +361,8 @@ class OnCallSpec : public UntypedOnCallSpecBase {
|
|||||||
enum CallReaction {
|
enum CallReaction {
|
||||||
kAllow,
|
kAllow,
|
||||||
kWarn,
|
kWarn,
|
||||||
kFail
|
kFail,
|
||||||
|
kDefault = kWarn // By default, warn about uninteresting calls.
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
|
@ -639,7 +639,7 @@ internal::CallReaction Mock::GetReactionOnUninterestingCalls(
|
|||||||
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
GTEST_LOCK_EXCLUDED_(internal::g_gmock_mutex) {
|
||||||
internal::MutexLock l(&internal::g_gmock_mutex);
|
internal::MutexLock l(&internal::g_gmock_mutex);
|
||||||
return (g_uninteresting_call_reaction.count(mock_obj) == 0) ?
|
return (g_uninteresting_call_reaction.count(mock_obj) == 0) ?
|
||||||
internal::kWarn : g_uninteresting_call_reaction[mock_obj];
|
internal::kDefault : g_uninteresting_call_reaction[mock_obj];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tells Google Mock to ignore mock_obj when checking for leaked mock
|
// Tells Google Mock to ignore mock_obj when checking for leaked mock
|
||||||
|
@ -110,6 +110,56 @@ class MockBar {
|
|||||||
|
|
||||||
#if GTEST_HAS_STREAM_REDIRECTION
|
#if GTEST_HAS_STREAM_REDIRECTION
|
||||||
|
|
||||||
|
// Tests that a raw mock generates warnings for uninteresting calls.
|
||||||
|
TEST(RawMockTest, WarningForUninterestingCall) {
|
||||||
|
const string saved_flag = GMOCK_FLAG(verbose);
|
||||||
|
GMOCK_FLAG(verbose) = "warning";
|
||||||
|
|
||||||
|
MockFoo raw_foo;
|
||||||
|
|
||||||
|
CaptureStdout();
|
||||||
|
raw_foo.DoThis();
|
||||||
|
raw_foo.DoThat(true);
|
||||||
|
EXPECT_THAT(GetCapturedStdout(),
|
||||||
|
HasSubstr("Uninteresting mock function call"));
|
||||||
|
|
||||||
|
GMOCK_FLAG(verbose) = saved_flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that a raw mock generates warnings for uninteresting calls
|
||||||
|
// that delete the mock object.
|
||||||
|
TEST(RawMockTest, WarningForUninterestingCallAfterDeath) {
|
||||||
|
const string saved_flag = GMOCK_FLAG(verbose);
|
||||||
|
GMOCK_FLAG(verbose) = "warning";
|
||||||
|
|
||||||
|
MockFoo* const raw_foo = new MockFoo;
|
||||||
|
|
||||||
|
ON_CALL(*raw_foo, DoThis())
|
||||||
|
.WillByDefault(Invoke(raw_foo, &MockFoo::Delete));
|
||||||
|
|
||||||
|
CaptureStdout();
|
||||||
|
raw_foo->DoThis();
|
||||||
|
EXPECT_THAT(GetCapturedStdout(),
|
||||||
|
HasSubstr("Uninteresting mock function call"));
|
||||||
|
|
||||||
|
GMOCK_FLAG(verbose) = saved_flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that a raw mock generates informational logs for
|
||||||
|
// uninteresting calls.
|
||||||
|
TEST(RawMockTest, InfoForUninterestingCall) {
|
||||||
|
MockFoo raw_foo;
|
||||||
|
|
||||||
|
const string saved_flag = GMOCK_FLAG(verbose);
|
||||||
|
GMOCK_FLAG(verbose) = "info";
|
||||||
|
CaptureStdout();
|
||||||
|
raw_foo.DoThis();
|
||||||
|
EXPECT_THAT(GetCapturedStdout(),
|
||||||
|
HasSubstr("Uninteresting mock function call"));
|
||||||
|
|
||||||
|
GMOCK_FLAG(verbose) = saved_flag;
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that a nice mock generates no warning for uninteresting calls.
|
// Tests that a nice mock generates no warning for uninteresting calls.
|
||||||
TEST(NiceMockTest, NoWarningForUninterestingCall) {
|
TEST(NiceMockTest, NoWarningForUninterestingCall) {
|
||||||
NiceMock<MockFoo> nice_foo;
|
NiceMock<MockFoo> nice_foo;
|
||||||
@ -145,10 +195,6 @@ TEST(NiceMockTest, InfoForUninterestingCall) {
|
|||||||
EXPECT_THAT(GetCapturedStdout(),
|
EXPECT_THAT(GetCapturedStdout(),
|
||||||
HasSubstr("Uninteresting mock function call"));
|
HasSubstr("Uninteresting mock function call"));
|
||||||
|
|
||||||
CaptureStdout();
|
|
||||||
nice_foo.DoThat(true);
|
|
||||||
EXPECT_THAT(GetCapturedStdout(),
|
|
||||||
HasSubstr("Uninteresting mock function call"));
|
|
||||||
GMOCK_FLAG(verbose) = saved_flag;
|
GMOCK_FLAG(verbose) = saved_flag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +85,7 @@ using testing::IsSubstring;
|
|||||||
using testing::Lt;
|
using testing::Lt;
|
||||||
using testing::Message;
|
using testing::Message;
|
||||||
using testing::Mock;
|
using testing::Mock;
|
||||||
|
using testing::NaggyMock;
|
||||||
using testing::Ne;
|
using testing::Ne;
|
||||||
using testing::Return;
|
using testing::Return;
|
||||||
using testing::Sequence;
|
using testing::Sequence;
|
||||||
@ -899,11 +900,12 @@ TEST(FunctionMockerTest, ReportsExpectCallLocationForExhausedActions) {
|
|||||||
EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output);
|
EXPECT_PRED_FORMAT2(IsSubstring, expect_call_location, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FunctionMockerTest, ReportsDefaultActionLocationOfUninterestingCalls) {
|
TEST(FunctionMockerTest,
|
||||||
|
ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock) {
|
||||||
std::string on_call_location;
|
std::string on_call_location;
|
||||||
CaptureStdout();
|
CaptureStdout();
|
||||||
{
|
{
|
||||||
MockB b;
|
NaggyMock<MockB> b;
|
||||||
on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
|
on_call_location = FormatFileLocation(__FILE__, __LINE__ + 1);
|
||||||
ON_CALL(b, DoB(_)).WillByDefault(Return(0));
|
ON_CALL(b, DoB(_)).WillByDefault(Return(0));
|
||||||
b.DoB(0);
|
b.DoB(0);
|
||||||
@ -1966,10 +1968,11 @@ class VerboseFlagPreservingFixture : public testing::Test {
|
|||||||
|
|
||||||
#if GTEST_HAS_STREAM_REDIRECTION
|
#if GTEST_HAS_STREAM_REDIRECTION
|
||||||
|
|
||||||
// Tests that an uninteresting mock function call generates a warning
|
// Tests that an uninteresting mock function call on a naggy mock
|
||||||
// containing the stack trace.
|
// generates a warning containing the stack trace.
|
||||||
TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) {
|
TEST(FunctionCallMessageTest,
|
||||||
MockC c;
|
UninterestingCallOnNaggyMockGeneratesFyiWithStackTrace) {
|
||||||
|
NaggyMock<MockC> c;
|
||||||
CaptureStdout();
|
CaptureStdout();
|
||||||
c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
|
c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
|
||||||
const std::string output = GetCapturedStdout();
|
const std::string output = GetCapturedStdout();
|
||||||
@ -1995,11 +1998,12 @@ TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) {
|
|||||||
# endif // NDEBUG
|
# endif // NDEBUG
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that an uninteresting mock function call causes the function
|
// Tests that an uninteresting mock function call on a naggy mock
|
||||||
// arguments and return value to be printed.
|
// causes the function arguments and return value to be printed.
|
||||||
TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) {
|
TEST(FunctionCallMessageTest,
|
||||||
|
UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue) {
|
||||||
// A non-void mock function.
|
// A non-void mock function.
|
||||||
MockB b;
|
NaggyMock<MockB> b;
|
||||||
CaptureStdout();
|
CaptureStdout();
|
||||||
b.DoB();
|
b.DoB();
|
||||||
const std::string output1 = GetCapturedStdout();
|
const std::string output1 = GetCapturedStdout();
|
||||||
@ -2011,7 +2015,7 @@ TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) {
|
|||||||
// Makes sure the return value is printed.
|
// Makes sure the return value is printed.
|
||||||
|
|
||||||
// A void mock function.
|
// A void mock function.
|
||||||
MockC c;
|
NaggyMock<MockC> c;
|
||||||
CaptureStdout();
|
CaptureStdout();
|
||||||
c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
|
c.VoidMethod(false, 5, "Hi", NULL, Printable(), Unprintable());
|
||||||
const std::string output2 = GetCapturedStdout();
|
const std::string output2 = GetCapturedStdout();
|
||||||
@ -2081,9 +2085,9 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
|
|||||||
"Binary");
|
"Binary");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests how the flag affects uninteresting calls.
|
// Tests how the flag affects uninteresting calls on a naggy mock.
|
||||||
void TestUninterestingCall(bool should_print) {
|
void TestUninterestingCallOnNaggyMock(bool should_print) {
|
||||||
MockA a;
|
NaggyMock<MockA> a;
|
||||||
|
|
||||||
// A void-returning function.
|
// A void-returning function.
|
||||||
CaptureStdout();
|
CaptureStdout();
|
||||||
@ -2117,7 +2121,7 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
|
|||||||
TEST_F(GMockVerboseFlagTest, Info) {
|
TEST_F(GMockVerboseFlagTest, Info) {
|
||||||
GMOCK_FLAG(verbose) = kInfoVerbosity;
|
GMOCK_FLAG(verbose) = kInfoVerbosity;
|
||||||
TestExpectedCall(true);
|
TestExpectedCall(true);
|
||||||
TestUninterestingCall(true);
|
TestUninterestingCallOnNaggyMock(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that --gmock_verbose=warning causes uninteresting calls to be
|
// Tests that --gmock_verbose=warning causes uninteresting calls to be
|
||||||
@ -2125,7 +2129,7 @@ TEST_F(GMockVerboseFlagTest, Info) {
|
|||||||
TEST_F(GMockVerboseFlagTest, Warning) {
|
TEST_F(GMockVerboseFlagTest, Warning) {
|
||||||
GMOCK_FLAG(verbose) = kWarningVerbosity;
|
GMOCK_FLAG(verbose) = kWarningVerbosity;
|
||||||
TestExpectedCall(false);
|
TestExpectedCall(false);
|
||||||
TestUninterestingCall(true);
|
TestUninterestingCallOnNaggyMock(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that --gmock_verbose=warning causes neither expected nor
|
// Tests that --gmock_verbose=warning causes neither expected nor
|
||||||
@ -2133,7 +2137,7 @@ TEST_F(GMockVerboseFlagTest, Warning) {
|
|||||||
TEST_F(GMockVerboseFlagTest, Error) {
|
TEST_F(GMockVerboseFlagTest, Error) {
|
||||||
GMOCK_FLAG(verbose) = kErrorVerbosity;
|
GMOCK_FLAG(verbose) = kErrorVerbosity;
|
||||||
TestExpectedCall(false);
|
TestExpectedCall(false);
|
||||||
TestUninterestingCall(false);
|
TestUninterestingCallOnNaggyMock(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect
|
// Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect
|
||||||
@ -2141,7 +2145,7 @@ TEST_F(GMockVerboseFlagTest, Error) {
|
|||||||
TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
|
TEST_F(GMockVerboseFlagTest, InvalidFlagIsTreatedAsWarning) {
|
||||||
GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning".
|
GMOCK_FLAG(verbose) = "invalid"; // Treated as "warning".
|
||||||
TestExpectedCall(false);
|
TestExpectedCall(false);
|
||||||
TestUninterestingCall(true);
|
TestUninterestingCallOnNaggyMock(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // GTEST_HAS_STREAM_REDIRECTION
|
#endif // GTEST_HAS_STREAM_REDIRECTION
|
||||||
|
@ -43,6 +43,7 @@ using testing::_;
|
|||||||
using testing::AnyNumber;
|
using testing::AnyNumber;
|
||||||
using testing::Ge;
|
using testing::Ge;
|
||||||
using testing::InSequence;
|
using testing::InSequence;
|
||||||
|
using testing::NaggyMock;
|
||||||
using testing::Ref;
|
using testing::Ref;
|
||||||
using testing::Return;
|
using testing::Return;
|
||||||
using testing::Sequence;
|
using testing::Sequence;
|
||||||
@ -61,7 +62,7 @@ class MockFoo {
|
|||||||
|
|
||||||
class GMockOutputTest : public testing::Test {
|
class GMockOutputTest : public testing::Test {
|
||||||
protected:
|
protected:
|
||||||
MockFoo foo_;
|
NaggyMock<MockFoo> foo_;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(GMockOutputTest, ExpectedCall) {
|
TEST_F(GMockOutputTest, ExpectedCall) {
|
||||||
|
Loading…
Reference in New Issue
Block a user