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> | |
45 #include <google/protobuf/util/internal/testdata/books.pb.h> | 44 #include <google/protobuf/util/internal/testdata/books.pb.h> |
46 #include <google/protobuf/util/internal/testdata/field_mask.pb.h> | 45 #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> | |
53 #include <google/protobuf/util/internal/type_info_test_helper.h> | 46 #include <google/protobuf/util/internal/type_info_test_helper.h> |
54 #include <google/protobuf/util/internal/constants.h> | 47 #include <google/protobuf/util/internal/constants.h> |
55 #include <google/protobuf/util/message_differencer.h> | 48 #include <google/protobuf/util/message_differencer.h> |
56 #include <google/protobuf/stubs/bytestream.h> | 49 #include <google/protobuf/stubs/bytestream.h> |
57 #include <google/protobuf/stubs/strutil.h> | 50 #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> |
58 #include <gtest/gtest.h> | 56 #include <gtest/gtest.h> |
59 | 57 |
60 | 58 |
61 namespace google { | 59 namespace google { |
62 namespace protobuf { | 60 namespace protobuf { |
63 namespace util { | 61 namespace util { |
64 namespace converter { | 62 namespace converter { |
65 | 63 |
66 using google::protobuf::testing::AnyM; | |
67 using google::protobuf::testing::AnyOut; | |
68 using google::protobuf::testing::Author; | 64 using google::protobuf::testing::Author; |
69 using google::protobuf::testing::Book; | 65 using google::protobuf::testing::Book; |
70 using google::protobuf::testing::FieldMaskTest; | 66 using google::protobuf::testing::Book_Data; |
71 using google::protobuf::testing::Int32Wrapper; | |
72 using google::protobuf::testing::MapIn; | |
73 using google::protobuf::testing::Primitive; | 67 using google::protobuf::testing::Primitive; |
74 using google::protobuf::testing::Proto3Message; | |
75 using google::protobuf::testing::Publisher; | 68 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; | |
80 using google::protobuf::Descriptor; | 69 using google::protobuf::Descriptor; |
81 using google::protobuf::DescriptorPool; | 70 using google::protobuf::DescriptorPool; |
82 using google::protobuf::DynamicMessageFactory; | 71 using google::protobuf::DynamicMessageFactory; |
83 using google::protobuf::FileDescriptorProto; | 72 using google::protobuf::FileDescriptorProto; |
84 using google::protobuf::Message; | 73 using google::protobuf::Message; |
| 74 using google::protobuf::io::ArrayInputStream; |
85 using strings::GrowingArrayByteSink; | 75 using strings::GrowingArrayByteSink; |
86 using ::testing::_; | 76 using ::testing::_; |
87 using ::testing::Args; | 77 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; |
88 | 85 |
89 | 86 |
90 namespace { | 87 namespace { |
91 string GetTypeUrl(const Descriptor* descriptor) { | 88 string GetTypeUrl(const Descriptor* descriptor) { |
92 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(); | 89 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(); |
93 } | 90 } |
94 } // namespace | 91 } // namespace |
95 | 92 |
96 #if __cplusplus >= 201103L | 93 #if __cplusplus >= 201103L |
97 using std::get; | 94 using std::get; |
98 #else | 95 #else |
99 using std::tr1::get; | 96 using std::tr1::get; |
100 #endif | 97 #endif |
101 | 98 |
102 class BaseProtoStreamObjectWriterTest | 99 class BaseProtoStreamObjectWriterTest |
103 : public ::testing::TestWithParam<testing::TypeInfoSource> { | 100 : public ::testing::TestWithParam<testing::TypeInfoSource> { |
104 protected: | 101 protected: |
105 BaseProtoStreamObjectWriterTest() | 102 BaseProtoStreamObjectWriterTest() |
106 : helper_(GetParam()), | 103 : helper_(GetParam()), |
107 listener_(), | 104 listener_(), |
108 output_(new GrowingArrayByteSink(1000)), | 105 output_(new GrowingArrayByteSink(1000)), |
109 ow_() {} | 106 ow_() {} |
110 | 107 |
111 explicit BaseProtoStreamObjectWriterTest(const Descriptor* descriptor) | 108 explicit BaseProtoStreamObjectWriterTest(const Descriptor* descriptor) |
112 : helper_(GetParam()), | 109 : helper_(GetParam()), |
113 listener_(), | 110 listener_(), |
114 output_(new GrowingArrayByteSink(1000)), | 111 output_(new GrowingArrayByteSink(1000)), |
115 ow_() { | 112 ow_() { |
116 std::vector<const Descriptor*> descriptors; | 113 vector<const Descriptor*> descriptors; |
117 descriptors.push_back(descriptor); | 114 descriptors.push_back(descriptor); |
118 ResetTypeInfo(descriptors); | 115 ResetTypeInfo(descriptors); |
119 } | 116 } |
120 | 117 |
121 explicit BaseProtoStreamObjectWriterTest( | 118 explicit BaseProtoStreamObjectWriterTest( |
122 std::vector<const Descriptor*> descriptors) | 119 vector<const Descriptor*> descriptors) |
123 : helper_(GetParam()), | 120 : helper_(GetParam()), |
124 listener_(), | 121 listener_(), |
125 output_(new GrowingArrayByteSink(1000)), | 122 output_(new GrowingArrayByteSink(1000)), |
126 ow_() { | 123 ow_() { |
127 ResetTypeInfo(descriptors); | 124 ResetTypeInfo(descriptors); |
128 } | 125 } |
129 | 126 |
130 void ResetTypeInfo(std::vector<const Descriptor*> descriptors) { | 127 void ResetTypeInfo(vector<const Descriptor*> descriptors) { |
131 GOOGLE_CHECK(!descriptors.empty()) << "Must have at least one descriptor!"; | 128 GOOGLE_CHECK(!descriptors.empty()) << "Must have at least one descriptor!"; |
132 helper_.ResetTypeInfo(descriptors); | 129 helper_.ResetTypeInfo(descriptors); |
133 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(descriptors[0]), output_.get(), | 130 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(descriptors[0]), output_.get(), |
134 &listener_, options_)); | 131 &listener_, options_)); |
135 } | 132 } |
136 | 133 |
137 void ResetTypeInfo(const Descriptor* descriptor) { | 134 void ResetTypeInfo(const Descriptor* descriptor) { |
138 std::vector<const Descriptor*> descriptors; | 135 vector<const Descriptor*> descriptors; |
139 descriptors.push_back(descriptor); | 136 descriptors.push_back(descriptor); |
140 ResetTypeInfo(descriptors); | 137 ResetTypeInfo(descriptors); |
141 } | 138 } |
142 | 139 |
143 virtual ~BaseProtoStreamObjectWriterTest() {} | 140 virtual ~BaseProtoStreamObjectWriterTest() {} |
144 | 141 |
145 void CheckOutput(const Message& expected, int expected_length) { | 142 void CheckOutput(const Message& expected, int expected_length) { |
146 size_t nbytes; | 143 size_t nbytes; |
147 google::protobuf::scoped_array<char> buffer(output_->GetBuffer(&nbytes)); | 144 google::protobuf::scoped_array<char> buffer(output_->GetBuffer(&nbytes)); |
148 if (expected_length >= 0) { | 145 if (expected_length >= 0) { |
(...skipping 30 matching lines...) Expand all Loading... |
179 if (actual.compare(expected) == 0) return true; | 176 if (actual.compare(expected) == 0) return true; |
180 *result_listener << "actual location is: " << actual; | 177 *result_listener << "actual location is: " << actual; |
181 return false; | 178 return false; |
182 } | 179 } |
183 | 180 |
184 class ProtoStreamObjectWriterTest : public BaseProtoStreamObjectWriterTest { | 181 class ProtoStreamObjectWriterTest : public BaseProtoStreamObjectWriterTest { |
185 protected: | 182 protected: |
186 ProtoStreamObjectWriterTest() | 183 ProtoStreamObjectWriterTest() |
187 : BaseProtoStreamObjectWriterTest(Book::descriptor()) {} | 184 : BaseProtoStreamObjectWriterTest(Book::descriptor()) {} |
188 | 185 |
189 void ResetProtoWriter() { | |
190 ResetTypeInfo(Book::descriptor()); | |
191 } | |
192 | |
193 virtual ~ProtoStreamObjectWriterTest() {} | 186 virtual ~ProtoStreamObjectWriterTest() {} |
194 }; | 187 }; |
195 | 188 |
196 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 189 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
197 ProtoStreamObjectWriterTest, | 190 ProtoStreamObjectWriterTest, |
198 ::testing::Values( | 191 ::testing::Values( |
199 testing::USE_TYPE_RESOLVER)); | 192 testing::USE_TYPE_RESOLVER)); |
200 | 193 |
201 TEST_P(ProtoStreamObjectWriterTest, EmptyObject) { | 194 TEST_P(ProtoStreamObjectWriterTest, EmptyObject) { |
202 Book empty; | 195 Book empty; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 | 257 |
265 ow_->StartObject("") | 258 ow_->StartObject("") |
266 ->StartObject("author") | 259 ->StartObject("author") |
267 ->RenderUint64("@id", 12345) | 260 ->RenderUint64("@id", 12345) |
268 ->RenderString("name", "robert") | 261 ->RenderString("name", "robert") |
269 ->EndObject() | 262 ->EndObject() |
270 ->EndObject(); | 263 ->EndObject(); |
271 CheckOutput(book); | 264 CheckOutput(book); |
272 } | 265 } |
273 | 266 |
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 | |
352 TEST_P(ProtoStreamObjectWriterTest, PrimitiveFromStringConversion) { | 267 TEST_P(ProtoStreamObjectWriterTest, PrimitiveFromStringConversion) { |
353 Primitive full; | 268 Primitive full; |
354 full.set_fix32(101); | 269 full.set_fix32(101); |
355 full.set_u32(102); | 270 full.set_u32(102); |
356 full.set_i32(-103); | 271 full.set_i32(-103); |
357 full.set_sf32(-104); | 272 full.set_sf32(-104); |
358 full.set_s32(-105); | 273 full.set_s32(-105); |
359 full.set_fix64(40000000001L); | 274 full.set_fix64(40000000001L); |
360 full.set_u64(40000000002L); | 275 full.set_u64(40000000002L); |
361 full.set_i64(-40000000003L); | 276 full.set_i64(-40000000003L); |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 ->StartObject("publisher") | 702 ->StartObject("publisher") |
788 ->RenderString("name", "propaganda") | 703 ->RenderString("name", "propaganda") |
789 ->StartList("alliance") | 704 ->StartList("alliance") |
790 ->EndList() | 705 ->EndList() |
791 ->EndObject() | 706 ->EndObject() |
792 ->RenderString("title", "Brainwashing") | 707 ->RenderString("title", "Brainwashing") |
793 ->EndObject(); | 708 ->EndObject(); |
794 CheckOutput(expected); | 709 CheckOutput(expected); |
795 } | 710 } |
796 | 711 |
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 | |
923 TEST_P(ProtoStreamObjectWriterTest, MissingRequiredField) { | 712 TEST_P(ProtoStreamObjectWriterTest, MissingRequiredField) { |
924 Book expected; | 713 Book expected; |
925 expected.set_title("My Title"); | 714 expected.set_title("My Title"); |
926 expected.set_allocated_publisher(new Publisher()); | 715 expected.set_allocated_publisher(new Publisher()); |
927 | 716 |
928 EXPECT_CALL(listener_, MissingField(_, StringPiece("name"))) | 717 EXPECT_CALL(listener_, MissingField(_, StringPiece("name"))) |
929 .With(Args<0>(HasObjectLocation("publisher"))); | 718 .With(Args<0>(HasObjectLocation("publisher"))); |
930 ow_->StartObject("") | 719 ow_->StartObject("") |
931 ->StartObject("publisher") | 720 ->StartObject("publisher") |
932 ->EndObject() | 721 ->EndObject() |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1071 Book empty; | 860 Book empty; |
1072 | 861 |
1073 ow_->RenderNull("length"); | 862 ow_->RenderNull("length"); |
1074 CheckOutput(empty, 0); | 863 CheckOutput(empty, 0); |
1075 } | 864 } |
1076 | 865 |
1077 class ProtoStreamObjectWriterTimestampDurationTest | 866 class ProtoStreamObjectWriterTimestampDurationTest |
1078 : public BaseProtoStreamObjectWriterTest { | 867 : public BaseProtoStreamObjectWriterTest { |
1079 protected: | 868 protected: |
1080 ProtoStreamObjectWriterTimestampDurationTest() { | 869 ProtoStreamObjectWriterTimestampDurationTest() { |
1081 std::vector<const Descriptor*> descriptors; | 870 vector<const Descriptor*> descriptors; |
1082 descriptors.push_back(TimestampDuration::descriptor()); | 871 descriptors.push_back(TimestampDuration::descriptor()); |
1083 descriptors.push_back(google::protobuf::Timestamp::descriptor()); | 872 descriptors.push_back(google::protobuf::Timestamp::descriptor()); |
1084 descriptors.push_back(google::protobuf::Duration::descriptor()); | 873 descriptors.push_back(google::protobuf::Duration::descriptor()); |
1085 ResetTypeInfo(descriptors); | 874 ResetTypeInfo(descriptors); |
1086 } | 875 } |
1087 }; | 876 }; |
1088 | 877 |
1089 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 878 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1090 ProtoStreamObjectWriterTimestampDurationTest, | 879 ProtoStreamObjectWriterTimestampDurationTest, |
1091 ::testing::Values( | 880 ::testing::Values( |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1425 } | 1214 } |
1426 | 1215 |
1427 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, | 1216 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, |
1428 MismatchedTimestampTypeInput) { | 1217 MismatchedTimestampTypeInput) { |
1429 TimestampDuration timestamp; | 1218 TimestampDuration timestamp; |
1430 EXPECT_CALL( | 1219 EXPECT_CALL( |
1431 listener_, | 1220 listener_, |
1432 InvalidValue( | 1221 InvalidValue( |
1433 _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), | 1222 _, StringPiece("type.googleapis.com/google.protobuf.Timestamp"), |
1434 StringPiece( | 1223 StringPiece( |
1435 "Field 'ts', Invalid data type for timestamp, value is 1"))) | 1224 "Field 'ts', Invalid data type for timestamp, value is null"))) |
1436 .With(Args<0>(HasObjectLocation("ts"))); | 1225 .With(Args<0>(HasObjectLocation("ts"))); |
1437 ow_->StartObject("")->RenderInt32("ts", 1)->EndObject(); | 1226 ow_->StartObject("")->RenderNull("ts")->EndObject(); |
1438 CheckOutput(timestamp); | 1227 CheckOutput(timestamp); |
1439 } | 1228 } |
1440 | 1229 |
1441 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, | 1230 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, |
1442 MismatchedDurationTypeInput) { | 1231 MismatchedDurationTypeInput) { |
1443 TimestampDuration duration; | 1232 TimestampDuration duration; |
1444 EXPECT_CALL( | 1233 EXPECT_CALL( |
1445 listener_, | 1234 listener_, |
1446 InvalidValue( | 1235 InvalidValue( |
1447 _, StringPiece("type.googleapis.com/google.protobuf.Duration"), | 1236 _, StringPiece("type.googleapis.com/google.protobuf.Duration"), |
1448 StringPiece( | 1237 StringPiece( |
1449 "Field 'dur', Invalid data type for duration, value is 1"))) | 1238 "Field 'dur', Invalid data type for duration, value is null"))) |
1450 .With(Args<0>(HasObjectLocation("dur"))); | 1239 .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); | |
1465 ow_->StartObject("")->RenderNull("dur")->EndObject(); | 1240 ow_->StartObject("")->RenderNull("dur")->EndObject(); |
1466 CheckOutput(duration); | 1241 CheckOutput(duration); |
1467 } | 1242 } |
1468 | 1243 |
1469 class ProtoStreamObjectWriterStructTest | 1244 class ProtoStreamObjectWriterStructTest |
1470 : public BaseProtoStreamObjectWriterTest { | 1245 : public BaseProtoStreamObjectWriterTest { |
1471 protected: | 1246 protected: |
1472 ProtoStreamObjectWriterStructTest() { ResetProtoWriter(); } | 1247 ProtoStreamObjectWriterStructTest() { ResetProtoWriter(); } |
1473 | 1248 |
1474 // Resets ProtoWriter with current set of options and other state. | 1249 // Resets ProtoWriter with current set of options and other state. |
1475 void ResetProtoWriter() { | 1250 void ResetProtoWriter() { |
1476 std::vector<const Descriptor*> descriptors; | 1251 vector<const Descriptor*> descriptors; |
1477 descriptors.push_back(StructType::descriptor()); | 1252 descriptors.push_back(StructType::descriptor()); |
1478 descriptors.push_back(google::protobuf::Struct::descriptor()); | 1253 descriptors.push_back(google::protobuf::Struct::descriptor()); |
1479 ResetTypeInfo(descriptors); | 1254 ResetTypeInfo(descriptors); |
1480 } | 1255 } |
1481 }; | 1256 }; |
1482 | 1257 |
1483 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1258 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1484 ProtoStreamObjectWriterStructTest, | 1259 ProtoStreamObjectWriterStructTest, |
1485 ::testing::Values( | 1260 ::testing::Values( |
1486 testing::USE_TYPE_RESOLVER)); | 1261 testing::USE_TYPE_RESOLVER)); |
(...skipping 29 matching lines...) Expand all Loading... |
1516 EXPECT_CALL( | 1291 EXPECT_CALL( |
1517 listener_, | 1292 listener_, |
1518 InvalidValue(_, StringPiece("type.googleapis.com/google.protobuf.Struct"), | 1293 InvalidValue(_, StringPiece("type.googleapis.com/google.protobuf.Struct"), |
1519 StringPiece("true"))) | 1294 StringPiece("true"))) |
1520 .With(Args<0>(HasObjectLocation("object"))); | 1295 .With(Args<0>(HasObjectLocation("object"))); |
1521 | 1296 |
1522 ow_->StartObject("")->RenderBool("object", true)->EndObject(); | 1297 ow_->StartObject("")->RenderBool("object", true)->EndObject(); |
1523 CheckOutput(struct_type); | 1298 CheckOutput(struct_type); |
1524 } | 1299 } |
1525 | 1300 |
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 | |
1548 TEST_P(ProtoStreamObjectWriterStructTest, SimpleRepeatedStructMapKeyTest) { | 1301 TEST_P(ProtoStreamObjectWriterStructTest, SimpleRepeatedStructMapKeyTest) { |
1549 EXPECT_CALL( | 1302 EXPECT_CALL( |
1550 listener_, | 1303 listener_, |
1551 InvalidName(_, StringPiece("gBike"), | 1304 InvalidName(_, StringPiece("gBike"), |
1552 StringPiece("Repeated map key: 'gBike' is already set."))); | 1305 StringPiece("Repeated map key: 'gBike' is already set."))); |
1553 ow_->StartObject("") | 1306 ow_->StartObject("") |
1554 ->StartObject("object") | 1307 ->StartObject("object") |
1555 ->RenderString("gBike", "v1") | 1308 ->RenderString("gBike", "v1") |
1556 ->RenderString("gBike", "v2") | 1309 ->RenderString("gBike", "v2") |
1557 ->EndObject() | 1310 ->EndObject() |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1605 ->StartObject("object") | 1358 ->StartObject("object") |
1606 ->RenderDouble("k1", 123) | 1359 ->RenderDouble("k1", 123) |
1607 ->RenderBool("k2", true) | 1360 ->RenderBool("k2", true) |
1608 ->RenderInt64("k3", -222222222) | 1361 ->RenderInt64("k3", -222222222) |
1609 ->RenderUint64("k4", 33333333) | 1362 ->RenderUint64("k4", 33333333) |
1610 ->EndObject() | 1363 ->EndObject() |
1611 ->EndObject(); | 1364 ->EndObject(); |
1612 CheckOutput(struct_type); | 1365 CheckOutput(struct_type); |
1613 } | 1366 } |
1614 | 1367 |
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 | |
1624 class ProtoStreamObjectWriterMapTest : public BaseProtoStreamObjectWriterTest { | 1368 class ProtoStreamObjectWriterMapTest : public BaseProtoStreamObjectWriterTest { |
1625 protected: | 1369 protected: |
1626 ProtoStreamObjectWriterMapTest() | 1370 ProtoStreamObjectWriterMapTest() |
1627 : BaseProtoStreamObjectWriterTest(MapIn::descriptor()) {} | 1371 : BaseProtoStreamObjectWriterTest(MapIn::descriptor()) {} |
1628 }; | 1372 }; |
1629 | 1373 |
1630 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1374 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1631 ProtoStreamObjectWriterMapTest, | 1375 ProtoStreamObjectWriterMapTest, |
1632 ::testing::Values( | 1376 ::testing::Values( |
1633 testing::USE_TYPE_RESOLVER)); | 1377 testing::USE_TYPE_RESOLVER)); |
(...skipping 23 matching lines...) Expand all Loading... |
1657 ->StartObject("map_input") | 1401 ->StartObject("map_input") |
1658 ->RenderString("k1", "v1") | 1402 ->RenderString("k1", "v1") |
1659 ->RenderString("k1", "v2") | 1403 ->RenderString("k1", "v2") |
1660 ->EndObject() | 1404 ->EndObject() |
1661 ->EndObject(); | 1405 ->EndObject(); |
1662 } | 1406 } |
1663 | 1407 |
1664 class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest { | 1408 class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest { |
1665 protected: | 1409 protected: |
1666 ProtoStreamObjectWriterAnyTest() { | 1410 ProtoStreamObjectWriterAnyTest() { |
1667 std::vector<const Descriptor*> descriptors; | 1411 vector<const Descriptor*> descriptors; |
1668 descriptors.push_back(AnyOut::descriptor()); | 1412 descriptors.push_back(AnyOut::descriptor()); |
1669 descriptors.push_back(Book::descriptor()); | 1413 descriptors.push_back(google::protobuf::DoubleValue::descriptor()); |
| 1414 descriptors.push_back(google::protobuf::Timestamp::descriptor()); |
1670 descriptors.push_back(google::protobuf::Any::descriptor()); | 1415 descriptors.push_back(google::protobuf::Any::descriptor()); |
1671 descriptors.push_back(google::protobuf::DoubleValue::descriptor()); | 1416 descriptors.push_back(google::protobuf::Value::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()); | 1417 descriptors.push_back(google::protobuf::Struct::descriptor()); |
1675 descriptors.push_back(google::protobuf::Timestamp::descriptor()); | |
1676 descriptors.push_back(google::protobuf::Value::descriptor()); | |
1677 ResetTypeInfo(descriptors); | 1418 ResetTypeInfo(descriptors); |
1678 } | 1419 } |
1679 }; | 1420 }; |
1680 | 1421 |
1681 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1422 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
1682 ProtoStreamObjectWriterAnyTest, | 1423 ProtoStreamObjectWriterAnyTest, |
1683 ::testing::Values( | 1424 ::testing::Values( |
1684 testing::USE_TYPE_RESOLVER)); | 1425 testing::USE_TYPE_RESOLVER)); |
1685 | 1426 |
1686 TEST_P(ProtoStreamObjectWriterAnyTest, AnyRenderSuccess) { | 1427 TEST_P(ProtoStreamObjectWriterAnyTest, AnyRenderSuccess) { |
(...skipping 12 matching lines...) Expand all Loading... |
1699 ->EndObject(); | 1440 ->EndObject(); |
1700 CheckOutput(any); | 1441 CheckOutput(any); |
1701 } | 1442 } |
1702 | 1443 |
1703 TEST_P(ProtoStreamObjectWriterAnyTest, RecursiveAny) { | 1444 TEST_P(ProtoStreamObjectWriterAnyTest, RecursiveAny) { |
1704 AnyOut out; | 1445 AnyOut out; |
1705 ::google::protobuf::Any* any = out.mutable_any(); | 1446 ::google::protobuf::Any* any = out.mutable_any(); |
1706 any->set_type_url("type.googleapis.com/google.protobuf.Any"); | 1447 any->set_type_url("type.googleapis.com/google.protobuf.Any"); |
1707 | 1448 |
1708 ::google::protobuf::Any nested_any; | 1449 ::google::protobuf::Any nested_any; |
1709 nested_any.set_type_url("type.googleapis.com/google.protobuf.testing.AnyM"); | 1450 nested_any.set_type_url( |
| 1451 "type.googleapis.com/google.protobuf.testing.anys.AnyM"); |
1710 | 1452 |
1711 AnyM m; | 1453 AnyM m; |
1712 m.set_foo("foovalue"); | 1454 m.set_foo("foovalue"); |
1713 nested_any.set_value(m.SerializeAsString()); | 1455 nested_any.set_value(m.SerializeAsString()); |
1714 | 1456 |
1715 any->set_value(nested_any.SerializeAsString()); | 1457 any->set_value(nested_any.SerializeAsString()); |
1716 | 1458 |
1717 ow_->StartObject("") | 1459 ow_->StartObject("") |
1718 ->StartObject("any") | 1460 ->StartObject("any") |
1719 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 1461 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
1720 ->StartObject("value") | 1462 ->StartObject("value") |
1721 ->RenderString("@type", | 1463 ->RenderString("@type", |
1722 "type.googleapis.com/google.protobuf.testing.AnyM") | 1464 "type.googleapis.com/google.protobuf.testing.anys.AnyM") |
1723 ->RenderString("foo", "foovalue") | 1465 ->RenderString("foo", "foovalue") |
1724 ->EndObject() | 1466 ->EndObject() |
1725 ->EndObject() | 1467 ->EndObject() |
1726 ->EndObject(); | 1468 ->EndObject(); |
1727 CheckOutput(out, 107); | |
1728 } | 1469 } |
1729 | 1470 |
1730 TEST_P(ProtoStreamObjectWriterAnyTest, DoubleRecursiveAny) { | 1471 TEST_P(ProtoStreamObjectWriterAnyTest, DoubleRecursiveAny) { |
1731 AnyOut out; | 1472 AnyOut out; |
1732 ::google::protobuf::Any* any = out.mutable_any(); | 1473 ::google::protobuf::Any* any = out.mutable_any(); |
1733 any->set_type_url("type.googleapis.com/google.protobuf.Any"); | 1474 any->set_type_url("type.googleapis.com/google.protobuf.Any"); |
1734 | 1475 |
1735 ::google::protobuf::Any nested_any; | 1476 ::google::protobuf::Any nested_any; |
1736 nested_any.set_type_url("type.googleapis.com/google.protobuf.Any"); | 1477 nested_any.set_type_url("type.googleapis.com/google.protobuf.Any"); |
1737 | 1478 |
1738 ::google::protobuf::Any second_nested_any; | 1479 ::google::protobuf::Any second_nested_any; |
1739 second_nested_any.set_type_url( | 1480 second_nested_any.set_type_url( |
1740 "type.googleapis.com/google.protobuf.testing.AnyM"); | 1481 "type.googleapis.com/google.protobuf.testing.anys.AnyM"); |
1741 | 1482 |
1742 AnyM m; | 1483 AnyM m; |
1743 m.set_foo("foovalue"); | 1484 m.set_foo("foovalue"); |
1744 second_nested_any.set_value(m.SerializeAsString()); | 1485 second_nested_any.set_value(m.SerializeAsString()); |
1745 | 1486 |
1746 nested_any.set_value(second_nested_any.SerializeAsString()); | 1487 nested_any.set_value(second_nested_any.SerializeAsString()); |
1747 any->set_value(nested_any.SerializeAsString()); | 1488 any->set_value(nested_any.SerializeAsString()); |
1748 | 1489 |
1749 ow_->StartObject("") | 1490 ow_->StartObject("") |
1750 ->StartObject("any") | 1491 ->StartObject("any") |
1751 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 1492 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
1752 ->StartObject("value") | 1493 ->StartObject("value") |
1753 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 1494 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
1754 ->StartObject("value") | 1495 ->StartObject("value") |
1755 ->RenderString("@type", | 1496 ->RenderString("@type", |
1756 "type.googleapis.com/google.protobuf.testing.AnyM") | 1497 "type.googleapis.com/google.protobuf.testing.anys.AnyM") |
1757 ->RenderString("foo", "foovalue") | 1498 ->RenderString("foo", "foovalue") |
1758 ->EndObject() | 1499 ->EndObject() |
1759 ->EndObject() | 1500 ->EndObject() |
1760 ->EndObject() | 1501 ->EndObject() |
1761 ->EndObject(); | 1502 ->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); | |
1852 } | 1503 } |
1853 | 1504 |
1854 TEST_P(ProtoStreamObjectWriterAnyTest, EmptyAnyFromEmptyObject) { | 1505 TEST_P(ProtoStreamObjectWriterAnyTest, EmptyAnyFromEmptyObject) { |
1855 AnyOut out; | 1506 AnyOut out; |
1856 out.mutable_any(); | 1507 out.mutable_any(); |
1857 | 1508 |
1858 EXPECT_CALL(listener_, InvalidValue(_, _, _)).Times(0); | |
1859 | |
1860 ow_->StartObject("")->StartObject("any")->EndObject()->EndObject(); | 1509 ow_->StartObject("")->StartObject("any")->EndObject()->EndObject(); |
1861 | 1510 |
1862 CheckOutput(out, 2); | 1511 CheckOutput(out, 2); |
1863 } | 1512 } |
1864 | 1513 |
1865 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails1) { | 1514 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails1) { |
1866 AnyOut any; | 1515 AnyOut any; |
1867 | 1516 |
1868 EXPECT_CALL(listener_, | 1517 EXPECT_CALL( |
1869 InvalidValue(_, StringPiece("Any"), | 1518 listener_, |
1870 StringPiece("Missing @type for any field in " | 1519 InvalidValue(_, StringPiece("Any"), |
1871 "google.protobuf.testing.AnyOut"))); | 1520 StringPiece("Missing or invalid @type for any field in " |
| 1521 "google.protobuf.testing.anys.AnyOut"))); |
1872 | 1522 |
1873 ow_->StartObject("") | 1523 ow_->StartObject("") |
1874 ->StartObject("any") | 1524 ->StartObject("any") |
1875 ->StartObject("another") | 1525 ->StartObject("another") |
1876 ->EndObject() | 1526 ->EndObject() |
1877 ->EndObject() | 1527 ->EndObject() |
1878 ->EndObject(); | 1528 ->EndObject(); |
1879 CheckOutput(any); | 1529 CheckOutput(any); |
1880 } | 1530 } |
1881 | 1531 |
1882 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails2) { | 1532 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails2) { |
1883 AnyOut any; | 1533 AnyOut any; |
1884 | 1534 |
1885 EXPECT_CALL(listener_, | 1535 EXPECT_CALL( |
1886 InvalidValue(_, StringPiece("Any"), | 1536 listener_, |
1887 StringPiece("Missing @type for any field in " | 1537 InvalidValue(_, StringPiece("Any"), |
1888 "google.protobuf.testing.AnyOut"))); | 1538 StringPiece("Missing or invalid @type for any field in " |
| 1539 "google.protobuf.testing.anys.AnyOut"))); |
1889 | 1540 |
1890 ow_->StartObject("") | 1541 ow_->StartObject("") |
1891 ->StartObject("any") | 1542 ->StartObject("any") |
1892 ->StartList("another") | 1543 ->StartList("another") |
1893 ->EndObject() | 1544 ->EndObject() |
1894 ->EndObject() | 1545 ->EndObject() |
1895 ->EndObject(); | 1546 ->EndObject(); |
1896 CheckOutput(any); | 1547 CheckOutput(any); |
1897 } | 1548 } |
1898 | 1549 |
1899 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails3) { | 1550 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithoutTypeUrlFails3) { |
1900 AnyOut any; | 1551 AnyOut any; |
1901 | 1552 |
1902 EXPECT_CALL(listener_, | 1553 EXPECT_CALL( |
1903 InvalidValue(_, StringPiece("Any"), | 1554 listener_, |
1904 StringPiece("Missing @type for any field in " | 1555 InvalidValue(_, StringPiece("Any"), |
1905 "google.protobuf.testing.AnyOut"))); | 1556 StringPiece("Missing or invalid @type for any field in " |
| 1557 "google.protobuf.testing.anys.AnyOut"))); |
1906 | 1558 |
1907 ow_->StartObject("") | 1559 ow_->StartObject("") |
1908 ->StartObject("any") | 1560 ->StartObject("any") |
1909 ->RenderString("value", "somevalue") | 1561 ->RenderString("value", "somevalue") |
1910 ->EndObject() | 1562 ->EndObject() |
1911 ->EndObject(); | 1563 ->EndObject(); |
1912 CheckOutput(any); | 1564 CheckOutput(any); |
1913 } | 1565 } |
1914 | 1566 |
1915 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithInvalidTypeUrlFails) { | 1567 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithInvalidTypeUrlFails) { |
(...skipping 24 matching lines...) Expand all Loading... |
1940 StringPiece("Invalid type URL, unknown type: some.Type"))); | 1592 StringPiece("Invalid type URL, unknown type: some.Type"))); |
1941 ow_->StartObject("") | 1593 ow_->StartObject("") |
1942 ->StartObject("any") | 1594 ->StartObject("any") |
1943 ->RenderString("@type", "type.googleapis.com/some.Type") | 1595 ->RenderString("@type", "type.googleapis.com/some.Type") |
1944 ->RenderDouble("value", 40.2) | 1596 ->RenderDouble("value", 40.2) |
1945 ->EndObject() | 1597 ->EndObject() |
1946 ->EndObject(); | 1598 ->EndObject(); |
1947 CheckOutput(any); | 1599 CheckOutput(any); |
1948 } | 1600 } |
1949 | 1601 |
1950 TEST_P(ProtoStreamObjectWriterAnyTest, AnyIncorrectInputTypeFails) { | 1602 TEST_P(ProtoStreamObjectWriterAnyTest, AnyNullInputFails) { |
1951 AnyOut any; | 1603 AnyOut any; |
1952 | 1604 |
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); | |
1965 ow_->StartObject("")->RenderNull("any")->EndObject(); | 1605 ow_->StartObject("")->RenderNull("any")->EndObject(); |
1966 CheckOutput(any); | 1606 CheckOutput(any); |
1967 } | 1607 } |
1968 | 1608 |
1969 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypeErrorTest) { | 1609 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypeErrorTest) { |
1970 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("Any"), | 1610 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("Any"), |
1971 StringPiece("Invalid time format: "))); | 1611 StringPiece("Invalid time format: "))); |
1972 | 1612 |
1973 AnyOut any; | 1613 AnyOut any; |
1974 google::protobuf::Any* any_type = any.mutable_any(); | 1614 google::protobuf::Any* any_type = any.mutable_any(); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2192 | 1832 |
2193 ow_->StartObject("") | 1833 ow_->StartObject("") |
2194 ->StartObject("any") | 1834 ->StartObject("any") |
2195 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") | 1835 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any") |
2196 ->RenderString("value", "") | 1836 ->RenderString("value", "") |
2197 ->EndObject() | 1837 ->EndObject() |
2198 ->EndObject(); | 1838 ->EndObject(); |
2199 CheckOutput(any); | 1839 CheckOutput(any); |
2200 } | 1840 } |
2201 | 1841 |
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 | |
2307 class ProtoStreamObjectWriterFieldMaskTest | 1842 class ProtoStreamObjectWriterFieldMaskTest |
2308 : public BaseProtoStreamObjectWriterTest { | 1843 : public BaseProtoStreamObjectWriterTest { |
2309 protected: | 1844 protected: |
2310 ProtoStreamObjectWriterFieldMaskTest() { | 1845 ProtoStreamObjectWriterFieldMaskTest() { |
2311 std::vector<const Descriptor*> descriptors; | 1846 vector<const Descriptor*> descriptors; |
2312 descriptors.push_back(FieldMaskTest::descriptor()); | 1847 descriptors.push_back(FieldMaskTest::descriptor()); |
2313 descriptors.push_back(google::protobuf::FieldMask::descriptor()); | 1848 descriptors.push_back(google::protobuf::FieldMask::descriptor()); |
2314 ResetTypeInfo(descriptors); | 1849 ResetTypeInfo(descriptors); |
2315 } | 1850 } |
2316 }; | 1851 }; |
2317 | 1852 |
2318 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 1853 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
2319 ProtoStreamObjectWriterFieldMaskTest, | 1854 ProtoStreamObjectWriterFieldMaskTest, |
2320 ::testing::Values( | 1855 ::testing::Values( |
2321 testing::USE_TYPE_RESOLVER)); | 1856 testing::USE_TYPE_RESOLVER)); |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2542 ow_->StartObject(""); | 2077 ow_->StartObject(""); |
2543 ow_->RenderString( | 2078 ow_->RenderString( |
2544 "single_mask", | 2079 "single_mask", |
2545 "path.to.map[\"(),[],\\\"'!@#$%^&*123_|War\xE5\xAD\x99,./?><\\\\\"]," | 2080 "path.to.map[\"(),[],\\\"'!@#$%^&*123_|War\xE5\xAD\x99,./?><\\\\\"]," |
2546 "path.to.map[\"key2\"]"); | 2081 "path.to.map[\"key2\"]"); |
2547 ow_->EndObject(); | 2082 ow_->EndObject(); |
2548 | 2083 |
2549 CheckOutput(expected); | 2084 CheckOutput(expected); |
2550 } | 2085 } |
2551 | 2086 |
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 | |
2582 class ProtoStreamObjectWriterOneOfsTest | 2087 class ProtoStreamObjectWriterOneOfsTest |
2583 : public BaseProtoStreamObjectWriterTest { | 2088 : public BaseProtoStreamObjectWriterTest { |
2584 protected: | 2089 protected: |
2585 ProtoStreamObjectWriterOneOfsTest() { | 2090 ProtoStreamObjectWriterOneOfsTest() { |
2586 std::vector<const Descriptor*> descriptors; | 2091 vector<const Descriptor*> descriptors; |
2587 descriptors.push_back(OneOfsRequest::descriptor()); | 2092 descriptors.push_back(OneOfsRequest::descriptor()); |
2588 descriptors.push_back(google::protobuf::Struct::descriptor()); | 2093 descriptors.push_back(google::protobuf::Struct::descriptor()); |
2589 ResetTypeInfo(descriptors); | 2094 ResetTypeInfo(descriptors); |
2590 } | 2095 } |
2591 }; | 2096 }; |
2592 | 2097 |
2593 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, | 2098 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, |
2594 ProtoStreamObjectWriterOneOfsTest, | 2099 ProtoStreamObjectWriterOneOfsTest, |
2595 ::testing::Values( | 2100 ::testing::Values( |
2596 testing::USE_TYPE_RESOLVER)); | 2101 testing::USE_TYPE_RESOLVER)); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2739 ow_->EndObject(); | 2244 ow_->EndObject(); |
2740 } | 2245 } |
2741 | 2246 |
2742 TEST_P(ProtoStreamObjectWriterOneOfsTest, | 2247 TEST_P(ProtoStreamObjectWriterOneOfsTest, |
2743 MultipleOneofsFailForOneofWithinAnyTest) { | 2248 MultipleOneofsFailForOneofWithinAnyTest) { |
2744 EXPECT_CALL(listener_, | 2249 EXPECT_CALL(listener_, |
2745 InvalidValue(_, StringPiece("oneof"), | 2250 InvalidValue(_, StringPiece("oneof"), |
2746 StringPiece("oneof field 'data' is already set. " | 2251 StringPiece("oneof field 'data' is already set. " |
2747 "Cannot set 'intData'"))); | 2252 "Cannot set 'intData'"))); |
2748 | 2253 |
| 2254 using google::protobuf::testing::oneofs::OneOfsRequest; |
2749 // JSON: | 2255 // JSON: |
2750 // { "anyData": | 2256 // { "anyData": |
2751 // { "@type": | 2257 // { "@type": |
2752 // "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest", | 2258 // "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest", |
2753 // "strData": "blah", | 2259 // "strData": "blah", |
2754 // "intData": 123 | 2260 // "intData": 123 |
2755 // } | 2261 // } |
2756 // } | 2262 // } |
2757 ow_->StartObject(""); | 2263 ow_->StartObject(""); |
2758 ow_->StartObject("anyData"); | 2264 ow_->StartObject("anyData"); |
2759 ow_->RenderString( | 2265 ow_->RenderString( |
2760 "@type", | 2266 "@type", |
2761 "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest"); | 2267 "type.googleapis.com/google.protobuf.testing.oneofs.OneOfsRequest"); |
2762 ow_->RenderString("strData", "blah"); | 2268 ow_->RenderString("strData", "blah"); |
2763 ow_->RenderInt32("intData", 123); | 2269 ow_->RenderInt32("intData", 123); |
2764 ow_->EndObject(); | 2270 ow_->EndObject(); |
2765 ow_->EndObject(); | 2271 ow_->EndObject(); |
2766 } | 2272 } |
2767 | 2273 |
2768 } // namespace converter | 2274 } // namespace converter |
2769 } // namespace util | 2275 } // namespace util |
2770 } // namespace protobuf | 2276 } // namespace protobuf |
2771 } // namespace google | 2277 } // namespace google |
OLD | NEW |