Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
yangql
googletest
Commits
c896504e
Commit
c896504e
authored
Mar 01, 2013
by
zhanyong.wan
Browse files
Improves the tests for nice, naggy, and strict mocks.
parent
20d1a235
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
77 additions
and
25 deletions
+77
-25
include/gmock/gmock-spec-builders.h
include/gmock/gmock-spec-builders.h
+2
-1
src/gmock-spec-builders.cc
src/gmock-spec-builders.cc
+1
-1
test/gmock-nice-strict_test.cc
test/gmock-nice-strict_test.cc
+50
-4
test/gmock-spec-builders_test.cc
test/gmock-spec-builders_test.cc
+22
-18
test/gmock_output_test_.cc
test/gmock_output_test_.cc
+2
-1
No files found.
include/gmock/gmock-spec-builders.h
View file @
c896504e
...
...
@@ -361,7 +361,8 @@ class OnCallSpec : public UntypedOnCallSpecBase {
enum
CallReaction
{
kAllow
,
kWarn
,
kFail
kFail
,
kDefault
=
kWarn
// By default, warn about uninteresting calls.
};
}
// namespace internal
...
...
src/gmock-spec-builders.cc
View file @
c896504e
...
...
@@ -639,7 +639,7 @@ internal::CallReaction Mock::GetReactionOnUninterestingCalls(
GTEST_LOCK_EXCLUDED_
(
internal
::
g_gmock_mutex
)
{
internal
::
MutexLock
l
(
&
internal
::
g_gmock_mutex
);
return
(
g_uninteresting_call_reaction
.
count
(
mock_obj
)
==
0
)
?
internal
::
k
Warn
:
g_uninteresting_call_reaction
[
mock_obj
];
internal
::
k
Default
:
g_uninteresting_call_reaction
[
mock_obj
];
}
// Tells Google Mock to ignore mock_obj when checking for leaked mock
...
...
test/gmock-nice-strict_test.cc
View file @
c896504e
...
...
@@ -110,6 +110,56 @@ class MockBar {
#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.
TEST
(
NiceMockTest
,
NoWarningForUninterestingCall
)
{
NiceMock
<
MockFoo
>
nice_foo
;
...
...
@@ -145,10 +195,6 @@ TEST(NiceMockTest, InfoForUninterestingCall) {
EXPECT_THAT
(
GetCapturedStdout
(),
HasSubstr
(
"Uninteresting mock function call"
));
CaptureStdout
();
nice_foo
.
DoThat
(
true
);
EXPECT_THAT
(
GetCapturedStdout
(),
HasSubstr
(
"Uninteresting mock function call"
));
GMOCK_FLAG
(
verbose
)
=
saved_flag
;
}
...
...
test/gmock-spec-builders_test.cc
View file @
c896504e
...
...
@@ -85,6 +85,7 @@ using testing::IsSubstring;
using
testing
::
Lt
;
using
testing
::
Message
;
using
testing
::
Mock
;
using
testing
::
NaggyMock
;
using
testing
::
Ne
;
using
testing
::
Return
;
using
testing
::
Sequence
;
...
...
@@ -899,11 +900,12 @@ TEST(FunctionMockerTest, ReportsExpectCallLocationForExhausedActions) {
EXPECT_PRED_FORMAT2
(
IsSubstring
,
expect_call_location
,
output
);
}
TEST
(
FunctionMockerTest
,
ReportsDefaultActionLocationOfUninterestingCalls
)
{
TEST
(
FunctionMockerTest
,
ReportsDefaultActionLocationOfUninterestingCallsForNaggyMock
)
{
std
::
string
on_call_location
;
CaptureStdout
();
{
MockB
b
;
NaggyMock
<
MockB
>
b
;
on_call_location
=
FormatFileLocation
(
__FILE__
,
__LINE__
+
1
);
ON_CALL
(
b
,
DoB
(
_
)).
WillByDefault
(
Return
(
0
));
b
.
DoB
(
0
);
...
...
@@ -1966,10 +1968,11 @@ class VerboseFlagPreservingFixture : public testing::Test {
#if GTEST_HAS_STREAM_REDIRECTION
// Tests that an uninteresting mock function call generates a warning
// containing the stack trace.
TEST
(
FunctionCallMessageTest
,
UninterestingCallGeneratesFyiWithStackTrace
)
{
MockC
c
;
// Tests that an uninteresting mock function call on a naggy mock
// generates a warning containing the stack trace.
TEST
(
FunctionCallMessageTest
,
UninterestingCallOnNaggyMockGeneratesFyiWithStackTrace
)
{
NaggyMock
<
MockC
>
c
;
CaptureStdout
();
c
.
VoidMethod
(
false
,
5
,
"Hi"
,
NULL
,
Printable
(),
Unprintable
());
const
std
::
string
output
=
GetCapturedStdout
();
...
...
@@ -1995,11 +1998,12 @@ TEST(FunctionCallMessageTest, UninterestingCallGeneratesFyiWithStackTrace) {
# endif // NDEBUG
}
// Tests that an uninteresting mock function call causes the function
// arguments and return value to be printed.
TEST
(
FunctionCallMessageTest
,
UninterestingCallPrintsArgumentsAndReturnValue
)
{
// Tests that an uninteresting mock function call on a naggy mock
// causes the function arguments and return value to be printed.
TEST
(
FunctionCallMessageTest
,
UninterestingCallOnNaggyMockPrintsArgumentsAndReturnValue
)
{
// A non-void mock function.
MockB
b
;
NaggyMock
<
MockB
>
b
;
CaptureStdout
();
b
.
DoB
();
const
std
::
string
output1
=
GetCapturedStdout
();
...
...
@@ -2011,7 +2015,7 @@ TEST(FunctionCallMessageTest, UninterestingCallPrintsArgumentsAndReturnValue) {
// Makes sure the return value is printed.
// A void mock function.
MockC
c
;
NaggyMock
<
MockC
>
c
;
CaptureStdout
();
c
.
VoidMethod
(
false
,
5
,
"Hi"
,
NULL
,
Printable
(),
Unprintable
());
const
std
::
string
output2
=
GetCapturedStdout
();
...
...
@@ -2081,9 +2085,9 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
"Binary"
);
}
// Tests how the flag affects uninteresting calls.
void
TestUninterestingCall
(
bool
should_print
)
{
MockA
a
;
// Tests how the flag affects uninteresting calls
on a naggy mock
.
void
TestUninterestingCall
OnNaggyMock
(
bool
should_print
)
{
NaggyMock
<
MockA
>
a
;
// A void-returning function.
CaptureStdout
();
...
...
@@ -2117,7 +2121,7 @@ class GMockVerboseFlagTest : public VerboseFlagPreservingFixture {
TEST_F
(
GMockVerboseFlagTest
,
Info
)
{
GMOCK_FLAG
(
verbose
)
=
kInfoVerbosity
;
TestExpectedCall
(
true
);
TestUninterestingCall
(
true
);
TestUninterestingCall
OnNaggyMock
(
true
);
}
// Tests that --gmock_verbose=warning causes uninteresting calls to be
...
...
@@ -2125,7 +2129,7 @@ TEST_F(GMockVerboseFlagTest, Info) {
TEST_F
(
GMockVerboseFlagTest
,
Warning
)
{
GMOCK_FLAG
(
verbose
)
=
kWarningVerbosity
;
TestExpectedCall
(
false
);
TestUninterestingCall
(
true
);
TestUninterestingCall
OnNaggyMock
(
true
);
}
// Tests that --gmock_verbose=warning causes neither expected nor
...
...
@@ -2133,7 +2137,7 @@ TEST_F(GMockVerboseFlagTest, Warning) {
TEST_F
(
GMockVerboseFlagTest
,
Error
)
{
GMOCK_FLAG
(
verbose
)
=
kErrorVerbosity
;
TestExpectedCall
(
false
);
TestUninterestingCall
(
false
);
TestUninterestingCall
OnNaggyMock
(
false
);
}
// Tests that --gmock_verbose=SOME_INVALID_VALUE has the same effect
...
...
@@ -2141,7 +2145,7 @@ TEST_F(GMockVerboseFlagTest, Error) {
TEST_F
(
GMockVerboseFlagTest
,
InvalidFlagIsTreatedAsWarning
)
{
GMOCK_FLAG
(
verbose
)
=
"invalid"
;
// Treated as "warning".
TestExpectedCall
(
false
);
TestUninterestingCall
(
true
);
TestUninterestingCall
OnNaggyMock
(
true
);
}
#endif // GTEST_HAS_STREAM_REDIRECTION
...
...
test/gmock_output_test_.cc
View file @
c896504e
...
...
@@ -43,6 +43,7 @@ using testing::_;
using
testing
::
AnyNumber
;
using
testing
::
Ge
;
using
testing
::
InSequence
;
using
testing
::
NaggyMock
;
using
testing
::
Ref
;
using
testing
::
Return
;
using
testing
::
Sequence
;
...
...
@@ -61,7 +62,7 @@ class MockFoo {
class
GMockOutputTest
:
public
testing
::
Test
{
protected:
MockFoo
foo_
;
NaggyMock
<
MockFoo
>
foo_
;
};
TEST_F
(
GMockOutputTest
,
ExpectedCall
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment