• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #ifndef GOOGLE_PROTOBUF_TEST_TEXTPROTO_H__
9 #define GOOGLE_PROTOBUF_TEST_TEXTPROTO_H__
10 
11 #include <gmock/gmock.h>
12 #include "absl/log/absl_check.h"
13 #include "absl/memory/memory.h"
14 #include "google/protobuf/dynamic_message.h"
15 #include "google/protobuf/text_format.h"
16 
17 // This file contains private helpers for dealing with textprotos in our
18 // tests.  We make no guarantees about the behavior in real-world situations,
19 // and these are only meant for basic unit-tests of protobuf internals.
20 namespace google {
21 namespace protobuf {
22 
23 MATCHER_P(EqualsProto, textproto, "") {
24   auto msg = absl::WrapUnique(arg.New());
25   return TextFormat::ParseFromString(textproto, msg.get()) &&
26          msg->DebugString() == arg.DebugString();
27 }
28 
29 MATCHER_P3(EqualsProtoSerialized, pool, type, textproto, "") {
30   const Descriptor* desc = pool->FindMessageTypeByName(type);
31   DynamicMessageFactory factory(pool);
32   auto msg = absl::WrapUnique(factory.GetPrototype(desc)->New());
33   return TextFormat::ParseFromString(textproto, msg.get()) &&
34          arg.SerializeAsString() == msg->SerializeAsString();
35 }
36 
37 class ParseTextOrDie {
38  public:
ParseTextOrDie(absl::string_view text)39   explicit ParseTextOrDie(absl::string_view text) : text_(text) {}
40   template <typename Proto>
Proto()41   operator Proto() {  // NOLINT(google-explicit-constructor)
42     Proto ret;
43     ABSL_CHECK(TextFormat::ParseFromString(text_, &ret));
44     return ret;
45   }
46 
47  private:
48   absl::string_view text_;
49 };
50 
51 }  // namespace protobuf
52 }  // namespace google
53 
54 #endif  // GOOGLE_PROTOBUF_TEST_TEXTPROTO_H__
55