1 #ifndef UPB_UPB_TEST_PARSE_TEXT_PROTO_H_
2 #define UPB_UPB_TEST_PARSE_TEXT_PROTO_H_
3 
4 #include <string>
5 
6 #include "gtest/gtest.h"
7 #include "google/protobuf/message.h"
8 #include "google/protobuf/text_format.h"
9 
10 namespace upb_test {
11 
12 // Replacement for Google ParseTextProtoOrDie.
13 // Only to be used in unit tests.
14 // Usage: MyMessage msg = ParseTextProtoOrDie(my_text_proto);
15 class ParseTextProtoOrDie {
16  public:
ParseTextProtoOrDie(absl::string_view text_proto)17   explicit ParseTextProtoOrDie(absl::string_view text_proto)
18       : text_proto_(text_proto) {}
19 
20   template <class T>
T()21   operator T() {  // NOLINT: Needed to support parsing text proto as appropriate
22                   // type.
23     T message;
24     if (!google::protobuf::TextFormat::ParseFromString(text_proto_, &message)) {
25       ADD_FAILURE() << "Failed to parse textproto: " << text_proto_;
26       abort();
27     }
28     return message;
29   }
30 
31  private:
32   std::string text_proto_;
33 };
34 
35 }  // namespace upb_test
36 
37 #endif  // UPB_UPB_TEST_PARSE_TEXT_PROTO_H_
38