• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  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 UPB_UPB_TEST_PARSE_TEXT_PROTO_H_
9 #define UPB_UPB_TEST_PARSE_TEXT_PROTO_H_
10 
11 #include <string>
12 
13 #include <gtest/gtest.h>
14 #include "google/protobuf/message.h"
15 #include "google/protobuf/text_format.h"
16 
17 namespace upb_test {
18 
19 // Replacement for Google ParseTextProtoOrDie.
20 // Only to be used in unit tests.
21 // Usage: MyMessage msg = ParseTextProtoOrDie(my_text_proto);
22 class ParseTextProtoOrDie {
23  public:
ParseTextProtoOrDie(absl::string_view text_proto)24   explicit ParseTextProtoOrDie(absl::string_view text_proto)
25       : text_proto_(text_proto) {}
26 
27   template <class T>
T()28   operator T() {  // NOLINT: Needed to support parsing text proto as appropriate
29                   // type.
30     T message;
31     if (!google::protobuf::TextFormat::ParseFromString(text_proto_, &message)) {
32       ADD_FAILURE() << "Failed to parse textproto: " << text_proto_;
33       abort();
34     }
35     return message;
36   }
37 
38  private:
39   std::string text_proto_;
40 };
41 
42 }  // namespace upb_test
43 
44 #endif  // UPB_UPB_TEST_PARSE_TEXT_PROTO_H_
45