Implements polymorphic matchers MatchesRegex(regex) and ContainsRegex(regex), which can be used as a Matcher as long as T can be converted to a string.
| 9519 | // ContainsRegex(regex), which can be used as a Matcher<T> as long as |
| 9520 | // T can be converted to a string. |
| 9521 | class MatchesRegexMatcher { |
| 9522 | public: |
| 9523 | MatchesRegexMatcher(const RE* regex, bool full_match) |
| 9524 | : regex_(regex), full_match_(full_match) {} |
| 9525 | |
| 9526 | #if GTEST_HAS_ABSL |
| 9527 | bool MatchAndExplain(const absl::string_view& s, |
| 9528 | MatchResultListener* listener) const { |
| 9529 | return MatchAndExplain(std::string(s), listener); |
| 9530 | } |
| 9531 | #endif // GTEST_HAS_ABSL |
| 9532 | |
| 9533 | // Accepts pointer types, particularly: |
| 9534 | // const char* |
| 9535 | // char* |
| 9536 | // const wchar_t* |
| 9537 | // wchar_t* |
| 9538 | template <typename CharType> |
| 9539 | bool MatchAndExplain(CharType* s, MatchResultListener* listener) const { |
| 9540 | return s != nullptr && MatchAndExplain(std::string(s), listener); |
| 9541 | } |
| 9542 | |
| 9543 | // Matches anything that can convert to std::string. |
| 9544 | // |
| 9545 | // This is a template, not just a plain function with const std::string&, |
| 9546 | // because absl::string_view has some interfering non-explicit constructors. |
| 9547 | template <class MatcheeStringType> |
| 9548 | bool MatchAndExplain(const MatcheeStringType& s, |
| 9549 | MatchResultListener* /* listener */) const { |
| 9550 | const std::string& s2(s); |
| 9551 | return full_match_ ? RE::FullMatch(s2, *regex_) |
| 9552 | : RE::PartialMatch(s2, *regex_); |
| 9553 | } |
| 9554 | |
| 9555 | void DescribeTo(::std::ostream* os) const { |
| 9556 | *os << (full_match_ ? "matches" : "contains") << " regular expression "; |
| 9557 | UniversalPrinter<std::string>::Print(regex_->pattern(), os); |
| 9558 | } |
| 9559 | |
| 9560 | void DescribeNegationTo(::std::ostream* os) const { |
| 9561 | *os << "doesn't " << (full_match_ ? "match" : "contain") |
| 9562 | << " regular expression "; |
| 9563 | UniversalPrinter<std::string>::Print(regex_->pattern(), os); |
| 9564 | } |
| 9565 | |
| 9566 | private: |
| 9567 | const std::shared_ptr<const RE> regex_; |
| 9568 | const bool full_match_; |
| 9569 | }; |
| 9570 | } // namespace internal |
| 9571 | |
| 9572 | // Matches a string that fully matches regular expression 'regex'. |
no outgoing calls