OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 #include <google/protobuf/field_mask.pb.h> | 35 #include <google/protobuf/field_mask.pb.h> |
36 #include <google/protobuf/timestamp.pb.h> | 36 #include <google/protobuf/timestamp.pb.h> |
37 #include <google/protobuf/wrappers.pb.h> | 37 #include <google/protobuf/wrappers.pb.h> |
38 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> | 38 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> |
39 #include <google/protobuf/descriptor.pb.h> | 39 #include <google/protobuf/descriptor.pb.h> |
40 #include <google/protobuf/descriptor.h> | 40 #include <google/protobuf/descriptor.h> |
41 #include <google/protobuf/dynamic_message.h> | 41 #include <google/protobuf/dynamic_message.h> |
42 #include <google/protobuf/message.h> | 42 #include <google/protobuf/message.h> |
43 #include <google/protobuf/util/internal/mock_error_listener.h> | 43 #include <google/protobuf/util/internal/mock_error_listener.h> |
| 44 #include <google/protobuf/util/internal/testdata/anys.pb.h> |
44 #include <google/protobuf/util/internal/testdata/books.pb.h> | 45 #include <google/protobuf/util/internal/testdata/books.pb.h> |
45 #include <google/protobuf/util/internal/testdata/field_mask.pb.h> | 46 #include <google/protobuf/util/internal/testdata/field_mask.pb.h> |
| 47 #include <google/protobuf/util/internal/testdata/maps.pb.h> |
| 48 #include <google/protobuf/util/internal/testdata/oneofs.pb.h> |
| 49 #include <google/protobuf/util/internal/testdata/proto3.pb.h> |
| 50 #include <google/protobuf/util/internal/testdata/struct.pb.h> |
| 51 #include <google/protobuf/util/internal/testdata/timestamp_duration.pb.h> |
| 52 #include <google/protobuf/util/internal/testdata/wrappers.pb.h> |
46 #include <google/protobuf/util/internal/type_info_test_helper.h> | 53 #include <google/protobuf/util/internal/type_info_test_helper.h> |
47 #include <google/protobuf/util/internal/constants.h> | 54 #include <google/protobuf/util/internal/constants.h> |
48 #include <google/protobuf/util/message_differencer.h> | 55 #include <google/protobuf/util/message_differencer.h> |
49 #include <google/protobuf/stubs/bytestream.h> | 56 #include <google/protobuf/stubs/bytestream.h> |
50 #include <google/protobuf/stubs/strutil.h> | 57 #include <google/protobuf/stubs/strutil.h> |
51 #include <google/protobuf/util/internal/testdata/anys.pb.h> | |
52 #include <google/protobuf/util/internal/testdata/maps.pb.h> | |
53 #include <google/protobuf/util/internal/testdata/oneofs.pb.h> | |
54 #include <google/protobuf/util/internal/testdata/struct.pb.h> | |
55 #include <google/protobuf/util/internal/testdata/timestamp_duration.pb.h> | |
56 #include <gtest/gtest.h> | 58 #include <gtest/gtest.h> |
57 | 59 |
58 | 60 |
59 namespace google { | 61 namespace google { |
60 namespace protobuf { | 62 namespace protobuf { |
61 namespace util { | 63 namespace util { |
62 namespace converter { | 64 namespace converter { |
63 | 65 |
| 66 using google::protobuf::testing::AnyM; |
| 67 using google::protobuf::testing::AnyOut; |
64 using google::protobuf::testing::Author; | 68 using google::protobuf::testing::Author; |
65 using google::protobuf::testing::Book; | 69 using google::protobuf::testing::Book; |
66 using google::protobuf::testing::Book_Data; | 70 using google::protobuf::testing::FieldMaskTest; |
| 71 using google::protobuf::testing::Int32Wrapper; |
| 72 using google::protobuf::testing::MapIn; |
67 using google::protobuf::testing::Primitive; | 73 using google::protobuf::testing::Primitive; |
| 74 using google::protobuf::testing::Proto3Message; |
68 using google::protobuf::testing::Publisher; | 75 using google::protobuf::testing::Publisher; |
| 76 using google::protobuf::testing::StructType; |
| 77 using google::protobuf::testing::TimestampDuration; |
| 78 using google::protobuf::testing::ValueWrapper; |
| 79 using google::protobuf::testing::oneofs::OneOfsRequest; |
69 using google::protobuf::Descriptor; | 80 using google::protobuf::Descriptor; |
70 using google::protobuf::DescriptorPool; | 81 using google::protobuf::DescriptorPool; |
71 using google::protobuf::DynamicMessageFactory; | 82 using google::protobuf::DynamicMessageFactory; |
72 using google::protobuf::FileDescriptorProto; | 83 using google::protobuf::FileDescriptorProto; |
73 using google::protobuf::Message; | 84 using google::protobuf::Message; |
74 using google::protobuf::io::ArrayInputStream; | |
75 using strings::GrowingArrayByteSink; | 85 using strings::GrowingArrayByteSink; |
76 using ::testing::_; | 86 using ::testing::_; |
77 using ::testing::Args; | 87 using ::testing::Args; |
78 using google::protobuf::testing::anys::AnyM; | |
79 using google::protobuf::testing::anys::AnyOut; | |
80 using google::protobuf::testing::oneofs::OneOfsRequest; | |
81 using google::protobuf::testing::FieldMaskTest; | |
82 using google::protobuf::testing::maps::MapIn; | |
83 using google::protobuf::testing::structs::StructType; | |
84 using google::protobuf::testing::timestampduration::TimestampDuration; | |
85 | 88 |
86 | 89 |
87 namespace { | 90 namespace { |
88 string GetTypeUrl(const Descriptor* descriptor) { | 91 string GetTypeUrl(const Descriptor* descriptor) { |
89 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(); | 92 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(); |
90 } | 93 } |
91 } // namespace | 94 } // namespace |
92 | 95 |
93 #if __cplusplus >= 201103L | 96 #if __cplusplus >= 201103L |
94 using std::get; | 97 using std::get; |
95 #else | 98 #else |
96 using std::tr1::get; | 99 using std::tr1::get; |
97 #endif | 100 #endif |
98 | 101 |
99 class BaseProtoStreamObjectWriterTest | 102 class BaseProtoStreamObjectWriterTest |
100 : public ::testing::TestWithParam<testing::TypeInfoSource> { | 103 : public ::testing::TestWithParam<testing::TypeInfoSource> { |
101 protected: | 104 protected: |
102 BaseProtoStreamObjectWriterTest() | 105 BaseProtoStreamObjectWriterTest() |
103 : helper_(GetParam()), | 106 : helper_(GetParam()), |
104 listener_(), | 107 listener_(), |
105 output_(new GrowingArrayByteSink(1000)), | 108 output_(new GrowingArrayByteSink(1000)), |
106 ow_() {} | 109 ow_() {} |
107 | 110 |
108 explicit BaseProtoStreamObjectWriterTest(const Descriptor* descriptor) | 111 explicit BaseProtoStreamObjectWriterTest(const Descriptor* descriptor) |
109 : helper_(GetParam()), | 112 : helper_(GetParam()), |
110 listener_(), | 113 listener_(), |
111 output_(new GrowingArrayByteSink(1000)), | 114 output_(new GrowingArrayByteSink(1000)), |
112 ow_() { | 115 ow_() { |
113 vector<const Descriptor*> descriptors; | 116 std::vector<const Descriptor*> descriptors; |
114 descriptors.push_back(descriptor); | 117 descriptors.push_back(descriptor); |
115 ResetTypeInfo(descriptors); | 118 ResetTypeInfo(descriptors); |
116 } | 119 } |
117 | 120 |
118 explicit BaseProtoStreamObjectWriterTest( | 121 explicit BaseProtoStreamObjectWriterTest( |
119 vector<const Descriptor*> descriptors) | 122 std::vector<const Descriptor*> descriptors) |
120 : helper_(GetParam()), | 123 : helper_(GetParam()), |
121 listener_(), | 124 listener_(), |
122 output_(new GrowingArrayByteSink(1000)), | 125 output_(new GrowingArrayByteSink(1000)), |
123 ow_() { | 126 ow_() { |
124 ResetTypeInfo(descriptors); | 127 ResetTypeInfo(descriptors); |
125 } | 128 } |
126 | 129 |
127 void ResetTypeInfo(vector<const Descriptor*> descriptors) { | 130 void ResetTypeInfo(std::vector<const Descriptor*> descriptors) { |
128 GOOGLE_CHECK(!descriptors.empty()) << "Must have at least one descriptor!"; | 131 GOOGLE_CHECK(!descriptors.empty()) << "Must have at least one descriptor!"; |
129 helper_.ResetTypeInfo(descriptors); | 132 helper_.ResetTypeInfo(descriptors); |
130 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(descriptors[0]), output_.get(), | 133 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(descriptors[0]), output_.get(), |
131 &listener_, options_)); | 134 &listener_, options_)); |
132 } | 135 } |
133 | 136 |
134 void ResetTypeInfo(const Descriptor* descriptor) { | 137 void ResetTypeInfo(const Descriptor* descriptor) { |
135 vector<const Descriptor*> descriptors; | 138 std::vector<const Descriptor*> descriptors; |
136 descriptors.push_back(descriptor); | 139 descriptors.push_back(descriptor); |
137 ResetTypeInfo(descriptors); | 140 ResetTypeInfo(descriptors); |
138 } | 141 } |
139 | 142 |
140 virtual ~BaseProtoStreamObjectWriterTest() {} | 143 virtual ~BaseProtoStreamObjectWriterTest() {} |
141 | 144 |
142 void CheckOutput(const Message& expected, int expected_length) { | 145 void CheckOutput(const Message& expected, int expected_length) { |
143 size_t nbytes; | 146 size_t nbytes; |
144 google::protobuf::scoped_array<char> buffer(output_->GetBuffer(&nbytes)); | 147 google::protobuf::scoped_array<char> buffer(output_->GetBuffer(&nbytes)); |
145 if (expected_length >= 0) { | 148 if (expected_length >= 0) { |
(...skipping 30 matching lines...) Expand all Loading... |
176 if (actual.compare(expected) == 0) return true; | 179 if (actual.compare(expected) == 0) return true; |
177 *result_listener << "actual location is: " << actual; | 180 *result_listener << "actual location is: " << actual; |
178 return false; | 181 return false; |
179 } | 182 } |
180 | 183 |
181 class ProtoStreamObjectWriterTest : public BaseProtoStreamObjectWriterTest { | 184 class ProtoStreamObjectWriterTest : public BaseProtoStreamObjectWriterTest { |
182 protected: | 185 protected: |
183 ProtoStreamObjectWriterTest() | 186 ProtoStreamObjectWriterTest() |
184 : BaseProtoStreamObjectWriterTest(Book::descriptor()) {} | 187 : BaseProtoStreamObjectWriterTest(Book::descriptor()) {} |
185 | 188 |
| 189 void ResetProtoWriter() { |
| 190 ResetTypeInfo(Book::descriptor()); |
| 191 } |
| 192 |
186 virtual ~ProtoStreamObjectWriterTest() {} | 193 virtual ~ProtoStreamObjectWriterTest() {} |
187 }; | 194 }; |
188 | 195 |
189 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 196 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
190 ProtoStreamObjectWriterTest, | 197 ProtoStreamObjectWriterTest, |
191 ::testing::Values( | 198 ::testing::Values( |
192 testing::USE_TYPE_RESOLVER)); | 199 testing::USE_TYPE_RESOLVER)); |
193 | 200 |
194 TEST_P(ProtoStreamObjectWriterTest, EmptyObject) { | 201 TEST_P(ProtoStreamObjectWriterTest, EmptyObject) { |
195 Book empty; | 202 Book empty; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 | 264 |
258 ow_->StartObject("") | 265 ow_->StartObject("") |
259 ->StartObject("author") | 266 ->StartObject("author") |
260 ->RenderUint64("@id", 12345) | 267 ->RenderUint64("@id", 12345) |
261 ->RenderString("name", "robert") | 268 ->RenderString("name", "robert") |
262 ->EndObject() | 269 ->EndObject() |
263 ->EndObject(); | 270 ->EndObject(); |
264 CheckOutput(book); | 271 CheckOutput(book); |
265 } | 272 } |
266 | 273 |
| 274 TEST_P(ProtoStreamObjectWriterTest, IntEnumValuesAreAccepted) { |
| 275 Book book; |
| 276 book.set_title("Some Book"); |
| 277 book.set_type(google::protobuf::testing::Book_Type_KIDS); |
| 278 Author* robert = book.mutable_author(); |
| 279 robert->set_name("robert"); |
| 280 |
| 281 ow_->StartObject("") |
| 282 ->RenderString("title", "Some Book") |
| 283 ->RenderString("type", "2") |
| 284 ->StartObject("author") |
| 285 ->RenderString("name", "robert") |
| 286 ->EndObject() |
| 287 ->EndObject(); |
| 288 CheckOutput(book); |
| 289 } |
| 290 |
| 291 TEST_P(ProtoStreamObjectWriterTest, EnumValuesWithoutUnderscoreAreAccepted) { |
| 292 Book book; |
| 293 book.set_title("Some Book"); |
| 294 book.set_type(google::protobuf::testing::Book_Type_ACTION_AND_ADVENTURE); |
| 295 Author* robert = book.mutable_author(); |
| 296 robert->set_name("robert"); |
| 297 |
| 298 options_.use_lower_camel_for_enums = true; |
| 299 ResetProtoWriter(); |
| 300 |
| 301 ow_->StartObject("") |
| 302 ->RenderString("title", "Some Book") |
| 303 ->RenderString("type", "ACTIONANDADVENTURE") |
| 304 ->StartObject("author") |
| 305 ->RenderString("name", "robert") |
| 306 ->EndObject() |
| 307 ->EndObject(); |
| 308 CheckOutput(book); |
| 309 } |
| 310 |
| 311 TEST_P(ProtoStreamObjectWriterTest, EnumValuesInCamelCaseAreAccepted) { |
| 312 Book book; |
| 313 book.set_title("Some Book"); |
| 314 book.set_type(google::protobuf::testing::Book_Type_ACTION_AND_ADVENTURE); |
| 315 Author* robert = book.mutable_author(); |
| 316 robert->set_name("robert"); |
| 317 |
| 318 options_.use_lower_camel_for_enums = true; |
| 319 ResetProtoWriter(); |
| 320 |
| 321 ow_->StartObject("") |
| 322 ->RenderString("title", "Some Book") |
| 323 ->RenderString("type", "actionAndAdventure") |
| 324 ->StartObject("author") |
| 325 ->RenderString("name", "robert") |
| 326 ->EndObject() |
| 327 ->EndObject(); |
| 328 CheckOutput(book); |
| 329 } |
| 330 |
| 331 TEST_P(ProtoStreamObjectWriterTest, |
| 332 EnumValuesInCamelCaseWithNameNotUppercaseAreAccepted) { |
| 333 Book book; |
| 334 book.set_title("Some Book"); |
| 335 book.set_type(google::protobuf::testing::Book_Type_arts_and_photography); |
| 336 Author* robert = book.mutable_author(); |
| 337 robert->set_name("robert"); |
| 338 |
| 339 options_.use_lower_camel_for_enums = true; |
| 340 ResetProtoWriter(); |
| 341 |
| 342 ow_->StartObject("") |
| 343 ->RenderString("title", "Some Book") |
| 344 ->RenderString("type", "artsAndPhotography") |
| 345 ->StartObject("author") |
| 346 ->RenderString("name", "robert") |
| 347 ->EndObject() |
| 348 ->EndObject(); |
| 349 CheckOutput(book); |
| 350 } |
| 351 |
267 TEST_P(ProtoStreamObjectWriterTest, PrimitiveFromStringConversion) { | 352 TEST_P(ProtoStreamObjectWriterTest, PrimitiveFromStringConversion) { |
268 Primitive full; | 353 Primitive full; |
269 full.set_fix32(101); | 354 full.set_fix32(101); |
270 full.set_u32(102); | 355 full.set_u32(102); |
271 full.set_i32(-103); | 356 full.set_i32(-103); |
272 full.set_sf32(-104); | 357 full.set_sf32(-104); |
273 full.set_s32(-105); | 358 full.set_s32(-105); |
274 full.set_fix64(40000000001L); | 359 full.set_fix64(40000000001L); |
275 full.set_u64(40000000002L); | 360 full.set_u64(40000000002L); |
276 full.set_i64(-40000000003L); | 361 full.set_i64(-40000000003L); |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 ->StartObject("publisher") | 787 ->StartObject("publisher") |
703 ->RenderString("name", "propaganda") | 788 ->RenderString("name", "propaganda") |
704 ->StartList("alliance") | 789 ->StartList("alliance") |
705 ->EndList() | 790 ->EndList() |
706 ->EndObject() | 791 ->EndObject() |
707 ->RenderString("title", "Brainwashing") | 792 ->RenderString("title", "Brainwashing") |
708 ->EndObject(); | 793 ->EndObject(); |
709 CheckOutput(expected); | 794 CheckOutput(expected); |
710 } | 795 } |
711 | 796 |
| 797 TEST_P(ProtoStreamObjectWriterTest, IgnoreUnknownFieldAtRoot) { |
| 798 Book empty; |
| 799 |
| 800 options_.ignore_unknown_fields = true; |
| 801 ResetProtoWriter(); |
| 802 |
| 803 EXPECT_CALL(listener_, InvalidName(_, _, _)).Times(0); |
| 804 ow_->StartObject("")->RenderString("unknown", "Nope!")->EndObject(); |
| 805 CheckOutput(empty, 0); |
| 806 } |
| 807 |
| 808 TEST_P(ProtoStreamObjectWriterTest, IgnoreUnknownFieldAtAuthorFriend) { |
| 809 Book expected; |
| 810 Author* paul = expected.mutable_author(); |
| 811 paul->set_name("Paul"); |
| 812 Author* mark = paul->add_friend_(); |
| 813 mark->set_name("Mark"); |
| 814 Author* john = paul->add_friend_(); |
| 815 john->set_name("John"); |
| 816 Author* luke = paul->add_friend_(); |
| 817 luke->set_name("Luke"); |
| 818 |
| 819 options_.ignore_unknown_fields = true; |
| 820 ResetProtoWriter(); |
| 821 |
| 822 EXPECT_CALL(listener_, InvalidName(_, _, _)).Times(0); |
| 823 ow_->StartObject("") |
| 824 ->StartObject("author") |
| 825 ->RenderString("name", "Paul") |
| 826 ->StartList("friend") |
| 827 ->StartObject("") |
| 828 ->RenderString("name", "Mark") |
| 829 ->EndObject() |
| 830 ->StartObject("") |
| 831 ->RenderString("name", "John") |
| 832 ->RenderString("address", "Patmos") |
| 833 ->EndObject() |
| 834 ->StartObject("") |
| 835 ->RenderString("name", "Luke") |
| 836 ->EndObject() |
| 837 ->EndList() |
| 838 ->EndObject() |
| 839 ->EndObject(); |
| 840 CheckOutput(expected); |
| 841 } |
| 842 |
| 843 TEST_P(ProtoStreamObjectWriterTest, IgnoreUnknownObjectAtRoot) { |
| 844 Book empty; |
| 845 |
| 846 options_.ignore_unknown_fields = true; |
| 847 ResetProtoWriter(); |
| 848 |
| 849 EXPECT_CALL(listener_, InvalidName(_, StringPiece("unknown"), |
| 850 StringPiece("Cannot find field."))) |
| 851 .Times(0); |
| 852 ow_->StartObject("")->StartObject("unknown")->EndObject()->EndObject(); |
| 853 CheckOutput(empty, 0); |
| 854 } |
| 855 |
| 856 TEST_P(ProtoStreamObjectWriterTest, IgnoreUnknownObjectAtAuthor) { |
| 857 Book expected; |
| 858 Author* author = expected.mutable_author(); |
| 859 author->set_name("William"); |
| 860 author->add_pseudonym("Bill"); |
| 861 |
| 862 options_.ignore_unknown_fields = true; |
| 863 ResetProtoWriter(); |
| 864 |
| 865 EXPECT_CALL(listener_, InvalidName(_, _, _)).Times(0); |
| 866 ow_->StartObject("") |
| 867 ->StartObject("author") |
| 868 ->RenderString("name", "William") |
| 869 ->StartObject("wife") |
| 870 ->RenderString("name", "Hilary") |
| 871 ->EndObject() |
| 872 ->RenderString("pseudonym", "Bill") |
| 873 ->EndObject() |
| 874 ->EndObject(); |
| 875 CheckOutput(expected); |
| 876 } |
| 877 |
| 878 TEST_P(ProtoStreamObjectWriterTest, IgnoreUnknownListAtRoot) { |
| 879 Book empty; |
| 880 |
| 881 options_.ignore_unknown_fields = true; |
| 882 ResetProtoWriter(); |
| 883 |
| 884 EXPECT_CALL(listener_, InvalidName(_, _, _)).Times(0); |
| 885 ow_->StartObject("")->StartList("unknown")->EndList()->EndObject(); |
| 886 CheckOutput(empty, 0); |
| 887 } |
| 888 |
| 889 TEST_P(ProtoStreamObjectWriterTest, IgnoreUnknownListAtPublisher) { |
| 890 Book expected; |
| 891 expected.set_title("Brainwashing"); |
| 892 Publisher* publisher = expected.mutable_publisher(); |
| 893 publisher->set_name("propaganda"); |
| 894 |
| 895 options_.ignore_unknown_fields = true; |
| 896 ResetProtoWriter(); |
| 897 |
| 898 EXPECT_CALL(listener_, InvalidName(_, _, _)).Times(0); |
| 899 ow_->StartObject("") |
| 900 ->StartObject("publisher") |
| 901 ->RenderString("name", "propaganda") |
| 902 ->StartList("alliance") |
| 903 ->EndList() |
| 904 ->EndObject() |
| 905 ->RenderString("title", "Brainwashing") |
| 906 ->EndObject(); |
| 907 CheckOutput(expected); |
| 908 } |
| 909 |
| 910 TEST_P(ProtoStreamObjectWriterTest, AcceptUnknownEnumValue) { |
| 911 ResetTypeInfo(Proto3Message::descriptor()); |
| 912 |
| 913 Proto3Message expected; |
| 914 expected.set_enum_value(static_cast<Proto3Message::NestedEnum>(12345)); |
| 915 |
| 916 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 917 ow_->StartObject("") |
| 918 ->RenderInt32("enumValue", 12345) |
| 919 ->EndObject(); |
| 920 CheckOutput(expected); |
| 921 } |
| 922 |
712 TEST_P(ProtoStreamObjectWriterTest, MissingRequiredField) { | 923 TEST_P(ProtoStreamObjectWriterTest, MissingRequiredField) { |
713 Book expected; | 924 Book expected; |
714 expected.set_title("My Title"); | 925 expected.set_title("My Title"); |
715 expected.set_allocated_publisher(new Publisher()); | 926 expected.set_allocated_publisher(new Publisher()); |
716 | 927 |
717 EXPECT_CALL(listener_, MissingField(_, StringPiece("name"))) | 928 EXPECT_CALL(listener_, MissingField(_, StringPiece("name"))) |
718 .With(Args<0>(HasObjectLocation("publisher"))); | 929 .With(Args<0>(HasObjectLocation("publisher"))); |
719 ow_->StartObject("") | 930 ow_->StartObject("") |
720 ->StartObject("publisher") | 931 ->StartObject("publisher") |
721 ->EndObject() | 932 ->EndObject() |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 Book empty; | 1071 Book empty; |
861 | 1072 |
862 ow_->RenderNull("length"); | 1073 ow_->RenderNull("length"); |
863 CheckOutput(empty, 0); | 1074 CheckOutput(empty, 0); |
864 } | 1075 } |
865 | 1076 |
866 class ProtoStreamObjectWriterTimestampDurationTest | 1077 class ProtoStreamObjectWriterTimestampDurationTest |
867 : public BaseProtoStreamObjectWriterTest { | 1078 : public BaseProtoStreamObjectWriterTest { |
868 protected: | 1079 protected: |
869 ProtoStreamObjectWriterTimestampDurationTest() { | 1080 ProtoStreamObjectWriterTimestampDurationTest() { |
870 vector<const Descriptor*> descriptors; | 1081 std::vector<const Descriptor*> descriptors; |
871 descriptors.push_back(TimestampDuration::descriptor()); | 1082 descriptors.push_back(TimestampDuration::descriptor()); |
872 descriptors.push_back(google::protobuf::Timestamp::descriptor()); | 1083 descriptors.push_back(google::protobuf::Timestamp::descriptor()); |
873 descriptors.push_back(google::protobuf::Duration::descriptor()); | 1084 descriptors.push_back(google::protobuf::Duration::descriptor()); |
874 ResetTypeInfo(descriptors); | 1085 ResetTypeInfo(descriptors); |
875 } | 1086 } |
876 }; | 1087 }; |
877 | 1088 |
878 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1089 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
879 ProtoStreamObjectWriterTimestampDurationTest, | 1090 ProtoStreamObjectWriterTimestampDurationTest, |
880 ::testing::Values( | 1091 ::testing::Values( |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1214 } | 1425 } |
1215 | 1426 |
1216 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, | 1427 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, |
1217 MismatchedTimestampTypeInput) { | 1428 MismatchedTimestampTypeInput) { |
1218 TimestampDuration timestamp; | 1429 TimestampDuration timestamp; |
1219 EXPECT_CALL( | 1430 EXPECT_CALL( |
1220 listener_, | 1431 listener_, |
1221 InvalidValue( | 1432 InvalidValue( |
1222 _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), | 1433 _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), |
1223 StringPiece( | 1434 StringPiece( |
1224 "Field 'ts', Invalid data type for timestamp, value is null"))) | 1435 "Field 'ts', Invalid data type for timestamp, value is 1"))) |
1225 .With(Args<0>(HasObjectLocation("ts"))); | 1436 .With(Args<0>(HasObjectLocation("ts"))); |
1226 ow_->StartObject("")->RenderNull("ts")->EndObject(); | 1437 ow_->StartObject("")->RenderInt32("ts", 1)->EndObject(); |
1227 CheckOutput(timestamp); | 1438 CheckOutput(timestamp); |
1228 } | 1439 } |
1229 | 1440 |
1230 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, | 1441 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, |
1231 MismatchedDurationTypeInput) { | 1442 MismatchedDurationTypeInput) { |
1232 TimestampDuration duration; | 1443 TimestampDuration duration; |
1233 EXPECT_CALL( | 1444 EXPECT_CALL( |
1234 listener_, | 1445 listener_, |
1235 InvalidValue( | 1446 InvalidValue( |
1236 _, StringPiece("type.googleapis.com/google.protobuf.Duration"), | 1447 _, StringPiece("type.googleapis.com/google.protobuf.Duration"), |
1237 StringPiece( | 1448 StringPiece( |
1238 "Field 'dur', Invalid data type for duration, value is null"))) | 1449 "Field 'dur', Invalid data type for duration, value is 1"))) |
1239 .With(Args<0>(HasObjectLocation("dur"))); | 1450 .With(Args<0>(HasObjectLocation("dur"))); |
| 1451 ow_->StartObject("")->RenderInt32("dur", 1)->EndObject(); |
| 1452 CheckOutput(duration); |
| 1453 } |
| 1454 |
| 1455 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, TimestampAcceptsNull) { |
| 1456 TimestampDuration timestamp; |
| 1457 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 1458 ow_->StartObject("")->RenderNull("ts")->EndObject(); |
| 1459 CheckOutput(timestamp); |
| 1460 } |
| 1461 |
| 1462 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, DurationAcceptsNull) { |
| 1463 TimestampDuration duration; |
| 1464 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
1240 ow_->StartObject("")->RenderNull("dur")->EndObject(); | 1465 ow_->StartObject("")->RenderNull("dur")->EndObject(); |
1241 CheckOutput(duration); | 1466 CheckOutput(duration); |
1242 } | 1467 } |
1243 | 1468 |
1244 class ProtoStreamObjectWriterStructTest | 1469 class ProtoStreamObjectWriterStructTest |
1245 : public BaseProtoStreamObjectWriterTest { | 1470 : public BaseProtoStreamObjectWriterTest { |
1246 protected: | 1471 protected: |
1247 ProtoStreamObjectWriterStructTest() { ResetProtoWriter(); } | 1472 ProtoStreamObjectWriterStructTest() { ResetProtoWriter(); } |
1248 | 1473 |
1249 // Resets ProtoWriter with current set of options and other state. | 1474 // Resets ProtoWriter with current set of options and other state. |
1250 void ResetProtoWriter() { | 1475 void ResetProtoWriter() { |
1251 vector<const Descriptor*> descriptors; | 1476 std::vector<const Descriptor*> descriptors; |
1252 descriptors.push_back(StructType::descriptor()); | 1477 descriptors.push_back(StructType::descriptor()); |
1253 descriptors.push_back(google::protobuf::Struct::descriptor()); | 1478 descriptors.push_back(google::protobuf::Struct::descriptor()); |
1254 ResetTypeInfo(descriptors); | 1479 ResetTypeInfo(descriptors); |
1255 } | 1480 } |
1256 }; | 1481 }; |
1257 | 1482 |
1258 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1483 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1259 ProtoStreamObjectWriterStructTest, | 1484 ProtoStreamObjectWriterStructTest, |
1260 ::testing::Values( | 1485 ::testing::Values( |
1261 testing::USE_TYPE_RESOLVER)); | 1486 testing::USE_TYPE_RESOLVER)); |
(...skipping 29 matching lines...) Expand all Loading... |
1291 EXPECT_CALL( | 1516 EXPECT_CALL( |
1292 listener_, | 1517 listener_, |
1293 InvalidValue(_, StringPiece("type.googleapis.com/google.protobuf.Struct"), | 1518 InvalidValue(_, StringPiece("type.googleapis.com/google.protobuf.Struct"), |
1294 StringPiece("true"))) | 1519 StringPiece("true"))) |
1295 .With(Args<0>(HasObjectLocation("object"))); | 1520 .With(Args<0>(HasObjectLocation("object"))); |
1296 | 1521 |
1297 ow_->StartObject("")->RenderBool("object", true)->EndObject(); | 1522 ow_->StartObject("")->RenderBool("object", true)->EndObject(); |
1298 CheckOutput(struct_type); | 1523 CheckOutput(struct_type); |
1299 } | 1524 } |
1300 | 1525 |
| 1526 TEST_P(ProtoStreamObjectWriterStructTest, StructAcceptsNull) { |
| 1527 StructType struct_type; |
| 1528 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 1529 |
| 1530 ow_->StartObject("")->RenderNull("object")->EndObject(); |
| 1531 CheckOutput(struct_type); |
| 1532 } |
| 1533 |
| 1534 TEST_P(ProtoStreamObjectWriterStructTest, StructValuePreservesNull) { |
| 1535 StructType struct_type; |
| 1536 (*struct_type.mutable_object()->mutable_fields())["key"].set_null_value( |
| 1537 google::protobuf::NULL_VALUE); |
| 1538 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 1539 |
| 1540 ow_->StartObject("") |
| 1541 ->StartObject("object") |
| 1542 ->RenderNull("key") |
| 1543 ->EndObject() |
| 1544 ->EndObject(); |
| 1545 CheckOutput(struct_type); |
| 1546 } |
| 1547 |
1301 TEST_P(ProtoStreamObjectWriterStructTest, SimpleRepeatedStructMapKeyTest) { | 1548 TEST_P(ProtoStreamObjectWriterStructTest, SimpleRepeatedStructMapKeyTest) { |
1302 EXPECT_CALL( | 1549 EXPECT_CALL( |
1303 listener_, | 1550 listener_, |
1304 InvalidName(_, StringPiece("gBike"), | 1551 InvalidName(_, StringPiece("gBike"), |
1305 StringPiece("Repeated map key: 'gBike' is already set."))); | 1552 StringPiece("Repeated map key: 'gBike' is already set."))); |
1306 ow_->StartObject("") | 1553 ow_->StartObject("") |
1307 ->StartObject("object") | 1554 ->StartObject("object") |
1308 ->RenderString("gBike", "v1") | 1555 ->RenderString("gBike", "v1") |
1309 ->RenderString("gBike", "v2") | 1556 ->RenderString("gBike", "v2") |
1310 ->EndObject() | 1557 ->EndObject() |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1358 ->StartObject("object") | 1605 ->StartObject("object") |
1359 ->RenderDouble("k1", 123) | 1606 ->RenderDouble("k1", 123) |
1360 ->RenderBool("k2", true) | 1607 ->RenderBool("k2", true) |
1361 ->RenderInt64("k3", -222222222) | 1608 ->RenderInt64("k3", -222222222) |
1362 ->RenderUint64("k4", 33333333) | 1609 ->RenderUint64("k4", 33333333) |
1363 ->EndObject() | 1610 ->EndObject() |
1364 ->EndObject(); | 1611 ->EndObject(); |
1365 CheckOutput(struct_type); | 1612 CheckOutput(struct_type); |
1366 } | 1613 } |
1367 | 1614 |
| 1615 TEST_P(ProtoStreamObjectWriterStructTest, ValuePreservesNull) { |
| 1616 ValueWrapper value; |
| 1617 value.mutable_value()->set_null_value(google::protobuf::NULL_VALUE); |
| 1618 |
| 1619 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 1620 ow_->StartObject("")->RenderNull("value")->EndObject(); |
| 1621 CheckOutput(value); |
| 1622 } |
| 1623 |
1368 class ProtoStreamObjectWriterMapTest : public BaseProtoStreamObjectWriterTest { | 1624 class ProtoStreamObjectWriterMapTest : public BaseProtoStreamObjectWriterTest { |
1369 protected: | 1625 protected: |
1370 ProtoStreamObjectWriterMapTest() | 1626 ProtoStreamObjectWriterMapTest() |
1371 : BaseProtoStreamObjectWriterTest(MapIn::descriptor()) {} | 1627 : BaseProtoStreamObjectWriterTest(MapIn::descriptor()) {} |
1372 }; | 1628 }; |
1373 | 1629 |
1374 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1630 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1375 ProtoStreamObjectWriterMapTest, | 1631 ProtoStreamObjectWriterMapTest, |
1376 ::testing::Values( | 1632 ::testing::Values( |
1377 testing::USE_TYPE_RESOLVER)); | 1633 testing::USE_TYPE_RESOLVER)); |
(...skipping 23 matching lines...) Expand all Loading... |
1401 ->StartObject("map_input") | 1657 ->StartObject("map_input") |
1402 ->RenderString("k1", "v1") | 1658 ->RenderString("k1", "v1") |
1403 ->RenderString("k1", "v2") | 1659 ->RenderString("k1", "v2") |
1404 ->EndObject() | 1660 ->EndObject() |
1405 ->EndObject(); | 1661 ->EndObject(); |
1406 } | 1662 } |
1407 | 1663 |
1408 class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest { | 1664 class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest { |
1409 protected: | 1665 protected: |
1410 ProtoStreamObjectWriterAnyTest() { | 1666 ProtoStreamObjectWriterAnyTest() { |
1411 vector<const Descriptor*> descriptors; | 1667 std::vector<const Descriptor*> descriptors; |
1412 descriptors.push_back(AnyOut::descriptor()); | 1668 descriptors.push_back(AnyOut::descriptor()); |
| 1669 descriptors.push_back(Book::descriptor()); |
| 1670 descriptors.push_back(google::protobuf::Any::descriptor()); |
1413 descriptors.push_back(google::protobuf::DoubleValue::descriptor()); | 1671 descriptors.push_back(google::protobuf::DoubleValue::descriptor()); |
| 1672 descriptors.push_back(google::protobuf::FieldMask::descriptor()); |
| 1673 descriptors.push_back(google::protobuf::Int32Value::descriptor()); |
| 1674 descriptors.push_back(google::protobuf::Struct::descriptor()); |
1414 descriptors.push_back(google::protobuf::Timestamp::descriptor()); | 1675 descriptors.push_back(google::protobuf::Timestamp::descriptor()); |
1415 descriptors.push_back(google::protobuf::Any::descriptor()); | |
1416 descriptors.push_back(google::protobuf::Value::descriptor()); | 1676 descriptors.push_back(google::protobuf::Value::descriptor()); |
1417 descriptors.push_back(google::protobuf::Struct::descriptor()); | |
1418 ResetTypeInfo(descriptors); | 1677 ResetTypeInfo(descriptors); |
1419 } | 1678 } |
1420 }; | 1679 }; |
1421 | 1680 |
1422 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1681 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1423 ProtoStreamObjectWriterAnyTest, | 1682 ProtoStreamObjectWriterAnyTest, |
1424 ::testing::Values( | 1683 ::testing::Values( |
1425 testing::USE_TYPE_RESOLVER)); | 1684 testing::USE_TYPE_RESOLVER)); |
1426 | 1685 |
1427 TEST_P(ProtoStreamObjectWriterAnyTest, AnyRenderSuccess) { | 1686 TEST_P(ProtoStreamObjectWriterAnyTest, AnyRenderSuccess) { |
(...skipping 12 matching lines...) Expand all Loading... |
1440 ->EndObject(); | 1699 ->EndObject(); |
1441 CheckOutput(any); | 1700 CheckOutput(any); |
1442 } | 1701 } |
1443 | 1702 |
1444 TEST_P(ProtoStreamObjectWriterAnyTest, RecursiveAny) { | 1703 TEST_P(ProtoStreamObjectWriterAnyTest, RecursiveAny) { |
1445 AnyOut out; | 1704 AnyOut out; |
1446 ::google::protobuf::Any* any = out.mutable_any(); | 1705 ::google::protobuf::Any* any = out.mutable_any(); |
1447 any->set_type_url("type.googleapis.com/google.protobuf.Any"); | 1706 any->set_type_url("type.googleapis.com/google.protobuf.Any"); |
1448 | 1707 |
1449 ::google::protobuf::Any nested_any; | 1708 ::google::protobuf::Any nested_any; |
1450 nested_any.set_type_url( | 1709 nested_any.set_type_url("type.googleapis.com/google.protobuf.testing.AnyM"); |
1451 "type.googleapis.com/google.protobuf.testing.anys.AnyM"); | |
1452 | 1710 |
1453 AnyM m; | 1711 AnyM m; |
1454 m.set_foo("foovalue"); | 1712 m.set_foo("foovalue"); |
1455 nested_any.set_value(m.SerializeAsString()); | 1713 nested_any.set_value(m.SerializeAsString()); |
1456 | 1714 |
1457 any->set_value(nested_any.SerializeAsString()); | 1715 any->set_value(nested_any.SerializeAsString()); |
1458 | 1716 |
1459 ow_->StartObject("") | 1717 ow_->StartObject("") |
1460 ->StartObject("any") | 1718 ->StartObject("any") |
1461 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 1719 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
1462 ->StartObject("value") | 1720 ->StartObject("value") |
1463 ->RenderString("@type", | 1721 ->RenderString("@type", |
1464 "type.googleapis.com/google.protobuf.testing.anys.AnyM") | 1722 "type.googleapis.com/google.protobuf.testing.AnyM") |
1465 ->RenderString("foo", "foovalue") | 1723 ->RenderString("foo", "foovalue") |
1466 ->EndObject() | 1724 ->EndObject() |
1467 ->EndObject() | 1725 ->EndObject() |
1468 ->EndObject(); | 1726 ->EndObject(); |
| 1727 CheckOutput(out, 107); |
1469 } | 1728 } |
1470 | 1729 |
1471 TEST_P(ProtoStreamObjectWriterAnyTest, DoubleRecursiveAny) { | 1730 TEST_P(ProtoStreamObjectWriterAnyTest, DoubleRecursiveAny) { |
1472 AnyOut out; | 1731 AnyOut out; |
1473 ::google::protobuf::Any* any = out.mutable_any(); | 1732 ::google::protobuf::Any* any = out.mutable_any(); |
1474 any->set_type_url("type.googleapis.com/google.protobuf.Any"); | 1733 any->set_type_url("type.googleapis.com/google.protobuf.Any"); |
1475 | 1734 |
1476 ::google::protobuf::Any nested_any; | 1735 ::google::protobuf::Any nested_any; |
1477 nested_any.set_type_url("type.googleapis.com/google.protobuf.Any"); | 1736 nested_any.set_type_url("type.googleapis.com/google.protobuf.Any"); |
1478 | 1737 |
1479 ::google::protobuf::Any second_nested_any; | 1738 ::google::protobuf::Any second_nested_any; |
1480 second_nested_any.set_type_url( | 1739 second_nested_any.set_type_url( |
1481 "type.googleapis.com/google.protobuf.testing.anys.AnyM"); | 1740 "type.googleapis.com/google.protobuf.testing.AnyM"); |
1482 | 1741 |
1483 AnyM m; | 1742 AnyM m; |
1484 m.set_foo("foovalue"); | 1743 m.set_foo("foovalue"); |
1485 second_nested_any.set_value(m.SerializeAsString()); | 1744 second_nested_any.set_value(m.SerializeAsString()); |
1486 | 1745 |
1487 nested_any.set_value(second_nested_any.SerializeAsString()); | 1746 nested_any.set_value(second_nested_any.SerializeAsString()); |
1488 any->set_value(nested_any.SerializeAsString()); | 1747 any->set_value(nested_any.SerializeAsString()); |
1489 | 1748 |
1490 ow_->StartObject("") | 1749 ow_->StartObject("") |
1491 ->StartObject("any") | 1750 ->StartObject("any") |
1492 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 1751 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
1493 ->StartObject("value") | 1752 ->StartObject("value") |
1494 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 1753 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
1495 ->StartObject("value") | 1754 ->StartObject("value") |
1496 ->RenderString("@type", | 1755 ->RenderString("@type", |
1497 "type.googleapis.com/google.protobuf.testing.anys.AnyM") | 1756 "type.googleapis.com/google.protobuf.testing.AnyM") |
1498 ->RenderString("foo", "foovalue") | 1757 ->RenderString("foo", "foovalue") |
1499 ->EndObject() | 1758 ->EndObject() |
1500 ->EndObject() | 1759 ->EndObject() |
1501 ->EndObject() | 1760 ->EndObject() |
1502 ->EndObject(); | 1761 ->EndObject(); |
| 1762 CheckOutput(out, 151); |
| 1763 } |
| 1764 |
| 1765 TEST_P(ProtoStreamObjectWriterAnyTest, TypeUrlAtEnd) { |
| 1766 Book book; |
| 1767 book.set_title("C++"); |
| 1768 book.set_length(1234); |
| 1769 book.set_content("Hello World!"); |
| 1770 |
| 1771 ::google::protobuf::Any any; |
| 1772 any.PackFrom(book); |
| 1773 |
| 1774 ::google::protobuf::Any outer_any; |
| 1775 outer_any.PackFrom(any); |
| 1776 |
| 1777 AnyOut out; |
| 1778 out.mutable_any()->PackFrom(outer_any); |
| 1779 |
| 1780 // Put the @type field at the end of each Any message. Parsers should |
| 1781 // be able to accept that. |
| 1782 ow_->StartObject("") |
| 1783 ->StartObject("any") |
| 1784 ->StartObject("value") |
| 1785 ->StartObject("value") |
| 1786 ->RenderString("title", "C++") |
| 1787 ->RenderInt32("length", 1234) |
| 1788 ->RenderBytes("content", "Hello World!") |
| 1789 ->RenderString("@type", |
| 1790 "type.googleapis.com/google.protobuf.testing.Book") |
| 1791 ->EndObject() |
| 1792 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
| 1793 ->EndObject() |
| 1794 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
| 1795 ->EndObject() |
| 1796 ->EndObject(); |
| 1797 CheckOutput(out); |
| 1798 } |
| 1799 |
| 1800 // Same as TypeUrlAtEnd, but use temporary string values to make sure we don't |
| 1801 // mistakenly store StringPiece objects pointing to invalid memory. |
| 1802 TEST_P(ProtoStreamObjectWriterAnyTest, TypeUrlAtEndWithTemporaryStrings) { |
| 1803 Book book; |
| 1804 book.set_title("C++"); |
| 1805 book.set_length(1234); |
| 1806 book.set_content("Hello World!"); |
| 1807 |
| 1808 ::google::protobuf::Any any; |
| 1809 any.PackFrom(book); |
| 1810 |
| 1811 ::google::protobuf::Any outer_any; |
| 1812 outer_any.PackFrom(any); |
| 1813 |
| 1814 AnyOut out; |
| 1815 out.mutable_any()->PackFrom(outer_any); |
| 1816 |
| 1817 string name, value; |
| 1818 // Put the @type field at the end of each Any message. Parsers should |
| 1819 // be able to accept that. |
| 1820 ow_->StartObject("")->StartObject("any"); |
| 1821 { |
| 1822 ow_->StartObject("value"); |
| 1823 { |
| 1824 ow_->StartObject("value"); |
| 1825 { |
| 1826 name = "title"; |
| 1827 value = "C++"; |
| 1828 ow_->RenderString(name, value); |
| 1829 name = "length"; |
| 1830 ow_->RenderInt32(name, 1234); |
| 1831 name = "content"; |
| 1832 value = "Hello World!"; |
| 1833 ow_->RenderBytes(name, value); |
| 1834 name = "@type"; |
| 1835 value = "type.googleapis.com/google.protobuf.testing.Book"; |
| 1836 ow_->RenderString(name, value); |
| 1837 } |
| 1838 ow_->EndObject(); |
| 1839 |
| 1840 name = "@type"; |
| 1841 value = "type.googleapis.com/google.protobuf.Any"; |
| 1842 ow_->RenderString(name, value); |
| 1843 } |
| 1844 ow_->EndObject(); |
| 1845 |
| 1846 name = "@type"; |
| 1847 value = "type.googleapis.com/google.protobuf.Any"; |
| 1848 ow_->RenderString(name, value); |
| 1849 } |
| 1850 ow_->EndObject()->EndObject(); |
| 1851 CheckOutput(out); |
1503 } | 1852 } |
1504 | 1853 |
1505 TEST_P(ProtoStreamObjectWriterAnyTest, EmptyAnyFromEmptyObject) { | 1854 TEST_P(ProtoStreamObjectWriterAnyTest, EmptyAnyFromEmptyObject) { |
1506 AnyOut out; | 1855 AnyOut out; |
1507 out.mutable_any(); | 1856 out.mutable_any(); |
1508 | 1857 |
| 1858 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 1859 |
1509 ow_->StartObject("")->StartObject("any")->EndObject()->EndObject(); | 1860 ow_->StartObject("")->StartObject("any")->EndObject()->EndObject(); |
1510 | 1861 |
1511 CheckOutput(out, 2); | 1862 CheckOutput(out, 2); |
1512 } | 1863 } |
1513 | 1864 |
1514 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails1) { | 1865 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails1) { |
1515 AnyOut any; | 1866 AnyOut any; |
1516 | 1867 |
1517 EXPECT_CALL( | 1868 EXPECT_CALL(listener_, |
1518 listener_, | 1869 InvalidValue(_, StringPiece("Any"), |
1519 InvalidValue(_, StringPiece("Any"), | 1870 StringPiece("Missing @type for any field in " |
1520 StringPiece("Missing or invalid @type for any field in " | 1871 "google.protobuf.testing.AnyOut"))); |
1521 "google.protobuf.testing.anys.AnyOut"))); | |
1522 | 1872 |
1523 ow_->StartObject("") | 1873 ow_->StartObject("") |
1524 ->StartObject("any") | 1874 ->StartObject("any") |
1525 ->StartObject("another") | 1875 ->StartObject("another") |
1526 ->EndObject() | 1876 ->EndObject() |
1527 ->EndObject() | 1877 ->EndObject() |
1528 ->EndObject(); | 1878 ->EndObject(); |
1529 CheckOutput(any); | 1879 CheckOutput(any); |
1530 } | 1880 } |
1531 | 1881 |
1532 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails2) { | 1882 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails2) { |
1533 AnyOut any; | 1883 AnyOut any; |
1534 | 1884 |
1535 EXPECT_CALL( | 1885 EXPECT_CALL(listener_, |
1536 listener_, | 1886 InvalidValue(_, StringPiece("Any"), |
1537 InvalidValue(_, StringPiece("Any"), | 1887 StringPiece("Missing @type for any field in " |
1538 StringPiece("Missing or invalid @type for any field in " | 1888 "google.protobuf.testing.AnyOut"))); |
1539 "google.protobuf.testing.anys.AnyOut"))); | |
1540 | 1889 |
1541 ow_->StartObject("") | 1890 ow_->StartObject("") |
1542 ->StartObject("any") | 1891 ->StartObject("any") |
1543 ->StartList("another") | 1892 ->StartList("another") |
1544 ->EndObject() | 1893 ->EndObject() |
1545 ->EndObject() | 1894 ->EndObject() |
1546 ->EndObject(); | 1895 ->EndObject(); |
1547 CheckOutput(any); | 1896 CheckOutput(any); |
1548 } | 1897 } |
1549 | 1898 |
1550 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails3) { | 1899 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails3) { |
1551 AnyOut any; | 1900 AnyOut any; |
1552 | 1901 |
1553 EXPECT_CALL( | 1902 EXPECT_CALL(listener_, |
1554 listener_, | 1903 InvalidValue(_, StringPiece("Any"), |
1555 InvalidValue(_, StringPiece("Any"), | 1904 StringPiece("Missing @type for any field in " |
1556 StringPiece("Missing or invalid @type for any field in " | 1905 "google.protobuf.testing.AnyOut"))); |
1557 "google.protobuf.testing.anys.AnyOut"))); | |
1558 | 1906 |
1559 ow_->StartObject("") | 1907 ow_->StartObject("") |
1560 ->StartObject("any") | 1908 ->StartObject("any") |
1561 ->RenderString("value", "somevalue") | 1909 ->RenderString("value", "somevalue") |
1562 ->EndObject() | 1910 ->EndObject() |
1563 ->EndObject(); | 1911 ->EndObject(); |
1564 CheckOutput(any); | 1912 CheckOutput(any); |
1565 } | 1913 } |
1566 | 1914 |
1567 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithInvalidTypeUrlFails) { | 1915 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithInvalidTypeUrlFails) { |
(...skipping 24 matching lines...) Expand all Loading... |
1592 StringPiece("Invalid type URL, unknown type: some.Type"))); | 1940 StringPiece("Invalid type URL, unknown type: some.Type"))); |
1593 ow_->StartObject("") | 1941 ow_->StartObject("") |
1594 ->StartObject("any") | 1942 ->StartObject("any") |
1595 ->RenderString("@type", "type.googleapis.com/some.Type") | 1943 ->RenderString("@type", "type.googleapis.com/some.Type") |
1596 ->RenderDouble("value", 40.2) | 1944 ->RenderDouble("value", 40.2) |
1597 ->EndObject() | 1945 ->EndObject() |
1598 ->EndObject(); | 1946 ->EndObject(); |
1599 CheckOutput(any); | 1947 CheckOutput(any); |
1600 } | 1948 } |
1601 | 1949 |
1602 TEST_P(ProtoStreamObjectWriterAnyTest, AnyNullInputFails) { | 1950 TEST_P(ProtoStreamObjectWriterAnyTest, AnyIncorrectInputTypeFails) { |
1603 AnyOut any; | 1951 AnyOut any; |
1604 | 1952 |
| 1953 EXPECT_CALL( |
| 1954 listener_, |
| 1955 InvalidValue(_, StringPiece("type.googleapis.com/google.protobuf.Any"), |
| 1956 StringPiece("1"))); |
| 1957 ow_->StartObject("")->RenderInt32("any", 1)->EndObject(); |
| 1958 CheckOutput(any); |
| 1959 } |
| 1960 |
| 1961 TEST_P(ProtoStreamObjectWriterAnyTest, AnyAcceptsNull) { |
| 1962 AnyOut any; |
| 1963 |
| 1964 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
1605 ow_->StartObject("")->RenderNull("any")->EndObject(); | 1965 ow_->StartObject("")->RenderNull("any")->EndObject(); |
1606 CheckOutput(any); | 1966 CheckOutput(any); |
1607 } | 1967 } |
1608 | 1968 |
1609 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypeErrorTest) { | 1969 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypeErrorTest) { |
1610 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("Any"), | 1970 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("Any"), |
1611 StringPiece("Invalid time format: "))); | 1971 StringPiece("Invalid time format: "))); |
1612 | 1972 |
1613 AnyOut any; | 1973 AnyOut any; |
1614 google::protobuf::Any* any_type = any.mutable_any(); | 1974 google::protobuf::Any* any_type = any.mutable_any(); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1832 | 2192 |
1833 ow_->StartObject("") | 2193 ow_->StartObject("") |
1834 ->StartObject("any") | 2194 ->StartObject("any") |
1835 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 2195 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
1836 ->RenderString("value", "") | 2196 ->RenderString("value", "") |
1837 ->EndObject() | 2197 ->EndObject() |
1838 ->EndObject(); | 2198 ->EndObject(); |
1839 CheckOutput(any); | 2199 CheckOutput(any); |
1840 } | 2200 } |
1841 | 2201 |
| 2202 // { |
| 2203 // "any": { |
| 2204 // "@type": "type.googleapis.com/google.protobuf.Any", |
| 2205 // "value": null |
| 2206 // } |
| 2207 // } |
| 2208 TEST_P(ProtoStreamObjectWriterAnyTest, AnyInAnyAcceptsNull) { |
| 2209 AnyOut out; |
| 2210 google::protobuf::Any empty; |
| 2211 out.mutable_any()->PackFrom(empty); |
| 2212 |
| 2213 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 2214 ow_->StartObject("") |
| 2215 ->StartObject("any") |
| 2216 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
| 2217 ->RenderNull("value") |
| 2218 ->EndObject() |
| 2219 ->EndObject(); |
| 2220 CheckOutput(out); |
| 2221 } |
| 2222 |
| 2223 // { |
| 2224 // "any": { |
| 2225 // "@type": "type.googleapis.com/google.protobuf.Timestamp", |
| 2226 // "value": null |
| 2227 // } |
| 2228 // } |
| 2229 TEST_P(ProtoStreamObjectWriterAnyTest, TimestampInAnyAcceptsNull) { |
| 2230 AnyOut out; |
| 2231 google::protobuf::Timestamp empty; |
| 2232 out.mutable_any()->PackFrom(empty); |
| 2233 |
| 2234 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 2235 ow_->StartObject("") |
| 2236 ->StartObject("any") |
| 2237 ->RenderString("@type", "type.googleapis.com/google.protobuf.Timestamp") |
| 2238 ->RenderNull("value") |
| 2239 ->EndObject() |
| 2240 ->EndObject(); |
| 2241 CheckOutput(out); |
| 2242 } |
| 2243 |
| 2244 // { |
| 2245 // "any": { |
| 2246 // "@type": "type.googleapis.com/google.protobuf.Duration", |
| 2247 // "value": null |
| 2248 // } |
| 2249 // } |
| 2250 TEST_P(ProtoStreamObjectWriterAnyTest, DurationInAnyAcceptsNull) { |
| 2251 AnyOut out; |
| 2252 google::protobuf::Duration empty; |
| 2253 out.mutable_any()->PackFrom(empty); |
| 2254 |
| 2255 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 2256 ow_->StartObject("") |
| 2257 ->StartObject("any") |
| 2258 ->RenderString("@type", "type.googleapis.com/google.protobuf.Duration") |
| 2259 ->RenderNull("value") |
| 2260 ->EndObject() |
| 2261 ->EndObject(); |
| 2262 CheckOutput(out); |
| 2263 } |
| 2264 |
| 2265 // { |
| 2266 // "any": { |
| 2267 // "@type": "type.googleapis.com/google.protobuf.FieldMask", |
| 2268 // "value": null |
| 2269 // } |
| 2270 // } |
| 2271 TEST_P(ProtoStreamObjectWriterAnyTest, FieldMaskInAnyAcceptsNull) { |
| 2272 AnyOut out; |
| 2273 google::protobuf::FieldMask empty; |
| 2274 out.mutable_any()->PackFrom(empty); |
| 2275 |
| 2276 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 2277 ow_->StartObject("") |
| 2278 ->StartObject("any") |
| 2279 ->RenderString("@type", "type.googleapis.com/google.protobuf.FieldMask") |
| 2280 ->RenderNull("value") |
| 2281 ->EndObject() |
| 2282 ->EndObject(); |
| 2283 CheckOutput(out); |
| 2284 } |
| 2285 |
| 2286 // { |
| 2287 // "any": { |
| 2288 // "@type": "type.googleapis.com/google.protobuf.Int32Value", |
| 2289 // "value": null |
| 2290 // } |
| 2291 // } |
| 2292 TEST_P(ProtoStreamObjectWriterAnyTest, WrapperInAnyAcceptsNull) { |
| 2293 AnyOut out; |
| 2294 google::protobuf::Int32Value empty; |
| 2295 out.mutable_any()->PackFrom(empty); |
| 2296 |
| 2297 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 2298 ow_->StartObject("") |
| 2299 ->StartObject("any") |
| 2300 ->RenderString("@type", "type.googleapis.com/google.protobuf.Int32Value") |
| 2301 ->RenderNull("value") |
| 2302 ->EndObject() |
| 2303 ->EndObject(); |
| 2304 CheckOutput(out); |
| 2305 } |
| 2306 |
1842 class ProtoStreamObjectWriterFieldMaskTest | 2307 class ProtoStreamObjectWriterFieldMaskTest |
1843 : public BaseProtoStreamObjectWriterTest { | 2308 : public BaseProtoStreamObjectWriterTest { |
1844 protected: | 2309 protected: |
1845 ProtoStreamObjectWriterFieldMaskTest() { | 2310 ProtoStreamObjectWriterFieldMaskTest() { |
1846 vector<const Descriptor*> descriptors; | 2311 std::vector<const Descriptor*> descriptors; |
1847 descriptors.push_back(FieldMaskTest::descriptor()); | 2312 descriptors.push_back(FieldMaskTest::descriptor()); |
1848 descriptors.push_back(google::protobuf::FieldMask::descriptor()); | 2313 descriptors.push_back(google::protobuf::FieldMask::descriptor()); |
1849 ResetTypeInfo(descriptors); | 2314 ResetTypeInfo(descriptors); |
1850 } | 2315 } |
1851 }; | 2316 }; |
1852 | 2317 |
1853 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 2318 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1854 ProtoStreamObjectWriterFieldMaskTest, | 2319 ProtoStreamObjectWriterFieldMaskTest, |
1855 ::testing::Values( | 2320 ::testing::Values( |
1856 testing::USE_TYPE_RESOLVER)); | 2321 testing::USE_TYPE_RESOLVER)); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2077 ow_->StartObject(""); | 2542 ow_->StartObject(""); |
2078 ow_->RenderString( | 2543 ow_->RenderString( |
2079 "single_mask", | 2544 "single_mask", |
2080 "path.to.map[\"(),[],\\\"'!@#$%^&*123_|War\xE5\xAD\x99,./?><\\\\\"]," | 2545 "path.to.map[\"(),[],\\\"'!@#$%^&*123_|War\xE5\xAD\x99,./?><\\\\\"]," |
2081 "path.to.map[\"key2\"]"); | 2546 "path.to.map[\"key2\"]"); |
2082 ow_->EndObject(); | 2547 ow_->EndObject(); |
2083 | 2548 |
2084 CheckOutput(expected); | 2549 CheckOutput(expected); |
2085 } | 2550 } |
2086 | 2551 |
| 2552 TEST_P(ProtoStreamObjectWriterFieldMaskTest, FieldMaskAcceptsNull) { |
| 2553 FieldMaskTest expected; |
| 2554 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); |
| 2555 ow_->StartObject("")->RenderNull("single_mask")->EndObject(); |
| 2556 CheckOutput(expected); |
| 2557 } |
| 2558 |
| 2559 class ProtoStreamObjectWriterWrappersTest |
| 2560 : public BaseProtoStreamObjectWriterTest { |
| 2561 protected: |
| 2562 ProtoStreamObjectWriterWrappersTest() { |
| 2563 std::vector<const Descriptor*> descriptors; |
| 2564 descriptors.push_back(Int32Wrapper::descriptor()); |
| 2565 descriptors.push_back(google::protobuf::Int32Value::descriptor()); |
| 2566 ResetTypeInfo(descriptors); |
| 2567 } |
| 2568 }; |
| 2569 |
| 2570 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
| 2571 ProtoStreamObjectWriterWrappersTest, |
| 2572 ::testing::Values( |
| 2573 testing::USE_TYPE_RESOLVER)); |
| 2574 |
| 2575 TEST_P(ProtoStreamObjectWriterWrappersTest, WrapperAcceptsNull) { |
| 2576 Int32Wrapper wrapper; |
| 2577 EXPECT_CALL(listener_, InvalidName(_, _, _)).Times(0); |
| 2578 ow_->StartObject("")->RenderNull("int32")->EndObject(); |
| 2579 CheckOutput(wrapper); |
| 2580 } |
| 2581 |
2087 class ProtoStreamObjectWriterOneOfsTest | 2582 class ProtoStreamObjectWriterOneOfsTest |
2088 : public BaseProtoStreamObjectWriterTest { | 2583 : public BaseProtoStreamObjectWriterTest { |
2089 protected: | 2584 protected: |
2090 ProtoStreamObjectWriterOneOfsTest() { | 2585 ProtoStreamObjectWriterOneOfsTest() { |
2091 vector<const Descriptor*> descriptors; | 2586 std::vector<const Descriptor*> descriptors; |
2092 descriptors.push_back(OneOfsRequest::descriptor()); | 2587 descriptors.push_back(OneOfsRequest::descriptor()); |
2093 descriptors.push_back(google::protobuf::Struct::descriptor()); | 2588 descriptors.push_back(google::protobuf::Struct::descriptor()); |
2094 ResetTypeInfo(descriptors); | 2589 ResetTypeInfo(descriptors); |
2095 } | 2590 } |
2096 }; | 2591 }; |
2097 | 2592 |
2098 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 2593 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
2099 ProtoStreamObjectWriterOneOfsTest, | 2594 ProtoStreamObjectWriterOneOfsTest, |
2100 ::testing::Values( | 2595 ::testing::Values( |
2101 testing::USE_TYPE_RESOLVER)); | 2596 testing::USE_TYPE_RESOLVER)); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2244 ow_->EndObject(); | 2739 ow_->EndObject(); |
2245 } | 2740 } |
2246 | 2741 |
2247 TEST_P(ProtoStreamObjectWriterOneOfsTest, | 2742 TEST_P(ProtoStreamObjectWriterOneOfsTest, |
2248 MultipleOneofsFailForOneofWithinAnyTest) { | 2743 MultipleOneofsFailForOneofWithinAnyTest) { |
2249 EXPECT_CALL(listener_, | 2744 EXPECT_CALL(listener_, |
2250 InvalidValue(_, StringPiece("oneof"), | 2745 InvalidValue(_, StringPiece("oneof"), |
2251 StringPiece("oneof field 'data' is already set. " | 2746 StringPiece("oneof field 'data' is already set. " |
2252 "Cannot set 'intData'"))); | 2747 "Cannot set 'intData'"))); |
2253 | 2748 |
2254 using google::protobuf::testing::oneofs::OneOfsRequest; | |
2255 // JSON: | 2749 // JSON: |
2256 // { "anyData": | 2750 // { "anyData": |
2257 // { "@type": | 2751 // { "@type": |
2258 // "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest", | 2752 // "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest", |
2259 // "strData": "blah", | 2753 // "strData": "blah", |
2260 // "intData": 123 | 2754 // "intData": 123 |
2261 // } | 2755 // } |
2262 // } | 2756 // } |
2263 ow_->StartObject(""); | 2757 ow_->StartObject(""); |
2264 ow_->StartObject("anyData"); | 2758 ow_->StartObject("anyData"); |
2265 ow_->RenderString( | 2759 ow_->RenderString( |
2266 "@type", | 2760 "@type", |
2267 "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest"); | 2761 "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest"); |
2268 ow_->RenderString("strData", "blah"); | 2762 ow_->RenderString("strData", "blah"); |
2269 ow_->RenderInt32("intData", 123); | 2763 ow_->RenderInt32("intData", 123); |
2270 ow_->EndObject(); | 2764 ow_->EndObject(); |
2271 ow_->EndObject(); | 2765 ow_->EndObject(); |
2272 } | 2766 } |
2273 | 2767 |
2274 } // namespace converter | 2768 } // namespace converter |
2275 } // namespace util | 2769 } // namespace util |
2276 } // namespace protobuf | 2770 } // namespace protobuf |
2277 } // namespace google | 2771 } // namespace google |
OLD | NEW |