mirror of
https://github.com/google/googletest.git
synced 2025-01-17 04:58:08 +08:00
Enables regex matchers on all platforms.
This commit is contained in:
parent
6953a725fc
commit
d14aaed74b
@ -1117,8 +1117,6 @@ bool MatchAndExplain(const EndsWithMatcher<StringType>& impl, T& s,
|
|||||||
return impl.Matches(s);
|
return impl.Matches(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// Implements polymorphic matchers MatchesRegex(regex) and
|
// Implements polymorphic matchers MatchesRegex(regex) and
|
||||||
// ContainsRegex(regex), which can be used as a Matcher<T> as long as
|
// ContainsRegex(regex), which can be used as a Matcher<T> as long as
|
||||||
// T can be converted to a string.
|
// T can be converted to a string.
|
||||||
@ -1165,8 +1163,6 @@ bool MatchAndExplain(const MatchesRegexMatcher& impl, T& s,
|
|||||||
return impl.Matches(s);
|
return impl.Matches(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// Implements a matcher that compares the two fields of a 2-tuple
|
// Implements a matcher that compares the two fields of a 2-tuple
|
||||||
// using one of the ==, <=, <, etc, operators. The two fields being
|
// using one of the ==, <=, <, etc, operators. The two fields being
|
||||||
// compared don't have to have the same type.
|
// compared don't have to have the same type.
|
||||||
@ -2718,8 +2714,6 @@ inline PolymorphicMatcher<internal::EndsWithMatcher<internal::string> >
|
|||||||
suffix));
|
suffix));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// Matches a string that fully matches regular expression 'regex'.
|
// Matches a string that fully matches regular expression 'regex'.
|
||||||
// The matcher takes ownership of 'regex'.
|
// The matcher takes ownership of 'regex'.
|
||||||
inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
|
inline PolymorphicMatcher<internal::MatchesRegexMatcher> MatchesRegex(
|
||||||
@ -2742,8 +2736,6 @@ inline PolymorphicMatcher<internal::MatchesRegexMatcher> ContainsRegex(
|
|||||||
return ContainsRegex(new internal::RE(regex));
|
return ContainsRegex(new internal::RE(regex));
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
|
#if GTEST_HAS_GLOBAL_WSTRING || GTEST_HAS_STD_WSTRING
|
||||||
// Wide string matchers.
|
// Wide string matchers.
|
||||||
|
|
||||||
|
@ -52,26 +52,8 @@
|
|||||||
|
|
||||||
#if GTEST_OS_LINUX
|
#if GTEST_OS_LINUX
|
||||||
|
|
||||||
// On some platforms, <regex.h> needs someone to define size_t, and
|
|
||||||
// won't compile otherwise. We can #include it here as we already
|
|
||||||
// included <stdlib.h>, which is guaranteed to define size_t through
|
|
||||||
// <stddef.h>.
|
|
||||||
#include <regex.h> // NOLINT
|
|
||||||
|
|
||||||
// Defines this iff Google Mock uses the enhanced POSIX regular
|
|
||||||
// expression syntax. This is public as it affects how a user uses
|
|
||||||
// regular expression matchers.
|
|
||||||
#define GMOCK_USES_POSIX_RE 1
|
|
||||||
|
|
||||||
#endif // GTEST_OS_LINUX
|
#endif // GTEST_OS_LINUX
|
||||||
|
|
||||||
#if defined(GMOCK_USES_PCRE) || defined(GMOCK_USES_POSIX_RE)
|
|
||||||
// Defines this iff regular expression matchers are supported. This
|
|
||||||
// is public as it tells a user whether he can use regular expression
|
|
||||||
// matchers.
|
|
||||||
#define GMOCK_HAS_REGEX 1
|
|
||||||
#endif // defined(GMOCK_USES_PCRE) || defined(GMOCK_USES_POSIX_RE)
|
|
||||||
|
|
||||||
namespace testing {
|
namespace testing {
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
|
@ -126,11 +126,9 @@ using testing::internal::linked_ptr;
|
|||||||
using testing::internal::scoped_ptr;
|
using testing::internal::scoped_ptr;
|
||||||
using testing::internal::string;
|
using testing::internal::string;
|
||||||
|
|
||||||
#ifdef GMOCK_HAS_REGEX
|
|
||||||
using testing::ContainsRegex;
|
using testing::ContainsRegex;
|
||||||
using testing::MatchesRegex;
|
using testing::MatchesRegex;
|
||||||
using testing::internal::RE;
|
using testing::internal::RE;
|
||||||
#endif // GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// For testing ExplainMatchResultTo().
|
// For testing ExplainMatchResultTo().
|
||||||
class GreaterThanMatcher : public MatcherInterface<int> {
|
class GreaterThanMatcher : public MatcherInterface<int> {
|
||||||
@ -1249,8 +1247,6 @@ TEST(EndsWithTest, CanDescribeSelf) {
|
|||||||
EXPECT_EQ("ends with \"Hi\"", Describe(m));
|
EXPECT_EQ("ends with \"Hi\"", Describe(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// Tests MatchesRegex().
|
// Tests MatchesRegex().
|
||||||
|
|
||||||
TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
|
TEST(MatchesRegexTest, MatchesStringMatchingGivenRegex) {
|
||||||
@ -1269,8 +1265,8 @@ TEST(MatchesRegexTest, CanDescribeSelf) {
|
|||||||
Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
|
Matcher<const std::string> m1 = MatchesRegex(string("Hi.*"));
|
||||||
EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
|
EXPECT_EQ("matches regular expression \"Hi.*\"", Describe(m1));
|
||||||
|
|
||||||
Matcher<const char*> m2 = MatchesRegex(new RE("[a-z].*"));
|
Matcher<const char*> m2 = MatchesRegex(new RE("a.*"));
|
||||||
EXPECT_EQ("matches regular expression \"[a-z].*\"", Describe(m2));
|
EXPECT_EQ("matches regular expression \"a.*\"", Describe(m2));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests ContainsRegex().
|
// Tests ContainsRegex().
|
||||||
@ -1291,10 +1287,9 @@ TEST(ContainsRegexTest, CanDescribeSelf) {
|
|||||||
Matcher<const std::string> m1 = ContainsRegex("Hi.*");
|
Matcher<const std::string> m1 = ContainsRegex("Hi.*");
|
||||||
EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
|
EXPECT_EQ("contains regular expression \"Hi.*\"", Describe(m1));
|
||||||
|
|
||||||
Matcher<const char*> m2 = ContainsRegex(new RE("[a-z].*"));
|
Matcher<const char*> m2 = ContainsRegex(new RE("a.*"));
|
||||||
EXPECT_EQ("contains regular expression \"[a-z].*\"", Describe(m2));
|
EXPECT_EQ("contains regular expression \"a.*\"", Describe(m2));
|
||||||
}
|
}
|
||||||
#endif // GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// Tests for wide strings.
|
// Tests for wide strings.
|
||||||
#if GTEST_HAS_STD_WSTRING
|
#if GTEST_HAS_STD_WSTRING
|
||||||
|
@ -68,6 +68,7 @@ using testing::AtMost;
|
|||||||
using testing::Between;
|
using testing::Between;
|
||||||
using testing::Cardinality;
|
using testing::Cardinality;
|
||||||
using testing::CardinalityInterface;
|
using testing::CardinalityInterface;
|
||||||
|
using testing::ContainsRegex;
|
||||||
using testing::Const;
|
using testing::Const;
|
||||||
using testing::DoAll;
|
using testing::DoAll;
|
||||||
using testing::DoDefault;
|
using testing::DoDefault;
|
||||||
@ -978,8 +979,6 @@ TEST(UnexpectedCallTest, UnmatchedArguments) {
|
|||||||
b.DoB(1);
|
b.DoB(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// Tests that Google Mock explains that an expectation with
|
// Tests that Google Mock explains that an expectation with
|
||||||
// unsatisfied pre-requisites doesn't match the call.
|
// unsatisfied pre-requisites doesn't match the call.
|
||||||
TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
|
TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
|
||||||
@ -1010,31 +1009,33 @@ TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) {
|
|||||||
|
|
||||||
// Verifies that the failure message contains the two unsatisfied
|
// Verifies that the failure message contains the two unsatisfied
|
||||||
// pre-requisites but not the satisfied one.
|
// pre-requisites but not the satisfied one.
|
||||||
const char* const pattern =
|
#if GTEST_USES_PCRE
|
||||||
#if GMOCK_USES_PCRE
|
EXPECT_THAT(r.message(), ContainsRegex(
|
||||||
// PCRE has trouble using (.|\n) to match any character, but
|
// PCRE has trouble using (.|\n) to match any character, but
|
||||||
// supports the (?s) prefix for using . to match any character.
|
// supports the (?s) prefix for using . to match any character.
|
||||||
"(?s)the following immediate pre-requisites are not satisfied:\n"
|
"(?s)the following immediate pre-requisites are not satisfied:\n"
|
||||||
".*: pre-requisite #0\n"
|
".*: pre-requisite #0\n"
|
||||||
".*: pre-requisite #1";
|
".*: pre-requisite #1"));
|
||||||
#else
|
#elif GTEST_USES_POSIX_RE
|
||||||
|
EXPECT_THAT(r.message(), ContainsRegex(
|
||||||
// POSIX RE doesn't understand the (?s) prefix, but has no trouble
|
// POSIX RE doesn't understand the (?s) prefix, but has no trouble
|
||||||
// with (.|\n).
|
// with (.|\n).
|
||||||
"the following immediate pre-requisites are not satisfied:\n"
|
"the following immediate pre-requisites are not satisfied:\n"
|
||||||
"(.|\n)*: pre-requisite #0\n"
|
"(.|\n)*: pre-requisite #0\n"
|
||||||
"(.|\n)*: pre-requisite #1";
|
"(.|\n)*: pre-requisite #1"));
|
||||||
#endif // GMOCK_USES_PCRE
|
#else
|
||||||
|
// We can only use Google Test's own simple regex.
|
||||||
|
EXPECT_THAT(r.message(), ContainsRegex(
|
||||||
|
"the following immediate pre-requisites are not satisfied:"));
|
||||||
|
EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #0"));
|
||||||
|
EXPECT_THAT(r.message(), ContainsRegex(": pre-requisite #1"));
|
||||||
|
#endif // GTEST_USES_PCRE
|
||||||
|
|
||||||
EXPECT_TRUE(
|
|
||||||
::testing::internal::RE::PartialMatch(r.message(), pattern))
|
|
||||||
<< " where the message is " << r.message();
|
|
||||||
b.DoB(1);
|
b.DoB(1);
|
||||||
b.DoB(3);
|
b.DoB(3);
|
||||||
b.DoB(4);
|
b.DoB(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) {
|
TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) {
|
||||||
MockA a;
|
MockA a;
|
||||||
// TODO(wan@google.com): We should really verify the output message,
|
// TODO(wan@google.com): We should really verify the output message,
|
||||||
|
@ -185,10 +185,8 @@ using testing::SetErrnoAndReturn;
|
|||||||
using testing::Throw;
|
using testing::Throw;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if GMOCK_HAS_REGEX
|
|
||||||
using testing::ContainsRegex;
|
using testing::ContainsRegex;
|
||||||
using testing::MatchesRegex;
|
using testing::MatchesRegex;
|
||||||
#endif
|
|
||||||
|
|
||||||
class Interface {
|
class Interface {
|
||||||
public:
|
public:
|
||||||
@ -547,7 +545,6 @@ TEST(LinkTest, TestMatchersFloatingPoint) {
|
|||||||
.WillByDefault(Return());
|
.WillByDefault(Return());
|
||||||
}
|
}
|
||||||
|
|
||||||
#if GMOCK_HAS_REGEX
|
|
||||||
// Tests the linkage of the ContainsRegex matcher.
|
// Tests the linkage of the ContainsRegex matcher.
|
||||||
TEST(LinkTest, TestMatcherContainsRegex) {
|
TEST(LinkTest, TestMatcherContainsRegex) {
|
||||||
Mock mock;
|
Mock mock;
|
||||||
@ -561,7 +558,6 @@ TEST(LinkTest, TestMatcherMatchesRegex) {
|
|||||||
|
|
||||||
ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
|
ON_CALL(mock, VoidFromString(MatchesRegex(".*"))).WillByDefault(Return());
|
||||||
}
|
}
|
||||||
#endif // GMOCK_HAS_REGEX
|
|
||||||
|
|
||||||
// Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
|
// Tests the linkage of the StartsWith, EndsWith, and HasSubstr matchers.
|
||||||
TEST(LinkTest, TestMatchersSubstrings) {
|
TEST(LinkTest, TestMatchersSubstrings) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user