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
9acd065a
Commit
9acd065a
authored
Jan 12, 2019
by
Abseil Team
Committed by
Gennadiy Civil
Jan 14, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Googletest export
Add move-only argument support to almost all remaining matchers. PiperOrigin-RevId: 229030728
parent
097407fd
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
34 deletions
+98
-34
gmock-matchers.h
googlemock/include/gmock/gmock-matchers.h
+6
-6
gmock-matchers_test.cc
googlemock/test/gmock-matchers_test.cc
+86
-21
gtest-matchers.h
googletest/include/gtest/gtest-matchers.h
+6
-7
No files found.
googlemock/include/gmock/gmock-matchers.h
View file @
9acd065a
...
...
@@ -900,7 +900,7 @@ class PairMatchBase {
public
:
template
<
typename
T1
,
typename
T2
>
operator
Matcher
<::
std
::
tuple
<
T1
,
T2
>>
()
const
{
return
Ma
keMatcher
(
new
Impl
<::
std
::
tuple
<
T1
,
T2
>
>
);
return
Ma
tcher
<::
std
::
tuple
<
T1
,
T2
>>
(
new
Impl
<
const
::
std
::
tuple
<
T1
,
T2
>&
>
);
}
template
<
typename
T1
,
typename
T2
>
operator
Matcher
<
const
::
std
::
tuple
<
T1
,
T2
>&>
()
const
{
...
...
@@ -2535,7 +2535,8 @@ class KeyMatcher {
template
<
typename
PairType
>
operator
Matcher
<
PairType
>
()
const
{
return
MakeMatcher
(
new
KeyMatcherImpl
<
PairType
>
(
matcher_for_key_
));
return
Matcher
<
PairType
>
(
new
KeyMatcherImpl
<
const
PairType
&>
(
matcher_for_key_
));
}
private
:
...
...
@@ -2640,9 +2641,8 @@ class PairMatcher {
template
<
typename
PairType
>
operator
Matcher
<
PairType
>
()
const
{
return
MakeMatcher
(
new
PairMatcherImpl
<
PairType
>
(
first_matcher_
,
second_matcher_
));
return
Matcher
<
PairType
>
(
new
PairMatcherImpl
<
const
PairType
&>
(
first_matcher_
,
second_matcher_
));
}
private
:
...
...
@@ -3205,7 +3205,7 @@ class OptionalMatcher {
template
<
typename
Optional
>
operator
Matcher
<
Optional
>
()
const
{
return
Ma
keMatcher
(
new
Impl
<
Optional
>
(
value_matcher_
));
return
Ma
tcher
<
Optional
>
(
new
Impl
<
const
Optional
&
>
(
value_matcher_
));
}
template
<
typename
Optional
>
...
...
googlemock/test/gmock-matchers_test.cc
View file @
9acd065a
...
...
@@ -1095,6 +1095,47 @@ TEST(NeTest, CanDescribeSelf) {
EXPECT_EQ
(
"isn't equal to 5"
,
Describe
(
m
));
}
class
MoveOnly
{
public
:
explicit
MoveOnly
(
int
i
)
:
i_
(
i
)
{}
MoveOnly
(
const
MoveOnly
&
)
=
delete
;
MoveOnly
(
MoveOnly
&&
)
=
default
;
MoveOnly
&
operator
=
(
const
MoveOnly
&
)
=
delete
;
MoveOnly
&
operator
=
(
MoveOnly
&&
)
=
default
;
bool
operator
==
(
const
MoveOnly
&
other
)
const
{
return
i_
==
other
.
i_
;
}
bool
operator
!=
(
const
MoveOnly
&
other
)
const
{
return
i_
!=
other
.
i_
;
}
bool
operator
<
(
const
MoveOnly
&
other
)
const
{
return
i_
<
other
.
i_
;
}
bool
operator
<=
(
const
MoveOnly
&
other
)
const
{
return
i_
<=
other
.
i_
;
}
bool
operator
>
(
const
MoveOnly
&
other
)
const
{
return
i_
>
other
.
i_
;
}
bool
operator
>=
(
const
MoveOnly
&
other
)
const
{
return
i_
>=
other
.
i_
;
}
private
:
int
i_
;
};
struct
MoveHelper
{
MOCK_METHOD1
(
Call
,
void
(
MoveOnly
));
};
TEST
(
ComparisonBaseTest
,
WorksWithMoveOnly
)
{
MoveOnly
m
{
0
};
MoveHelper
helper
;
EXPECT_CALL
(
helper
,
Call
(
Eq
(
ByRef
(
m
))));
helper
.
Call
(
MoveOnly
(
0
));
EXPECT_CALL
(
helper
,
Call
(
Ne
(
ByRef
(
m
))));
helper
.
Call
(
MoveOnly
(
1
));
EXPECT_CALL
(
helper
,
Call
(
Le
(
ByRef
(
m
))));
helper
.
Call
(
MoveOnly
(
0
));
EXPECT_CALL
(
helper
,
Call
(
Lt
(
ByRef
(
m
))));
helper
.
Call
(
MoveOnly
(
-
1
));
EXPECT_CALL
(
helper
,
Call
(
Ge
(
ByRef
(
m
))));
helper
.
Call
(
MoveOnly
(
0
));
EXPECT_CALL
(
helper
,
Call
(
Gt
(
ByRef
(
m
))));
helper
.
Call
(
MoveOnly
(
1
));
}
// Tests that IsNull() matches any NULL pointer of any type.
TEST
(
IsNullTest
,
MatchesNullPointer
)
{
Matcher
<
int
*>
m1
=
IsNull
();
...
...
@@ -1448,6 +1489,11 @@ TEST(KeyTest, MatchesCorrectly) {
EXPECT_THAT
(
p
,
Not
(
Key
(
Lt
(
25
))));
}
TEST
(
KeyTest
,
WorksWithMoveOnly
)
{
pair
<
std
::
unique_ptr
<
int
>
,
std
::
unique_ptr
<
int
>>
p
;
EXPECT_THAT
(
p
,
Key
(
Eq
(
nullptr
)));
}
template
<
size_t
I
>
struct
Tag
{};
...
...
@@ -1591,6 +1637,12 @@ TEST(PairTest, MatchesCorrectly) {
EXPECT_THAT
(
p
,
Not
(
Pair
(
Lt
(
13
),
HasSubstr
(
"a"
))));
}
TEST
(
PairTest
,
WorksWithMoveOnly
)
{
pair
<
std
::
unique_ptr
<
int
>
,
std
::
unique_ptr
<
int
>>
p
;
p
.
second
.
reset
(
new
int
(
7
));
EXPECT_THAT
(
p
,
Pair
(
Eq
(
nullptr
),
Ne
(
nullptr
)));
}
TEST
(
PairTest
,
SafelyCastsInnerMatchers
)
{
Matcher
<
int
>
is_positive
=
Gt
(
0
);
Matcher
<
int
>
is_negative
=
Lt
(
0
);
...
...
@@ -2244,6 +2296,15 @@ TEST(Ne2Test, CanDescribeSelf) {
EXPECT_EQ
(
"are an unequal pair"
,
Describe
(
m
));
}
TEST
(
PairMatchBaseTest
,
WorksWithMoveOnly
)
{
using
Pointers
=
std
::
tuple
<
std
::
unique_ptr
<
int
>
,
std
::
unique_ptr
<
int
>>
;
Matcher
<
Pointers
>
matcher
=
Eq
();
Pointers
pointers
;
// Tested values don't matter; the point is that matcher does not copy the
// matched values.
EXPECT_TRUE
(
matcher
.
Matches
(
pointers
));
}
// Tests that FloatEq() matches a 2-tuple where
// FloatEq(first field) matches the second field.
TEST
(
FloatEq2Test
,
MatchesEqualArguments
)
{
...
...
@@ -6566,49 +6627,53 @@ TEST(UnorderedPointwiseTest, WorksWithMoveOnly) {
// Sample optional type implementation with minimal requirements for use with
// Optional matcher.
class
SampleOptionalInt
{
template
<
typename
T
>
class
SampleOptional
{
public
:
typedef
int
value_type
;
explicit
SampleOptionalInt
(
int
value
)
:
value_
(
value
),
has_value_
(
true
)
{}
SampleOptionalInt
()
:
value_
(
0
),
has_value_
(
false
)
{}
operator
bool
()
const
{
return
has_value_
;
}
const
int
&
operator
*
()
const
{
return
value_
;
}
using
value_type
=
T
;
explicit
SampleOptional
(
T
value
)
:
value_
(
std
::
move
(
value
)),
has_value_
(
true
)
{}
SampleOptional
()
:
value_
(),
has_value_
(
false
)
{}
operator
bool
()
const
{
return
has_value_
;
}
const
T
&
operator
*
()
const
{
return
value_
;
}
private
:
int
value_
;
T
value_
;
bool
has_value_
;
};
TEST
(
OptionalTest
,
DescribesSelf
)
{
const
Matcher
<
SampleOptional
Int
>
m
=
Optional
(
Eq
(
1
));
const
Matcher
<
SampleOptional
<
int
>
>
m
=
Optional
(
Eq
(
1
));
EXPECT_EQ
(
"value is equal to 1"
,
Describe
(
m
));
}
TEST
(
OptionalTest
,
ExplainsSelf
)
{
const
Matcher
<
SampleOptional
Int
>
m
=
Optional
(
Eq
(
1
));
EXPECT_EQ
(
"whose value 1 matches"
,
Explain
(
m
,
SampleOptional
Int
(
1
)));
EXPECT_EQ
(
"whose value 2 doesn't match"
,
Explain
(
m
,
SampleOptional
Int
(
2
)));
const
Matcher
<
SampleOptional
<
int
>
>
m
=
Optional
(
Eq
(
1
));
EXPECT_EQ
(
"whose value 1 matches"
,
Explain
(
m
,
SampleOptional
<
int
>
(
1
)));
EXPECT_EQ
(
"whose value 2 doesn't match"
,
Explain
(
m
,
SampleOptional
<
int
>
(
2
)));
}
TEST
(
OptionalTest
,
MatchesNonEmptyOptional
)
{
const
Matcher
<
SampleOptional
Int
>
m1
=
Optional
(
1
);
const
Matcher
<
SampleOptional
Int
>
m2
=
Optional
(
Eq
(
2
));
const
Matcher
<
SampleOptional
Int
>
m3
=
Optional
(
Lt
(
3
));
SampleOptional
Int
opt
(
1
);
const
Matcher
<
SampleOptional
<
int
>
>
m1
=
Optional
(
1
);
const
Matcher
<
SampleOptional
<
int
>
>
m2
=
Optional
(
Eq
(
2
));
const
Matcher
<
SampleOptional
<
int
>
>
m3
=
Optional
(
Lt
(
3
));
SampleOptional
<
int
>
opt
(
1
);
EXPECT_TRUE
(
m1
.
Matches
(
opt
));
EXPECT_FALSE
(
m2
.
Matches
(
opt
));
EXPECT_TRUE
(
m3
.
Matches
(
opt
));
}
TEST
(
OptionalTest
,
DoesNotMatchNullopt
)
{
const
Matcher
<
SampleOptional
Int
>
m
=
Optional
(
1
);
SampleOptional
Int
empty
;
const
Matcher
<
SampleOptional
<
int
>
>
m
=
Optional
(
1
);
SampleOptional
<
int
>
empty
;
EXPECT_FALSE
(
m
.
Matches
(
empty
));
}
TEST
(
OptionalTest
,
WorksWithMoveOnly
)
{
Matcher
<
SampleOptional
<
std
::
unique_ptr
<
int
>>>
m
=
Optional
(
Eq
(
nullptr
));
EXPECT_TRUE
(
m
.
Matches
(
SampleOptional
<
std
::
unique_ptr
<
int
>>
(
nullptr
)));
}
class
SampleVariantIntString
{
public
:
SampleVariantIntString
(
int
i
)
:
i_
(
i
),
has_int_
(
true
)
{}
...
...
googletest/include/gtest/gtest-matchers.h
View file @
9acd065a
...
...
@@ -555,13 +555,12 @@ class PolymorphicMatcher {
Impl
impl_
;
};
// Creates a matcher from its implementation.
This is easier to use
//
than the Matcher<T> constructor as it doesn't require you to
//
explicitly write the template argument, e.g.
// Creates a matcher from its implementation.
//
DEPRECATED: Especially in the generic code, prefer:
//
Matcher<T>(new MyMatcherImpl<const T&>(...));
//
// MakeMatcher(foo);
// vs
// Matcher<const string&>(foo);
// MakeMatcher may create a Matcher that accepts its argument by value, which
// leads to unnecessary copies & lack of support for non-copyable types.
template
<
typename
T
>
inline
Matcher
<
T
>
MakeMatcher
(
const
MatcherInterface
<
T
>*
impl
)
{
return
Matcher
<
T
>
(
impl
);
...
...
@@ -596,7 +595,7 @@ class ComparisonBase {
explicit
ComparisonBase
(
const
Rhs
&
rhs
)
:
rhs_
(
rhs
)
{}
template
<
typename
Lhs
>
operator
Matcher
<
Lhs
>
()
const
{
return
Ma
keMatcher
(
new
Impl
<
Lhs
>
(
rhs_
));
return
Ma
tcher
<
Lhs
>
(
new
Impl
<
const
Lhs
&
>
(
rhs_
));
}
private
:
...
...
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