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 // http://code.google.com/p/protobuf/ | 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. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
(...skipping 25 matching lines...) Expand all Loading... |
39 // rather than unittest failures, which may be surprising. However, testing | 39 // rather than unittest failures, which may be surprising. However, testing |
40 // the output of the C++ generator directly would be very hard. We can't very | 40 // the output of the C++ generator directly would be very hard. We can't very |
41 // well just check it against golden files since those files would have to be | 41 // well just check it against golden files since those files would have to be |
42 // updated for any small change; such a test would be very brittle and probably | 42 // updated for any small change; such a test would be very brittle and probably |
43 // not very helpful. What we really want to test is that the code compiles | 43 // not very helpful. What we really want to test is that the code compiles |
44 // correctly and produces the interfaces we expect, which is why this test | 44 // correctly and produces the interfaces we expect, which is why this test |
45 // is written this way. | 45 // is written this way. |
46 | 46 |
47 #include <google/protobuf/compiler/cpp/cpp_unittest.h> | 47 #include <google/protobuf/compiler/cpp/cpp_unittest.h> |
48 | 48 |
| 49 #include <memory> |
| 50 #ifndef _SHARED_PTR_H |
| 51 #include <google/protobuf/stubs/shared_ptr.h> |
| 52 #endif |
49 #include <vector> | 53 #include <vector> |
50 | 54 |
51 #include <google/protobuf/unittest.pb.h> | 55 #include <google/protobuf/unittest.pb.h> |
52 #include <google/protobuf/unittest_optimize_for.pb.h> | 56 #include <google/protobuf/unittest_optimize_for.pb.h> |
53 #include <google/protobuf/unittest_embed_optimize_for.pb.h> | 57 #include <google/protobuf/unittest_embed_optimize_for.pb.h> |
| 58 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) |
| 59 // We exclude this large proto from cmake build because it's too large for |
| 60 // visual studio to compile (report internal errors). |
| 61 #include <google/protobuf/unittest_enormous_descriptor.pb.h> |
| 62 #endif |
54 #include <google/protobuf/unittest_no_generic_services.pb.h> | 63 #include <google/protobuf/unittest_no_generic_services.pb.h> |
55 #include <google/protobuf/test_util.h> | 64 #include <google/protobuf/test_util.h> |
| 65 #include <google/protobuf/compiler/cpp/cpp_helpers.h> |
56 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> | 66 #include <google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h> |
57 #include <google/protobuf/compiler/importer.h> | 67 #include <google/protobuf/compiler/importer.h> |
58 #include <google/protobuf/io/coded_stream.h> | 68 #include <google/protobuf/io/coded_stream.h> |
59 #include <google/protobuf/io/zero_copy_stream_impl.h> | 69 #include <google/protobuf/io/zero_copy_stream_impl.h> |
60 #include <google/protobuf/descriptor.h> | 70 #include <google/protobuf/descriptor.h> |
61 #include <google/protobuf/descriptor.pb.h> | 71 #include <google/protobuf/descriptor.pb.h> |
62 #include <google/protobuf/dynamic_message.h> | 72 #include <google/protobuf/dynamic_message.h> |
63 | 73 |
| 74 #include <google/protobuf/stubs/callback.h> |
64 #include <google/protobuf/stubs/common.h> | 75 #include <google/protobuf/stubs/common.h> |
| 76 #include <google/protobuf/stubs/logging.h> |
65 #include <google/protobuf/stubs/strutil.h> | 77 #include <google/protobuf/stubs/strutil.h> |
66 #include <google/protobuf/stubs/substitute.h> | 78 #include <google/protobuf/stubs/substitute.h> |
67 #include <google/protobuf/testing/googletest.h> | 79 #include <google/protobuf/testing/googletest.h> |
68 #include <gtest/gtest.h> | 80 #include <gtest/gtest.h> |
69 #include <google/protobuf/stubs/stl_util.h> | 81 #include <google/protobuf/stubs/stl_util.h> |
70 | 82 |
71 namespace google { | 83 namespace google { |
72 namespace protobuf { | 84 namespace protobuf { |
| 85 using internal::NewPermanentCallback; |
73 namespace compiler { | 86 namespace compiler { |
74 namespace cpp { | 87 namespace cpp { |
75 | 88 |
76 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler. | 89 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler. |
77 namespace cpp_unittest { | 90 namespace cpp_unittest { |
78 | 91 |
79 namespace protobuf_unittest = ::protobuf_unittest; | 92 namespace protobuf_unittest = ::protobuf_unittest; |
80 | 93 |
81 | 94 |
82 class MockErrorCollector : public MultiFileErrorCollector { | 95 class MockErrorCollector : public MultiFileErrorCollector { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // Test that descriptors are generated correctly by converting them to | 131 // Test that descriptors are generated correctly by converting them to |
119 // FileDescriptorProtos and comparing. | 132 // FileDescriptorProtos and comparing. |
120 FileDescriptorProto generated_decsriptor_proto, parsed_descriptor_proto; | 133 FileDescriptorProto generated_decsriptor_proto, parsed_descriptor_proto; |
121 generated_descriptor->CopyTo(&generated_decsriptor_proto); | 134 generated_descriptor->CopyTo(&generated_decsriptor_proto); |
122 parsed_descriptor->CopyTo(&parsed_descriptor_proto); | 135 parsed_descriptor->CopyTo(&parsed_descriptor_proto); |
123 | 136 |
124 EXPECT_EQ(parsed_descriptor_proto.DebugString(), | 137 EXPECT_EQ(parsed_descriptor_proto.DebugString(), |
125 generated_decsriptor_proto.DebugString()); | 138 generated_decsriptor_proto.DebugString()); |
126 } | 139 } |
127 | 140 |
| 141 #if !defined(GOOGLE_PROTOBUF_CMAKE_BUILD) && !defined(_MSC_VER) |
| 142 // Test that generated code has proper descriptors: |
| 143 // Touch a descriptor generated from an enormous message to validate special |
| 144 // handling for descriptors exceeding the C++ standard's recommended minimum |
| 145 // limit for string literal size |
| 146 TEST(GeneratedDescriptorTest, EnormousDescriptor) { |
| 147 const Descriptor* generated_descriptor = |
| 148 TestEnormousDescriptor::descriptor(); |
| 149 |
| 150 EXPECT_TRUE(generated_descriptor != NULL); |
| 151 } |
| 152 #endif |
| 153 |
128 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS | 154 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS |
129 | 155 |
130 // =================================================================== | 156 // =================================================================== |
131 | 157 |
132 TEST(GeneratedMessageTest, Defaults) { | 158 TEST(GeneratedMessageTest, Defaults) { |
133 // Check that all default values are set correctly in the initial message. | 159 // Check that all default values are set correctly in the initial message. |
134 unittest::TestAllTypes message; | 160 unittest::TestAllTypes message; |
135 | 161 |
136 TestUtil::ExpectClear(message); | 162 TestUtil::ExpectClear(message); |
137 | 163 |
138 // Messages should return pointers to default instances until first use. | 164 // Messages should return pointers to default instances until first use. |
139 // (This is not checked by ExpectClear() since it is not actually true after | 165 // (This is not checked by ExpectClear() since it is not actually true after |
140 // the fields have been set and then cleared.) | 166 // the fields have been set and then cleared.) |
141 EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(), | 167 EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(), |
142 &message.optionalgroup()); | 168 &message.optionalgroup()); |
143 EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(), | 169 EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(), |
144 &message.optional_nested_message()); | 170 &message.optional_nested_message()); |
145 EXPECT_EQ(&unittest::ForeignMessage::default_instance(), | 171 EXPECT_EQ(&unittest::ForeignMessage::default_instance(), |
146 &message.optional_foreign_message()); | 172 &message.optional_foreign_message()); |
147 EXPECT_EQ(&unittest_import::ImportMessage::default_instance(), | 173 EXPECT_EQ(&unittest_import::ImportMessage::default_instance(), |
148 &message.optional_import_message()); | 174 &message.optional_import_message()); |
149 } | 175 } |
150 | 176 |
| 177 #ifndef PROTOBUF_USE_DLLS |
| 178 TEST(GeneratedMessageTest, Int32StringConversion) { |
| 179 EXPECT_EQ("971", Int32ToString(971)); |
| 180 EXPECT_EQ("(~0x7fffffff)", Int32ToString(kint32min)); |
| 181 EXPECT_EQ("2147483647", Int32ToString(kint32max)); |
| 182 } |
| 183 |
| 184 TEST(GeneratedMessageTest, Int64StringConversion) { |
| 185 EXPECT_EQ("GOOGLE_LONGLONG(971)", Int64ToString(971)); |
| 186 EXPECT_EQ("GOOGLE_LONGLONG(-2147483648)", Int64ToString(kint32min)); |
| 187 EXPECT_EQ("GOOGLE_LONGLONG(~0x7fffffffffffffff)", Int64ToString(kint64min)); |
| 188 EXPECT_EQ("GOOGLE_LONGLONG(9223372036854775807)", Int64ToString(kint64max)); |
| 189 } |
| 190 #endif // !PROTOBUF_USE_DLLS |
| 191 |
151 TEST(GeneratedMessageTest, FloatingPointDefaults) { | 192 TEST(GeneratedMessageTest, FloatingPointDefaults) { |
152 const unittest::TestExtremeDefaultValues& extreme_default = | 193 const unittest::TestExtremeDefaultValues& extreme_default = |
153 unittest::TestExtremeDefaultValues::default_instance(); | 194 unittest::TestExtremeDefaultValues::default_instance(); |
154 | 195 |
155 EXPECT_EQ(0.0f, extreme_default.zero_float()); | 196 EXPECT_EQ(0.0f, extreme_default.zero_float()); |
156 EXPECT_EQ(1.0f, extreme_default.one_float()); | 197 EXPECT_EQ(1.0f, extreme_default.one_float()); |
157 EXPECT_EQ(1.5f, extreme_default.small_float()); | 198 EXPECT_EQ(1.5f, extreme_default.small_float()); |
158 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); | 199 EXPECT_EQ(-1.0f, extreme_default.negative_one_float()); |
159 EXPECT_EQ(-1.5f, extreme_default.negative_float()); | 200 EXPECT_EQ(-1.5f, extreme_default.negative_float()); |
160 EXPECT_EQ(2.0e8f, extreme_default.large_float()); | 201 EXPECT_EQ(2.0e8f, extreme_default.large_float()); |
(...skipping 13 matching lines...) Expand all Loading... |
174 TEST(GeneratedMessageTest, Trigraph) { | 215 TEST(GeneratedMessageTest, Trigraph) { |
175 const unittest::TestExtremeDefaultValues& extreme_default = | 216 const unittest::TestExtremeDefaultValues& extreme_default = |
176 unittest::TestExtremeDefaultValues::default_instance(); | 217 unittest::TestExtremeDefaultValues::default_instance(); |
177 | 218 |
178 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); | 219 EXPECT_EQ("? ? ?? ?? ??? ?\?/ ?\?-", extreme_default.cpp_trigraph()); |
179 } | 220 } |
180 | 221 |
181 TEST(GeneratedMessageTest, ExtremeSmallIntegerDefault) { | 222 TEST(GeneratedMessageTest, ExtremeSmallIntegerDefault) { |
182 const unittest::TestExtremeDefaultValues& extreme_default = | 223 const unittest::TestExtremeDefaultValues& extreme_default = |
183 unittest::TestExtremeDefaultValues::default_instance(); | 224 unittest::TestExtremeDefaultValues::default_instance(); |
184 EXPECT_EQ(-0x80000000, kint32min); | 225 EXPECT_EQ(~0x7fffffff, kint32min); |
185 EXPECT_EQ(GOOGLE_LONGLONG(-0x8000000000000000), kint64min); | 226 EXPECT_EQ(GOOGLE_LONGLONG(~0x7fffffffffffffff), kint64min); |
186 EXPECT_EQ(kint32min, extreme_default.really_small_int32()); | 227 EXPECT_EQ(kint32min, extreme_default.really_small_int32()); |
187 EXPECT_EQ(kint64min, extreme_default.really_small_int64()); | 228 EXPECT_EQ(kint64min, extreme_default.really_small_int64()); |
188 } | 229 } |
189 | 230 |
190 TEST(GeneratedMessageTest, Accessors) { | 231 TEST(GeneratedMessageTest, Accessors) { |
191 // Set every field to a unique value then go back and check all those | 232 // Set every field to a unique value then go back and check all those |
192 // values. | 233 // values. |
193 unittest::TestAllTypes message; | 234 unittest::TestAllTypes message; |
194 | 235 |
195 TestUtil::SetAllFields(&message); | 236 TestUtil::SetAllFields(&message); |
(...skipping 30 matching lines...) Expand all Loading... |
226 // Check that release_foo() starts out NULL, and gives us a value | 267 // Check that release_foo() starts out NULL, and gives us a value |
227 // that we can delete after it's been set. | 268 // that we can delete after it's been set. |
228 unittest::TestAllTypes message; | 269 unittest::TestAllTypes message; |
229 | 270 |
230 EXPECT_EQ(NULL, message.release_default_string()); | 271 EXPECT_EQ(NULL, message.release_default_string()); |
231 EXPECT_FALSE(message.has_default_string()); | 272 EXPECT_FALSE(message.has_default_string()); |
232 EXPECT_EQ("hello", message.default_string()); | 273 EXPECT_EQ("hello", message.default_string()); |
233 | 274 |
234 message.set_default_string("blah"); | 275 message.set_default_string("blah"); |
235 EXPECT_TRUE(message.has_default_string()); | 276 EXPECT_TRUE(message.has_default_string()); |
236 string* str = message.release_default_string(); | 277 google::protobuf::scoped_ptr<string> str(message.release_default_string()); |
237 EXPECT_FALSE(message.has_default_string()); | 278 EXPECT_FALSE(message.has_default_string()); |
238 ASSERT_TRUE(str != NULL); | 279 ASSERT_TRUE(str != NULL); |
239 EXPECT_EQ("blah", *str); | 280 EXPECT_EQ("blah", *str); |
240 delete str; | |
241 | 281 |
242 EXPECT_EQ(NULL, message.release_default_string()); | 282 EXPECT_EQ(NULL, message.release_default_string()); |
243 EXPECT_FALSE(message.has_default_string()); | 283 EXPECT_FALSE(message.has_default_string()); |
244 EXPECT_EQ("hello", message.default_string()); | 284 EXPECT_EQ("hello", message.default_string()); |
245 } | 285 } |
246 | 286 |
247 TEST(GeneratedMessageTest, ReleaseMessage) { | 287 TEST(GeneratedMessageTest, ReleaseMessage) { |
248 // Check that release_foo() starts out NULL, and gives us a value | 288 // Check that release_foo() starts out NULL, and gives us a value |
249 // that we can delete after it's been set. | 289 // that we can delete after it's been set. |
250 unittest::TestAllTypes message; | 290 unittest::TestAllTypes message; |
251 | 291 |
252 EXPECT_EQ(NULL, message.release_optional_nested_message()); | 292 EXPECT_EQ(NULL, message.release_optional_nested_message()); |
253 EXPECT_FALSE(message.has_optional_nested_message()); | 293 EXPECT_FALSE(message.has_optional_nested_message()); |
254 | 294 |
255 message.mutable_optional_nested_message()->set_bb(1); | 295 message.mutable_optional_nested_message()->set_bb(1); |
256 unittest::TestAllTypes::NestedMessage* nest = | 296 google::protobuf::scoped_ptr<unittest::TestAllTypes::NestedMessage> nest( |
257 message.release_optional_nested_message(); | 297 message.release_optional_nested_message()); |
258 EXPECT_FALSE(message.has_optional_nested_message()); | 298 EXPECT_FALSE(message.has_optional_nested_message()); |
259 ASSERT_TRUE(nest != NULL); | 299 ASSERT_TRUE(nest != NULL); |
260 EXPECT_EQ(1, nest->bb()); | 300 EXPECT_EQ(1, nest->bb()); |
261 delete nest; | |
262 | 301 |
263 EXPECT_EQ(NULL, message.release_optional_nested_message()); | 302 EXPECT_EQ(NULL, message.release_optional_nested_message()); |
264 EXPECT_FALSE(message.has_optional_nested_message()); | 303 EXPECT_FALSE(message.has_optional_nested_message()); |
265 } | 304 } |
266 | 305 |
267 TEST(GeneratedMessageTest, SetAllocatedString) { | 306 TEST(GeneratedMessageTest, SetAllocatedString) { |
268 // Check that set_allocated_foo() works for strings. | 307 // Check that set_allocated_foo() works for strings. |
269 unittest::TestAllTypes message; | 308 unittest::TestAllTypes message; |
270 | 309 |
271 EXPECT_FALSE(message.has_optional_string()); | 310 EXPECT_FALSE(message.has_optional_string()); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 // Verify that we can use a char*,length to add to a repeated string field. | 413 // Verify that we can use a char*,length to add to a repeated string field. |
375 message.add_repeated_string("abcdef", 3); | 414 message.add_repeated_string("abcdef", 3); |
376 EXPECT_EQ(1, message.repeated_string_size()); | 415 EXPECT_EQ(1, message.repeated_string_size()); |
377 EXPECT_EQ("abc", message.repeated_string(0)); | 416 EXPECT_EQ("abc", message.repeated_string(0)); |
378 | 417 |
379 // Verify that we can use a char*,length to set a repeated string field. | 418 // Verify that we can use a char*,length to set a repeated string field. |
380 message.set_repeated_string(0, "wxyz", 2); | 419 message.set_repeated_string(0, "wxyz", 2); |
381 EXPECT_EQ("wx", message.repeated_string(0)); | 420 EXPECT_EQ("wx", message.repeated_string(0)); |
382 } | 421 } |
383 | 422 |
| 423 |
384 TEST(GeneratedMessageTest, CopyFrom) { | 424 TEST(GeneratedMessageTest, CopyFrom) { |
385 unittest::TestAllTypes message1, message2; | 425 unittest::TestAllTypes message1, message2; |
386 | 426 |
387 TestUtil::SetAllFields(&message1); | 427 TestUtil::SetAllFields(&message1); |
388 message2.CopyFrom(message1); | 428 message2.CopyFrom(message1); |
389 TestUtil::ExpectAllFieldsSet(message2); | 429 TestUtil::ExpectAllFieldsSet(message2); |
390 | 430 |
391 // Copying from self should be a no-op. | 431 // Copying from self should be a no-op. |
392 message2.CopyFrom(message2); | 432 message2.CopyFrom(message2); |
393 TestUtil::ExpectAllFieldsSet(message2); | 433 TestUtil::ExpectAllFieldsSet(message2); |
394 } | 434 } |
395 | 435 |
| 436 |
396 TEST(GeneratedMessageTest, SwapWithEmpty) { | 437 TEST(GeneratedMessageTest, SwapWithEmpty) { |
397 unittest::TestAllTypes message1, message2; | 438 unittest::TestAllTypes message1, message2; |
398 TestUtil::SetAllFields(&message1); | 439 TestUtil::SetAllFields(&message1); |
399 | 440 |
400 TestUtil::ExpectAllFieldsSet(message1); | 441 TestUtil::ExpectAllFieldsSet(message1); |
401 TestUtil::ExpectClear(message2); | 442 TestUtil::ExpectClear(message2); |
402 message1.Swap(&message2); | 443 message1.Swap(&message2); |
403 TestUtil::ExpectAllFieldsSet(message2); | 444 TestUtil::ExpectAllFieldsSet(message2); |
404 TestUtil::ExpectClear(message1); | 445 TestUtil::ExpectClear(message1); |
405 } | 446 } |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 | 550 |
510 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS | 551 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS |
511 | 552 |
512 TEST(GeneratedMessageTest, DynamicMessageCopyFrom) { | 553 TEST(GeneratedMessageTest, DynamicMessageCopyFrom) { |
513 // Test copying from a DynamicMessage, which must fall back to using | 554 // Test copying from a DynamicMessage, which must fall back to using |
514 // reflection. | 555 // reflection. |
515 unittest::TestAllTypes message2; | 556 unittest::TestAllTypes message2; |
516 | 557 |
517 // Construct a new version of the dynamic message via the factory. | 558 // Construct a new version of the dynamic message via the factory. |
518 DynamicMessageFactory factory; | 559 DynamicMessageFactory factory; |
519 scoped_ptr<Message> message1; | 560 google::protobuf::scoped_ptr<Message> message1; |
520 message1.reset(factory.GetPrototype( | 561 message1.reset(factory.GetPrototype( |
521 unittest::TestAllTypes::descriptor())->New()); | 562 unittest::TestAllTypes::descriptor())->New()); |
522 | 563 |
523 TestUtil::ReflectionTester reflection_tester( | 564 TestUtil::ReflectionTester reflection_tester( |
524 unittest::TestAllTypes::descriptor()); | 565 unittest::TestAllTypes::descriptor()); |
525 reflection_tester.SetAllFieldsViaReflection(message1.get()); | 566 reflection_tester.SetAllFieldsViaReflection(message1.get()); |
526 | 567 |
527 message2.CopyFrom(*message1); | 568 message2.CopyFrom(*message1); |
528 | 569 |
529 TestUtil::ExpectAllFieldsSet(message2); | 570 TestUtil::ExpectAllFieldsSet(message2); |
(...skipping 26 matching lines...) Expand all Loading... |
556 | 597 |
557 TestUtil::ExpectAllFieldsSet(message1); | 598 TestUtil::ExpectAllFieldsSet(message1); |
558 } | 599 } |
559 | 600 |
560 #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \ | 601 #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \ |
561 !defined(GOOGLE_PROTOBUF_NO_RTTI) | 602 !defined(GOOGLE_PROTOBUF_NO_RTTI) |
562 #ifdef PROTOBUF_HAS_DEATH_TEST | 603 #ifdef PROTOBUF_HAS_DEATH_TEST |
563 | 604 |
564 TEST(GeneratedMessageTest, MergeFromSelf) { | 605 TEST(GeneratedMessageTest, MergeFromSelf) { |
565 unittest::TestAllTypes message; | 606 unittest::TestAllTypes message; |
566 EXPECT_DEATH(message.MergeFrom(message), "&from"); | 607 EXPECT_DEATH(message.MergeFrom(message), "Check failed:.*pb[.]cc"); |
567 EXPECT_DEATH(message.MergeFrom(implicit_cast<const Message&>(message)), | 608 EXPECT_DEATH(message.MergeFrom(implicit_cast<const Message&>(message)), |
568 "&from"); | 609 "Check failed:.*pb[.]cc"); |
569 } | 610 } |
570 | 611 |
571 #endif // PROTOBUF_HAS_DEATH_TEST | 612 #endif // PROTOBUF_HAS_DEATH_TEST |
572 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS || !GOOGLE_PROTOBUF_NO_RTTI | 613 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS || !GOOGLE_PROTOBUF_NO_RTTI |
573 | 614 |
574 // Test the generated SerializeWithCachedSizesToArray(), | 615 // Test the generated SerializeWithCachedSizesToArray(), |
575 TEST(GeneratedMessageTest, SerializationToArray) { | 616 TEST(GeneratedMessageTest, SerializationToArray) { |
576 unittest::TestAllTypes message1, message2; | 617 unittest::TestAllTypes message1, message2; |
577 string data; | 618 string data; |
578 TestUtil::SetAllFields(&message1); | 619 TestUtil::SetAllFields(&message1); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
756 // following is just a token usage to insure that the code is, in fact, | 797 // following is just a token usage to insure that the code is, in fact, |
757 // being compiled and linked. | 798 // being compiled and linked. |
758 | 799 |
759 protobuf_unittest::TestConflictingSymbolNames message; | 800 protobuf_unittest::TestConflictingSymbolNames message; |
760 message.set_uint32(1); | 801 message.set_uint32(1); |
761 EXPECT_EQ(3, message.ByteSize()); | 802 EXPECT_EQ(3, message.ByteSize()); |
762 | 803 |
763 message.set_friend_(5); | 804 message.set_friend_(5); |
764 EXPECT_EQ(5, message.friend_()); | 805 EXPECT_EQ(5, message.friend_()); |
765 | 806 |
| 807 message.set_class_(6); |
| 808 EXPECT_EQ(6, message.class_()); |
| 809 |
766 // Instantiate extension template functions to test conflicting template | 810 // Instantiate extension template functions to test conflicting template |
767 // parameter names. | 811 // parameter names. |
768 typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessag
e; | 812 typedef protobuf_unittest::TestConflictingSymbolNamesExtension ExtensionMessag
e; |
769 message.AddExtension(ExtensionMessage::repeated_int32_ext, 123); | 813 message.AddExtension(ExtensionMessage::repeated_int32_ext, 123); |
770 EXPECT_EQ(123, | 814 EXPECT_EQ(123, |
771 message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); | 815 message.GetExtension(ExtensionMessage::repeated_int32_ext, 0)); |
772 } | 816 } |
773 | 817 |
| 818 TEST(GeneratedMessageTest, TestConflictingEnumNames) { |
| 819 protobuf_unittest::TestConflictingEnumNames message; |
| 820 message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_Neste
dConflictingEnum_and_); |
| 821 EXPECT_EQ(1, message.conflicting_enum()); |
| 822 message.set_conflicting_enum(protobuf_unittest::TestConflictingEnumNames_Neste
dConflictingEnum_XOR); |
| 823 EXPECT_EQ(5, message.conflicting_enum()); |
| 824 |
| 825 |
| 826 protobuf_unittest::ConflictingEnum conflicting_enum; |
| 827 conflicting_enum = protobuf_unittest::NOT_EQ; |
| 828 EXPECT_EQ(1, conflicting_enum); |
| 829 conflicting_enum = protobuf_unittest::return_; |
| 830 EXPECT_EQ(3, conflicting_enum); |
| 831 } |
| 832 |
774 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS | 833 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS |
775 | 834 |
776 TEST(GeneratedMessageTest, TestOptimizedForSize) { | 835 TEST(GeneratedMessageTest, TestOptimizedForSize) { |
777 // We rely on the tests in reflection_ops_unittest and wire_format_unittest | 836 // We rely on the tests in reflection_ops_unittest and wire_format_unittest |
778 // to really test that reflection-based methods work. Here we are mostly | 837 // to really test that reflection-based methods work. Here we are mostly |
779 // just making sure that TestOptimizedForSize actually builds and seems to | 838 // just making sure that TestOptimizedForSize actually builds and seems to |
780 // function. | 839 // function. |
781 | 840 |
782 protobuf_unittest::TestOptimizedForSize message, message2; | 841 protobuf_unittest::TestOptimizedForSize message, message2; |
783 message.set_i(1); | 842 message.set_i(1); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
833 // nested message type. NestedMessage is simple enough (1 int field) that it | 892 // nested message type. NestedMessage is simple enough (1 int field) that it |
834 // is equal to sizeof(NestedMessage) | 893 // is equal to sizeof(NestedMessage) |
835 message1.mutable_optional_nested_message(); | 894 message1.mutable_optional_nested_message(); |
836 ASSERT_EQ(sizeof(unittest::TestAllTypes::NestedMessage), | 895 ASSERT_EQ(sizeof(unittest::TestAllTypes::NestedMessage), |
837 message1.optional_nested_message().SpaceUsed()); | 896 message1.optional_nested_message().SpaceUsed()); |
838 EXPECT_EQ(previous_size + | 897 EXPECT_EQ(previous_size + |
839 sizeof(unittest::TestAllTypes::NestedMessage), | 898 sizeof(unittest::TestAllTypes::NestedMessage), |
840 message1.SpaceUsed()); | 899 message1.SpaceUsed()); |
841 } | 900 } |
842 | 901 |
| 902 TEST(GeneratedMessageTest, TestOneofSpaceUsed) { |
| 903 unittest::TestOneof2 message1; |
| 904 EXPECT_LE(sizeof(unittest::TestOneof2), message1.SpaceUsed()); |
| 905 |
| 906 const int empty_message_size = message1.SpaceUsed(); |
| 907 // Setting primitive types shouldn't affect the space used. |
| 908 message1.set_foo_int(123); |
| 909 message1.set_bar_int(12345); |
| 910 EXPECT_EQ(empty_message_size, message1.SpaceUsed()); |
| 911 |
| 912 // Setting a string in oneof to a small value should only increase SpaceUsed() |
| 913 // by the size of a string object. |
| 914 message1.set_foo_string("abc"); |
| 915 EXPECT_LE(empty_message_size + sizeof(string), message1.SpaceUsed()); |
| 916 |
| 917 // Setting a string in oneof to a value larger than the string object itself |
| 918 // should increase SpaceUsed(), because it cannot store the value internally. |
| 919 message1.set_foo_string(string(sizeof(string) + 1, 'x')); |
| 920 int min_expected_increase = message1.foo_string().capacity() + |
| 921 sizeof(string); |
| 922 EXPECT_LE(empty_message_size + min_expected_increase, |
| 923 message1.SpaceUsed()); |
| 924 |
| 925 // Setting a message in oneof should delete the other fields and increase the |
| 926 // size by the size of the nested message type. NestedMessage is simple enough |
| 927 // that it is equal to sizeof(NestedMessage) |
| 928 message1.mutable_foo_message(); |
| 929 ASSERT_EQ(sizeof(unittest::TestOneof2::NestedMessage), |
| 930 message1.foo_message().SpaceUsed()); |
| 931 EXPECT_EQ(empty_message_size + |
| 932 sizeof(unittest::TestOneof2::NestedMessage), |
| 933 message1.SpaceUsed()); |
| 934 } |
| 935 |
843 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS | 936 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS |
844 | 937 |
845 | 938 |
846 TEST(GeneratedMessageTest, FieldConstantValues) { | 939 TEST(GeneratedMessageTest, FieldConstantValues) { |
847 unittest::TestRequired message; | 940 unittest::TestRequired message; |
848 EXPECT_EQ(unittest::TestAllTypes_NestedMessage::kBbFieldNumber, 1); | 941 EXPECT_EQ(unittest::TestAllTypes_NestedMessage::kBbFieldNumber, 1); |
849 EXPECT_EQ(unittest::TestAllTypes::kOptionalInt32FieldNumber, 1); | 942 EXPECT_EQ(unittest::TestAllTypes::kOptionalInt32FieldNumber, 1); |
850 EXPECT_EQ(unittest::TestAllTypes::kOptionalgroupFieldNumber, 16); | 943 EXPECT_EQ(unittest::TestAllTypes::kOptionalgroupFieldNumber, 16); |
851 EXPECT_EQ(unittest::TestAllTypes::kOptionalNestedMessageFieldNumber, 18); | 944 EXPECT_EQ(unittest::TestAllTypes::kOptionalNestedMessageFieldNumber, 18); |
852 EXPECT_EQ(unittest::TestAllTypes::kOptionalNestedEnumFieldNumber, 21); | 945 EXPECT_EQ(unittest::TestAllTypes::kOptionalNestedEnumFieldNumber, 21); |
853 EXPECT_EQ(unittest::TestAllTypes::kRepeatedInt32FieldNumber, 31); | 946 EXPECT_EQ(unittest::TestAllTypes::kRepeatedInt32FieldNumber, 31); |
854 EXPECT_EQ(unittest::TestAllTypes::kRepeatedgroupFieldNumber, 46); | 947 EXPECT_EQ(unittest::TestAllTypes::kRepeatedgroupFieldNumber, 46); |
855 EXPECT_EQ(unittest::TestAllTypes::kRepeatedNestedMessageFieldNumber, 48); | 948 EXPECT_EQ(unittest::TestAllTypes::kRepeatedNestedMessageFieldNumber, 48); |
856 EXPECT_EQ(unittest::TestAllTypes::kRepeatedNestedEnumFieldNumber, 51); | 949 EXPECT_EQ(unittest::TestAllTypes::kRepeatedNestedEnumFieldNumber, 51); |
857 } | 950 } |
858 | 951 |
859 TEST(GeneratedMessageTest, ExtensionConstantValues) { | 952 TEST(GeneratedMessageTest, ExtensionConstantValues) { |
860 EXPECT_EQ(unittest::TestRequired::kSingleFieldNumber, 1000); | 953 EXPECT_EQ(unittest::TestRequired::kSingleFieldNumber, 1000); |
861 EXPECT_EQ(unittest::TestRequired::kMultiFieldNumber, 1001); | 954 EXPECT_EQ(unittest::TestRequired::kMultiFieldNumber, 1001); |
862 EXPECT_EQ(unittest::kOptionalInt32ExtensionFieldNumber, 1); | 955 EXPECT_EQ(unittest::kOptionalInt32ExtensionFieldNumber, 1); |
863 EXPECT_EQ(unittest::kOptionalgroupExtensionFieldNumber, 16); | 956 EXPECT_EQ(unittest::kOptionalgroupExtensionFieldNumber, 16); |
864 EXPECT_EQ(unittest::kOptionalNestedMessageExtensionFieldNumber, 18); | 957 EXPECT_EQ(unittest::kOptionalNestedMessageExtensionFieldNumber, 18); |
865 EXPECT_EQ(unittest::kOptionalNestedEnumExtensionFieldNumber, 21); | 958 EXPECT_EQ(unittest::kOptionalNestedEnumExtensionFieldNumber, 21); |
866 EXPECT_EQ(unittest::kRepeatedInt32ExtensionFieldNumber, 31); | 959 EXPECT_EQ(unittest::kRepeatedInt32ExtensionFieldNumber, 31); |
867 EXPECT_EQ(unittest::kRepeatedgroupExtensionFieldNumber, 46); | 960 EXPECT_EQ(unittest::kRepeatedgroupExtensionFieldNumber, 46); |
868 EXPECT_EQ(unittest::kRepeatedNestedMessageExtensionFieldNumber, 48); | 961 EXPECT_EQ(unittest::kRepeatedNestedMessageExtensionFieldNumber, 48); |
869 EXPECT_EQ(unittest::kRepeatedNestedEnumExtensionFieldNumber, 51); | 962 EXPECT_EQ(unittest::kRepeatedNestedEnumExtensionFieldNumber, 51); |
870 } | 963 } |
871 | 964 |
| 965 TEST(GeneratedMessageTest, ParseFromTruncated) { |
| 966 const string long_string = string(128, 'q'); |
| 967 FileDescriptorProto p; |
| 968 p.add_extension()->set_name(long_string); |
| 969 const string msg = p.SerializeAsString(); |
| 970 int successful_count = 0; |
| 971 for (int i = 0; i <= msg.size(); i++) { |
| 972 if (p.ParseFromArray(msg.c_str(), i)) { |
| 973 ++successful_count; |
| 974 } |
| 975 } |
| 976 // We don't really care about how often we succeeded. |
| 977 // As long as we didn't crash, we're happy. |
| 978 EXPECT_GE(successful_count, 1); |
| 979 } |
| 980 |
872 // =================================================================== | 981 // =================================================================== |
873 | 982 |
874 TEST(GeneratedEnumTest, EnumValuesAsSwitchCases) { | 983 TEST(GeneratedEnumTest, EnumValuesAsSwitchCases) { |
875 // Test that our nested enum values can be used as switch cases. This test | 984 // Test that our nested enum values can be used as switch cases. This test |
876 // doesn't actually do anything, the proof that it works is that it | 985 // doesn't actually do anything, the proof that it works is that it |
877 // compiles. | 986 // compiles. |
878 int i =0; | 987 int i =0; |
879 unittest::TestAllTypes::NestedEnum a = unittest::TestAllTypes::BAR; | 988 unittest::TestAllTypes::NestedEnum a = unittest::TestAllTypes::BAR; |
880 switch (a) { | 989 switch (a) { |
881 case unittest::TestAllTypes::FOO: | 990 case unittest::TestAllTypes::FOO: |
882 i = 1; | 991 i = 1; |
883 break; | 992 break; |
884 case unittest::TestAllTypes::BAR: | 993 case unittest::TestAllTypes::BAR: |
885 i = 2; | 994 i = 2; |
886 break; | 995 break; |
887 case unittest::TestAllTypes::BAZ: | 996 case unittest::TestAllTypes::BAZ: |
888 i = 3; | 997 i = 3; |
889 break; | 998 break; |
| 999 case unittest::TestAllTypes::NEG: |
| 1000 i = -1; |
| 1001 break; |
890 // no default case: We want to make sure the compiler recognizes that | 1002 // no default case: We want to make sure the compiler recognizes that |
891 // all cases are covered. (GCC warns if you do not cover all cases of | 1003 // all cases are covered. (GCC warns if you do not cover all cases of |
892 // an enum in a switch.) | 1004 // an enum in a switch.) |
893 } | 1005 } |
894 | 1006 |
895 // Token check just for fun. | 1007 // Token check just for fun. |
896 EXPECT_EQ(2, i); | 1008 EXPECT_EQ(2, i); |
897 } | 1009 } |
898 | 1010 |
899 TEST(GeneratedEnumTest, IsValidValue) { | 1011 TEST(GeneratedEnumTest, IsValidValue) { |
900 // Test enum IsValidValue. | 1012 // Test enum IsValidValue. |
901 EXPECT_TRUE(unittest::TestAllTypes::NestedEnum_IsValid(1)); | 1013 EXPECT_TRUE(unittest::TestAllTypes::NestedEnum_IsValid(1)); |
902 EXPECT_TRUE(unittest::TestAllTypes::NestedEnum_IsValid(2)); | 1014 EXPECT_TRUE(unittest::TestAllTypes::NestedEnum_IsValid(2)); |
903 EXPECT_TRUE(unittest::TestAllTypes::NestedEnum_IsValid(3)); | 1015 EXPECT_TRUE(unittest::TestAllTypes::NestedEnum_IsValid(3)); |
904 | 1016 |
905 EXPECT_FALSE(unittest::TestAllTypes::NestedEnum_IsValid(0)); | 1017 EXPECT_FALSE(unittest::TestAllTypes::NestedEnum_IsValid(0)); |
906 EXPECT_FALSE(unittest::TestAllTypes::NestedEnum_IsValid(4)); | 1018 EXPECT_FALSE(unittest::TestAllTypes::NestedEnum_IsValid(4)); |
907 | 1019 |
908 // Make sure it also works when there are dups. | 1020 // Make sure it also works when there are dups. |
909 EXPECT_TRUE(unittest::TestEnumWithDupValue_IsValid(1)); | 1021 EXPECT_TRUE(unittest::TestEnumWithDupValue_IsValid(1)); |
910 EXPECT_TRUE(unittest::TestEnumWithDupValue_IsValid(2)); | 1022 EXPECT_TRUE(unittest::TestEnumWithDupValue_IsValid(2)); |
911 EXPECT_TRUE(unittest::TestEnumWithDupValue_IsValid(3)); | 1023 EXPECT_TRUE(unittest::TestEnumWithDupValue_IsValid(3)); |
912 | 1024 |
913 EXPECT_FALSE(unittest::TestEnumWithDupValue_IsValid(0)); | 1025 EXPECT_FALSE(unittest::TestEnumWithDupValue_IsValid(0)); |
914 EXPECT_FALSE(unittest::TestEnumWithDupValue_IsValid(4)); | 1026 EXPECT_FALSE(unittest::TestEnumWithDupValue_IsValid(4)); |
915 } | 1027 } |
916 | 1028 |
917 TEST(GeneratedEnumTest, MinAndMax) { | 1029 TEST(GeneratedEnumTest, MinAndMax) { |
918 EXPECT_EQ(unittest::TestAllTypes::FOO, | 1030 EXPECT_EQ(unittest::TestAllTypes::NEG, |
919 unittest::TestAllTypes::NestedEnum_MIN); | 1031 unittest::TestAllTypes::NestedEnum_MIN); |
920 EXPECT_EQ(unittest::TestAllTypes::BAZ, | 1032 EXPECT_EQ(unittest::TestAllTypes::BAZ, |
921 unittest::TestAllTypes::NestedEnum_MAX); | 1033 unittest::TestAllTypes::NestedEnum_MAX); |
922 EXPECT_EQ(4, unittest::TestAllTypes::NestedEnum_ARRAYSIZE); | 1034 EXPECT_EQ(4, unittest::TestAllTypes::NestedEnum_ARRAYSIZE); |
923 | 1035 |
924 EXPECT_EQ(unittest::FOREIGN_FOO, unittest::ForeignEnum_MIN); | 1036 EXPECT_EQ(unittest::FOREIGN_FOO, unittest::ForeignEnum_MIN); |
925 EXPECT_EQ(unittest::FOREIGN_BAZ, unittest::ForeignEnum_MAX); | 1037 EXPECT_EQ(unittest::FOREIGN_BAZ, unittest::ForeignEnum_MAX); |
926 EXPECT_EQ(7, unittest::ForeignEnum_ARRAYSIZE); | 1038 EXPECT_EQ(7, unittest::ForeignEnum_ARRAYSIZE); |
927 | 1039 |
928 EXPECT_EQ(1, unittest::TestEnumWithDupValue_MIN); | 1040 EXPECT_EQ(1, unittest::TestEnumWithDupValue_MIN); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
982 EXPECT_EQ(unittest::TestAllTypes::NestedEnum_descriptor(), | 1094 EXPECT_EQ(unittest::TestAllTypes::NestedEnum_descriptor(), |
983 GetEnumDescriptor<unittest::TestAllTypes::NestedEnum>()); | 1095 GetEnumDescriptor<unittest::TestAllTypes::NestedEnum>()); |
984 EXPECT_EQ(unittest::ForeignEnum_descriptor(), | 1096 EXPECT_EQ(unittest::ForeignEnum_descriptor(), |
985 GetEnumDescriptor<unittest::ForeignEnum>()); | 1097 GetEnumDescriptor<unittest::ForeignEnum>()); |
986 EXPECT_EQ(unittest::TestEnumWithDupValue_descriptor(), | 1098 EXPECT_EQ(unittest::TestEnumWithDupValue_descriptor(), |
987 GetEnumDescriptor<unittest::TestEnumWithDupValue>()); | 1099 GetEnumDescriptor<unittest::TestEnumWithDupValue>()); |
988 EXPECT_EQ(unittest::TestSparseEnum_descriptor(), | 1100 EXPECT_EQ(unittest::TestSparseEnum_descriptor(), |
989 GetEnumDescriptor<unittest::TestSparseEnum>()); | 1101 GetEnumDescriptor<unittest::TestSparseEnum>()); |
990 } | 1102 } |
991 | 1103 |
| 1104 enum NonProtoEnum { |
| 1105 kFoo = 1, |
| 1106 }; |
| 1107 |
| 1108 TEST(GeneratedEnumTest, IsProtoEnumTypeTrait) { |
| 1109 EXPECT_TRUE(is_proto_enum<unittest::TestAllTypes::NestedEnum>::value); |
| 1110 EXPECT_TRUE(is_proto_enum<unittest::ForeignEnum>::value); |
| 1111 EXPECT_TRUE(is_proto_enum<unittest::TestEnumWithDupValue>::value); |
| 1112 EXPECT_TRUE(is_proto_enum<unittest::TestSparseEnum>::value); |
| 1113 |
| 1114 EXPECT_FALSE(is_proto_enum<int>::value); |
| 1115 EXPECT_FALSE(is_proto_enum<NonProtoEnum>::value); |
| 1116 } |
| 1117 |
992 #endif // PROTOBUF_TEST_NO_DESCRIPTORS | 1118 #endif // PROTOBUF_TEST_NO_DESCRIPTORS |
993 | 1119 |
994 // =================================================================== | 1120 // =================================================================== |
995 | 1121 |
996 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS | 1122 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS |
997 | 1123 |
998 // Support code for testing services. | 1124 // Support code for testing services. |
999 class GeneratedServiceTest : public testing::Test { | 1125 class GeneratedServiceTest : public testing::Test { |
1000 protected: | 1126 protected: |
1001 class MockTestService : public unittest::TestService { | 1127 class MockTestService : public unittest::TestService { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1142 MockController mock_controller_; | 1268 MockController mock_controller_; |
1143 | 1269 |
1144 MockRpcChannel mock_channel_; | 1270 MockRpcChannel mock_channel_; |
1145 unittest::TestService::Stub stub_; | 1271 unittest::TestService::Stub stub_; |
1146 | 1272 |
1147 // Just so we don't have to re-define these with every test. | 1273 // Just so we don't have to re-define these with every test. |
1148 unittest::FooRequest foo_request_; | 1274 unittest::FooRequest foo_request_; |
1149 unittest::FooResponse foo_response_; | 1275 unittest::FooResponse foo_response_; |
1150 unittest::BarRequest bar_request_; | 1276 unittest::BarRequest bar_request_; |
1151 unittest::BarResponse bar_response_; | 1277 unittest::BarResponse bar_response_; |
1152 scoped_ptr<Closure> done_; | 1278 google::protobuf::scoped_ptr<Closure> done_; |
1153 }; | 1279 }; |
1154 | 1280 |
1155 TEST_F(GeneratedServiceTest, GetDescriptor) { | 1281 TEST_F(GeneratedServiceTest, GetDescriptor) { |
1156 // Test that GetDescriptor() works. | 1282 // Test that GetDescriptor() works. |
1157 | 1283 |
1158 EXPECT_EQ(descriptor_, mock_service_.GetDescriptor()); | 1284 EXPECT_EQ(descriptor_, mock_service_.GetDescriptor()); |
1159 } | 1285 } |
1160 | 1286 |
1161 TEST_F(GeneratedServiceTest, GetChannel) { | 1287 TEST_F(GeneratedServiceTest, GetChannel) { |
1162 EXPECT_EQ(&mock_channel_, stub_.channel()); | 1288 EXPECT_EQ(&mock_channel_, stub_.channel()); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1281 | 1407 |
1282 ExpectUnimplementedController controller; | 1408 ExpectUnimplementedController controller; |
1283 | 1409 |
1284 // Call Foo. | 1410 // Call Foo. |
1285 unimplemented_service.Foo(&controller, &foo_request_, &foo_response_, | 1411 unimplemented_service.Foo(&controller, &foo_request_, &foo_response_, |
1286 done_.get()); | 1412 done_.get()); |
1287 | 1413 |
1288 EXPECT_TRUE(controller.called_); | 1414 EXPECT_TRUE(controller.called_); |
1289 } | 1415 } |
1290 | 1416 |
| 1417 // =================================================================== |
| 1418 |
| 1419 class OneofTest : public testing::Test { |
| 1420 protected: |
| 1421 virtual void SetUp() { |
| 1422 } |
| 1423 |
| 1424 void ExpectEnumCasesWork(const unittest::TestOneof2 &message) { |
| 1425 switch (message.foo_case()) { |
| 1426 case unittest::TestOneof2::kFooInt: |
| 1427 EXPECT_TRUE(message.has_foo_int()); |
| 1428 break; |
| 1429 case unittest::TestOneof2::kFooString: |
| 1430 EXPECT_TRUE(message.has_foo_string()); |
| 1431 break; |
| 1432 case unittest::TestOneof2::kFooCord: |
| 1433 EXPECT_TRUE(message.has_foo_cord()); |
| 1434 break; |
| 1435 case unittest::TestOneof2::kFooStringPiece: |
| 1436 EXPECT_TRUE(message.has_foo_string_piece()); |
| 1437 break; |
| 1438 case unittest::TestOneof2::kFooBytes: |
| 1439 EXPECT_TRUE(message.has_foo_bytes()); |
| 1440 break; |
| 1441 case unittest::TestOneof2::kFooEnum: |
| 1442 EXPECT_TRUE(message.has_foo_enum()); |
| 1443 break; |
| 1444 case unittest::TestOneof2::kFooMessage: |
| 1445 EXPECT_TRUE(message.has_foo_message()); |
| 1446 break; |
| 1447 case unittest::TestOneof2::kFoogroup: |
| 1448 EXPECT_TRUE(message.has_foogroup()); |
| 1449 break; |
| 1450 case unittest::TestOneof2::kFooLazyMessage: |
| 1451 EXPECT_TRUE(message.has_foo_lazy_message()); |
| 1452 break; |
| 1453 case unittest::TestOneof2::FOO_NOT_SET: |
| 1454 break; |
| 1455 } |
| 1456 } |
| 1457 }; |
| 1458 |
| 1459 TEST_F(OneofTest, SettingOneFieldClearsOthers) { |
| 1460 unittest::TestOneof2 message; |
| 1461 |
| 1462 message.set_foo_int(123); |
| 1463 EXPECT_TRUE(message.has_foo_int()); |
| 1464 TestUtil::ExpectAtMostOneFieldSetInOneof(message); |
| 1465 |
| 1466 message.set_foo_string("foo"); |
| 1467 EXPECT_TRUE(message.has_foo_string()); |
| 1468 TestUtil::ExpectAtMostOneFieldSetInOneof(message); |
| 1469 |
| 1470 |
| 1471 message.set_foo_bytes("qux"); |
| 1472 EXPECT_TRUE(message.has_foo_bytes()); |
| 1473 TestUtil::ExpectAtMostOneFieldSetInOneof(message); |
| 1474 |
| 1475 message.set_foo_enum(unittest::TestOneof2::FOO); |
| 1476 EXPECT_TRUE(message.has_foo_enum()); |
| 1477 TestUtil::ExpectAtMostOneFieldSetInOneof(message); |
| 1478 |
| 1479 message.mutable_foo_message()->set_qux_int(234); |
| 1480 EXPECT_TRUE(message.has_foo_message()); |
| 1481 TestUtil::ExpectAtMostOneFieldSetInOneof(message); |
| 1482 |
| 1483 message.mutable_foogroup()->set_a(345); |
| 1484 EXPECT_TRUE(message.has_foogroup()); |
| 1485 TestUtil::ExpectAtMostOneFieldSetInOneof(message); |
| 1486 |
| 1487 |
| 1488 // we repeat this because we didn't test if this properly clears other fields |
| 1489 // at the beginning. |
| 1490 message.set_foo_int(123); |
| 1491 EXPECT_TRUE(message.has_foo_int()); |
| 1492 TestUtil::ExpectAtMostOneFieldSetInOneof(message); |
| 1493 } |
| 1494 |
| 1495 TEST_F(OneofTest, EnumCases) { |
| 1496 unittest::TestOneof2 message; |
| 1497 |
| 1498 message.set_foo_int(123); |
| 1499 ExpectEnumCasesWork(message); |
| 1500 message.set_foo_string("foo"); |
| 1501 ExpectEnumCasesWork(message); |
| 1502 message.set_foo_bytes("qux"); |
| 1503 ExpectEnumCasesWork(message); |
| 1504 message.set_foo_enum(unittest::TestOneof2::FOO); |
| 1505 ExpectEnumCasesWork(message); |
| 1506 message.mutable_foo_message()->set_qux_int(234); |
| 1507 ExpectEnumCasesWork(message); |
| 1508 message.mutable_foogroup()->set_a(345); |
| 1509 ExpectEnumCasesWork(message); |
| 1510 } |
| 1511 |
| 1512 TEST_F(OneofTest, PrimitiveType) { |
| 1513 unittest::TestOneof2 message; |
| 1514 // Unset field returns default value |
| 1515 EXPECT_EQ(message.foo_int(), 0); |
| 1516 |
| 1517 message.set_foo_int(123); |
| 1518 EXPECT_TRUE(message.has_foo_int()); |
| 1519 EXPECT_EQ(message.foo_int(), 123); |
| 1520 message.clear_foo_int(); |
| 1521 EXPECT_FALSE(message.has_foo_int()); |
| 1522 } |
| 1523 |
| 1524 TEST_F(OneofTest, EnumType) { |
| 1525 unittest::TestOneof2 message; |
| 1526 // Unset field returns default value |
| 1527 EXPECT_EQ(message.foo_enum(), 1); |
| 1528 |
| 1529 message.set_foo_enum(unittest::TestOneof2::FOO); |
| 1530 EXPECT_TRUE(message.has_foo_enum()); |
| 1531 EXPECT_EQ(message.foo_enum(), unittest::TestOneof2::FOO); |
| 1532 message.clear_foo_enum(); |
| 1533 EXPECT_FALSE(message.has_foo_enum()); |
| 1534 } |
| 1535 |
| 1536 TEST_F(OneofTest, SetString) { |
| 1537 // Check that setting a string field in various ways works |
| 1538 unittest::TestOneof2 message; |
| 1539 |
| 1540 // Unset field returns default value |
| 1541 EXPECT_EQ(message.foo_string(), ""); |
| 1542 |
| 1543 message.set_foo_string("foo"); |
| 1544 EXPECT_TRUE(message.has_foo_string()); |
| 1545 EXPECT_EQ(message.foo_string(), "foo"); |
| 1546 message.clear_foo_string(); |
| 1547 EXPECT_FALSE(message.has_foo_string()); |
| 1548 |
| 1549 message.set_foo_string(string("bar")); |
| 1550 EXPECT_TRUE(message.has_foo_string()); |
| 1551 EXPECT_EQ(message.foo_string(), "bar"); |
| 1552 message.clear_foo_string(); |
| 1553 EXPECT_FALSE(message.has_foo_string()); |
| 1554 |
| 1555 |
| 1556 message.set_foo_string("qux", 3); |
| 1557 EXPECT_TRUE(message.has_foo_string()); |
| 1558 EXPECT_EQ(message.foo_string(), "qux"); |
| 1559 message.clear_foo_string(); |
| 1560 EXPECT_FALSE(message.has_foo_string()); |
| 1561 |
| 1562 message.mutable_foo_string()->assign("quux"); |
| 1563 EXPECT_TRUE(message.has_foo_string()); |
| 1564 EXPECT_EQ(message.foo_string(), "quux"); |
| 1565 message.clear_foo_string(); |
| 1566 EXPECT_FALSE(message.has_foo_string()); |
| 1567 |
| 1568 message.set_foo_string("corge"); |
| 1569 EXPECT_TRUE(message.has_foo_string()); |
| 1570 EXPECT_EQ(message.foo_string(), "corge"); |
| 1571 message.clear_foo_string(); |
| 1572 EXPECT_FALSE(message.has_foo_string()); |
| 1573 } |
| 1574 |
| 1575 TEST_F(OneofTest, ReleaseString) { |
| 1576 // Check that release_foo() starts out NULL, and gives us a value |
| 1577 // that we can delete after it's been set. |
| 1578 unittest::TestOneof2 message; |
| 1579 |
| 1580 EXPECT_EQ(NULL, message.release_foo_string()); |
| 1581 EXPECT_FALSE(message.has_foo_string()); |
| 1582 |
| 1583 message.set_foo_string("blah"); |
| 1584 EXPECT_TRUE(message.has_foo_string()); |
| 1585 google::protobuf::scoped_ptr<string> str(message.release_foo_string()); |
| 1586 EXPECT_FALSE(message.has_foo_string()); |
| 1587 ASSERT_TRUE(str != NULL); |
| 1588 EXPECT_EQ("blah", *str); |
| 1589 |
| 1590 EXPECT_EQ(NULL, message.release_foo_string()); |
| 1591 EXPECT_FALSE(message.has_foo_string()); |
| 1592 } |
| 1593 |
| 1594 TEST_F(OneofTest, SetAllocatedString) { |
| 1595 // Check that set_allocated_foo() works for strings. |
| 1596 unittest::TestOneof2 message; |
| 1597 |
| 1598 EXPECT_FALSE(message.has_foo_string()); |
| 1599 const string kHello("hello"); |
| 1600 message.set_foo_string(kHello); |
| 1601 EXPECT_TRUE(message.has_foo_string()); |
| 1602 |
| 1603 message.set_allocated_foo_string(NULL); |
| 1604 EXPECT_FALSE(message.has_foo_string()); |
| 1605 EXPECT_EQ("", message.foo_string()); |
| 1606 |
| 1607 message.set_allocated_foo_string(new string(kHello)); |
| 1608 EXPECT_TRUE(message.has_foo_string()); |
| 1609 EXPECT_EQ(kHello, message.foo_string()); |
| 1610 } |
| 1611 |
| 1612 |
| 1613 TEST_F(OneofTest, SetMessage) { |
| 1614 // Check that setting a message field works |
| 1615 unittest::TestOneof2 message; |
| 1616 |
| 1617 // Unset field returns default instance |
| 1618 EXPECT_EQ(&message.foo_message(), |
| 1619 &unittest::TestOneof2_NestedMessage::default_instance()); |
| 1620 EXPECT_EQ(message.foo_message().qux_int(), 0); |
| 1621 |
| 1622 message.mutable_foo_message()->set_qux_int(234); |
| 1623 EXPECT_TRUE(message.has_foo_message()); |
| 1624 EXPECT_EQ(message.foo_message().qux_int(), 234); |
| 1625 message.clear_foo_message(); |
| 1626 EXPECT_FALSE(message.has_foo_message()); |
| 1627 } |
| 1628 |
| 1629 TEST_F(OneofTest, ReleaseMessage) { |
| 1630 // Check that release_foo() starts out NULL, and gives us a value |
| 1631 // that we can delete after it's been set. |
| 1632 unittest::TestOneof2 message; |
| 1633 |
| 1634 EXPECT_EQ(NULL, message.release_foo_message()); |
| 1635 EXPECT_FALSE(message.has_foo_message()); |
| 1636 |
| 1637 message.mutable_foo_message()->set_qux_int(1); |
| 1638 EXPECT_TRUE(message.has_foo_message()); |
| 1639 google::protobuf::scoped_ptr<unittest::TestOneof2_NestedMessage> mes( |
| 1640 message.release_foo_message()); |
| 1641 EXPECT_FALSE(message.has_foo_message()); |
| 1642 ASSERT_TRUE(mes != NULL); |
| 1643 EXPECT_EQ(1, mes->qux_int()); |
| 1644 |
| 1645 EXPECT_EQ(NULL, message.release_foo_message()); |
| 1646 EXPECT_FALSE(message.has_foo_message()); |
| 1647 } |
| 1648 |
| 1649 TEST_F(OneofTest, SetAllocatedMessage) { |
| 1650 // Check that set_allocated_foo() works for messages. |
| 1651 unittest::TestOneof2 message; |
| 1652 |
| 1653 EXPECT_FALSE(message.has_foo_message()); |
| 1654 |
| 1655 message.mutable_foo_message()->set_qux_int(1); |
| 1656 EXPECT_TRUE(message.has_foo_message()); |
| 1657 |
| 1658 message.set_allocated_foo_message(NULL); |
| 1659 EXPECT_FALSE(message.has_foo_message()); |
| 1660 EXPECT_EQ(&message.foo_message(), |
| 1661 &unittest::TestOneof2_NestedMessage::default_instance()); |
| 1662 |
| 1663 message.mutable_foo_message()->set_qux_int(1); |
| 1664 unittest::TestOneof2_NestedMessage* mes = message.release_foo_message(); |
| 1665 ASSERT_TRUE(mes != NULL); |
| 1666 EXPECT_FALSE(message.has_foo_message()); |
| 1667 |
| 1668 message.set_allocated_foo_message(mes); |
| 1669 EXPECT_TRUE(message.has_foo_message()); |
| 1670 EXPECT_EQ(1, message.foo_message().qux_int()); |
| 1671 } |
| 1672 |
| 1673 |
| 1674 TEST_F(OneofTest, Clear) { |
| 1675 unittest::TestOneof2 message; |
| 1676 |
| 1677 message.set_foo_int(1); |
| 1678 EXPECT_TRUE(message.has_foo_int()); |
| 1679 message.clear_foo_int(); |
| 1680 EXPECT_FALSE(message.has_foo_int()); |
| 1681 } |
| 1682 |
| 1683 TEST_F(OneofTest, Defaults) { |
| 1684 unittest::TestOneof2 message; |
| 1685 |
| 1686 EXPECT_FALSE(message.has_foo_int()); |
| 1687 EXPECT_EQ(message.foo_int(), 0); |
| 1688 |
| 1689 EXPECT_FALSE(message.has_foo_string()); |
| 1690 EXPECT_EQ(message.foo_string(), ""); |
| 1691 |
| 1692 |
| 1693 EXPECT_FALSE(message.has_foo_bytes()); |
| 1694 EXPECT_EQ(message.foo_bytes(), ""); |
| 1695 |
| 1696 EXPECT_FALSE(message.has_foo_enum()); |
| 1697 EXPECT_EQ(message.foo_enum(), 1); |
| 1698 |
| 1699 EXPECT_FALSE(message.has_foo_message()); |
| 1700 EXPECT_EQ(message.foo_message().qux_int(), 0); |
| 1701 |
| 1702 EXPECT_FALSE(message.has_foogroup()); |
| 1703 EXPECT_EQ(message.foogroup().a(), 0); |
| 1704 |
| 1705 |
| 1706 EXPECT_FALSE(message.has_bar_int()); |
| 1707 EXPECT_EQ(message.bar_int(), 5); |
| 1708 |
| 1709 EXPECT_FALSE(message.has_bar_string()); |
| 1710 EXPECT_EQ(message.bar_string(), "STRING"); |
| 1711 |
| 1712 |
| 1713 EXPECT_FALSE(message.has_bar_bytes()); |
| 1714 EXPECT_EQ(message.bar_bytes(), "BYTES"); |
| 1715 |
| 1716 EXPECT_FALSE(message.has_bar_enum()); |
| 1717 EXPECT_EQ(message.bar_enum(), 2); |
| 1718 } |
| 1719 |
| 1720 TEST_F(OneofTest, SwapWithEmpty) { |
| 1721 unittest::TestOneof2 message1, message2; |
| 1722 message1.set_foo_string("FOO"); |
| 1723 EXPECT_TRUE(message1.has_foo_string()); |
| 1724 message1.Swap(&message2); |
| 1725 EXPECT_FALSE(message1.has_foo_string()); |
| 1726 EXPECT_TRUE(message2.has_foo_string()); |
| 1727 EXPECT_EQ(message2.foo_string(), "FOO"); |
| 1728 } |
| 1729 |
| 1730 TEST_F(OneofTest, SwapWithSelf) { |
| 1731 unittest::TestOneof2 message; |
| 1732 message.set_foo_string("FOO"); |
| 1733 EXPECT_TRUE(message.has_foo_string()); |
| 1734 message.Swap(&message); |
| 1735 EXPECT_TRUE(message.has_foo_string()); |
| 1736 EXPECT_EQ(message.foo_string(), "FOO"); |
| 1737 } |
| 1738 |
| 1739 TEST_F(OneofTest, SwapBothHasFields) { |
| 1740 unittest::TestOneof2 message1, message2; |
| 1741 |
| 1742 message1.set_foo_string("FOO"); |
| 1743 EXPECT_TRUE(message1.has_foo_string()); |
| 1744 message2.mutable_foo_message()->set_qux_int(1); |
| 1745 EXPECT_TRUE(message2.has_foo_message()); |
| 1746 |
| 1747 message1.Swap(&message2); |
| 1748 EXPECT_FALSE(message1.has_foo_string()); |
| 1749 EXPECT_FALSE(message2.has_foo_message()); |
| 1750 EXPECT_TRUE(message1.has_foo_message()); |
| 1751 EXPECT_EQ(message1.foo_message().qux_int(), 1); |
| 1752 EXPECT_TRUE(message2.has_foo_string()); |
| 1753 EXPECT_EQ(message2.foo_string(), "FOO"); |
| 1754 } |
| 1755 |
| 1756 TEST_F(OneofTest, CopyConstructor) { |
| 1757 unittest::TestOneof2 message1; |
| 1758 message1.set_foo_bytes("FOO"); |
| 1759 |
| 1760 unittest::TestOneof2 message2(message1); |
| 1761 EXPECT_TRUE(message2.has_foo_bytes()); |
| 1762 EXPECT_EQ(message2.foo_bytes(), "FOO"); |
| 1763 } |
| 1764 |
| 1765 TEST_F(OneofTest, CopyFrom) { |
| 1766 unittest::TestOneof2 message1, message2; |
| 1767 message1.set_foo_enum(unittest::TestOneof2::BAR); |
| 1768 EXPECT_TRUE(message1.has_foo_enum()); |
| 1769 |
| 1770 message2.CopyFrom(message1); |
| 1771 EXPECT_TRUE(message2.has_foo_enum()); |
| 1772 EXPECT_EQ(message2.foo_enum(), unittest::TestOneof2::BAR); |
| 1773 |
| 1774 // Copying from self should be a no-op. |
| 1775 message2.CopyFrom(message2); |
| 1776 EXPECT_TRUE(message2.has_foo_enum()); |
| 1777 EXPECT_EQ(message2.foo_enum(), unittest::TestOneof2::BAR); |
| 1778 } |
| 1779 |
| 1780 TEST_F(OneofTest, CopyAssignmentOperator) { |
| 1781 unittest::TestOneof2 message1; |
| 1782 message1.mutable_foo_message()->set_qux_int(123); |
| 1783 EXPECT_TRUE(message1.has_foo_message()); |
| 1784 |
| 1785 unittest::TestOneof2 message2; |
| 1786 message2 = message1; |
| 1787 EXPECT_EQ(message2.foo_message().qux_int(), 123); |
| 1788 |
| 1789 // Make sure that self-assignment does something sane. |
| 1790 message2 = message2; |
| 1791 EXPECT_EQ(message2.foo_message().qux_int(), 123); |
| 1792 } |
| 1793 |
| 1794 TEST_F(OneofTest, UpcastCopyFrom) { |
| 1795 // Test the CopyFrom method that takes in the generic const Message& |
| 1796 // parameter. |
| 1797 unittest::TestOneof2 message1, message2; |
| 1798 message1.mutable_foogroup()->set_a(123); |
| 1799 EXPECT_TRUE(message1.has_foogroup()); |
| 1800 |
| 1801 const Message* source = implicit_cast<const Message*>(&message1); |
| 1802 message2.CopyFrom(*source); |
| 1803 |
| 1804 EXPECT_TRUE(message2.has_foogroup()); |
| 1805 EXPECT_EQ(message2.foogroup().a(), 123); |
| 1806 } |
| 1807 |
| 1808 // Test the generated SerializeWithCachedSizesToArray(), |
| 1809 // This indirectly tests MergePartialFromCodedStream() |
| 1810 // We have to test each field type separately because we cannot set them at the |
| 1811 // same time |
| 1812 TEST_F(OneofTest, SerializationToArray) { |
| 1813 // Primitive type |
| 1814 { |
| 1815 unittest::TestOneof2 message1, message2; |
| 1816 string data; |
| 1817 message1.set_foo_int(123); |
| 1818 int size = message1.ByteSize(); |
| 1819 data.resize(size); |
| 1820 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data)); |
| 1821 uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 1822 EXPECT_EQ(size, end - start); |
| 1823 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1824 EXPECT_EQ(message2.foo_int(), 123); |
| 1825 } |
| 1826 |
| 1827 // String |
| 1828 { |
| 1829 unittest::TestOneof2 message1, message2; |
| 1830 string data; |
| 1831 message1.set_foo_string("foo"); |
| 1832 int size = message1.ByteSize(); |
| 1833 data.resize(size); |
| 1834 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data)); |
| 1835 uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 1836 EXPECT_EQ(size, end - start); |
| 1837 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1838 EXPECT_EQ(message2.foo_string(), "foo"); |
| 1839 } |
| 1840 |
| 1841 |
| 1842 // Bytes |
| 1843 { |
| 1844 unittest::TestOneof2 message1, message2; |
| 1845 string data; |
| 1846 message1.set_foo_bytes("qux"); |
| 1847 int size = message1.ByteSize(); |
| 1848 data.resize(size); |
| 1849 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data)); |
| 1850 uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 1851 EXPECT_EQ(size, end - start); |
| 1852 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1853 EXPECT_EQ(message2.foo_bytes(), "qux"); |
| 1854 } |
| 1855 |
| 1856 // Enum |
| 1857 { |
| 1858 unittest::TestOneof2 message1, message2; |
| 1859 string data; |
| 1860 message1.set_foo_enum(unittest::TestOneof2::FOO); |
| 1861 int size = message1.ByteSize(); |
| 1862 data.resize(size); |
| 1863 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data)); |
| 1864 uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 1865 EXPECT_EQ(size, end - start); |
| 1866 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1867 EXPECT_EQ(message2.foo_enum(), unittest::TestOneof2::FOO); |
| 1868 } |
| 1869 |
| 1870 // Message |
| 1871 { |
| 1872 unittest::TestOneof2 message1, message2; |
| 1873 string data; |
| 1874 message1.mutable_foo_message()->set_qux_int(234); |
| 1875 int size = message1.ByteSize(); |
| 1876 data.resize(size); |
| 1877 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data)); |
| 1878 uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 1879 EXPECT_EQ(size, end - start); |
| 1880 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1881 EXPECT_EQ(message2.foo_message().qux_int(), 234); |
| 1882 } |
| 1883 |
| 1884 // Group |
| 1885 { |
| 1886 unittest::TestOneof2 message1, message2; |
| 1887 string data; |
| 1888 message1.mutable_foogroup()->set_a(345); |
| 1889 int size = message1.ByteSize(); |
| 1890 data.resize(size); |
| 1891 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data)); |
| 1892 uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 1893 EXPECT_EQ(size, end - start); |
| 1894 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1895 EXPECT_EQ(message2.foogroup().a(), 345); |
| 1896 } |
| 1897 |
| 1898 } |
| 1899 |
| 1900 // Test the generated SerializeWithCachedSizes() by forcing the buffer to write |
| 1901 // one byte at a time. |
| 1902 // This indirectly tests MergePartialFromCodedStream() |
| 1903 // We have to test each field type separately because we cannot set them at the |
| 1904 // same time |
| 1905 TEST_F(OneofTest, SerializationToStream) { |
| 1906 // Primitive type |
| 1907 { |
| 1908 unittest::TestOneof2 message1, message2; |
| 1909 string data; |
| 1910 message1.set_foo_int(123); |
| 1911 int size = message1.ByteSize(); |
| 1912 data.resize(size); |
| 1913 |
| 1914 { |
| 1915 // Allow the output stream to buffer only one byte at a time. |
| 1916 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1); |
| 1917 io::CodedOutputStream output_stream(&array_stream); |
| 1918 message1.SerializeWithCachedSizes(&output_stream); |
| 1919 EXPECT_FALSE(output_stream.HadError()); |
| 1920 EXPECT_EQ(size, output_stream.ByteCount()); |
| 1921 } |
| 1922 |
| 1923 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1924 EXPECT_EQ(message2.foo_int(), 123); |
| 1925 } |
| 1926 |
| 1927 // String |
| 1928 { |
| 1929 unittest::TestOneof2 message1, message2; |
| 1930 string data; |
| 1931 message1.set_foo_string("foo"); |
| 1932 int size = message1.ByteSize(); |
| 1933 data.resize(size); |
| 1934 |
| 1935 { |
| 1936 // Allow the output stream to buffer only one byte at a time. |
| 1937 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1); |
| 1938 io::CodedOutputStream output_stream(&array_stream); |
| 1939 message1.SerializeWithCachedSizes(&output_stream); |
| 1940 EXPECT_FALSE(output_stream.HadError()); |
| 1941 EXPECT_EQ(size, output_stream.ByteCount()); |
| 1942 } |
| 1943 |
| 1944 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1945 EXPECT_EQ(message2.foo_string(), "foo"); |
| 1946 } |
| 1947 |
| 1948 |
| 1949 // Bytes |
| 1950 { |
| 1951 unittest::TestOneof2 message1, message2; |
| 1952 string data; |
| 1953 message1.set_foo_bytes("qux"); |
| 1954 int size = message1.ByteSize(); |
| 1955 data.resize(size); |
| 1956 |
| 1957 { |
| 1958 // Allow the output stream to buffer only one byte at a time. |
| 1959 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1); |
| 1960 io::CodedOutputStream output_stream(&array_stream); |
| 1961 message1.SerializeWithCachedSizes(&output_stream); |
| 1962 EXPECT_FALSE(output_stream.HadError()); |
| 1963 EXPECT_EQ(size, output_stream.ByteCount()); |
| 1964 } |
| 1965 |
| 1966 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1967 EXPECT_EQ(message2.foo_bytes(), "qux"); |
| 1968 } |
| 1969 |
| 1970 // Enum |
| 1971 { |
| 1972 unittest::TestOneof2 message1, message2; |
| 1973 string data; |
| 1974 message1.set_foo_enum(unittest::TestOneof2::FOO); |
| 1975 int size = message1.ByteSize(); |
| 1976 data.resize(size); |
| 1977 |
| 1978 { |
| 1979 // Allow the output stream to buffer only one byte at a time. |
| 1980 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1); |
| 1981 io::CodedOutputStream output_stream(&array_stream); |
| 1982 message1.SerializeWithCachedSizes(&output_stream); |
| 1983 EXPECT_FALSE(output_stream.HadError()); |
| 1984 EXPECT_EQ(size, output_stream.ByteCount()); |
| 1985 } |
| 1986 |
| 1987 EXPECT_TRUE(message2.ParseFromString(data)); |
| 1988 EXPECT_EQ(message2.foo_enum(), unittest::TestOneof2::FOO); |
| 1989 } |
| 1990 |
| 1991 // Message |
| 1992 { |
| 1993 unittest::TestOneof2 message1, message2; |
| 1994 string data; |
| 1995 message1.mutable_foo_message()->set_qux_int(234); |
| 1996 int size = message1.ByteSize(); |
| 1997 data.resize(size); |
| 1998 |
| 1999 { |
| 2000 // Allow the output stream to buffer only one byte at a time. |
| 2001 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1); |
| 2002 io::CodedOutputStream output_stream(&array_stream); |
| 2003 message1.SerializeWithCachedSizes(&output_stream); |
| 2004 EXPECT_FALSE(output_stream.HadError()); |
| 2005 EXPECT_EQ(size, output_stream.ByteCount()); |
| 2006 } |
| 2007 |
| 2008 EXPECT_TRUE(message2.ParseFromString(data)); |
| 2009 EXPECT_EQ(message2.foo_message().qux_int(), 234); |
| 2010 } |
| 2011 |
| 2012 // Group |
| 2013 { |
| 2014 unittest::TestOneof2 message1, message2; |
| 2015 string data; |
| 2016 message1.mutable_foogroup()->set_a(345); |
| 2017 int size = message1.ByteSize(); |
| 2018 data.resize(size); |
| 2019 |
| 2020 { |
| 2021 // Allow the output stream to buffer only one byte at a time. |
| 2022 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1); |
| 2023 io::CodedOutputStream output_stream(&array_stream); |
| 2024 message1.SerializeWithCachedSizes(&output_stream); |
| 2025 EXPECT_FALSE(output_stream.HadError()); |
| 2026 EXPECT_EQ(size, output_stream.ByteCount()); |
| 2027 } |
| 2028 |
| 2029 EXPECT_TRUE(message2.ParseFromString(data)); |
| 2030 EXPECT_EQ(message2.foogroup().a(), 345); |
| 2031 } |
| 2032 |
| 2033 } |
| 2034 |
| 2035 TEST_F(OneofTest, MergeFrom) { |
| 2036 unittest::TestOneof2 message1, message2; |
| 2037 |
| 2038 message1.set_foo_int(123); |
| 2039 message2.MergeFrom(message1); |
| 2040 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); |
| 2041 EXPECT_TRUE(message2.has_foo_int()); |
| 2042 EXPECT_EQ(message2.foo_int(), 123); |
| 2043 |
| 2044 message1.set_foo_string("foo"); |
| 2045 message2.MergeFrom(message1); |
| 2046 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); |
| 2047 EXPECT_TRUE(message2.has_foo_string()); |
| 2048 EXPECT_EQ(message2.foo_string(), "foo"); |
| 2049 |
| 2050 |
| 2051 message1.set_foo_bytes("qux"); |
| 2052 message2.MergeFrom(message1); |
| 2053 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); |
| 2054 EXPECT_TRUE(message2.has_foo_bytes()); |
| 2055 EXPECT_EQ(message2.foo_bytes(), "qux"); |
| 2056 |
| 2057 message1.set_foo_enum(unittest::TestOneof2::FOO); |
| 2058 message2.MergeFrom(message1); |
| 2059 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); |
| 2060 EXPECT_TRUE(message2.has_foo_enum()); |
| 2061 EXPECT_EQ(message2.foo_enum(), unittest::TestOneof2::FOO); |
| 2062 |
| 2063 message1.mutable_foo_message()->set_qux_int(234); |
| 2064 message2.MergeFrom(message1); |
| 2065 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); |
| 2066 EXPECT_TRUE(message2.has_foo_message()); |
| 2067 EXPECT_EQ(message2.foo_message().qux_int(), 234); |
| 2068 |
| 2069 message1.mutable_foogroup()->set_a(345); |
| 2070 message2.MergeFrom(message1); |
| 2071 TestUtil::ExpectAtMostOneFieldSetInOneof(message2); |
| 2072 EXPECT_TRUE(message2.has_foogroup()); |
| 2073 EXPECT_EQ(message2.foogroup().a(), 345); |
| 2074 |
| 2075 } |
| 2076 |
1291 } // namespace cpp_unittest | 2077 } // namespace cpp_unittest |
1292 } // namespace cpp | 2078 } // namespace cpp |
1293 } // namespace compiler | 2079 } // namespace compiler |
1294 | 2080 |
1295 namespace no_generic_services_test { | 2081 namespace no_generic_services_test { |
1296 // Verify that no class called "TestService" was defined in | 2082 // Verify that no class called "TestService" was defined in |
1297 // unittest_no_generic_services.pb.h by defining a different type by the same | 2083 // unittest_no_generic_services.pb.h by defining a different type by the same |
1298 // name. If such a service was generated, this will not compile. | 2084 // name. If such a service was generated, this will not compile. |
1299 struct TestService { | 2085 struct TestService { |
1300 int i; | 2086 int i; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1345 DescriptorPool::generated_pool()->InternalIsFileLoaded( | 2131 DescriptorPool::generated_pool()->InternalIsFileLoaded( |
1346 "google/protobuf/unittest.proto")); | 2132 "google/protobuf/unittest.proto")); |
1347 } | 2133 } |
1348 | 2134 |
1349 } // namespace cpp_unittest | 2135 } // namespace cpp_unittest |
1350 | 2136 |
1351 } // namespace cpp | 2137 } // namespace cpp |
1352 } // namespace compiler | 2138 } // namespace compiler |
1353 } // namespace protobuf | 2139 } // namespace protobuf |
1354 } // namespace google | 2140 } // namespace google |
OLD | NEW |