OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 215 |
216 TEST(GeneratedMessageReflectionTest, SwapFields) { | 216 TEST(GeneratedMessageReflectionTest, SwapFields) { |
217 unittest::TestAllTypes message1, message2; | 217 unittest::TestAllTypes message1, message2; |
218 message1.set_optional_double(12.3); | 218 message1.set_optional_double(12.3); |
219 message1.mutable_repeated_int32()->Add(10); | 219 message1.mutable_repeated_int32()->Add(10); |
220 message1.mutable_repeated_int32()->Add(20); | 220 message1.mutable_repeated_int32()->Add(20); |
221 | 221 |
222 message2.set_optional_string("hello"); | 222 message2.set_optional_string("hello"); |
223 message2.mutable_repeated_int64()->Add(30); | 223 message2.mutable_repeated_int64()->Add(30); |
224 | 224 |
225 vector<const FieldDescriptor*> fields; | 225 std::vector<const FieldDescriptor*> fields; |
226 const Descriptor* descriptor = message1.GetDescriptor(); | 226 const Descriptor* descriptor = message1.GetDescriptor(); |
227 fields.push_back(descriptor->FindFieldByName("optional_double")); | 227 fields.push_back(descriptor->FindFieldByName("optional_double")); |
228 fields.push_back(descriptor->FindFieldByName("repeated_int32")); | 228 fields.push_back(descriptor->FindFieldByName("repeated_int32")); |
229 fields.push_back(descriptor->FindFieldByName("optional_string")); | 229 fields.push_back(descriptor->FindFieldByName("optional_string")); |
230 fields.push_back(descriptor->FindFieldByName("optional_uint64")); | 230 fields.push_back(descriptor->FindFieldByName("optional_uint64")); |
231 | 231 |
232 const Reflection* reflection = message1.GetReflection(); | 232 const Reflection* reflection = message1.GetReflection(); |
233 reflection->SwapFields(&message1, &message2, fields); | 233 reflection->SwapFields(&message1, &message2, fields); |
234 | 234 |
235 EXPECT_FALSE(message1.has_optional_double()); | 235 EXPECT_FALSE(message1.has_optional_double()); |
(...skipping 12 matching lines...) Expand all Loading... |
248 EXPECT_EQ(1, message2.repeated_int64_size()); | 248 EXPECT_EQ(1, message2.repeated_int64_size()); |
249 EXPECT_FALSE(message2.has_optional_uint64()); | 249 EXPECT_FALSE(message2.has_optional_uint64()); |
250 } | 250 } |
251 | 251 |
252 TEST(GeneratedMessageReflectionTest, SwapFieldsAll) { | 252 TEST(GeneratedMessageReflectionTest, SwapFieldsAll) { |
253 unittest::TestAllTypes message1; | 253 unittest::TestAllTypes message1; |
254 unittest::TestAllTypes message2; | 254 unittest::TestAllTypes message2; |
255 | 255 |
256 TestUtil::SetAllFields(&message2); | 256 TestUtil::SetAllFields(&message2); |
257 | 257 |
258 vector<const FieldDescriptor*> fields; | 258 std::vector<const FieldDescriptor*> fields; |
259 const Reflection* reflection = message1.GetReflection(); | 259 const Reflection* reflection = message1.GetReflection(); |
260 reflection->ListFields(message2, &fields); | 260 reflection->ListFields(message2, &fields); |
261 reflection->SwapFields(&message1, &message2, fields); | 261 reflection->SwapFields(&message1, &message2, fields); |
262 | 262 |
263 TestUtil::ExpectAllFieldsSet(message1); | 263 TestUtil::ExpectAllFieldsSet(message1); |
264 TestUtil::ExpectClear(message2); | 264 TestUtil::ExpectClear(message2); |
265 } | 265 } |
266 | 266 |
267 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtension) { | 267 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtension) { |
268 unittest::TestAllExtensions message1; | 268 unittest::TestAllExtensions message1; |
269 unittest::TestAllExtensions message2; | 269 unittest::TestAllExtensions message2; |
270 | 270 |
271 TestUtil::SetAllExtensions(&message1); | 271 TestUtil::SetAllExtensions(&message1); |
272 | 272 |
273 vector<const FieldDescriptor*> fields; | 273 std::vector<const FieldDescriptor*> fields; |
274 const Reflection* reflection = message1.GetReflection(); | 274 const Reflection* reflection = message1.GetReflection(); |
275 reflection->ListFields(message1, &fields); | 275 reflection->ListFields(message1, &fields); |
276 reflection->SwapFields(&message1, &message2, fields); | 276 reflection->SwapFields(&message1, &message2, fields); |
277 | 277 |
278 TestUtil::ExpectExtensionsClear(message1); | 278 TestUtil::ExpectExtensionsClear(message1); |
279 TestUtil::ExpectAllExtensionsSet(message2); | 279 TestUtil::ExpectAllExtensionsSet(message2); |
280 } | 280 } |
281 | 281 |
282 TEST(GeneratedMessageReflectionTest, SwapOneof) { | 282 TEST(GeneratedMessageReflectionTest, SwapOneof) { |
283 unittest::TestOneof2 message1, message2; | 283 unittest::TestOneof2 message1, message2; |
(...skipping 15 matching lines...) Expand all Loading... |
299 reflection->Swap(&message1, &message2); | 299 reflection->Swap(&message1, &message2); |
300 | 300 |
301 TestUtil::ExpectOneofSet2(message1); | 301 TestUtil::ExpectOneofSet2(message1); |
302 TestUtil::ExpectOneofSet1(message2); | 302 TestUtil::ExpectOneofSet1(message2); |
303 } | 303 } |
304 | 304 |
305 TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) { | 305 TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) { |
306 unittest::TestOneof2 message1, message2; | 306 unittest::TestOneof2 message1, message2; |
307 TestUtil::SetOneof1(&message1); | 307 TestUtil::SetOneof1(&message1); |
308 | 308 |
309 vector<const FieldDescriptor*> fields; | 309 std::vector<const FieldDescriptor*> fields; |
310 const Descriptor* descriptor = message1.GetDescriptor(); | 310 const Descriptor* descriptor = message1.GetDescriptor(); |
311 for (int i = 0; i < descriptor->field_count(); i++) { | 311 for (int i = 0; i < descriptor->field_count(); i++) { |
312 fields.push_back(descriptor->field(i)); | 312 fields.push_back(descriptor->field(i)); |
313 } | 313 } |
314 const Reflection* reflection = message1.GetReflection(); | 314 const Reflection* reflection = message1.GetReflection(); |
315 reflection->SwapFields(&message1, &message2, fields); | 315 reflection->SwapFields(&message1, &message2, fields); |
316 | 316 |
317 TestUtil::ExpectOneofClear(message1); | 317 TestUtil::ExpectOneofClear(message1); |
318 TestUtil::ExpectOneofSet1(message2); | 318 TestUtil::ExpectOneofSet1(message2); |
319 } | 319 } |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), nested
); | 601 reflection->AddAllocatedMessage(&message, F("repeated_nested_message"), nested
); |
602 EXPECT_EQ(1, message.repeated_nested_message_size()); | 602 EXPECT_EQ(1, message.repeated_nested_message_size()); |
603 EXPECT_EQ(11, message.repeated_nested_message(0).bb()); | 603 EXPECT_EQ(11, message.repeated_nested_message(0).bb()); |
604 } | 604 } |
605 | 605 |
606 TEST(GeneratedMessageReflectionTest, ListFieldsOneOf) { | 606 TEST(GeneratedMessageReflectionTest, ListFieldsOneOf) { |
607 unittest::TestOneof2 message; | 607 unittest::TestOneof2 message; |
608 TestUtil::SetOneof1(&message); | 608 TestUtil::SetOneof1(&message); |
609 | 609 |
610 const Reflection* reflection = message.GetReflection(); | 610 const Reflection* reflection = message.GetReflection(); |
611 vector<const FieldDescriptor*> fields; | 611 std::vector<const FieldDescriptor*> fields; |
612 reflection->ListFields(message, &fields); | 612 reflection->ListFields(message, &fields); |
613 EXPECT_EQ(4, fields.size()); | 613 EXPECT_EQ(4, fields.size()); |
614 } | 614 } |
615 | 615 |
616 TEST(GeneratedMessageReflectionTest, Oneof) { | 616 TEST(GeneratedMessageReflectionTest, Oneof) { |
617 unittest::TestOneof2 message; | 617 unittest::TestOneof2 message; |
618 const Descriptor* descriptor = message.GetDescriptor(); | 618 const Descriptor* descriptor = message.GetDescriptor(); |
619 const Reflection* reflection = message.GetReflection(); | 619 const Reflection* reflection = message.GetReflection(); |
620 | 620 |
621 // Check default values. | 621 // Check default values. |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
788 | 788 |
789 EXPECT_TRUE(released != NULL); | 789 EXPECT_TRUE(released != NULL); |
790 EXPECT_EQ(&sub_message, released); | 790 EXPECT_EQ(&sub_message, released); |
791 delete released; | 791 delete released; |
792 | 792 |
793 released = reflection->ReleaseMessage( | 793 released = reflection->ReleaseMessage( |
794 &message, descriptor->FindFieldByName("foo_lazy_message")); | 794 &message, descriptor->FindFieldByName("foo_lazy_message")); |
795 EXPECT_TRUE(released == NULL); | 795 EXPECT_TRUE(released == NULL); |
796 } | 796 } |
797 | 797 |
| 798 TEST(GeneratedMessageReflectionTest, ArenaReleaseMessageTest) { |
| 799 ::google::protobuf::Arena arena; |
| 800 unittest::TestAllTypes* message = |
| 801 ::google::protobuf::Arena::CreateMessage<unittest::TestAllTypes>(&arena); |
| 802 TestUtil::ReflectionTester reflection_tester( |
| 803 unittest::TestAllTypes::descriptor()); |
| 804 |
| 805 // When nothing is set, we expect all released messages to be NULL. |
| 806 reflection_tester.ExpectMessagesReleasedViaReflection( |
| 807 message, TestUtil::ReflectionTester::IS_NULL); |
| 808 |
| 809 // After fields are set we should get non-NULL releases. |
| 810 reflection_tester.SetAllFieldsViaReflection(message); |
| 811 reflection_tester.ExpectMessagesReleasedViaReflection( |
| 812 message, TestUtil::ReflectionTester::NOT_NULL); |
| 813 |
| 814 // After Clear() we may or may not get a message from ReleaseMessage(). |
| 815 // This is implementation specific. |
| 816 reflection_tester.SetAllFieldsViaReflection(message); |
| 817 message->Clear(); |
| 818 reflection_tester.ExpectMessagesReleasedViaReflection( |
| 819 message, TestUtil::ReflectionTester::CAN_BE_NULL); |
| 820 } |
| 821 |
| 822 TEST(GeneratedMessageReflectionTest, ArenaReleaseExtensionMessageTest) { |
| 823 ::google::protobuf::Arena arena; |
| 824 unittest::TestAllExtensions* message = |
| 825 ::google::protobuf::Arena::CreateMessage<unittest::TestAllExtensions>(&are
na); |
| 826 TestUtil::ReflectionTester reflection_tester( |
| 827 unittest::TestAllExtensions::descriptor()); |
| 828 |
| 829 // When nothing is set, we expect all released messages to be NULL. |
| 830 reflection_tester.ExpectMessagesReleasedViaReflection( |
| 831 message, TestUtil::ReflectionTester::IS_NULL); |
| 832 |
| 833 // After fields are set we should get non-NULL releases. |
| 834 reflection_tester.SetAllFieldsViaReflection(message); |
| 835 reflection_tester.ExpectMessagesReleasedViaReflection( |
| 836 message, TestUtil::ReflectionTester::NOT_NULL); |
| 837 |
| 838 // After Clear() we may or may not get a message from ReleaseMessage(). |
| 839 // This is implementation specific. |
| 840 reflection_tester.SetAllFieldsViaReflection(message); |
| 841 message->Clear(); |
| 842 reflection_tester.ExpectMessagesReleasedViaReflection( |
| 843 message, TestUtil::ReflectionTester::CAN_BE_NULL); |
| 844 } |
| 845 |
| 846 TEST(GeneratedMessageReflectionTest, ArenaReleaseOneofMessageTest) { |
| 847 ::google::protobuf::Arena arena; |
| 848 unittest::TestOneof2* message = |
| 849 ::google::protobuf::Arena::CreateMessage<unittest::TestOneof2>(&arena); |
| 850 TestUtil::ReflectionTester::SetOneofViaReflection(message); |
| 851 |
| 852 const Descriptor* descriptor = unittest::TestOneof2::descriptor(); |
| 853 const Reflection* reflection = message->GetReflection(); |
| 854 Message* released = reflection->ReleaseMessage( |
| 855 message, descriptor->FindFieldByName("foo_lazy_message")); |
| 856 |
| 857 EXPECT_TRUE(released != NULL); |
| 858 delete released; |
| 859 |
| 860 released = reflection->ReleaseMessage( |
| 861 message, descriptor->FindFieldByName("foo_lazy_message")); |
| 862 EXPECT_TRUE(released == NULL); |
| 863 } |
| 864 |
798 #ifdef PROTOBUF_HAS_DEATH_TEST | 865 #ifdef PROTOBUF_HAS_DEATH_TEST |
799 | 866 |
800 TEST(GeneratedMessageReflectionTest, UsageErrors) { | 867 TEST(GeneratedMessageReflectionTest, UsageErrors) { |
801 unittest::TestAllTypes message; | 868 unittest::TestAllTypes message; |
802 const Reflection* reflection = message.GetReflection(); | 869 const Reflection* reflection = message.GetReflection(); |
803 const Descriptor* descriptor = message.GetDescriptor(); | 870 const Descriptor* descriptor = message.GetDescriptor(); |
804 | 871 |
805 #define f(NAME) descriptor->FindFieldByName(NAME) | 872 #define f(NAME) descriptor->FindFieldByName(NAME) |
806 | 873 |
807 // Testing every single failure mode would be too much work. Let's just | 874 // Testing every single failure mode would be too much work. Let's just |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
843 | 910 |
844 #undef f | 911 #undef f |
845 } | 912 } |
846 | 913 |
847 #endif // PROTOBUF_HAS_DEATH_TEST | 914 #endif // PROTOBUF_HAS_DEATH_TEST |
848 | 915 |
849 | 916 |
850 } // namespace | 917 } // namespace |
851 } // namespace protobuf | 918 } // namespace protobuf |
852 } // namespace google | 919 } // namespace google |
OLD | NEW |