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
08b78779
Commit
08b78779
authored
Jul 07, 2020
by
ofats
Committed by
Gennadiy Rozental
Jul 09, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Googletest export
Replace ByRef with std::ref everywhere in docs. PiperOrigin-RevId: 320002303
parent
9aaaaf3f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
21 deletions
+18
-21
cheat_sheet.md
googlemock/docs/cheat_sheet.md
+6
-6
cook_book.md
googlemock/docs/cook_book.md
+12
-15
No files found.
googlemock/docs/cheat_sheet.md
View file @
08b78779
...
...
@@ -279,9 +279,10 @@ Matcher | Description
Except
`Ref()`
, these matchers make a
*copy*
of
`value`
in case it's modified or
destructed later. If the compiler complains that
`value`
doesn't have a public
copy constructor, try wrap it in
`ByRef()`
, e.g.
`Eq(ByRef(non_copyable_value))`
. If you do that, make sure
`non_copyable_value`
is not changed afterwards, or the meaning of your matcher will be changed.
copy constructor, try wrap it in
`std::ref()`
, e.g.
`Eq(std::ref(non_copyable_value))`
. If you do that, make sure
`non_copyable_value`
is not changed afterwards, or the meaning of your matcher
will be changed.
`IsTrue`
and
`IsFalse`
are useful when you need to use a matcher, or for types
that can be explicitly converted to Boolean, but are not implicitly converted to
...
...
@@ -586,13 +587,12 @@ callback type instead of a derived one, e.g.
```
In
`InvokeArgument<N>(...)`
, if an argument needs to be passed by reference,
wrap it inside
`
ByR
ef()`
. For example,
wrap it inside
`
std::r
ef()`
. For example,
```
cpp
using
::
testing
::
ByRef
;
using
::
testing
::
InvokeArgument
;
...
InvokeArgument
<
2
>
(
5
,
string
(
"Hi"
),
ByR
ef
(
foo
))
InvokeArgument
<
2
>
(
5
,
string
(
"Hi"
),
std
::
r
ef
(
foo
))
```
calls the mock function's #2 argument, passing to it
`5`
and
`string("Hi")`
by
...
...
googlemock/docs/cook_book.md
View file @
08b78779
...
...
@@ -1180,15 +1180,14 @@ executed. Just tell gMock that it should save a reference to `bar`, instead of a
copy of it. Here's how:
```
cpp
using
::
testing
::
ByRef
;
using
::
testing
::
Eq
;
using
::
testing
::
Lt
;
...
// Expects that Foo()'s argument == bar.
EXPECT_CALL
(
mock_obj
,
Foo
(
Eq
(
ByR
ef
(
bar
))));
EXPECT_CALL
(
mock_obj
,
Foo
(
Eq
(
std
::
r
ef
(
bar
))));
// Expects that Foo()'s argument < bar.
EXPECT_CALL
(
mock_obj
,
Foo
(
Lt
(
ByR
ef
(
bar
))));
EXPECT_CALL
(
mock_obj
,
Foo
(
Lt
(
std
::
r
ef
(
bar
))));
```
Remember: if you do this, don't change
`bar`
after the
`EXPECT_CALL()`
, or the
...
...
@@ -1851,10 +1850,9 @@ Methods"). However, gMock doesn't let you use `ReturnRef()` in a mock function
whose return type is not a reference, as doing that usually indicates a user
error. So, what shall you do?
Though you may be tempted, DO NOT use
`
ByR
ef()`
:
Though you may be tempted, DO NOT use
`
std::r
ef()`
:
```
cpp
using
testing
::
ByRef
;
using
testing
::
Return
;
class
MockFoo
:
public
Foo
{
...
...
@@ -1865,7 +1863,7 @@ class MockFoo : public Foo {
int
x
=
0
;
MockFoo
foo
;
EXPECT_CALL
(
foo
,
GetValue
())
.
WillRepeatedly
(
Return
(
ByR
ef
(
x
)));
// Wrong!
.
WillRepeatedly
(
Return
(
std
::
r
ef
(
x
)));
// Wrong!
x
=
42
;
EXPECT_EQ
(
42
,
foo
.
GetValue
());
```
...
...
@@ -1881,9 +1879,9 @@ Expected: 42
The reason is that
`Return(*value*)`
converts
`value`
to the actual return type
of the mock function at the time when the action is
*created*
, not when it is
*executed*
. (This behavior was chosen for the action to be safe when
`value`
is
a proxy object that references some temporary objects.) As a result,
`ByRef(x)`
is converted to an
`int`
value (instead of a
`const int&`
) when the expectatio
n
is set, and
`Return(ByR
ef(x))`
will always return 0.
a proxy object that references some temporary objects.) As a result,
`std::ref(x)`
is converted to an
`int`
value (instead of a
`const int&`
) whe
n
the expectation is set, and
`Return(std::r
ef(x))`
will always return 0.
`ReturnPointee(pointer)`
was provided to solve this problem specifically. It
returns the value pointed to by
`pointer`
at the time the action is
*executed*
:
...
...
@@ -2376,7 +2374,7 @@ using ::testing::InvokeArgument;
```
What if the callable takes an argument by reference? No problem - just wrap it
inside
`
ByR
ef()`
:
inside
`
std::r
ef()`
:
```
cpp
...
...
...
@@ -2385,20 +2383,19 @@ inside `ByRef()`:
(
override
));
...
using
::
testing
::
_
;
using
::
testing
::
ByRef
;
using
::
testing
::
InvokeArgument
;
...
MockFoo
foo
;
Helper
helper
;
...
EXPECT_CALL
(
foo
,
Bar
(
_
))
.
WillOnce
(
InvokeArgument
<
0
>
(
5
,
ByR
ef
(
helper
)));
//
ByRef(helper) guarantees that a reference to helper, not a copy of it,
// will be passed to the callback.
.
WillOnce
(
InvokeArgument
<
0
>
(
5
,
std
::
r
ef
(
helper
)));
//
std::ref(helper) guarantees that a reference to helper, not a copy of
//
it,
will be passed to the callback.
```
What if the callable takes an argument by reference and we do
**not**
wrap the
argument in
`
ByR
ef()`
? Then
`InvokeArgument()`
will
*make a copy*
of the
argument in
`
std::r
ef()`
? Then
`InvokeArgument()`
will
*make a copy*
of the
argument, and pass a
*reference to the copy*
, instead of a reference to the
original value, to the callable. This is especially handy when the argument is a
temporary value:
...
...
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