Sam McCall | 0cdc04a | 2023-06-27 03:33:00 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "nullability/proto_matchers.h" |
| 6 | |
| 7 | #include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" |
| 8 | #include "third_party/protobuf/text_format.h" |
| 9 | |
| 10 | namespace clang::tidy::nullability { |
| 11 | namespace { |
| 12 | |
| 13 | class EqualsProtoMatcher |
| 14 | : public testing::MatcherInterface<const proto2::Message &> { |
| 15 | std::string Expected; |
| 16 | |
| 17 | public: |
| 18 | EqualsProtoMatcher(llvm::StringRef Expected) : Expected(Expected) {} |
| 19 | |
| 20 | bool MatchAndExplain(const proto2::Message &M, |
| 21 | testing::MatchResultListener *Listener) const override { |
| 22 | std::unique_ptr<proto2::Message> Parsed(M.New()); |
| 23 | if (!proto2::TextFormat::ParseFromString(Expected, Parsed.get())) { |
| 24 | *Listener << "where <<<\n" |
| 25 | << Expected << "\n>>> doesn't parse as " << M.GetTypeName(); |
| 26 | return false; |
| 27 | } |
| 28 | // Compare textual representations. |
| 29 | std::string PrintedExpected, PrintedActual; |
| 30 | if (!proto2::TextFormat::PrintToString(*Parsed, &PrintedExpected)) { |
| 31 | *Listener << "where expected message failed to print!"; |
| 32 | return false; |
| 33 | } |
| 34 | if (!proto2::TextFormat::PrintToString(M, &PrintedActual)) { |
| 35 | *Listener << "where actual message failed to print!"; |
| 36 | return false; |
| 37 | } |
| 38 | return testing::ExplainMatchResult(PrintedExpected, PrintedActual, |
| 39 | Listener); |
| 40 | } |
| 41 | |
| 42 | void DescribeTo(std::ostream *OS) const override { |
| 43 | *OS << "equals proto <<<\n" << Expected << "\n>>>"; |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | testing::Matcher<const proto2::Message &> EqualsProto(llvm::StringRef Textual) { |
| 50 | return testing::MakeMatcher(new EqualsProtoMatcher(Textual)); |
| 51 | } |
| 52 | |
| 53 | } // namespace clang::tidy::nullability |