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 | |
Dmitri Gribenko | 742c4c3 | 2023-07-31 12:32:09 -0700 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <ostream> |
| 9 | #include <string> |
| 10 | |
Googler | f7154ea | 2023-12-11 09:30:25 -0800 | [diff] [blame] | 11 | #include "absl/base/nullability.h" |
Dmitri Gribenko | 742c4c3 | 2023-07-31 12:32:09 -0700 | [diff] [blame] | 12 | #include "llvm/ADT/StringRef.h" |
Sam McCall | 0cdc04a | 2023-06-27 03:33:00 -0700 | [diff] [blame] | 13 | #include "third_party/llvm/llvm-project/third-party/unittest/googlemock/include/gmock/gmock.h" |
Dmitri Gribenko | 742c4c3 | 2023-07-31 12:32:09 -0700 | [diff] [blame] | 14 | #include "third_party/protobuf/message.h" |
Sam McCall | 0cdc04a | 2023-06-27 03:33:00 -0700 | [diff] [blame] | 15 | #include "third_party/protobuf/text_format.h" |
| 16 | |
| 17 | namespace clang::tidy::nullability { |
| 18 | namespace { |
| 19 | |
| 20 | class EqualsProtoMatcher |
| 21 | : public testing::MatcherInterface<const proto2::Message &> { |
| 22 | std::string Expected; |
| 23 | |
| 24 | public: |
| 25 | EqualsProtoMatcher(llvm::StringRef Expected) : Expected(Expected) {} |
| 26 | |
Googler | f7154ea | 2023-12-11 09:30:25 -0800 | [diff] [blame] | 27 | bool MatchAndExplain( |
| 28 | const proto2::Message &M, |
| 29 | absl::Nonnull<testing::MatchResultListener *> Listener) const override { |
Sam McCall | 0cdc04a | 2023-06-27 03:33:00 -0700 | [diff] [blame] | 30 | std::unique_ptr<proto2::Message> Parsed(M.New()); |
| 31 | if (!proto2::TextFormat::ParseFromString(Expected, Parsed.get())) { |
| 32 | *Listener << "where <<<\n" |
| 33 | << Expected << "\n>>> doesn't parse as " << M.GetTypeName(); |
| 34 | return false; |
| 35 | } |
| 36 | // Compare textual representations. |
| 37 | std::string PrintedExpected, PrintedActual; |
| 38 | if (!proto2::TextFormat::PrintToString(*Parsed, &PrintedExpected)) { |
| 39 | *Listener << "where expected message failed to print!"; |
| 40 | return false; |
| 41 | } |
| 42 | if (!proto2::TextFormat::PrintToString(M, &PrintedActual)) { |
| 43 | *Listener << "where actual message failed to print!"; |
| 44 | return false; |
| 45 | } |
| 46 | return testing::ExplainMatchResult(PrintedExpected, PrintedActual, |
| 47 | Listener); |
| 48 | } |
| 49 | |
Googler | f7154ea | 2023-12-11 09:30:25 -0800 | [diff] [blame] | 50 | void DescribeTo(absl::Nonnull<std::ostream *> OS) const override { |
Sam McCall | 0cdc04a | 2023-06-27 03:33:00 -0700 | [diff] [blame] | 51 | *OS << "equals proto <<<\n" << Expected << "\n>>>"; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | } // namespace |
| 56 | |
| 57 | testing::Matcher<const proto2::Message &> EqualsProto(llvm::StringRef Textual) { |
| 58 | return testing::MakeMatcher(new EqualsProtoMatcher(Textual)); |
| 59 | } |
| 60 | |
| 61 | } // namespace clang::tidy::nullability |