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
909b1ccf
Commit
909b1ccf
authored
Feb 25, 2020
by
Abseil Team
Committed by
CJ Johnson
Feb 28, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Googletest export
Relax the implementation of MatcherCast to allow conversion of `Matcher<T>` to `Matcher<const T&>`. They have the same match signature. PiperOrigin-RevId: 297115843
parent
fd538161
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
7 deletions
+15
-7
gmock-matchers.h
googlemock/include/gmock/gmock-matchers.h
+10
-3
gmock-matchers_test.cc
googlemock/test/gmock-matchers_test.cc
+5
-4
No files found.
googlemock/include/gmock/gmock-matchers.h
View file @
909b1ccf
...
...
@@ -424,7 +424,14 @@ class MatcherCastImpl<T, Matcher<U> > {
!
std
::
is_base_of
<
FromType
,
ToType
>::
value
,
"Can't implicitly convert from <base> to <derived>"
);
return
source_matcher_
.
MatchAndExplain
(
static_cast
<
U
>
(
x
),
listener
);
// Do the cast to `U` explicitly if necessary.
// Otherwise, let implicit conversions do the trick.
using
CastType
=
typename
std
::
conditional
<
std
::
is_convertible
<
T
&
,
const
U
&>::
value
,
T
&
,
U
>::
type
;
return
source_matcher_
.
MatchAndExplain
(
static_cast
<
CastType
>
(
x
),
listener
);
}
void
DescribeTo
(
::
std
::
ostream
*
os
)
const
override
{
...
...
@@ -524,8 +531,8 @@ inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher_or_value) {
template
<
typename
T
,
typename
U
>
inline
Matcher
<
T
>
SafeMatcherCast
(
const
Matcher
<
U
>&
matcher
)
{
// Enforce that T can be implicitly converted to U.
GTEST_COMPILE_ASSERT_
((
std
::
is_convertible
<
T
,
U
>::
value
)
,
"T must be implicitly convertible to U"
);
static_assert
(
std
::
is_convertible
<
const
T
&
,
const
U
&>::
value
,
"T must be implicitly convertible to U"
);
// Enforce that we are not converting a non-reference type T to a reference
// type U.
GTEST_COMPILE_ASSERT_
(
...
...
googlemock/test/gmock-matchers_test.cc
View file @
909b1ccf
...
...
@@ -765,10 +765,11 @@ TEST(SafeMatcherCastTest, FromConstReferenceToReference) {
// Tests that MatcherCast<const T&>(m) works when m is a Matcher<T>.
TEST
(
SafeMatcherCastTest
,
FromNonReferenceToConstReference
)
{
Matcher
<
int
>
m1
=
Eq
(
0
);
Matcher
<
const
int
&>
m2
=
SafeMatcherCast
<
const
int
&>
(
m1
);
EXPECT_TRUE
(
m2
.
Matches
(
0
));
EXPECT_FALSE
(
m2
.
Matches
(
1
));
Matcher
<
std
::
unique_ptr
<
int
>>
m1
=
IsNull
();
Matcher
<
const
std
::
unique_ptr
<
int
>&>
m2
=
SafeMatcherCast
<
const
std
::
unique_ptr
<
int
>&>
(
m1
);
EXPECT_TRUE
(
m2
.
Matches
(
std
::
unique_ptr
<
int
>
()));
EXPECT_FALSE
(
m2
.
Matches
(
std
::
unique_ptr
<
int
>
(
new
int
)));
}
// Tests that SafeMatcherCast<T&>(m) works when m is a Matcher<T>.
...
...
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