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
5f2a6ca4
"googlemock/vscode:/vscode.git/clone" did not exist on "0f6885405c980d5479d6177f7223d4bc7bacca6b"
Commit
5f2a6ca4
authored
Dec 03, 2013
by
kosak
Browse files
Don't copy the argument in SafeMatcherCast because it's not safe.
parent
88080ee9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
5 deletions
+28
-5
include/gmock/gmock-matchers.h
include/gmock/gmock-matchers.h
+5
-5
test/gmock-matchers_test.cc
test/gmock-matchers_test.cc
+23
-0
No files found.
include/gmock/gmock-matchers.h
View file @
5f2a6ca4
...
@@ -489,7 +489,7 @@ namespace internal {
...
@@ -489,7 +489,7 @@ namespace internal {
template
<
typename
T
,
typename
M
>
template
<
typename
T
,
typename
M
>
class
MatcherCastImpl
{
class
MatcherCastImpl
{
public:
public:
static
Matcher
<
T
>
Cast
(
M
polymorphic_matcher_or_value
)
{
static
Matcher
<
T
>
Cast
(
const
M
&
polymorphic_matcher_or_value
)
{
// M can be a polymorhic matcher, in which case we want to use
// M can be a polymorhic matcher, in which case we want to use
// its conversion operator to create Matcher<T>. Or it can be a value
// its conversion operator to create Matcher<T>. Or it can be a value
// that should be passed to the Matcher<T>'s constructor.
// that should be passed to the Matcher<T>'s constructor.
...
@@ -510,14 +510,14 @@ class MatcherCastImpl {
...
@@ -510,14 +510,14 @@ class MatcherCastImpl {
}
}
private:
private:
static
Matcher
<
T
>
CastImpl
(
M
value
,
BooleanConstant
<
false
>
)
{
static
Matcher
<
T
>
CastImpl
(
const
M
&
value
,
BooleanConstant
<
false
>
)
{
// M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic
// M can't be implicitly converted to Matcher<T>, so M isn't a polymorphic
// matcher. It must be a value then. Use direct initialization to create
// matcher. It must be a value then. Use direct initialization to create
// a matcher.
// a matcher.
return
Matcher
<
T
>
(
ImplicitCast_
<
T
>
(
value
));
return
Matcher
<
T
>
(
ImplicitCast_
<
T
>
(
value
));
}
}
static
Matcher
<
T
>
CastImpl
(
M
polymorphic_matcher_or_value
,
static
Matcher
<
T
>
CastImpl
(
const
M
&
polymorphic_matcher_or_value
,
BooleanConstant
<
true
>
)
{
BooleanConstant
<
true
>
)
{
// M is implicitly convertible to Matcher<T>, which means that either
// M is implicitly convertible to Matcher<T>, which means that either
// M is a polymorhpic matcher or Matcher<T> has an implicit constructor
// M is a polymorhpic matcher or Matcher<T> has an implicit constructor
...
@@ -582,7 +582,7 @@ class MatcherCastImpl<T, Matcher<T> > {
...
@@ -582,7 +582,7 @@ class MatcherCastImpl<T, Matcher<T> > {
// matcher m and returns a Matcher<T>. It compiles only when T can be
// matcher m and returns a Matcher<T>. It compiles only when T can be
// statically converted to the argument type of m.
// statically converted to the argument type of m.
template
<
typename
T
,
typename
M
>
template
<
typename
T
,
typename
M
>
inline
Matcher
<
T
>
MatcherCast
(
M
matcher
)
{
inline
Matcher
<
T
>
MatcherCast
(
const
M
&
matcher
)
{
return
internal
::
MatcherCastImpl
<
T
,
M
>::
Cast
(
matcher
);
return
internal
::
MatcherCastImpl
<
T
,
M
>::
Cast
(
matcher
);
}
}
...
@@ -599,7 +599,7 @@ class SafeMatcherCastImpl {
...
@@ -599,7 +599,7 @@ class SafeMatcherCastImpl {
// This overload handles polymorphic matchers and values only since
// This overload handles polymorphic matchers and values only since
// monomorphic matchers are handled by the next one.
// monomorphic matchers are handled by the next one.
template
<
typename
M
>
template
<
typename
M
>
static
inline
Matcher
<
T
>
Cast
(
M
polymorphic_matcher_or_value
)
{
static
inline
Matcher
<
T
>
Cast
(
const
M
&
polymorphic_matcher_or_value
)
{
return
internal
::
MatcherCastImpl
<
T
,
M
>::
Cast
(
polymorphic_matcher_or_value
);
return
internal
::
MatcherCastImpl
<
T
,
M
>::
Cast
(
polymorphic_matcher_or_value
);
}
}
...
...
test/gmock-matchers_test.cc
View file @
5f2a6ca4
...
@@ -636,6 +636,22 @@ TEST(MatcherCastTest, FromConvertibleFromAny) {
...
@@ -636,6 +636,22 @@ TEST(MatcherCastTest, FromConvertibleFromAny) {
EXPECT_FALSE
(
m
.
Matches
(
ConvertibleFromAny
(
2
)));
EXPECT_FALSE
(
m
.
Matches
(
ConvertibleFromAny
(
2
)));
}
}
struct
IntReferenceWrapper
{
IntReferenceWrapper
(
const
int
&
a_value
)
:
value
(
&
a_value
)
{}
const
int
*
value
;
};
bool
operator
==
(
const
IntReferenceWrapper
&
a
,
const
IntReferenceWrapper
&
b
)
{
return
a
.
value
==
b
.
value
;
}
TEST
(
MatcherCastTest
,
ValueIsNotCopied
)
{
int
n
=
42
;
Matcher
<
IntReferenceWrapper
>
m
=
MatcherCast
<
IntReferenceWrapper
>
(
n
);
// Verify that the matcher holds a reference to n, not to its temporary copy.
EXPECT_TRUE
(
m
.
Matches
(
n
));
}
class
Base
{};
class
Base
{};
class
Derived
:
public
Base
{};
class
Derived
:
public
Base
{};
...
@@ -724,6 +740,13 @@ TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
...
@@ -724,6 +740,13 @@ TEST(SafeMatcherCastTest, FromConvertibleFromAny) {
EXPECT_FALSE
(
m
.
Matches
(
ConvertibleFromAny
(
2
)));
EXPECT_FALSE
(
m
.
Matches
(
ConvertibleFromAny
(
2
)));
}
}
TEST
(
SafeMatcherCastTest
,
ValueIsNotCopied
)
{
int
n
=
42
;
Matcher
<
IntReferenceWrapper
>
m
=
SafeMatcherCast
<
IntReferenceWrapper
>
(
n
);
// Verify that the matcher holds a reference to n, not to its temporary copy.
EXPECT_TRUE
(
m
.
Matches
(
n
));
}
// Tests that A<T>() matches any value of type T.
// Tests that A<T>() matches any value of type T.
TEST
(
ATest
,
MatchesAnyValue
)
{
TEST
(
ATest
,
MatchesAnyValue
)
{
// Tests a matcher for a value type.
// Tests a matcher for a value type.
...
...
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