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
2d970ee3
Commit
2d970ee3
authored
Sep 24, 2009
by
zhanyong.wan
Browse files
Adds the IsNull() matcher.
parent
f7af24c7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
0 deletions
+53
-0
include/gmock/gmock-matchers.h
include/gmock/gmock-matchers.h
+18
-0
test/gmock-matchers_test.cc
test/gmock-matchers_test.cc
+27
-0
test/gmock_link_test.h
test/gmock_link_test.h
+8
-0
No files found.
include/gmock/gmock-matchers.h
View file @
2d970ee3
...
@@ -621,6 +621,19 @@ GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to");
...
@@ -621,6 +621,19 @@ GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to");
#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_
#undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_
// Implements the polymorphic IsNull() matcher, which matches any
// pointer that is NULL.
class
IsNullMatcher
{
public:
template
<
typename
T
>
bool
Matches
(
T
*
p
)
const
{
return
p
==
NULL
;
}
void
DescribeTo
(
::
std
::
ostream
*
os
)
const
{
*
os
<<
"is NULL"
;
}
void
DescribeNegationTo
(
::
std
::
ostream
*
os
)
const
{
*
os
<<
"is not NULL"
;
}
};
// Implements the polymorphic NotNull() matcher, which matches any
// Implements the polymorphic NotNull() matcher, which matches any
// pointer that is not NULL.
// pointer that is not NULL.
class
NotNullMatcher
{
class
NotNullMatcher
{
...
@@ -2319,6 +2332,11 @@ inline internal::NeMatcher<Rhs> Ne(Rhs x) {
...
@@ -2319,6 +2332,11 @@ inline internal::NeMatcher<Rhs> Ne(Rhs x) {
return
internal
::
NeMatcher
<
Rhs
>
(
x
);
return
internal
::
NeMatcher
<
Rhs
>
(
x
);
}
}
// Creates a polymorphic matcher that matches any NULL pointer.
inline
PolymorphicMatcher
<
internal
::
IsNullMatcher
>
IsNull
()
{
return
MakePolymorphicMatcher
(
internal
::
IsNullMatcher
());
}
// Creates a polymorphic matcher that matches any non-NULL pointer.
// Creates a polymorphic matcher that matches any non-NULL pointer.
// This is convenient as Not(NULL) doesn't compile (the compiler
// This is convenient as Not(NULL) doesn't compile (the compiler
// thinks that that expression is comparing a pointer with an integer).
// thinks that that expression is comparing a pointer with an integer).
...
...
test/gmock-matchers_test.cc
View file @
2d970ee3
...
@@ -78,6 +78,7 @@ using testing::FloatEq;
...
@@ -78,6 +78,7 @@ using testing::FloatEq;
using
testing
::
Ge
;
using
testing
::
Ge
;
using
testing
::
Gt
;
using
testing
::
Gt
;
using
testing
::
HasSubstr
;
using
testing
::
HasSubstr
;
using
testing
::
IsNull
;
using
testing
::
Key
;
using
testing
::
Key
;
using
testing
::
Le
;
using
testing
::
Le
;
using
testing
::
Lt
;
using
testing
::
Lt
;
...
@@ -685,6 +686,32 @@ TEST(NeTest, CanDescribeSelf) {
...
@@ -685,6 +686,32 @@ TEST(NeTest, CanDescribeSelf) {
EXPECT_EQ
(
"is not equal to 5"
,
Describe
(
m
));
EXPECT_EQ
(
"is not equal to 5"
,
Describe
(
m
));
}
}
// Tests that IsNull() matches any NULL pointer of any type.
TEST
(
IsNullTest
,
MatchesNullPointer
)
{
Matcher
<
int
*>
m1
=
IsNull
();
int
*
p1
=
NULL
;
int
n
=
0
;
EXPECT_TRUE
(
m1
.
Matches
(
p1
));
EXPECT_FALSE
(
m1
.
Matches
(
&
n
));
Matcher
<
const
char
*>
m2
=
IsNull
();
const
char
*
p2
=
NULL
;
EXPECT_TRUE
(
m2
.
Matches
(
p2
));
EXPECT_FALSE
(
m2
.
Matches
(
"hi"
));
Matcher
<
void
*>
m3
=
IsNull
();
void
*
p3
=
NULL
;
EXPECT_TRUE
(
m3
.
Matches
(
p3
));
EXPECT_FALSE
(
m3
.
Matches
(
reinterpret_cast
<
void
*>
(
0xbeef
)));
}
// Tests that IsNull() describes itself properly.
TEST
(
IsNullTest
,
CanDescribeSelf
)
{
Matcher
<
int
*>
m
=
IsNull
();
EXPECT_EQ
(
"is NULL"
,
Describe
(
m
));
EXPECT_EQ
(
"is not NULL"
,
DescribeNegation
(
m
));
}
// Tests that NotNull() matches any non-NULL pointer of any type.
// Tests that NotNull() matches any non-NULL pointer of any type.
TEST
(
NotNullTest
,
MatchesNonNullPointer
)
{
TEST
(
NotNullTest
,
MatchesNonNullPointer
)
{
Matcher
<
int
*>
m1
=
NotNull
();
Matcher
<
int
*>
m1
=
NotNull
();
...
...
test/gmock_link_test.h
View file @
2d970ee3
...
@@ -147,6 +147,7 @@ using testing::IgnoreResult;
...
@@ -147,6 +147,7 @@ using testing::IgnoreResult;
using
testing
::
Invoke
;
using
testing
::
Invoke
;
using
testing
::
InvokeArgument
;
using
testing
::
InvokeArgument
;
using
testing
::
InvokeWithoutArgs
;
using
testing
::
InvokeWithoutArgs
;
using
testing
::
IsNull
;
using
testing
::
Le
;
using
testing
::
Le
;
using
testing
::
Lt
;
using
testing
::
Lt
;
using
testing
::
Matcher
;
using
testing
::
Matcher
;
...
@@ -491,6 +492,13 @@ TEST(LinkTest, TestMatcherNotNull) {
...
@@ -491,6 +492,13 @@ TEST(LinkTest, TestMatcherNotNull) {
ON_CALL
(
mock
,
VoidFromString
(
NotNull
())).
WillByDefault
(
Return
());
ON_CALL
(
mock
,
VoidFromString
(
NotNull
())).
WillByDefault
(
Return
());
}
}
// Tests the linkage of the IsNull matcher.
TEST
(
LinkTest
,
TestMatcherIsNull
)
{
Mock
mock
;
ON_CALL
(
mock
,
VoidFromString
(
IsNull
())).
WillByDefault
(
Return
());
}
// Tests the linkage of the Ref matcher.
// Tests the linkage of the Ref matcher.
TEST
(
LinkTest
,
TestMatcherRef
)
{
TEST
(
LinkTest
,
TestMatcherRef
)
{
Mock
mock
;
Mock
mock
;
...
...
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