Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
googletest
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
googletest
Commits
a6f06bf2
Unverified
Commit
a6f06bf2
authored
Apr 19, 2018
by
Gennadiy Civil
Committed by
GitHub
Apr 19, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1582 from dnsunderland/parameterless
Introduce parameterless expectations
parents
1114a020
884bcdaf
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
263 additions
and
13 deletions
+263
-13
gmock-generated-function-mockers.h
googlemock/include/gmock/gmock-generated-function-mockers.h
+0
-0
gmock-generated-function-mockers.h.pump
...ock/include/gmock/gmock-generated-function-mockers.h.pump
+60
-0
gmock-spec-builders.h
googlemock/include/gmock/gmock-spec-builders.h
+78
-12
gmock-internal-utils.h
googlemock/include/gmock/internal/gmock-internal-utils.h
+15
-0
gmock-internal-utils.cc
googlemock/src/gmock-internal-utils.cc
+2
-0
gmock-matchers_test.cc
googlemock/test/gmock-matchers_test.cc
+38
-1
gmock-spec-builders_test.cc
googlemock/test/gmock-spec-builders_test.cc
+70
-0
No files found.
googlemock/include/gmock/gmock-generated-function-mockers.h
View file @
a6f06bf2
This diff is collapsed.
Click to expand it.
googlemock/include/gmock/gmock-generated-function-mockers.h.pump
View file @
a6f06bf2
...
@@ -94,6 +94,58 @@ class FunctionMocker<R($As)> : public
...
@@ -94,6 +94,58 @@ class FunctionMocker<R($As)> : public
]]
]]
// Removes the given pointer; this is a helper for the expectation setter method
// for parameterless matchers.
//
// We want to make sure that the user cannot set a parameterless expectation on
// overloaded methods, including methods which are overloaded on const. Example:
//
// class MockClass {
// MOCK_METHOD0(GetName, string&());
// MOCK_CONST_METHOD0(GetName, const string&());
// };
//
// TEST() {
// // This should be an error, as it's not clear which overload is expected.
// EXPECT_CALL(mock, GetName).WillOnce(ReturnRef(value));
// }
//
// Here are the generated expectation-setter methods:
//
// class MockClass {
// // Overload 1
// MockSpec<string&()> gmock_GetName() { … }
// // Overload 2. Declared const so that the compiler will generate an
// // error when trying to resolve between this and overload 4 in
// // 'gmock_GetName(WithoutMatchers(), nullptr)'.
// MockSpec<string&()> gmock_GetName(
// const WithoutMatchers&, const Function<string&()>*) const {
// // Removes const from this, calls overload 1
// return AdjustConstness_(this)->gmock_GetName();
// }
//
// // Overload 3
// const string& gmock_GetName() const { … }
// // Overload 4
// MockSpec<const string&()> gmock_GetName(
// const WithoutMatchers&, const Function<const string&()>*) const {
// // Does not remove const, calls overload 3
// return AdjustConstness_const(this)->gmock_GetName();
// }
// }
//
template
<
typename
MockType
>
const
MockType
*
AdjustConstness_const
(
const
MockType
*
mock
)
{
return
mock
;
}
// Removes const from and returns the given pointer; this is a helper for the
// expectation setter method for parameterless matchers.
template
<
typename
MockType
>
MockType
*
AdjustConstness_
(
const
MockType
*
mock
)
{
return
const_cast
<
MockType
*>
(
mock
);
}
}
// namespace internal
}
// namespace internal
// The style guide prohibits "using" statements in a namespace scope
// The style guide prohibits "using" statements in a namespace scope
...
@@ -135,6 +187,8 @@ $var as = [[$for j, \
...
@@ -135,6 +187,8 @@ $var as = [[$for j, \
$
var
matcher_arg_as
=
[[
$
for
j
,
\
$
var
matcher_arg_as
=
[[
$
for
j
,
\
[[
GMOCK_MATCHER_
(
tn
,
$
j
,
__VA_ARGS__
)
gmock_a
$
j
]]]]
[[
GMOCK_MATCHER_
(
tn
,
$
j
,
__VA_ARGS__
)
gmock_a
$
j
]]]]
$
var
matcher_as
=
[[
$
for
j
,
[[
gmock_a
$
j
]]]]
$
var
matcher_as
=
[[
$
for
j
,
[[
gmock_a
$
j
]]]]
$
var
anything_matchers
=
[[
$
for
j
,
\
[[
::
testing
::
A
<
GMOCK_ARG_
(
tn
,
$
j
,
__VA_ARGS__
)
>
()]]]]
// INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
// INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!!
#define GMOCK_METHOD$i[[]]_(tn, constness, ct, Method, ...) \
#define GMOCK_METHOD$i[[]]_(tn, constness, ct, Method, ...) \
GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \
GMOCK_RESULT_(tn, __VA_ARGS__) ct Method( \
...
@@ -150,6 +204,12 @@ $var matcher_as = [[$for j, [[gmock_a$j]]]]
...
@@ -150,6 +204,12 @@ $var matcher_as = [[$for j, [[gmock_a$j]]]]
GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this); \
GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_($i, constness, Method).With($matcher_as); \
return GMOCK_MOCKER_($i, constness, Method).With($matcher_as); \
} \
} \
::testing::MockSpec<__VA_ARGS__> gmock_##Method( \
const ::testing::internal::WithoutMatchers&, \
constness ::testing::internal::Function<__VA_ARGS__>* ) const { \
return ::testing::internal::AdjustConstness_##constness(this)-> \
gmock_##Method($anything_matchers); \
} \
mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_($i, constness, Method)
mutable ::testing::FunctionMocker<__VA_ARGS__> GMOCK_MOCKER_($i, constness, Method)
...
...
googlemock/include/gmock/gmock-spec-builders.h
View file @
a6f06bf2
...
@@ -1282,6 +1282,13 @@ class MockSpec {
...
@@ -1282,6 +1282,13 @@ class MockSpec {
file
,
line
,
source_text
,
matchers_
);
file
,
line
,
source_text
,
matchers_
);
}
}
// This operator overload is used to swallow the superfluous parameter list
// introduced by the ON/EXPECT_CALL macros. See the macro comments for more
// explanation.
MockSpec
<
F
>&
operator
()(
const
internal
::
WithoutMatchers
&
,
void
*
const
)
{
return
*
this
;
}
private
:
private
:
template
<
typename
Function
>
template
<
typename
Function
>
friend
class
internal
::
FunctionMocker
;
friend
class
internal
::
FunctionMocker
;
...
@@ -1836,17 +1843,76 @@ inline Expectation::Expectation(internal::ExpectationBase& exp) // NOLINT
...
@@ -1836,17 +1843,76 @@ inline Expectation::Expectation(internal::ExpectationBase& exp) // NOLINT
}
// namespace testing
}
// namespace testing
// A separate macro is required to avoid compile errors when the name
// Implementation for ON_CALL and EXPECT_CALL macros. A separate macro is
// of the method used in call is a result of macro expansion.
// required to avoid compile errors when the name of the method used in call is
// See CompilesWithMethodNameExpandedFromMacro tests in
// a result of macro expansion. See CompilesWithMethodNameExpandedFromMacro
// internal/gmock-spec-builders_test.cc for more details.
// tests in internal/gmock-spec-builders_test.cc for more details.
#define GMOCK_ON_CALL_IMPL_(obj, call) \
//
((obj).gmock_##call).InternalDefaultActionSetAt(__FILE__, __LINE__, \
// This macro supports statements both with and without parameter matchers. If
#obj, #call)
// the parameter list is omitted, gMock will accept any parameters, which allows
#define ON_CALL(obj, call) GMOCK_ON_CALL_IMPL_(obj, call)
// tests to be written that don't need to encode the number of method
// parameter. This technique may only be used for non-overloaded methods.
#define GMOCK_EXPECT_CALL_IMPL_(obj, call) \
//
((obj).gmock_##call).InternalExpectedAt(__FILE__, __LINE__, #obj, #call)
// // These are the same:
#define EXPECT_CALL(obj, call) GMOCK_EXPECT_CALL_IMPL_(obj, call)
// ON_CALL(mock, NoArgsMethod()).WillByDefault(…);
// ON_CALL(mock, NoArgsMethod).WillByDefault(…);
//
// // As are these:
// ON_CALL(mock, TwoArgsMethod(_, _)).WillByDefault(…);
// ON_CALL(mock, TwoArgsMethod).WillByDefault(…);
//
// // Can also specify args if you want, of course:
// ON_CALL(mock, TwoArgsMethod(_, 45)).WillByDefault(…);
//
// // Overloads work as long as you specify parameters:
// ON_CALL(mock, OverloadedMethod(_)).WillByDefault(…);
// ON_CALL(mock, OverloadedMethod(_, _)).WillByDefault(…);
//
// // Oops! Which overload did you want?
// ON_CALL(mock, OverloadedMethod).WillByDefault(…);
// => ERROR: call to member function 'gmock_OverloadedMethod' is ambiguous
//
// How this works: The mock class uses two overloads of the gmock_Method
// expectation setter method plus an operator() overload on the MockSpec object.
// In the matcher list form, the macro expands to:
//
// // This statement:
// ON_CALL(mock, TwoArgsMethod(_, 45))…
//
// // …expands to:
// mock.gmock_TwoArgsMethod(_, 45)(WithoutMatchers(), nullptr)…
// |-------------v---------------||------------v-------------|
// invokes first overload swallowed by operator()
//
// // …which is essentially:
// mock.gmock_TwoArgsMethod(_, 45)…
//
// Whereas the form without a matcher list:
//
// // This statement:
// ON_CALL(mock, TwoArgsMethod)…
//
// // …expands to:
// mock.gmock_TwoArgsMethod(WithoutMatchers(), nullptr)…
// |-----------------------v--------------------------|
// invokes second overload
//
// // …which is essentially:
// mock.gmock_TwoArgsMethod(_, _)…
//
// The WithoutMatchers() argument is used to disambiguate overloads and to
// block the caller from accidentally invoking the second overload directly. The
// second argument is an internal type derived from the method signature. The
// failure to disambiguate two overloads of this method in the ON_CALL statement
// is how we block callers from setting expectations on overloaded methods.
#define GMOCK_ON_CALL_IMPL_(mock_expr, Setter, call) \
((mock_expr).gmock_##call)(::testing::internal::GetWithoutMatchers(), NULL) \
.Setter(__FILE__, __LINE__, #mock_expr, #call)
#define ON_CALL(obj, call) \
GMOCK_ON_CALL_IMPL_(obj, InternalDefaultActionSetAt, call)
#define EXPECT_CALL(obj, call) \
GMOCK_ON_CALL_IMPL_(obj, InternalExpectedAt, call)
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
#endif // GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_
googlemock/include/gmock/internal/gmock-internal-utils.h
View file @
a6f06bf2
...
@@ -344,6 +344,21 @@ GTEST_API_ bool LogIsVisible(LogSeverity severity);
...
@@ -344,6 +344,21 @@ GTEST_API_ bool LogIsVisible(LogSeverity severity);
GTEST_API_
void
Log
(
LogSeverity
severity
,
const
std
::
string
&
message
,
GTEST_API_
void
Log
(
LogSeverity
severity
,
const
std
::
string
&
message
,
int
stack_frames_to_skip
);
int
stack_frames_to_skip
);
// A marker class that is used to resolve parameterless expectations to the
// correct overload. This must not be instantiable, to prevent client code from
// accidentally resolving to the overload; for example:
//
// ON_CALL(mock, Method({}, nullptr))…
//
class
WithoutMatchers
{
private
:
WithoutMatchers
()
{}
friend
GTEST_API_
WithoutMatchers
GetWithoutMatchers
();
};
// Internal use only: access the singleton instance of WithoutMatchers.
GTEST_API_
WithoutMatchers
GetWithoutMatchers
();
// TODO(wan@google.com): group all type utilities together.
// TODO(wan@google.com): group all type utilities together.
// Type traits.
// Type traits.
...
...
googlemock/src/gmock-internal-utils.cc
View file @
a6f06bf2
...
@@ -188,6 +188,8 @@ GTEST_API_ void Log(LogSeverity severity, const std::string& message,
...
@@ -188,6 +188,8 @@ GTEST_API_ void Log(LogSeverity severity, const std::string& message,
std
::
cout
<<
::
std
::
flush
;
std
::
cout
<<
::
std
::
flush
;
}
}
GTEST_API_
WithoutMatchers
GetWithoutMatchers
()
{
return
WithoutMatchers
();
}
GTEST_API_
void
IllegalDoDefault
(
const
char
*
file
,
int
line
)
{
GTEST_API_
void
IllegalDoDefault
(
const
char
*
file
,
int
line
)
{
internal
::
Assert
(
internal
::
Assert
(
false
,
file
,
line
,
false
,
file
,
line
,
...
...
googlemock/test/gmock-matchers_test.cc
View file @
a6f06bf2
...
@@ -3093,6 +3093,44 @@ TEST(AllArgsTest, WorksInWithClause) {
...
@@ -3093,6 +3093,44 @@ TEST(AllArgsTest, WorksInWithClause) {
EXPECT_EQ
(
2
,
helper
.
Helper
(
'a'
,
1
));
EXPECT_EQ
(
2
,
helper
.
Helper
(
'a'
,
1
));
}
}
class
OptionalMatchersHelper
{
public
:
OptionalMatchersHelper
()
{}
MOCK_METHOD0
(
NoArgs
,
int
());
MOCK_METHOD1
(
OneArg
,
int
(
int
y
));
MOCK_METHOD2
(
TwoArgs
,
int
(
char
x
,
int
y
));
MOCK_METHOD1
(
Overloaded
,
int
(
char
x
));
MOCK_METHOD2
(
Overloaded
,
int
(
char
x
,
int
y
));
private
:
GTEST_DISALLOW_COPY_AND_ASSIGN_
(
OptionalMatchersHelper
);
};
TEST
(
AllArgsTest
,
WorksWithoutMatchers
)
{
OptionalMatchersHelper
helper
;
ON_CALL
(
helper
,
NoArgs
).
WillByDefault
(
Return
(
10
));
ON_CALL
(
helper
,
OneArg
).
WillByDefault
(
Return
(
20
));
ON_CALL
(
helper
,
TwoArgs
).
WillByDefault
(
Return
(
30
));
EXPECT_EQ
(
10
,
helper
.
NoArgs
());
EXPECT_EQ
(
20
,
helper
.
OneArg
(
1
));
EXPECT_EQ
(
30
,
helper
.
TwoArgs
(
'\1'
,
2
));
EXPECT_CALL
(
helper
,
NoArgs
).
Times
(
1
);
EXPECT_CALL
(
helper
,
OneArg
).
WillOnce
(
Return
(
100
));
EXPECT_CALL
(
helper
,
OneArg
(
17
)).
WillOnce
(
Return
(
200
));
EXPECT_CALL
(
helper
,
TwoArgs
).
Times
(
0
);
EXPECT_EQ
(
10
,
helper
.
NoArgs
());
EXPECT_EQ
(
100
,
helper
.
OneArg
(
1
));
EXPECT_EQ
(
200
,
helper
.
OneArg
(
17
));
}
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
// Tests that ASSERT_THAT() and EXPECT_THAT() work when the value
// matches the matcher.
// matches the matcher.
TEST
(
MatcherAssertionTest
,
WorksWhenMatcherIsSatisfied
)
{
TEST
(
MatcherAssertionTest
,
WorksWhenMatcherIsSatisfied
)
{
...
@@ -6726,4 +6764,3 @@ TEST(NotTest, WorksOnMoveOnlyType) {
...
@@ -6726,4 +6764,3 @@ TEST(NotTest, WorksOnMoveOnlyType) {
#if defined_MSC_VER
#if defined_MSC_VER
# pragma warning(pop)
# pragma warning(pop)
#endif
#endif
googlemock/test/gmock-spec-builders_test.cc
View file @
a6f06bf2
...
@@ -89,6 +89,7 @@ using testing::Mock;
...
@@ -89,6 +89,7 @@ using testing::Mock;
using
testing
::
NaggyMock
;
using
testing
::
NaggyMock
;
using
testing
::
Ne
;
using
testing
::
Ne
;
using
testing
::
Return
;
using
testing
::
Return
;
using
testing
::
SaveArg
;
using
testing
::
Sequence
;
using
testing
::
Sequence
;
using
testing
::
SetArgPointee
;
using
testing
::
SetArgPointee
;
using
testing
::
internal
::
ExpectationTester
;
using
testing
::
internal
::
ExpectationTester
;
...
@@ -2681,6 +2682,75 @@ TEST(SynchronizationTest, CanCallMockMethodInAction) {
...
@@ -2681,6 +2682,75 @@ TEST(SynchronizationTest, CanCallMockMethodInAction) {
// EXPECT_CALL() did not specify an action.
// EXPECT_CALL() did not specify an action.
}
}
TEST
(
ParameterlessExpectationsTest
,
CanSetExpectationsWithoutMatchers
)
{
MockA
a
;
int
do_a_arg0
=
0
;
ON_CALL
(
a
,
DoA
).
WillByDefault
(
SaveArg
<
0
>
(
&
do_a_arg0
));
int
do_a_47_arg0
=
0
;
ON_CALL
(
a
,
DoA
(
47
)).
WillByDefault
(
SaveArg
<
0
>
(
&
do_a_47_arg0
));
a
.
DoA
(
17
);
EXPECT_THAT
(
do_a_arg0
,
17
);
EXPECT_THAT
(
do_a_47_arg0
,
0
);
a
.
DoA
(
47
);
EXPECT_THAT
(
do_a_arg0
,
17
);
EXPECT_THAT
(
do_a_47_arg0
,
47
);
ON_CALL
(
a
,
Binary
).
WillByDefault
(
Return
(
true
));
ON_CALL
(
a
,
Binary
(
_
,
14
)).
WillByDefault
(
Return
(
false
));
EXPECT_THAT
(
a
.
Binary
(
14
,
17
),
true
);
EXPECT_THAT
(
a
.
Binary
(
17
,
14
),
false
);
}
TEST
(
ParameterlessExpectationsTest
,
CanSetExpectationsForOverloadedMethods
)
{
MockB
b
;
ON_CALL
(
b
,
DoB
()).
WillByDefault
(
Return
(
9
));
ON_CALL
(
b
,
DoB
(
5
)).
WillByDefault
(
Return
(
11
));
EXPECT_THAT
(
b
.
DoB
(),
9
);
EXPECT_THAT
(
b
.
DoB
(
1
),
0
);
// default value
EXPECT_THAT
(
b
.
DoB
(
5
),
11
);
}
struct
MockWithConstMethods
{
public
:
MOCK_CONST_METHOD1
(
Foo
,
int
(
int
));
MOCK_CONST_METHOD2
(
Bar
,
int
(
int
,
const
char
*
));
};
TEST
(
ParameterlessExpectationsTest
,
CanSetExpectationsForConstMethods
)
{
MockWithConstMethods
mock
;
ON_CALL
(
mock
,
Foo
).
WillByDefault
(
Return
(
7
));
ON_CALL
(
mock
,
Bar
).
WillByDefault
(
Return
(
33
));
EXPECT_THAT
(
mock
.
Foo
(
17
),
7
);
EXPECT_THAT
(
mock
.
Bar
(
27
,
"purple"
),
33
);
}
class
MockConstOverload
{
public
:
MOCK_METHOD1
(
Overloaded
,
int
(
int
));
MOCK_CONST_METHOD1
(
Overloaded
,
int
(
int
));
};
TEST
(
ParameterlessExpectationsTest
,
CanSetExpectationsForConstOverloadedMethods
)
{
MockConstOverload
mock
;
ON_CALL
(
mock
,
Overloaded
(
_
)).
WillByDefault
(
Return
(
7
));
ON_CALL
(
mock
,
Overloaded
(
5
)).
WillByDefault
(
Return
(
9
));
ON_CALL
(
Const
(
mock
),
Overloaded
(
5
)).
WillByDefault
(
Return
(
11
));
ON_CALL
(
Const
(
mock
),
Overloaded
(
7
)).
WillByDefault
(
Return
(
13
));
EXPECT_THAT
(
mock
.
Overloaded
(
1
),
7
);
EXPECT_THAT
(
mock
.
Overloaded
(
5
),
9
);
EXPECT_THAT
(
mock
.
Overloaded
(
7
),
7
);
const
MockConstOverload
&
const_mock
=
mock
;
EXPECT_THAT
(
const_mock
.
Overloaded
(
1
),
0
);
EXPECT_THAT
(
const_mock
.
Overloaded
(
5
),
11
);
EXPECT_THAT
(
const_mock
.
Overloaded
(
7
),
13
);
}
}
// namespace
}
// namespace
// Allows the user to define their own main and then invoke gmock_main
// Allows the user to define their own main and then invoke gmock_main
...
...
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