Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: third_party/protobuf/src/google/protobuf/util/internal/protostream_objectwriter_test.cc

Issue 1983203003: Update third_party/protobuf to protobuf-v3.0.0-beta-3 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix compile error Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 using google::protobuf::testing::structs::StructType; 83 using google::protobuf::testing::structs::StructType;
84 using google::protobuf::testing::timestampduration::TimestampDuration; 84 using google::protobuf::testing::timestampduration::TimestampDuration;
85 85
86 86
87 namespace { 87 namespace {
88 string GetTypeUrl(const Descriptor* descriptor) { 88 string GetTypeUrl(const Descriptor* descriptor) {
89 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name(); 89 return string(kTypeServiceBaseUrl) + "/" + descriptor->full_name();
90 } 90 }
91 } // namespace 91 } // namespace
92 92
93 #if __cplusplus >= 201103L
94 using std::get;
95 #else
96 using std::tr1::get;
97 #endif
98
93 class BaseProtoStreamObjectWriterTest 99 class BaseProtoStreamObjectWriterTest
94 : public ::testing::TestWithParam<testing::TypeInfoSource> { 100 : public ::testing::TestWithParam<testing::TypeInfoSource> {
95 protected: 101 protected:
96 BaseProtoStreamObjectWriterTest() 102 BaseProtoStreamObjectWriterTest()
97 : helper_(GetParam()), 103 : helper_(GetParam()),
98 listener_(), 104 listener_(),
99 output_(new GrowingArrayByteSink(1000)), 105 output_(new GrowingArrayByteSink(1000)),
100 ow_() {} 106 ow_() {}
101 107
102 explicit BaseProtoStreamObjectWriterTest(const Descriptor* descriptor) 108 explicit BaseProtoStreamObjectWriterTest(const Descriptor* descriptor)
(...skipping 12 matching lines...) Expand all
115 listener_(), 121 listener_(),
116 output_(new GrowingArrayByteSink(1000)), 122 output_(new GrowingArrayByteSink(1000)),
117 ow_() { 123 ow_() {
118 ResetTypeInfo(descriptors); 124 ResetTypeInfo(descriptors);
119 } 125 }
120 126
121 void ResetTypeInfo(vector<const Descriptor*> descriptors) { 127 void ResetTypeInfo(vector<const Descriptor*> descriptors) {
122 GOOGLE_CHECK(!descriptors.empty()) << "Must have at least one descriptor!"; 128 GOOGLE_CHECK(!descriptors.empty()) << "Must have at least one descriptor!";
123 helper_.ResetTypeInfo(descriptors); 129 helper_.ResetTypeInfo(descriptors);
124 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(descriptors[0]), output_.get(), 130 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(descriptors[0]), output_.get(),
125 &listener_)); 131 &listener_, options_));
132 }
133
134 void ResetTypeInfo(const Descriptor* descriptor) {
135 vector<const Descriptor*> descriptors;
136 descriptors.push_back(descriptor);
137 ResetTypeInfo(descriptors);
126 } 138 }
127 139
128 virtual ~BaseProtoStreamObjectWriterTest() {} 140 virtual ~BaseProtoStreamObjectWriterTest() {}
129 141
130 void CheckOutput(const Message& expected, int expected_length) { 142 void CheckOutput(const Message& expected, int expected_length) {
131 size_t nbytes; 143 size_t nbytes;
132 google::protobuf::scoped_array<char> buffer(output_->GetBuffer(&nbytes)); 144 google::protobuf::scoped_array<char> buffer(output_->GetBuffer(&nbytes));
133 if (expected_length >= 0) { 145 if (expected_length >= 0) {
134 EXPECT_EQ(expected_length, nbytes); 146 EXPECT_EQ(expected_length, nbytes);
135 } 147 }
(...skipping 12 matching lines...) Expand all
148 void CheckOutput(const Message& expected) { CheckOutput(expected, -1); } 160 void CheckOutput(const Message& expected) { CheckOutput(expected, -1); }
149 161
150 const google::protobuf::Type* GetType(const Descriptor* descriptor) { 162 const google::protobuf::Type* GetType(const Descriptor* descriptor) {
151 return helper_.GetTypeInfo()->GetTypeByTypeUrl(GetTypeUrl(descriptor)); 163 return helper_.GetTypeInfo()->GetTypeByTypeUrl(GetTypeUrl(descriptor));
152 } 164 }
153 165
154 testing::TypeInfoTestHelper helper_; 166 testing::TypeInfoTestHelper helper_;
155 MockErrorListener listener_; 167 MockErrorListener listener_;
156 google::protobuf::scoped_ptr<GrowingArrayByteSink> output_; 168 google::protobuf::scoped_ptr<GrowingArrayByteSink> output_;
157 google::protobuf::scoped_ptr<ProtoStreamObjectWriter> ow_; 169 google::protobuf::scoped_ptr<ProtoStreamObjectWriter> ow_;
170 ProtoStreamObjectWriter::Options options_;
158 }; 171 };
159 172
160 MATCHER_P(HasObjectLocation, expected, 173 MATCHER_P(HasObjectLocation, expected,
161 "Verifies the expected object location") { 174 "Verifies the expected object location") {
162 string actual; 175 string actual = get<0>(arg).ToString();
163 #if __cplusplus >= 201103L
164 actual = std::get<0>(arg).ToString();
165 #else
166 actual = std::tr1::get<0>(arg).ToString();
167 #endif
168 if (actual.compare(expected) == 0) return true; 176 if (actual.compare(expected) == 0) return true;
169 *result_listener << "actual location is: " << actual; 177 *result_listener << "actual location is: " << actual;
170 return false; 178 return false;
171 } 179 }
172 180
173 class ProtoStreamObjectWriterTest : public BaseProtoStreamObjectWriterTest { 181 class ProtoStreamObjectWriterTest : public BaseProtoStreamObjectWriterTest {
174 protected: 182 protected:
175 ProtoStreamObjectWriterTest() 183 ProtoStreamObjectWriterTest()
176 : BaseProtoStreamObjectWriterTest(Book::descriptor()) {} 184 : BaseProtoStreamObjectWriterTest(Book::descriptor()) {}
177 185
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 full.add_rep_u64(80000000002L); 290 full.add_rep_u64(80000000002L);
283 full.add_rep_i64(-80000000003L); 291 full.add_rep_i64(-80000000003L);
284 full.add_rep_sf64(-80000000004L); 292 full.add_rep_sf64(-80000000004L);
285 full.add_rep_s64(-80000000005L); 293 full.add_rep_s64(-80000000005L);
286 full.add_rep_str("string2"); 294 full.add_rep_str("string2");
287 full.add_rep_bytes("More Bytes"); 295 full.add_rep_bytes("More Bytes");
288 full.add_rep_float(6.14f); 296 full.add_rep_float(6.14f);
289 full.add_rep_double(-8.05L); 297 full.add_rep_double(-8.05L);
290 full.add_rep_bool(false); 298 full.add_rep_bool(false);
291 299
292 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(Primitive::descriptor()), 300 ResetTypeInfo(Primitive::descriptor());
293 output_.get(), &listener_));
294 301
295 ow_->StartObject("") 302 ow_->StartObject("")
296 ->RenderString("fix32", "101") 303 ->RenderString("fix32", "101")
297 ->RenderString("u32", "102") 304 ->RenderString("u32", "102")
298 ->RenderString("i32", "-103") 305 ->RenderString("i32", "-103")
299 ->RenderString("sf32", "-104") 306 ->RenderString("sf32", "-104")
300 ->RenderString("s32", "-105") 307 ->RenderString("s32", "-105")
301 ->RenderString("fix64", "40000000001") 308 ->RenderString("fix64", "40000000001")
302 ->RenderString("u64", "40000000002") 309 ->RenderString("u64", "40000000002")
303 ->RenderString("i64", "-40000000003") 310 ->RenderString("i64", "-40000000003")
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
356 ->EndObject(); 363 ->EndObject();
357 CheckOutput(full); 364 CheckOutput(full);
358 } 365 }
359 366
360 TEST_P(ProtoStreamObjectWriterTest, InfinityInputTest) { 367 TEST_P(ProtoStreamObjectWriterTest, InfinityInputTest) {
361 Primitive full; 368 Primitive full;
362 full.set_double_(std::numeric_limits<double>::infinity()); 369 full.set_double_(std::numeric_limits<double>::infinity());
363 full.set_float_(std::numeric_limits<float>::infinity()); 370 full.set_float_(std::numeric_limits<float>::infinity());
364 full.set_str("-Infinity"); 371 full.set_str("-Infinity");
365 372
366 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(Primitive::descriptor()), 373 ResetTypeInfo(Primitive::descriptor());
367 output_.get(), &listener_));
368 374
369 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_INT32"), 375 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_INT32"),
370 StringPiece("\"Infinity\""))) 376 StringPiece("\"Infinity\"")))
371 .With(Args<0>(HasObjectLocation("i32"))); 377 .With(Args<0>(HasObjectLocation("i32")));
372 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"), 378 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"),
373 StringPiece("\"Infinity\""))) 379 StringPiece("\"Infinity\"")))
374 .With(Args<0>(HasObjectLocation("u32"))); 380 .With(Args<0>(HasObjectLocation("u32")));
375 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_SFIXED64"), 381 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_SFIXED64"),
376 StringPiece("\"-Infinity\""))) 382 StringPiece("\"-Infinity\"")))
377 .With(Args<0>(HasObjectLocation("sf64"))); 383 .With(Args<0>(HasObjectLocation("sf64")));
(...skipping 12 matching lines...) Expand all
390 ->EndObject(); 396 ->EndObject();
391 CheckOutput(full); 397 CheckOutput(full);
392 } 398 }
393 399
394 TEST_P(ProtoStreamObjectWriterTest, NaNInputTest) { 400 TEST_P(ProtoStreamObjectWriterTest, NaNInputTest) {
395 Primitive full; 401 Primitive full;
396 full.set_double_(std::numeric_limits<double>::quiet_NaN()); 402 full.set_double_(std::numeric_limits<double>::quiet_NaN());
397 full.set_float_(std::numeric_limits<float>::quiet_NaN()); 403 full.set_float_(std::numeric_limits<float>::quiet_NaN());
398 full.set_str("NaN"); 404 full.set_str("NaN");
399 405
400 ow_.reset(helper_.NewProtoWriter(GetTypeUrl(Primitive::descriptor()), 406 ResetTypeInfo(Primitive::descriptor());
401 output_.get(), &listener_));
402 407
403 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_INT32"), 408 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_INT32"),
404 StringPiece("\"NaN\""))) 409 StringPiece("\"NaN\"")))
405 .With(Args<0>(HasObjectLocation("i32"))); 410 .With(Args<0>(HasObjectLocation("i32")));
406 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"), 411 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_UINT32"),
407 StringPiece("\"NaN\""))) 412 StringPiece("\"NaN\"")))
408 .With(Args<0>(HasObjectLocation("u32"))); 413 .With(Args<0>(HasObjectLocation("u32")));
409 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_SFIXED64"), 414 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("TYPE_SFIXED64"),
410 StringPiece("\"NaN\""))) 415 StringPiece("\"NaN\"")))
411 .With(Args<0>(HasObjectLocation("sf64"))); 416 .With(Args<0>(HasObjectLocation("sf64")));
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 google::protobuf::Timestamp* ts = timestamp.mutable_ts(); 885 google::protobuf::Timestamp* ts = timestamp.mutable_ts();
881 ts->set_seconds(1448249855); 886 ts->set_seconds(1448249855);
882 ts->set_nanos(33155000); 887 ts->set_nanos(33155000);
883 888
884 ow_->StartObject("") 889 ow_->StartObject("")
885 ->RenderString("ts", "2015-11-23T03:37:35.033155Z") 890 ->RenderString("ts", "2015-11-23T03:37:35.033155Z")
886 ->EndObject(); 891 ->EndObject();
887 CheckOutput(timestamp); 892 CheckOutput(timestamp);
888 } 893 }
889 894
895 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
896 ParseTimestampYearNotZeroPadded) {
897 TimestampDuration timestamp;
898 google::protobuf::Timestamp* ts = timestamp.mutable_ts();
899 ts->set_seconds(-61665654145);
900 ts->set_nanos(33155000);
901
902 ow_->StartObject("")
903 ->RenderString("ts", "15-11-23T03:37:35.033155Z")
904 ->EndObject();
905 CheckOutput(timestamp);
906 }
907
908 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
909 ParseTimestampYearZeroPadded) {
910 TimestampDuration timestamp;
911 google::protobuf::Timestamp* ts = timestamp.mutable_ts();
912 ts->set_seconds(-61665654145);
913 ts->set_nanos(33155000);
914
915 ow_->StartObject("")
916 ->RenderString("ts", "0015-11-23T03:37:35.033155Z")
917 ->EndObject();
918 CheckOutput(timestamp);
919 }
920
921 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
922 ParseTimestampWithPositiveOffset) {
923 TimestampDuration timestamp;
924 google::protobuf::Timestamp* ts = timestamp.mutable_ts();
925 ts->set_seconds(1448249855);
926 ts->set_nanos(33155000);
927
928 ow_->StartObject("")
929 ->RenderString("ts", "2015-11-23T11:47:35.033155+08:10")
930 ->EndObject();
931 CheckOutput(timestamp);
932 }
933
934 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
935 ParseTimestampWithNegativeOffset) {
936 TimestampDuration timestamp;
937 google::protobuf::Timestamp* ts = timestamp.mutable_ts();
938 ts->set_seconds(1448249855);
939 ts->set_nanos(33155000);
940
941 ow_->StartObject("")
942 ->RenderString("ts", "2015-11-22T19:47:35.033155-07:50")
943 ->EndObject();
944 CheckOutput(timestamp);
945 }
946
947 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
948 TimestampWithInvalidOffset1) {
949 TimestampDuration timestamp;
950
951 EXPECT_CALL(
952 listener_,
953 InvalidValue(_,
954 StringPiece("type.googleapis.com/google.protobuf.Timestamp"),
955 StringPiece("Field 'ts', Invalid time format: "
956 "2016-03-07T15:14:23+")));
957
958 ow_->StartObject("")->RenderString("ts", "2016-03-07T15:14:23+")->EndObject();
959 CheckOutput(timestamp);
960 }
961
962 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
963 TimestampWithInvalidOffset2) {
964 TimestampDuration timestamp;
965
966 EXPECT_CALL(
967 listener_,
968 InvalidValue(_,
969 StringPiece("type.googleapis.com/google.protobuf.Timestamp"),
970 StringPiece("Field 'ts', Invalid time format: "
971 "2016-03-07T15:14:23+08-10")));
972
973 ow_->StartObject("")
974 ->RenderString("ts", "2016-03-07T15:14:23+08-10")
975 ->EndObject();
976 CheckOutput(timestamp);
977 }
978
979 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
980 TimestampWithInvalidOffset3) {
981 TimestampDuration timestamp;
982
983 EXPECT_CALL(
984 listener_,
985 InvalidValue(_,
986 StringPiece("type.googleapis.com/google.protobuf.Timestamp"),
987 StringPiece("Field 'ts', Invalid time format: "
988 "2016-03-07T15:14:23+24:10")));
989
990 ow_->StartObject("")
991 ->RenderString("ts", "2016-03-07T15:14:23+24:10")
992 ->EndObject();
993 CheckOutput(timestamp);
994 }
995
996 TEST_P(ProtoStreamObjectWriterTimestampDurationTest,
997 TimestampWithInvalidOffset4) {
998 TimestampDuration timestamp;
999
1000 EXPECT_CALL(
1001 listener_,
1002 InvalidValue(_,
1003 StringPiece("type.googleapis.com/google.protobuf.Timestamp"),
1004 StringPiece("Field 'ts', Invalid time format: "
1005 "2016-03-07T15:14:23+04:60")));
1006
1007 ow_->StartObject("")
1008 ->RenderString("ts", "2016-03-07T15:14:23+04:60")
1009 ->EndObject();
1010 CheckOutput(timestamp);
1011 }
1012
890 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError1) { 1013 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError1) {
891 TimestampDuration timestamp; 1014 TimestampDuration timestamp;
892 1015
893 EXPECT_CALL( 1016 EXPECT_CALL(
894 listener_, 1017 listener_,
895 InvalidValue(_, 1018 InvalidValue(_,
896 StringPiece("type.googleapis.com/google.protobuf.Timestamp"), 1019 StringPiece("type.googleapis.com/google.protobuf.Timestamp"),
897 StringPiece("Field 'ts', Invalid time format: "))); 1020 StringPiece("Field 'ts', Invalid time format: ")));
898 1021
899 ow_->StartObject("")->RenderString("ts", "")->EndObject(); 1022 ow_->StartObject("")->RenderString("ts", "")->EndObject();
(...skipping 30 matching lines...) Expand all
930 } 1053 }
931 1054
932 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError4) { 1055 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError4) {
933 TimestampDuration timestamp; 1056 TimestampDuration timestamp;
934 1057
935 EXPECT_CALL( 1058 EXPECT_CALL(
936 listener_, 1059 listener_,
937 InvalidValue(_, 1060 InvalidValue(_,
938 StringPiece("type.googleapis.com/google.protobuf.Timestamp"), 1061 StringPiece("type.googleapis.com/google.protobuf.Timestamp"),
939 StringPiece("Field 'ts', Invalid time format: " 1062 StringPiece("Field 'ts', Invalid time format: "
940 "-8032-10-18T00:00:00.000Z"))); 1063 "-8031-10-18T00:00:00.000Z")));
941 1064
942 ow_->StartObject("") 1065 ow_->StartObject("")
943 ->RenderString("ts", "-8032-10-18T00:00:00.000Z") 1066 ->RenderString("ts", "-8031-10-18T00:00:00.000Z")
944 ->EndObject(); 1067 ->EndObject();
945 CheckOutput(timestamp); 1068 CheckOutput(timestamp);
946 } 1069 }
947 1070
948 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError5) { 1071 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError5) {
949 TimestampDuration timestamp; 1072 TimestampDuration timestamp;
950 1073
951 EXPECT_CALL( 1074 EXPECT_CALL(
952 listener_, 1075 listener_,
953 InvalidValue(_, 1076 InvalidValue(_,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
989 StringPiece("Field 'ts', Invalid time format: " 1112 StringPiece("Field 'ts', Invalid time format: "
990 "2015-11-23T03:37:35.033abc155Z"))); 1113 "2015-11-23T03:37:35.033abc155Z")));
991 1114
992 ow_->StartObject("") 1115 ow_->StartObject("")
993 // Non-numeric characters in the Timestamp nanos is not allowed. 1116 // Non-numeric characters in the Timestamp nanos is not allowed.
994 ->RenderString("ts", "2015-11-23T03:37:35.033abc155Z") 1117 ->RenderString("ts", "2015-11-23T03:37:35.033abc155Z")
995 ->EndObject(); 1118 ->EndObject();
996 CheckOutput(timestamp); 1119 CheckOutput(timestamp);
997 } 1120 }
998 1121
1122 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, InvalidTimestampError8) {
1123 TimestampDuration timestamp;
1124
1125 EXPECT_CALL(
1126 listener_,
1127 InvalidValue(_,
1128 StringPiece("type.googleapis.com/google.protobuf.Timestamp"),
1129 StringPiece("Field 'ts', Invalid time format: "
1130 "0-12-31T23:59:59.000Z")));
1131
1132 ow_->StartObject("")
1133 ->RenderString("ts", "0-12-31T23:59:59.000Z")
1134 ->EndObject();
1135 CheckOutput(timestamp);
1136 }
1137
999 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, ParseDuration) { 1138 TEST_P(ProtoStreamObjectWriterTimestampDurationTest, ParseDuration) {
1000 TimestampDuration duration; 1139 TimestampDuration duration;
1001 google::protobuf::Duration* dur = duration.mutable_dur(); 1140 google::protobuf::Duration* dur = duration.mutable_dur();
1002 dur->set_seconds(1448216930); 1141 dur->set_seconds(1448216930);
1003 dur->set_nanos(132262000); 1142 dur->set_nanos(132262000);
1004 1143
1005 ow_->StartObject("")->RenderString("dur", "1448216930.132262s")->EndObject(); 1144 ow_->StartObject("")->RenderString("dur", "1448216930.132262s")->EndObject();
1006 CheckOutput(duration); 1145 CheckOutput(duration);
1007 } 1146 }
1008 1147
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 StringPiece( 1237 StringPiece(
1099 "Field 'dur', Invalid data type for duration, value is null"))) 1238 "Field 'dur', Invalid data type for duration, value is null")))
1100 .With(Args<0>(HasObjectLocation("dur"))); 1239 .With(Args<0>(HasObjectLocation("dur")));
1101 ow_->StartObject("")->RenderNull("dur")->EndObject(); 1240 ow_->StartObject("")->RenderNull("dur")->EndObject();
1102 CheckOutput(duration); 1241 CheckOutput(duration);
1103 } 1242 }
1104 1243
1105 class ProtoStreamObjectWriterStructTest 1244 class ProtoStreamObjectWriterStructTest
1106 : public BaseProtoStreamObjectWriterTest { 1245 : public BaseProtoStreamObjectWriterTest {
1107 protected: 1246 protected:
1108 ProtoStreamObjectWriterStructTest() { 1247 ProtoStreamObjectWriterStructTest() { ResetProtoWriter(); }
1248
1249 // Resets ProtoWriter with current set of options and other state.
1250 void ResetProtoWriter() {
1109 vector<const Descriptor*> descriptors; 1251 vector<const Descriptor*> descriptors;
1110 descriptors.push_back(StructType::descriptor()); 1252 descriptors.push_back(StructType::descriptor());
1111 descriptors.push_back(google::protobuf::Struct::descriptor()); 1253 descriptors.push_back(google::protobuf::Struct::descriptor());
1112 ResetTypeInfo(descriptors); 1254 ResetTypeInfo(descriptors);
1113 } 1255 }
1114 }; 1256 };
1115 1257
1116 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, 1258 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest,
1117 ProtoStreamObjectWriterStructTest, 1259 ProtoStreamObjectWriterStructTest,
1118 ::testing::Values( 1260 ::testing::Values(
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 ->StartObject("k1") 1336 ->StartObject("k1")
1195 ->RenderString("sub_k1", "v1") 1337 ->RenderString("sub_k1", "v1")
1196 ->EndObject() 1338 ->EndObject()
1197 ->StartObject("k1") 1339 ->StartObject("k1")
1198 ->RenderString("sub_k2", "v2") 1340 ->RenderString("sub_k2", "v2")
1199 ->EndObject() 1341 ->EndObject()
1200 ->EndObject() 1342 ->EndObject()
1201 ->EndObject(); 1343 ->EndObject();
1202 } 1344 }
1203 1345
1346 TEST_P(ProtoStreamObjectWriterStructTest, OptionStructIntAsStringsTest) {
1347 StructType struct_type;
1348 google::protobuf::Struct* s = struct_type.mutable_object();
1349 s->mutable_fields()->operator[]("k1").set_number_value(123);
1350 s->mutable_fields()->operator[]("k2").set_bool_value(true);
1351 s->mutable_fields()->operator[]("k3").set_string_value("-222222222");
1352 s->mutable_fields()->operator[]("k4").set_string_value("33333333");
1353
1354 options_.struct_integers_as_strings = true;
1355 ResetProtoWriter();
1356
1357 ow_->StartObject("")
1358 ->StartObject("object")
1359 ->RenderDouble("k1", 123)
1360 ->RenderBool("k2", true)
1361 ->RenderInt64("k3", -222222222)
1362 ->RenderUint64("k4", 33333333)
1363 ->EndObject()
1364 ->EndObject();
1365 CheckOutput(struct_type);
1366 }
1367
1204 class ProtoStreamObjectWriterMapTest : public BaseProtoStreamObjectWriterTest { 1368 class ProtoStreamObjectWriterMapTest : public BaseProtoStreamObjectWriterTest {
1205 protected: 1369 protected:
1206 ProtoStreamObjectWriterMapTest() 1370 ProtoStreamObjectWriterMapTest()
1207 : BaseProtoStreamObjectWriterTest(MapIn::descriptor()) {} 1371 : BaseProtoStreamObjectWriterTest(MapIn::descriptor()) {}
1208 }; 1372 };
1209 1373
1210 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, 1374 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest,
1211 ProtoStreamObjectWriterMapTest, 1375 ProtoStreamObjectWriterMapTest,
1212 ::testing::Values( 1376 ::testing::Values(
1213 testing::USE_TYPE_RESOLVER)); 1377 testing::USE_TYPE_RESOLVER));
(...skipping 28 matching lines...) Expand all
1242 } 1406 }
1243 1407
1244 class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest { 1408 class ProtoStreamObjectWriterAnyTest : public BaseProtoStreamObjectWriterTest {
1245 protected: 1409 protected:
1246 ProtoStreamObjectWriterAnyTest() { 1410 ProtoStreamObjectWriterAnyTest() {
1247 vector<const Descriptor*> descriptors; 1411 vector<const Descriptor*> descriptors;
1248 descriptors.push_back(AnyOut::descriptor()); 1412 descriptors.push_back(AnyOut::descriptor());
1249 descriptors.push_back(google::protobuf::DoubleValue::descriptor()); 1413 descriptors.push_back(google::protobuf::DoubleValue::descriptor());
1250 descriptors.push_back(google::protobuf::Timestamp::descriptor()); 1414 descriptors.push_back(google::protobuf::Timestamp::descriptor());
1251 descriptors.push_back(google::protobuf::Any::descriptor()); 1415 descriptors.push_back(google::protobuf::Any::descriptor());
1416 descriptors.push_back(google::protobuf::Value::descriptor());
1417 descriptors.push_back(google::protobuf::Struct::descriptor());
1252 ResetTypeInfo(descriptors); 1418 ResetTypeInfo(descriptors);
1253 } 1419 }
1254 }; 1420 };
1255 1421
1256 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest, 1422 INSTANTIATE_TEST_CASE_P(DifferentTypeInfoSourceTest,
1257 ProtoStreamObjectWriterAnyTest, 1423 ProtoStreamObjectWriterAnyTest,
1258 ::testing::Values( 1424 ::testing::Values(
1259 testing::USE_TYPE_RESOLVER)); 1425 testing::USE_TYPE_RESOLVER));
1260 1426
1261 TEST_P(ProtoStreamObjectWriterAnyTest, AnyRenderSuccess) { 1427 TEST_P(ProtoStreamObjectWriterAnyTest, AnyRenderSuccess) {
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1450 1616
1451 ow_->StartObject("") 1617 ow_->StartObject("")
1452 ->StartObject("any") 1618 ->StartObject("any")
1453 ->RenderString("@type", "type.googleapis.com/google.protobuf.Timestamp") 1619 ->RenderString("@type", "type.googleapis.com/google.protobuf.Timestamp")
1454 ->RenderString("value", "") 1620 ->RenderString("value", "")
1455 ->EndObject() 1621 ->EndObject()
1456 ->EndObject(); 1622 ->EndObject();
1457 CheckOutput(any); 1623 CheckOutput(any);
1458 } 1624 }
1459 1625
1626 // Test the following case:
1627 //
1628 // {
1629 // "any": {
1630 // "@type": "type.googleapis.com/google.protobuf.Value",
1631 // "value": "abc"
1632 // }
1633 // }
1634 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithNestedPrimitiveValue) {
1635 AnyOut out;
1636 ::google::protobuf::Any* any = out.mutable_any();
1637
1638 ::google::protobuf::Value value;
1639 value.set_string_value("abc");
1640 any->PackFrom(value);
1641
1642 ow_->StartObject("")
1643 ->StartObject("any")
1644 ->RenderString("@type", "type.googleapis.com/google.protobuf.Value")
1645 ->RenderString("value", "abc")
1646 ->EndObject()
1647 ->EndObject();
1648 CheckOutput(out);
1649 }
1650
1651 // Test the following case:
1652 //
1653 // {
1654 // "any": {
1655 // "@type": "type.googleapis.com/google.protobuf.Value",
1656 // "value": {
1657 // "foo": "abc"
1658 // }
1659 // }
1660 // }
1661 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithNestedObjectValue) {
1662 AnyOut out;
1663 ::google::protobuf::Any* any = out.mutable_any();
1664
1665 ::google::protobuf::Value value;
1666 (*value.mutable_struct_value()->mutable_fields())["foo"].set_string_value(
1667 "abc");
1668 any->PackFrom(value);
1669
1670 ow_->StartObject("")
1671 ->StartObject("any")
1672 ->RenderString("@type", "type.googleapis.com/google.protobuf.Value")
1673 ->StartObject("value")
1674 ->RenderString("foo", "abc")
1675 ->EndObject()
1676 ->EndObject()
1677 ->EndObject();
1678 CheckOutput(out);
1679 }
1680
1681 // Test the following case:
1682 //
1683 // {
1684 // "any": {
1685 // "@type": "type.googleapis.com/google.protobuf.Value",
1686 // "value": ["hello"],
1687 // }
1688 // }
1689 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWithNestedArrayValue) {
1690 AnyOut out;
1691 ::google::protobuf::Any* any = out.mutable_any();
1692
1693 ::google::protobuf::Value value;
1694 value.mutable_list_value()->add_values()->set_string_value("hello");
1695 any->PackFrom(value);
1696
1697 ow_->StartObject("")
1698 ->StartObject("any")
1699 ->RenderString("@type", "type.googleapis.com/google.protobuf.Value")
1700 ->StartList("value")
1701 ->RenderString("", "hello")
1702 ->EndList()
1703 ->EndObject()
1704 ->EndObject()
1705 ->EndObject();
1706 CheckOutput(out);
1707 }
1708
1709 // Test the following case:
1710 //
1711 // {
1712 // "any": {
1713 // "@type": "type.googleapis.com/google.protobuf.Value",
1714 // "not_value": ""
1715 // }
1716 // }
1717 TEST_P(ProtoStreamObjectWriterAnyTest,
1718 AnyWellKnownTypesNoValueFieldForPrimitive) {
1719 EXPECT_CALL(
1720 listener_,
1721 InvalidValue(
1722 _, StringPiece("Any"),
1723 StringPiece("Expect a \"value\" field for well-known types.")));
1724 AnyOut any;
1725 google::protobuf::Any* any_type = any.mutable_any();
1726 any_type->set_type_url("type.googleapis.com/google.protobuf.Value");
1727
1728 ow_->StartObject("")
1729 ->StartObject("any")
1730 ->RenderString("@type", "type.googleapis.com/google.protobuf.Value")
1731 ->RenderString("not_value", "")
1732 ->EndObject()
1733 ->EndObject();
1734 CheckOutput(any);
1735 }
1736
1737 // Test the following case:
1738 //
1739 // {
1740 // "any": {
1741 // "@type": "type.googleapis.com/google.protobuf.Value",
1742 // "not_value": {}
1743 // }
1744 // }
1745 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesNoValueFieldForObject) {
1746 EXPECT_CALL(
1747 listener_,
1748 InvalidValue(
1749 _, StringPiece("Any"),
1750 StringPiece("Expect a \"value\" field for well-known types.")));
1751 AnyOut any;
1752 google::protobuf::Any* any_type = any.mutable_any();
1753 any_type->set_type_url("type.googleapis.com/google.protobuf.Value");
1754
1755 ow_->StartObject("")
1756 ->StartObject("any")
1757 ->RenderString("@type", "type.googleapis.com/google.protobuf.Value")
1758 ->StartObject("not_value")
1759 ->EndObject()
1760 ->EndObject()
1761 ->EndObject();
1762 CheckOutput(any);
1763 }
1764
1765 // Test the following case:
1766 //
1767 // {
1768 // "any": {
1769 // "@type": "type.googleapis.com/google.protobuf.Value",
1770 // "not_value": [],
1771 // }
1772 // }
1773 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesNoValueFieldForArray) {
1774 EXPECT_CALL(
1775 listener_,
1776 InvalidValue(
1777 _, StringPiece("Any"),
1778 StringPiece("Expect a \"value\" field for well-known types.")));
1779 AnyOut any;
1780 google::protobuf::Any* any_type = any.mutable_any();
1781 any_type->set_type_url("type.googleapis.com/google.protobuf.Value");
1782
1783 ow_->StartObject("")
1784 ->StartObject("any")
1785 ->RenderString("@type", "type.googleapis.com/google.protobuf.Value")
1786 ->StartList("not_value")
1787 ->EndList()
1788 ->EndObject()
1789 ->EndObject()
1790 ->EndObject();
1791 CheckOutput(any);
1792 }
1793
1794 // Test the following case:
1795 //
1796 // {
1797 // "any": {
1798 // "@type": "type.googleapis.com/google.protobuf.Struct",
1799 // "value": "",
1800 // }
1801 // }
1802 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesExpectObjectForStruct) {
1803 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("Any"),
1804 StringPiece("Expect a JSON object.")));
1805 AnyOut any;
1806 google::protobuf::Any* any_type = any.mutable_any();
1807 any_type->set_type_url("type.googleapis.com/google.protobuf.Struct");
1808
1809 ow_->StartObject("")
1810 ->StartObject("any")
1811 ->RenderString("@type", "type.googleapis.com/google.protobuf.Struct")
1812 ->RenderString("value", "")
1813 ->EndObject()
1814 ->EndObject();
1815 CheckOutput(any);
1816 }
1817
1818 // Test the following case:
1819 //
1820 // {
1821 // "any": {
1822 // "@type": "type.googleapis.com/google.protobuf.Any",
1823 // "value": "",
1824 // }
1825 // }
1826 TEST_P(ProtoStreamObjectWriterAnyTest, AnyWellKnownTypesExpectObjectForAny) {
1827 EXPECT_CALL(listener_, InvalidValue(_, StringPiece("Any"),
1828 StringPiece("Expect a JSON object.")));
1829 AnyOut any;
1830 google::protobuf::Any* any_type = any.mutable_any();
1831 any_type->set_type_url("type.googleapis.com/google.protobuf.Any");
1832
1833 ow_->StartObject("")
1834 ->StartObject("any")
1835 ->RenderString("@type", "type.googleapis.com/google.protobuf.Any")
1836 ->RenderString("value", "")
1837 ->EndObject()
1838 ->EndObject();
1839 CheckOutput(any);
1840 }
1841
1460 class ProtoStreamObjectWriterFieldMaskTest 1842 class ProtoStreamObjectWriterFieldMaskTest
1461 : public BaseProtoStreamObjectWriterTest { 1843 : public BaseProtoStreamObjectWriterTest {
1462 protected: 1844 protected:
1463 ProtoStreamObjectWriterFieldMaskTest() { 1845 ProtoStreamObjectWriterFieldMaskTest() {
1464 vector<const Descriptor*> descriptors; 1846 vector<const Descriptor*> descriptors;
1465 descriptors.push_back(FieldMaskTest::descriptor()); 1847 descriptors.push_back(FieldMaskTest::descriptor());
1466 descriptors.push_back(google::protobuf::FieldMask::descriptor()); 1848 descriptors.push_back(google::protobuf::FieldMask::descriptor());
1467 ResetTypeInfo(descriptors); 1849 ResetTypeInfo(descriptors);
1468 } 1850 }
1469 }; 1851 };
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
1886 ow_->RenderString("strData", "blah"); 2268 ow_->RenderString("strData", "blah");
1887 ow_->RenderInt32("intData", 123); 2269 ow_->RenderInt32("intData", 123);
1888 ow_->EndObject(); 2270 ow_->EndObject();
1889 ow_->EndObject(); 2271 ow_->EndObject();
1890 } 2272 }
1891 2273
1892 } // namespace converter 2274 } // namespace converter
1893 } // namespace util 2275 } // namespace util
1894 } // namespace protobuf 2276 } // namespace protobuf
1895 } // namespace google 2277 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698