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
6f5fd0d7
Commit
6f5fd0d7
authored
Feb 11, 2020
by
Abseil Team
Committed by
Mark Barolak
Feb 11, 2020
Browse files
Googletest export
Add gmock Matcher<std::string_view> specialization. PiperOrigin-RevId: 294443240
parent
d0930731
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
197 additions
and
164 deletions
+197
-164
googlemock/include/gmock/gmock-matchers.h
googlemock/include/gmock/gmock-matchers.h
+20
-20
googlemock/test/gmock-matchers_test.cc
googlemock/test/gmock-matchers_test.cc
+95
-94
googletest/include/gtest/gtest-matchers.h
googletest/include/gtest/gtest-matchers.h
+19
-19
googletest/include/gtest/gtest-printers.h
googletest/include/gtest/gtest-printers.h
+13
-12
googletest/include/gtest/internal/gtest-port.h
googletest/include/gtest/internal/gtest-port.h
+31
-0
googletest/src/gtest-matchers.cc
googletest/src/gtest-matchers.cc
+14
-14
googletest/test/googletest-printers-test.cc
googletest/test/googletest-printers-test.cc
+5
-5
No files found.
googlemock/include/gmock/gmock-matchers.h
View file @
6f5fd0d7
...
...
@@ -906,15 +906,15 @@ class StrEqualityMatcher {
bool
case_sensitive
)
:
string_
(
str
),
expect_eq_
(
expect_eq
),
case_sensitive_
(
case_sensitive
)
{}
#if GTEST_
HAS_ABSL
bool
MatchAndExplain
(
const
abs
l
::
s
tring
_v
iew
&
s
,
#if GTEST_
INTERNAL_HAS_STRING_VIEW
bool
MatchAndExplain
(
const
interna
l
::
S
tring
V
iew
&
s
,
MatchResultListener
*
listener
)
const
{
// This should fail to compile if
absl::s
tring
_v
iew is used with wide
// This should fail to compile if
S
tring
V
iew is used with wide
// strings.
const
StringType
&
str
=
std
::
string
(
s
);
return
MatchAndExplain
(
str
,
listener
);
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
...
...
@@ -932,7 +932,7 @@ class StrEqualityMatcher {
// Matches anything that can convert to StringType.
//
// This is a template, not just a plain function with const StringType&,
// because
absl::s
tring
_v
iew has some interfering non-explicit constructors.
// because
S
tring
V
iew has some interfering non-explicit constructors.
template
<
typename
MatcheeStringType
>
bool
MatchAndExplain
(
const
MatcheeStringType
&
s
,
MatchResultListener
*
/* listener */
)
const
{
...
...
@@ -976,15 +976,15 @@ class HasSubstrMatcher {
explicit
HasSubstrMatcher
(
const
StringType
&
substring
)
:
substring_
(
substring
)
{}
#if GTEST_
HAS_ABSL
bool
MatchAndExplain
(
const
abs
l
::
s
tring
_v
iew
&
s
,
#if GTEST_
INTERNAL_HAS_STRING_VIEW
bool
MatchAndExplain
(
const
interna
l
::
S
tring
V
iew
&
s
,
MatchResultListener
*
listener
)
const
{
// This should fail to compile if
absl::s
tring
_v
iew is used with wide
// This should fail to compile if
S
tring
V
iew is used with wide
// strings.
const
StringType
&
str
=
std
::
string
(
s
);
return
MatchAndExplain
(
str
,
listener
);
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
...
...
@@ -999,7 +999,7 @@ class HasSubstrMatcher {
// Matches anything that can convert to StringType.
//
// This is a template, not just a plain function with const StringType&,
// because
absl::s
tring
_v
iew has some interfering non-explicit constructors.
// because
S
tring
V
iew has some interfering non-explicit constructors.
template
<
typename
MatcheeStringType
>
bool
MatchAndExplain
(
const
MatcheeStringType
&
s
,
MatchResultListener
*
/* listener */
)
const
{
...
...
@@ -1032,15 +1032,15 @@ class StartsWithMatcher {
explicit
StartsWithMatcher
(
const
StringType
&
prefix
)
:
prefix_
(
prefix
)
{
}
#if GTEST_
HAS_ABSL
bool
MatchAndExplain
(
const
abs
l
::
s
tring
_v
iew
&
s
,
#if GTEST_
INTERNAL_HAS_STRING_VIEW
bool
MatchAndExplain
(
const
interna
l
::
S
tring
V
iew
&
s
,
MatchResultListener
*
listener
)
const
{
// This should fail to compile if
absl::s
tring
_v
iew is used with wide
// This should fail to compile if
S
tring
V
iew is used with wide
// strings.
const
StringType
&
str
=
std
::
string
(
s
);
return
MatchAndExplain
(
str
,
listener
);
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
...
...
@@ -1055,7 +1055,7 @@ class StartsWithMatcher {
// Matches anything that can convert to StringType.
//
// This is a template, not just a plain function with const StringType&,
// because
absl::s
tring
_v
iew has some interfering non-explicit constructors.
// because
S
tring
V
iew has some interfering non-explicit constructors.
template
<
typename
MatcheeStringType
>
bool
MatchAndExplain
(
const
MatcheeStringType
&
s
,
MatchResultListener
*
/* listener */
)
const
{
...
...
@@ -1088,15 +1088,15 @@ class EndsWithMatcher {
public:
explicit
EndsWithMatcher
(
const
StringType
&
suffix
)
:
suffix_
(
suffix
)
{}
#if GTEST_
HAS_ABSL
bool
MatchAndExplain
(
const
abs
l
::
s
tring
_v
iew
&
s
,
#if GTEST_
INTERNAL_HAS_STRING_VIEW
bool
MatchAndExplain
(
const
interna
l
::
S
tring
V
iew
&
s
,
MatchResultListener
*
listener
)
const
{
// This should fail to compile if
absl::s
tring
_v
iew is used with wide
// This should fail to compile if
S
tring
V
iew is used with wide
// strings.
const
StringType
&
str
=
std
::
string
(
s
);
return
MatchAndExplain
(
str
,
listener
);
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
...
...
@@ -1111,7 +1111,7 @@ class EndsWithMatcher {
// Matches anything that can convert to StringType.
//
// This is a template, not just a plain function with const StringType&,
// because
absl::s
tring
_v
iew has some interfering non-explicit constructors.
// because
S
tring
V
iew has some interfering non-explicit constructors.
template
<
typename
MatcheeStringType
>
bool
MatchAndExplain
(
const
MatcheeStringType
&
s
,
MatchResultListener
*
/* listener */
)
const
{
...
...
googlemock/test/gmock-matchers_test.cc
View file @
6f5fd0d7
...
...
@@ -351,43 +351,43 @@ TEST(StringMatcherTest, CanBeImplicitlyConstructedFromString) {
EXPECT_FALSE
(
m2
.
Matches
(
"hello"
));
}
#if GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
// Tests that a C-string literal can be implicitly converted to a
// Matcher<
absl::s
tring
_v
iew> or Matcher<const
absl::s
tring
_v
iew&>.
// Matcher<
S
tring
V
iew> or Matcher<const
S
tring
V
iew&>.
TEST
(
StringViewMatcherTest
,
CanBeImplicitlyConstructedFromCStringLiteral
)
{
Matcher
<
abs
l
::
s
tring
_v
iew
>
m1
=
"cats"
;
Matcher
<
interna
l
::
S
tring
V
iew
>
m1
=
"cats"
;
EXPECT_TRUE
(
m1
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m1
.
Matches
(
"dogs"
));
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m2
=
"cats"
;
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m2
=
"cats"
;
EXPECT_TRUE
(
m2
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m2
.
Matches
(
"dogs"
));
}
// Tests that a std::string object can be implicitly converted to a
// Matcher<
absl::s
tring
_v
iew> or Matcher<const
absl::s
tring
_v
iew&>.
// Matcher<
S
tring
V
iew> or Matcher<const
S
tring
V
iew&>.
TEST
(
StringViewMatcherTest
,
CanBeImplicitlyConstructedFromString
)
{
Matcher
<
abs
l
::
s
tring
_v
iew
>
m1
=
std
::
string
(
"cats"
);
Matcher
<
interna
l
::
S
tring
V
iew
>
m1
=
std
::
string
(
"cats"
);
EXPECT_TRUE
(
m1
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m1
.
Matches
(
"dogs"
));
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m2
=
std
::
string
(
"cats"
);
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m2
=
std
::
string
(
"cats"
);
EXPECT_TRUE
(
m2
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m2
.
Matches
(
"dogs"
));
}
// Tests that a
absl::s
tring
_v
iew object can be implicitly converted to a
// Matcher<
absl::s
tring
_v
iew> or Matcher<const
absl::s
tring
_v
iew&>.
// Tests that a
S
tring
V
iew object can be implicitly converted to a
// Matcher<
S
tring
V
iew> or Matcher<const
S
tring
V
iew&>.
TEST
(
StringViewMatcherTest
,
CanBeImplicitlyConstructedFromStringView
)
{
Matcher
<
abs
l
::
s
tring
_v
iew
>
m1
=
abs
l
::
s
tring
_v
iew
(
"cats"
);
Matcher
<
interna
l
::
S
tring
V
iew
>
m1
=
interna
l
::
S
tring
V
iew
(
"cats"
);
EXPECT_TRUE
(
m1
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m1
.
Matches
(
"dogs"
));
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m2
=
abs
l
::
s
tring
_v
iew
(
"cats"
);
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m2
=
interna
l
::
S
tring
V
iew
(
"cats"
);
EXPECT_TRUE
(
m2
.
Matches
(
"cats"
));
EXPECT_FALSE
(
m2
.
Matches
(
"dogs"
));
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Tests that a std::reference_wrapper<std::string> object can be implicitly
// converted to a Matcher<std::string> or Matcher<const std::string&> via Eq().
...
...
@@ -1235,17 +1235,17 @@ TEST(StrEqTest, MatchesEqualString) {
EXPECT_TRUE
(
m2
.
Matches
(
"Hello"
));
EXPECT_FALSE
(
m2
.
Matches
(
"Hi"
));
#if GTEST_
HAS_ABSL
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m3
=
StrEq
(
"Hello"
);
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"Hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
#if GTEST_
INTERNAL_HAS_STRING_VIEW
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m3
=
StrEq
(
"Hello"
);
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"Hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
()));
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m_empty
=
StrEq
(
""
);
EXPECT_TRUE
(
m_empty
.
Matches
(
abs
l
::
s
tring
_v
iew
(
""
)));
EXPECT_TRUE
(
m_empty
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
EXPECT_FALSE
(
m_empty
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"hello"
)));
#endif // GTEST_
HAS_ABSL
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m_empty
=
StrEq
(
""
);
EXPECT_TRUE
(
m_empty
.
Matches
(
interna
l
::
S
tring
V
iew
(
""
)));
EXPECT_TRUE
(
m_empty
.
Matches
(
interna
l
::
S
tring
V
iew
()));
EXPECT_FALSE
(
m_empty
.
Matches
(
interna
l
::
S
tring
V
iew
(
"hello"
)));
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
TEST
(
StrEqTest
,
CanDescribeSelf
)
{
...
...
@@ -1272,12 +1272,12 @@ TEST(StrNeTest, MatchesUnequalString) {
EXPECT_TRUE
(
m2
.
Matches
(
"hello"
));
EXPECT_FALSE
(
m2
.
Matches
(
"Hello"
));
#if GTEST_
HAS_ABSL
Matcher
<
const
abs
l
::
s
tring
_v
iew
>
m3
=
StrNe
(
"Hello"
);
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
""
)));
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"Hello"
)));
#endif // GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
Matcher
<
const
interna
l
::
S
tring
V
iew
>
m3
=
StrNe
(
"Hello"
);
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
""
)));
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
()));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"Hello"
)));
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
TEST
(
StrNeTest
,
CanDescribeSelf
)
{
...
...
@@ -1296,13 +1296,13 @@ TEST(StrCaseEqTest, MatchesEqualStringIgnoringCase) {
EXPECT_TRUE
(
m2
.
Matches
(
"hello"
));
EXPECT_FALSE
(
m2
.
Matches
(
"Hi"
));
#if GTEST_
HAS_ABSL
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m3
=
StrCaseEq
(
std
::
string
(
"Hello"
));
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"Hello"
)));
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"Hi"
)));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
#endif // GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m3
=
StrCaseEq
(
std
::
string
(
"Hello"
));
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"Hello"
)));
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"Hi"
)));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
()));
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
TEST
(
StrCaseEqTest
,
MatchesEqualStringWith0IgnoringCase
)
{
...
...
@@ -1346,13 +1346,13 @@ TEST(StrCaseNeTest, MatchesUnequalStringIgnoringCase) {
EXPECT_TRUE
(
m2
.
Matches
(
""
));
EXPECT_FALSE
(
m2
.
Matches
(
"Hello"
));
#if GTEST_
HAS_ABSL
Matcher
<
const
abs
l
::
s
tring
_v
iew
>
m3
=
StrCaseNe
(
"Hello"
);
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"Hi"
)));
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"Hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"hello"
)));
#endif // GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
Matcher
<
const
interna
l
::
S
tring
V
iew
>
m3
=
StrCaseNe
(
"Hello"
);
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"Hi"
)));
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
()));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"Hello"
)));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"hello"
)));
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
TEST
(
StrCaseNeTest
,
CanDescribeSelf
)
{
...
...
@@ -1393,25 +1393,25 @@ TEST(HasSubstrTest, WorksForCStrings) {
EXPECT_FALSE
(
m_empty
.
Matches
(
nullptr
));
}
#if GTEST_
HAS_ABSL
// Tests that HasSubstr() works for matching
absl::s
tring
_v
iew-typed values.
#if GTEST_
INTERNAL_HAS_STRING_VIEW
// Tests that HasSubstr() works for matching
S
tring
V
iew-typed values.
TEST
(
HasSubstrTest
,
WorksForStringViewClasses
)
{
const
Matcher
<
abs
l
::
s
tring
_v
iew
>
m1
=
HasSubstr
(
"foo"
);
EXPECT_TRUE
(
m1
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"I love food."
)));
EXPECT_FALSE
(
m1
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"tofo"
)));
EXPECT_FALSE
(
m1
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
const
Matcher
<
interna
l
::
S
tring
V
iew
>
m1
=
HasSubstr
(
"foo"
);
EXPECT_TRUE
(
m1
.
Matches
(
interna
l
::
S
tring
V
iew
(
"I love food."
)));
EXPECT_FALSE
(
m1
.
Matches
(
interna
l
::
S
tring
V
iew
(
"tofo"
)));
EXPECT_FALSE
(
m1
.
Matches
(
interna
l
::
S
tring
V
iew
()));
const
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m2
=
HasSubstr
(
"foo"
);
EXPECT_TRUE
(
m2
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"I love food."
)));
EXPECT_FALSE
(
m2
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"tofo"
)));
EXPECT_FALSE
(
m2
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
const
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m2
=
HasSubstr
(
"foo"
);
EXPECT_TRUE
(
m2
.
Matches
(
interna
l
::
S
tring
V
iew
(
"I love food."
)));
EXPECT_FALSE
(
m2
.
Matches
(
interna
l
::
S
tring
V
iew
(
"tofo"
)));
EXPECT_FALSE
(
m2
.
Matches
(
interna
l
::
S
tring
V
iew
()));
const
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m3
=
HasSubstr
(
""
);
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"foo"
)));
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
""
)));
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
const
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m3
=
HasSubstr
(
""
);
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"foo"
)));
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
""
)));
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
()));
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Tests that HasSubstr(s) describes itself properly.
TEST
(
HasSubstrTest
,
CanDescribeSelf
)
{
...
...
@@ -1648,12 +1648,12 @@ TEST(StartsWithTest, MatchesStringWithGivenPrefix) {
EXPECT_FALSE
(
m2
.
Matches
(
"H"
));
EXPECT_FALSE
(
m2
.
Matches
(
" Hi"
));
#if GTEST_
HAS_ABSL
const
Matcher
<
abs
l
::
s
tring
_v
iew
>
m_empty
=
StartsWith
(
""
);
EXPECT_TRUE
(
m_empty
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
EXPECT_TRUE
(
m_empty
.
Matches
(
abs
l
::
s
tring
_v
iew
(
""
)));
EXPECT_TRUE
(
m_empty
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"not empty"
)));
#endif // GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
const
Matcher
<
interna
l
::
S
tring
V
iew
>
m_empty
=
StartsWith
(
""
);
EXPECT_TRUE
(
m_empty
.
Matches
(
interna
l
::
S
tring
V
iew
()));
EXPECT_TRUE
(
m_empty
.
Matches
(
interna
l
::
S
tring
V
iew
(
""
)));
EXPECT_TRUE
(
m_empty
.
Matches
(
interna
l
::
S
tring
V
iew
(
"not empty"
)));
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
TEST
(
StartsWithTest
,
CanDescribeSelf
)
{
...
...
@@ -1676,13 +1676,13 @@ TEST(EndsWithTest, MatchesStringWithGivenSuffix) {
EXPECT_FALSE
(
m2
.
Matches
(
"i"
));
EXPECT_FALSE
(
m2
.
Matches
(
"Hi "
));
#if GTEST_
HAS_ABSL
const
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m4
=
EndsWith
(
""
);
#if GTEST_
INTERNAL_HAS_STRING_VIEW
const
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m4
=
EndsWith
(
""
);
EXPECT_TRUE
(
m4
.
Matches
(
"Hi"
));
EXPECT_TRUE
(
m4
.
Matches
(
""
));
EXPECT_TRUE
(
m4
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
EXPECT_TRUE
(
m4
.
Matches
(
abs
l
::
s
tring
_v
iew
(
""
)));
#endif // GTEST_
HAS_ABSL
EXPECT_TRUE
(
m4
.
Matches
(
interna
l
::
S
tring
V
iew
()));
EXPECT_TRUE
(
m4
.
Matches
(
interna
l
::
S
tring
V
iew
(
""
)));
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
TEST
(
EndsWithTest
,
CanDescribeSelf
)
{
...
...
@@ -1703,16 +1703,16 @@ TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
EXPECT_FALSE
(
m2
.
Matches
(
"az1"
));
EXPECT_FALSE
(
m2
.
Matches
(
"1az"
));
#if GTEST_
HAS_ABSL
const
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m3
=
MatchesRegex
(
"a.*z"
);
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"az"
)));
EXPECT_TRUE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"abcz"
)));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
(
"1az"
)));
EXPECT_FALSE
(
m3
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
const
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
m4
=
MatchesRegex
(
""
);
EXPECT_TRUE
(
m4
.
Matches
(
abs
l
::
s
tring
_v
iew
(
""
)));
EXPECT_TRUE
(
m4
.
Matches
(
abs
l
::
s
tring
_v
iew
()));
#endif // GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
const
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m3
=
MatchesRegex
(
"a.*z"
);
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"az"
)));
EXPECT_TRUE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"abcz"
)));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
(
"1az"
)));
EXPECT_FALSE
(
m3
.
Matches
(
interna
l
::
S
tring
V
iew
()));
const
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
m4
=
MatchesRegex
(
""
);
EXPECT_TRUE
(
m4
.
Matches
(
interna
l
::
S
tring
V
iew
(
""
)));
EXPECT_TRUE
(
m4
.
Matches
(
interna
l
::
S
tring
V
iew
()));
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
TEST
(
MatchesRegexTest
,
CanDescribeSelf
)
{
...
...
@@ -1722,10 +1722,10 @@ TEST(MatchesRegexTest, CanDescribeSelf) {
Matcher
<
const
char
*>
m2
=
MatchesRegex
(
new
RE
(
"a.*"
));
EXPECT_EQ
(
"matches regular expression
\"
a.*
\"
"
,
Describe
(
m2
));
#if GTEST_
HAS_ABSL
Matcher
<
const
abs
l
::
s
tring
_v
iew
>
m3
=
MatchesRegex
(
new
RE
(
"0.*"
));
#if GTEST_
INTERNAL_HAS_STRING_VIEW
Matcher
<
const
interna
l
::
S
tring
V
iew
>
m3
=
MatchesRegex
(
new
RE
(
"0.*"
));
EXPECT_EQ
(
"matches regular expression
\"
0.*
\"
"
,
Describe
(
m3
));
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
// Tests ContainsRegex().
...
...
@@ -1741,16 +1741,17 @@ TEST(ContainsRegexTest, MatchesStringContainingGivenRegex) {
EXPECT_TRUE
(
m2
.
Matches
(
"az1"
));
EXPECT_FALSE
(
m2
.
Matches
(
"1a"
));
#if GTEST_HAS_ABSL
const
Matcher
<
const
absl
::
string_view
&>
m3
=
ContainsRegex
(
new
RE
(
"a.*z"
));
EXPECT_TRUE
(
m3
.
Matches
(
absl
::
string_view
(
"azbz"
)));
EXPECT_TRUE
(
m3
.
Matches
(
absl
::
string_view
(
"az1"
)));
EXPECT_FALSE
(
m3
.
Matches
(
absl
::
string_view
(
"1a"
)));
EXPECT_FALSE
(
m3
.
Matches
(
absl
::
string_view
()));
const
Matcher
<
const
absl
::
string_view
&>
m4
=
ContainsRegex
(
""
);
EXPECT_TRUE
(
m4
.
Matches
(
absl
::
string_view
(
""
)));
EXPECT_TRUE
(
m4
.
Matches
(
absl
::
string_view
()));
#endif // GTEST_HAS_ABSL
#if GTEST_INTERNAL_HAS_STRING_VIEW
const
Matcher
<
const
internal
::
StringView
&>
m3
=
ContainsRegex
(
new
RE
(
"a.*z"
));
EXPECT_TRUE
(
m3
.
Matches
(
internal
::
StringView
(
"azbz"
)));
EXPECT_TRUE
(
m3
.
Matches
(
internal
::
StringView
(
"az1"
)));
EXPECT_FALSE
(
m3
.
Matches
(
internal
::
StringView
(
"1a"
)));
EXPECT_FALSE
(
m3
.
Matches
(
internal
::
StringView
()));
const
Matcher
<
const
internal
::
StringView
&>
m4
=
ContainsRegex
(
""
);
EXPECT_TRUE
(
m4
.
Matches
(
internal
::
StringView
(
""
)));
EXPECT_TRUE
(
m4
.
Matches
(
internal
::
StringView
()));
#endif // GTEST_INTERNAL_HAS_STRING_VIEW
}
TEST
(
ContainsRegexTest
,
CanDescribeSelf
)
{
...
...
@@ -1760,10 +1761,10 @@ TEST(ContainsRegexTest, CanDescribeSelf) {
Matcher
<
const
char
*>
m2
=
ContainsRegex
(
new
RE
(
"a.*"
));
EXPECT_EQ
(
"contains regular expression
\"
a.*
\"
"
,
Describe
(
m2
));
#if GTEST_
HAS_ABSL
Matcher
<
const
abs
l
::
s
tring
_v
iew
>
m3
=
ContainsRegex
(
new
RE
(
"0.*"
));
#if GTEST_
INTERNAL_HAS_STRING_VIEW
Matcher
<
const
interna
l
::
S
tring
V
iew
>
m3
=
ContainsRegex
(
new
RE
(
"0.*"
));
EXPECT_EQ
(
"contains regular expression
\"
0.*
\"
"
,
Describe
(
m3
));
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
// Tests for wide strings.
...
...
googletest/include/gtest/gtest-matchers.h
View file @
6f5fd0d7
...
...
@@ -384,18 +384,18 @@ class GTEST_API_ Matcher<std::string>
Matcher
(
const
char
*
s
);
// NOLINT
};
#if GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
// The following two specializations allow the user to write str
// instead of Eq(str) and "foo" instead of Eq("foo") when a absl::string_view
// matcher is expected.
template
<
>
class
GTEST_API_
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>
:
public
internal
::
MatcherBase
<
const
abs
l
::
s
tring
_v
iew
&>
{
class
GTEST_API_
Matcher
<
const
interna
l
::
S
tring
V
iew
&>
:
public
internal
::
MatcherBase
<
const
interna
l
::
S
tring
V
iew
&>
{
public:
Matcher
()
{}
explicit
Matcher
(
const
MatcherInterface
<
const
abs
l
::
s
tring
_v
iew
&>*
impl
)
:
internal
::
MatcherBase
<
const
abs
l
::
s
tring
_v
iew
&>
(
impl
)
{}
explicit
Matcher
(
const
MatcherInterface
<
const
interna
l
::
S
tring
V
iew
&>*
impl
)
:
internal
::
MatcherBase
<
const
interna
l
::
S
tring
V
iew
&>
(
impl
)
{}
// Allows the user to write str instead of Eq(str) sometimes, where
// str is a std::string object.
...
...
@@ -404,20 +404,20 @@ class GTEST_API_ Matcher<const absl::string_view&>
// Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher
(
const
char
*
s
);
// NOLINT
// Allows the user to pass absl::string_views directly.
Matcher
(
abs
l
::
s
tring
_v
iew
s
);
// NOLINT
// Allows the user to pass absl::string_views
or std::string_views
directly.
Matcher
(
interna
l
::
S
tring
V
iew
s
);
// NOLINT
};
template
<
>
class
GTEST_API_
Matcher
<
abs
l
::
s
tring
_v
iew
>
:
public
internal
::
MatcherBase
<
abs
l
::
s
tring
_v
iew
>
{
class
GTEST_API_
Matcher
<
interna
l
::
S
tring
V
iew
>
:
public
internal
::
MatcherBase
<
interna
l
::
S
tring
V
iew
>
{
public:
Matcher
()
{}
explicit
Matcher
(
const
MatcherInterface
<
const
abs
l
::
s
tring
_v
iew
&>*
impl
)
:
internal
::
MatcherBase
<
abs
l
::
s
tring
_v
iew
>
(
impl
)
{}
explicit
Matcher
(
const
MatcherInterface
<
abs
l
::
s
tring
_v
iew
>*
impl
)
:
internal
::
MatcherBase
<
abs
l
::
s
tring
_v
iew
>
(
impl
)
{}
explicit
Matcher
(
const
MatcherInterface
<
const
interna
l
::
S
tring
V
iew
&>*
impl
)
:
internal
::
MatcherBase
<
interna
l
::
S
tring
V
iew
>
(
impl
)
{}
explicit
Matcher
(
const
MatcherInterface
<
interna
l
::
S
tring
V
iew
>*
impl
)
:
internal
::
MatcherBase
<
interna
l
::
S
tring
V
iew
>
(
impl
)
{}
// Allows the user to write str instead of Eq(str) sometimes, where
// str is a std::string object.
...
...
@@ -426,10 +426,10 @@ class GTEST_API_ Matcher<absl::string_view>
// Allows the user to write "foo" instead of Eq("foo") sometimes.
Matcher
(
const
char
*
s
);
// NOLINT
// Allows the user to pass absl::string_views directly.
Matcher
(
abs
l
::
s
tring
_v
iew
s
);
// NOLINT
// Allows the user to pass absl::string_views
or std::string_views
directly.
Matcher
(
interna
l
::
S
tring
V
iew
s
);
// NOLINT
};
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Prints a matcher in a human-readable format.
template
<
typename
T
>
...
...
@@ -620,12 +620,12 @@ class MatchesRegexMatcher {
MatchesRegexMatcher
(
const
RE
*
regex
,
bool
full_match
)
:
regex_
(
regex
),
full_match_
(
full_match
)
{}
#if GTEST_
HAS_ABSL
bool
MatchAndExplain
(
const
abs
l
::
s
tring
_v
iew
&
s
,
#if GTEST_
INTERNAL_HAS_STRING_VIEW
bool
MatchAndExplain
(
const
interna
l
::
S
tring
V
iew
&
s
,
MatchResultListener
*
listener
)
const
{
return
MatchAndExplain
(
std
::
string
(
s
),
listener
);
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Accepts pointer types, particularly:
// const char*
...
...
googletest/include/gtest/gtest-printers.h
View file @
6f5fd0d7
...
...
@@ -135,9 +135,9 @@ enum TypeKind {
kProtobuf
,
// a protobuf type
kConvertibleToInteger
,
// a type implicitly convertible to BiggestInt
// (e.g. a named or unnamed enum type)
#if GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
kConvertibleToStringView
,
// a type implicitly convertible to
// absl::string_view
// absl::string_view
or std::string_view
#endif
kOtherType
// anything else
};
...
...
@@ -191,12 +191,13 @@ class TypeWithoutFormatter<T, kConvertibleToInteger> {
}
};
#if GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
template
<
typename
T
>
class
TypeWithoutFormatter
<
T
,
kConvertibleToStringView
>
{
public:
// Since T has neither operator<< nor PrintTo() but can be implicitly
// converted to absl::string_view, we print it as a absl::string_view.
// converted to absl::string_view, we print it as a absl::string_view
// (or std::string_view).
//
// Note: the implementation is further below, as it depends on
// internal::PrintTo symbol which is defined later in the file.
...
...
@@ -237,9 +238,9 @@ template <typename Char, typename CharTraits, typename T>
const
T
&
,
internal
::
BiggestInt
>::
value
?
kConvertibleToInteger
:
#if GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
std
::
is_convertible
<
const
T
&
,
abs
l
::
s
tring
_v
iew
>::
value
const
T
&
,
interna
l
::
S
tring
V
iew
>::
value
?
kConvertibleToStringView
:
#endif
...
...
@@ -601,12 +602,12 @@ inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
}
#endif // GTEST_HAS_STD_WSTRING
#if GTEST_
HAS_ABSL
// Overload for
abs
l::
s
tring
_v
iew.
inline
void
PrintTo
(
abs
l
::
s
tring
_v
iew
sp
,
::
std
::
ostream
*
os
)
{
#if GTEST_
INTERNAL_HAS_STRING_VIEW
// Overload for
interna
l::
S
tring
V
iew.
inline
void
PrintTo
(
interna
l
::
S
tring
V
iew
sp
,
::
std
::
ostream
*
os
)
{
PrintTo
(
::
std
::
string
(
sp
),
os
);
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
inline
void
PrintTo
(
std
::
nullptr_t
,
::
std
::
ostream
*
os
)
{
*
os
<<
"(nullptr)"
;
}
...
...
@@ -899,12 +900,12 @@ Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
}
// namespace internal
#if GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
namespace
internal2
{
template
<
typename
T
>
void
TypeWithoutFormatter
<
T
,
kConvertibleToStringView
>::
PrintValue
(
const
T
&
value
,
::
std
::
ostream
*
os
)
{
internal
::
PrintTo
(
abs
l
::
s
tring
_v
iew
(
value
),
os
);
internal
::
PrintTo
(
interna
l
::
S
tring
V
iew
(
value
),
os
);
}
}
// namespace internal2
#endif
...
...
googletest/include/gtest/internal/gtest-port.h
View file @
6f5fd0d7
...
...
@@ -199,6 +199,9 @@
// suppressed (constant conditional).
// GTEST_INTENTIONAL_CONST_COND_POP_ - finish code section where MSVC C4127
// is suppressed.
// GTEST_INTERNAL_HAS_STRING_VIEW - for enabling Matcher<std::string_view> or
// Matcher<absl::string_view>
// specializations.
//
// Synchronization:
// Mutex, MutexLock, ThreadLocal, GetThreadCount()
...
...
@@ -2220,4 +2223,32 @@ const char* StringFromGTestEnv(const char* flag, const char* default_val);
#endif // !defined(GTEST_INTERNAL_DEPRECATED)
#if GTEST_HAS_ABSL
// Always use absl::string_view for Matcher<> specializations if googletest
// is built with absl support.
# define GTEST_INTERNAL_HAS_STRING_VIEW 1
#include "absl/strings/string_view.h"
namespace
testing
{
namespace
internal
{
using
StringView
=
::
absl
::
string_view
;
}
// namespace internal
}
// namespace testing
#else
# ifdef __has_include
# if __has_include(<string_view>) && __cplusplus >= 201703L
// Otherwise for C++17 and higher use std::string_view for Matcher<>
// specializations.
# define GTEST_INTERNAL_HAS_STRING_VIEW 1
#include <string_view>
namespace
testing
{
namespace
internal
{
using
StringView
=
::
std
::
string_view
;
}
// namespace internal
}
// namespace testing
// The case where absl is configured NOT to alias std::string_view is not
// supported.
# endif // __has_include(<string_view>) && __cplusplus >= 201703L
# endif // __has_include
#endif // GTEST_HAS_ABSL
#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_
googletest/src/gtest-matchers.cc
View file @
6f5fd0d7
...
...
@@ -58,40 +58,40 @@ Matcher<std::string>::Matcher(const std::string& s) { *this = Eq(s); }
// s.
Matcher
<
std
::
string
>::
Matcher
(
const
char
*
s
)
{
*
this
=
Eq
(
std
::
string
(
s
));
}
#if GTEST_
HAS_ABSL
// Constructs a matcher that matches a const
absl::s
tring
_v
iew& whose value is
#if GTEST_
INTERNAL_HAS_STRING_VIEW
// Constructs a matcher that matches a const
S
tring
V
iew& whose value is
// equal to s.
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>::
Matcher
(
const
std
::
string
&
s
)
{
Matcher
<
const
interna
l
::
S
tring
V
iew
&>::
Matcher
(
const
std
::
string
&
s
)
{
*
this
=
Eq
(
s
);
}
// Constructs a matcher that matches a const
absl::s
tring
_v
iew& whose value is
// Constructs a matcher that matches a const
S
tring
V
iew& whose value is
// equal to s.
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>::
Matcher
(
const
char
*
s
)
{
Matcher
<
const
interna
l
::
S
tring
V
iew
&>::
Matcher
(
const
char
*
s
)
{
*
this
=
Eq
(
std
::
string
(
s
));
}
// Constructs a matcher that matches a const
absl::s
tring
_v
iew& whose value is
// Constructs a matcher that matches a const
S
tring
V
iew& whose value is
// equal to s.
Matcher
<
const
abs
l
::
s
tring
_v
iew
&>::
Matcher
(
abs
l
::
s
tring
_v
iew
s
)
{
Matcher
<
const
interna
l
::
S
tring
V
iew
&>::
Matcher
(
interna
l
::
S
tring
V
iew
s
)
{
*
this
=
Eq
(
std
::
string
(
s
));
}
// Constructs a matcher that matches a
absl::s
tring
_v
iew whose value is equal to
// Constructs a matcher that matches a
S
tring
V
iew whose value is equal to
// s.
Matcher
<
abs
l
::
s
tring
_v
iew
>::
Matcher
(
const
std
::
string
&
s
)
{
*
this
=
Eq
(
s
);
}
Matcher
<
interna
l
::
S
tring
V
iew
>::
Matcher
(
const
std
::
string
&
s
)
{
*
this
=
Eq
(
s
);
}
// Constructs a matcher that matches a
absl::s
tring
_v
iew whose value is equal to
// Constructs a matcher that matches a
S
tring
V
iew whose value is equal to
// s.
Matcher
<
abs
l
::
s
tring
_v
iew
>::
Matcher
(
const
char
*
s
)
{
Matcher
<
interna
l
::
S
tring
V
iew
>::
Matcher
(
const
char
*
s
)
{
*
this
=
Eq
(
std
::
string
(
s
));
}
// Constructs a matcher that matches a
absl::s
tring
_v
iew whose value is equal to
// Constructs a matcher that matches a
S
tring
V
iew whose value is equal to
// s.
Matcher
<
abs
l
::
s
tring
_v
iew
>::
Matcher
(
abs
l
::
s
tring
_v
iew
s
)
{
Matcher
<
interna
l
::
S
tring
V
iew
>::
Matcher
(
interna
l
::
S
tring
V
iew
s
)
{
*
this
=
Eq
(
std
::
string
(
s
));
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
}
// namespace testing
googletest/test/googletest-printers-test.cc
View file @
6f5fd0d7
...
...
@@ -760,22 +760,22 @@ TEST(PrintTypeWithGenericStreamingTest, TypeImplicitlyConvertible) {
EXPECT_EQ
(
"AllowsGenericStreamingAndImplicitConversionTemplate"
,
Print
(
a
));
}
#if GTEST_
HAS_ABSL
#if GTEST_
INTERNAL_HAS_STRING_VIEW
// Tests printing
::abs
l::
s
tring
_v
iew.
// Tests printing
interna
l::
S
tring
V
iew.
TEST
(
PrintStringViewTest
,
SimpleStringView
)
{
const
::
abs
l
::
s
tring
_v
iew
sp
=
"Hello"
;
const
interna
l
::
S
tring
V
iew
sp
=
"Hello"
;
EXPECT_EQ
(
"
\"
Hello
\"
"
,
Print
(
sp
));
}
TEST
(
PrintStringViewTest
,
UnprintableCharacters
)
{
const
char
str
[]
=
"NUL (
\0
) and
\r\t
"
;
const
::
abs
l
::
s
tring
_v
iew
sp
(
str
,
sizeof
(
str
)
-
1
);
const
interna
l
::
S
tring
V
iew
sp
(
str
,
sizeof
(
str
)
-
1
);
EXPECT_EQ
(
"
\"
NUL (
\\
0) and
\\
r
\\
t
\"
"
,
Print
(
sp
));
}
#endif // GTEST_
HAS_ABSL
#endif // GTEST_
INTERNAL_HAS_STRING_VIEW
// Tests printing STL containers.
...
...
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