Googletest export

Introduce a new matcher for unescaping Base-64 strings to gmock.

PiperOrigin-RevId: 388471904
This commit is contained in:
Abseil Team
2021-08-03 12:19:54 -04:00
committed by Andy Soffer
parent c22ce88775
commit 652ec31f9f
6 changed files with 181 additions and 10 deletions

View File

@@ -1122,6 +1122,45 @@ class EndsWithMatcher {
const StringType suffix_;
};
// Implements the polymorphic WhenBase64Unescaped(matcher) matcher, which can be
// used as a Matcher<T> as long as T can be converted to a string.
class WhenBase64UnescapedMatcher {
public:
using is_gtest_matcher = void;
explicit WhenBase64UnescapedMatcher(
const Matcher<const std::string&>& internal_matcher)
: internal_matcher_(internal_matcher) {}
// Matches anything that can convert to std::string.
template <typename MatcheeStringType>
bool MatchAndExplain(const MatcheeStringType& s,
MatchResultListener* listener) const {
const std::string s2(s); // NOLINT (needed for working with string_view).
std::string unescaped;
if (!internal::Base64Unescape(s2, &unescaped)) {
if (listener != nullptr) {
*listener << "is not a valid base64 escaped string";
}
return false;
}
return MatchPrintAndExplain(unescaped, internal_matcher_, listener);
}
void DescribeTo(::std::ostream* os) const {
*os << "matches after Base64Unescape ";
internal_matcher_.DescribeTo(os);
}
void DescribeNegationTo(::std::ostream* os) const {
*os << "does not match after Base64Unescape ";
internal_matcher_.DescribeTo(os);
}
private:
const Matcher<const std::string&> internal_matcher_;
};
// Implements a matcher that compares the two fields of a 2-tuple
// using one of the ==, <=, <, etc, operators. The two fields being
// compared don't have to have the same type.
@@ -4986,6 +5025,14 @@ inline internal::AddressMatcher<InnerMatcher> Address(
const InnerMatcher& inner_matcher) {
return internal::AddressMatcher<InnerMatcher>(inner_matcher);
}
// Matches a base64 escaped string, when the unescaped string matches the
// internal matcher.
template <typename MatcherType>
internal::WhenBase64UnescapedMatcher WhenBase64Unescaped(
const MatcherType& internal_matcher) {
return internal::WhenBase64UnescapedMatcher(internal_matcher);
}
} // namespace no_adl
// Returns a predicate that is satisfied by anything that matches the