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
1f122a06
Commit
1f122a06
authored
Mar 25, 2013
by
zhanyong.wan
Browse files
Adds special support for matching StringPiece. Pulls in gtest r646.
parent
2eab17b7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
0 deletions
+118
-0
include/gmock/gmock-matchers.h
include/gmock/gmock-matchers.h
+45
-0
src/gmock-matchers.cc
src/gmock-matchers.cc
+35
-0
test/gmock-matchers_test.cc
test/gmock-matchers_test.cc
+38
-0
No files found.
include/gmock/gmock-matchers.h
View file @
1f122a06
...
...
@@ -318,6 +318,51 @@ class GTEST_API_ Matcher<internal::string>
Matcher
(
const
char
*
s
);
// NOLINT
};
#if GTEST_HAS_STRING_PIECE_
// The following two specializations allow the user to write str
// instead of Eq(str) and "foo" instead of Eq("foo") when a StringPiece
// matcher is expected.
template
<>
class
GTEST_API_
Matcher
<
const
StringPiece
&>
:
public
internal
::
MatcherBase
<
const
StringPiece
&>
{
public:
Matcher
()
{}
explicit
Matcher
(
const
MatcherInterface
<
const
StringPiece
&>*
impl
)
:
internal
::
MatcherBase
<
const
StringPiece
&>
(
impl
)
{}
// Allows the user to write str instead of Eq(str) sometimes, where
// str is a string object.
Matcher
(
const
internal
::
string
&
s
);
// NOLINT
// Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher
(
const
char
*
s
);
// NOLINT
// Allows the user to pass StringPieces directly.
Matcher
(
StringPiece
s
);
// NOLINT
};
template
<>
class
GTEST_API_
Matcher
<
StringPiece
>
:
public
internal
::
MatcherBase
<
StringPiece
>
{
public:
Matcher
()
{}
explicit
Matcher
(
const
MatcherInterface
<
StringPiece
>*
impl
)
:
internal
::
MatcherBase
<
StringPiece
>
(
impl
)
{}
// Allows the user to write str instead of Eq(str) sometimes, where
// str is a string object.
Matcher
(
const
internal
::
string
&
s
);
// NOLINT
// Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher
(
const
char
*
s
);
// NOLINT
// Allows the user to pass StringPieces directly.
Matcher
(
StringPiece
s
);
// NOLINT
};
#endif // GTEST_HAS_STRING_PIECE_
// The PolymorphicMatcher class template makes it easy to implement a
// polymorphic matcher (i.e. a matcher that can match values of more
// than one type, e.g. Eq(n) and NotNull()).
...
...
src/gmock-matchers.cc
View file @
1f122a06
...
...
@@ -63,6 +63,41 @@ Matcher<internal::string>::Matcher(const char* s) {
*
this
=
Eq
(
internal
::
string
(
s
));
}
#if GTEST_HAS_STRING_PIECE_
// Constructs a matcher that matches a const StringPiece& whose value is
// equal to s.
Matcher
<
const
StringPiece
&>::
Matcher
(
const
internal
::
string
&
s
)
{
*
this
=
Eq
(
s
);
}
// Constructs a matcher that matches a const StringPiece& whose value is
// equal to s.
Matcher
<
const
StringPiece
&>::
Matcher
(
const
char
*
s
)
{
*
this
=
Eq
(
internal
::
string
(
s
));
}
// Constructs a matcher that matches a const StringPiece& whose value is
// equal to s.
Matcher
<
const
StringPiece
&>::
Matcher
(
StringPiece
s
)
{
*
this
=
Eq
(
s
.
ToString
());
}
// Constructs a matcher that matches a StringPiece whose value is equal to s.
Matcher
<
StringPiece
>::
Matcher
(
const
internal
::
string
&
s
)
{
*
this
=
Eq
(
s
);
}
// Constructs a matcher that matches a StringPiece whose value is equal to s.
Matcher
<
StringPiece
>::
Matcher
(
const
char
*
s
)
{
*
this
=
Eq
(
internal
::
string
(
s
));
}
// Constructs a matcher that matches a StringPiece whose value is equal to s.
Matcher
<
StringPiece
>::
Matcher
(
StringPiece
s
)
{
*
this
=
Eq
(
s
.
ToString
());
}
#endif // GTEST_HAS_STRING_PIECE_
namespace
internal
{
// Joins a vector of strings as if they are fields of a tuple; returns
...
...
test/gmock-matchers_test.cc
View file @
1f122a06
...
...
@@ -364,6 +364,44 @@ TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
EXPECT_FALSE
(
m2
.
Matches
(
"hello"
));
}
#if GTEST_HAS_STRING_PIECE_
// Tests that a C-string literal can be implicitly converted to a
// Matcher<StringPiece> or Matcher<const StringPiece&>.
TEST
(
StringPieceMatcherTest
,
CanBeImplicitlyConstructedFromCStringLiteral
)
{
Matcher
<
StringPiece
>
m1
=
"cats"
;
EXPECT_TRUE
(
m1
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m1
.
Matches
(
"dogs"
));
Matcher
<
const
StringPiece
&>
m2
=
"cats"
;
EXPECT_TRUE
(
m2
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m2
.
Matches
(
"dogs"
));
}
// Tests that a string object can be implicitly converted to a
// Matcher<StringPiece> or Matcher<const StringPiece&>.
TEST
(
StringPieceMatcherTest
,
CanBeImplicitlyConstructedFromString
)
{
Matcher
<
StringPiece
>
m1
=
string
(
"cats"
);
EXPECT_TRUE
(
m1
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m1
.
Matches
(
"dogs"
));
Matcher
<
const
StringPiece
&>
m2
=
string
(
"cats"
);
EXPECT_TRUE
(
m2
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m2
.
Matches
(
"dogs"
));
}
// Tests that a StringPiece object can be implicitly converted to a
// Matcher<StringPiece> or Matcher<const StringPiece&>.
TEST
(
StringPieceMatcherTest
,
CanBeImplicitlyConstructedFromStringPiece
)
{
Matcher
<
StringPiece
>
m1
=
StringPiece
(
"cats"
);
EXPECT_TRUE
(
m1
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m1
.
Matches
(
"dogs"
));
Matcher
<
const
StringPiece
&>
m2
=
StringPiece
(
"cats"
);
EXPECT_TRUE
(
m2
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m2
.
Matches
(
"dogs"
));
}
#endif // GTEST_HAS_STRING_PIECE_
// Tests that MakeMatcher() constructs a Matcher<T> from a
// MatcherInterface* without requiring the user to explicitly
// write the 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