• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 //
35 // To test GeneratedMessageReflection, we actually let the protocol compiler
36 // generate a full protocol message implementation and then test its
37 // reflection interface.  This is much easier and more maintainable than
38 // trying to create our own Message class for GeneratedMessageReflection
39 // to wrap.
40 //
41 // The tests here closely mirror some of the tests in
42 // compiler/cpp/unittest, except using the reflection interface
43 // rather than generated accessors.
44 
45 #include <google/protobuf/generated_message_reflection.h>
46 #include <memory>
47 
48 #include <google/protobuf/test_util.h>
49 #include <google/protobuf/unittest.pb.h>
50 #include <google/protobuf/arena.h>
51 #include <google/protobuf/descriptor.h>
52 
53 #include <google/protobuf/stubs/logging.h>
54 #include <google/protobuf/stubs/common.h>
55 #include <google/protobuf/testing/googletest.h>
56 #include <gtest/gtest.h>
57 
58 namespace google {
59 namespace protobuf {
60 
61 namespace {
62 
63 // Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
F(const std::string & name)64 const FieldDescriptor* F(const std::string& name) {
65   const FieldDescriptor* result =
66       unittest::TestAllTypes::descriptor()->FindFieldByName(name);
67   GOOGLE_CHECK(result != NULL);
68   return result;
69 }
70 
TEST(GeneratedMessageReflectionTest,Defaults)71 TEST(GeneratedMessageReflectionTest, Defaults) {
72   // Check that all default values are set correctly in the initial message.
73   unittest::TestAllTypes message;
74   TestUtil::ReflectionTester reflection_tester(
75       unittest::TestAllTypes::descriptor());
76 
77   reflection_tester.ExpectClearViaReflection(message);
78 
79   const Reflection* reflection = message.GetReflection();
80 
81   // Messages should return pointers to default instances until first use.
82   // (This is not checked by ExpectClear() since it is not actually true after
83   // the fields have been set and then cleared.)
84   EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
85             &reflection->GetMessage(message, F("optionalgroup")));
86   EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
87             &reflection->GetMessage(message, F("optional_nested_message")));
88   EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
89             &reflection->GetMessage(message, F("optional_foreign_message")));
90   EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
91             &reflection->GetMessage(message, F("optional_import_message")));
92 }
93 
TEST(GeneratedMessageReflectionTest,Accessors)94 TEST(GeneratedMessageReflectionTest, Accessors) {
95   // Set every field to a unique value then go back and check all those
96   // values.
97   unittest::TestAllTypes message;
98   TestUtil::ReflectionTester reflection_tester(
99       unittest::TestAllTypes::descriptor());
100 
101   reflection_tester.SetAllFieldsViaReflection(&message);
102   TestUtil::ExpectAllFieldsSet(message);
103   reflection_tester.ExpectAllFieldsSetViaReflection(message);
104 
105   reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
106   TestUtil::ExpectRepeatedFieldsModified(message);
107 }
108 
TEST(GeneratedMessageReflectionTest,GetStringReference)109 TEST(GeneratedMessageReflectionTest, GetStringReference) {
110   // Test that GetStringReference() returns the underlying string when it
111   // is a normal string field.
112   unittest::TestAllTypes message;
113   message.set_optional_string("foo");
114   message.add_repeated_string("foo");
115 
116   const Reflection* reflection = message.GetReflection();
117   std::string scratch;
118 
119   EXPECT_EQ(
120       &message.optional_string(),
121       &reflection->GetStringReference(message, F("optional_string"), &scratch))
122       << "For simple string fields, GetStringReference() should return a "
123          "reference to the underlying string.";
124   EXPECT_EQ(&message.repeated_string(0),
125             &reflection->GetRepeatedStringReference(
126                 message, F("repeated_string"), 0, &scratch))
127       << "For simple string fields, GetRepeatedStringReference() should "
128          "return "
129          "a reference to the underlying string.";
130 }
131 
132 
TEST(GeneratedMessageReflectionTest,DefaultsAfterClear)133 TEST(GeneratedMessageReflectionTest, DefaultsAfterClear) {
134   // Check that after setting all fields and then clearing, getting an
135   // embedded message does NOT return the default instance.
136   unittest::TestAllTypes message;
137   TestUtil::ReflectionTester reflection_tester(
138       unittest::TestAllTypes::descriptor());
139 
140   TestUtil::SetAllFields(&message);
141   message.Clear();
142 
143   const Reflection* reflection = message.GetReflection();
144 
145   EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
146             &reflection->GetMessage(message, F("optionalgroup")));
147   EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
148             &reflection->GetMessage(message, F("optional_nested_message")));
149   EXPECT_NE(&unittest::ForeignMessage::default_instance(),
150             &reflection->GetMessage(message, F("optional_foreign_message")));
151   EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
152             &reflection->GetMessage(message, F("optional_import_message")));
153 }
154 
TEST(GeneratedMessageReflectionTest,Swap)155 TEST(GeneratedMessageReflectionTest, Swap) {
156   unittest::TestAllTypes message1;
157   unittest::TestAllTypes message2;
158 
159   TestUtil::SetAllFields(&message1);
160 
161   const Reflection* reflection = message1.GetReflection();
162   reflection->Swap(&message1, &message2);
163 
164   TestUtil::ExpectClear(message1);
165   TestUtil::ExpectAllFieldsSet(message2);
166 }
167 
TEST(GeneratedMessageReflectionTest,SwapWithBothSet)168 TEST(GeneratedMessageReflectionTest, SwapWithBothSet) {
169   unittest::TestAllTypes message1;
170   unittest::TestAllTypes message2;
171 
172   TestUtil::SetAllFields(&message1);
173   TestUtil::SetAllFields(&message2);
174   TestUtil::ModifyRepeatedFields(&message2);
175 
176   const Reflection* reflection = message1.GetReflection();
177   reflection->Swap(&message1, &message2);
178 
179   TestUtil::ExpectRepeatedFieldsModified(message1);
180   TestUtil::ExpectAllFieldsSet(message2);
181 
182   message1.set_optional_int32(532819);
183 
184   reflection->Swap(&message1, &message2);
185 
186   EXPECT_EQ(532819, message2.optional_int32());
187 }
188 
TEST(GeneratedMessageReflectionTest,SwapExtensions)189 TEST(GeneratedMessageReflectionTest, SwapExtensions) {
190   unittest::TestAllExtensions message1;
191   unittest::TestAllExtensions message2;
192 
193   TestUtil::SetAllExtensions(&message1);
194 
195   const Reflection* reflection = message1.GetReflection();
196   reflection->Swap(&message1, &message2);
197 
198   TestUtil::ExpectExtensionsClear(message1);
199   TestUtil::ExpectAllExtensionsSet(message2);
200 }
201 
TEST(GeneratedMessageReflectionTest,SwapUnknown)202 TEST(GeneratedMessageReflectionTest, SwapUnknown) {
203   unittest::TestEmptyMessage message1, message2;
204 
205   message1.mutable_unknown_fields()->AddVarint(1234, 1);
206 
207   EXPECT_EQ(1, message1.unknown_fields().field_count());
208   EXPECT_EQ(0, message2.unknown_fields().field_count());
209   const Reflection* reflection = message1.GetReflection();
210   reflection->Swap(&message1, &message2);
211   EXPECT_EQ(0, message1.unknown_fields().field_count());
212   EXPECT_EQ(1, message2.unknown_fields().field_count());
213 }
214 
TEST(GeneratedMessageReflectionTest,SwapFields)215 TEST(GeneratedMessageReflectionTest, SwapFields) {
216   unittest::TestAllTypes message1, message2;
217   message1.set_optional_double(12.3);
218   message1.mutable_repeated_int32()->Add(10);
219   message1.mutable_repeated_int32()->Add(20);
220 
221   message2.set_optional_string("hello");
222   message2.mutable_repeated_int64()->Add(30);
223 
224   std::vector<const FieldDescriptor*> fields;
225   const Descriptor* descriptor = message1.GetDescriptor();
226   fields.push_back(descriptor->FindFieldByName("optional_double"));
227   fields.push_back(descriptor->FindFieldByName("repeated_int32"));
228   fields.push_back(descriptor->FindFieldByName("optional_string"));
229   fields.push_back(descriptor->FindFieldByName("optional_uint64"));
230 
231   const Reflection* reflection = message1.GetReflection();
232   reflection->SwapFields(&message1, &message2, fields);
233 
234   EXPECT_FALSE(message1.has_optional_double());
235   EXPECT_EQ(0, message1.repeated_int32_size());
236   EXPECT_TRUE(message1.has_optional_string());
237   EXPECT_EQ("hello", message1.optional_string());
238   EXPECT_EQ(0, message1.repeated_int64_size());
239   EXPECT_FALSE(message1.has_optional_uint64());
240 
241   EXPECT_TRUE(message2.has_optional_double());
242   EXPECT_EQ(12.3, message2.optional_double());
243   EXPECT_EQ(2, message2.repeated_int32_size());
244   EXPECT_EQ(10, message2.repeated_int32(0));
245   EXPECT_EQ(20, message2.repeated_int32(1));
246   EXPECT_FALSE(message2.has_optional_string());
247   EXPECT_EQ(1, message2.repeated_int64_size());
248   EXPECT_FALSE(message2.has_optional_uint64());
249 }
250 
TEST(GeneratedMessageReflectionTest,SwapFieldsAll)251 TEST(GeneratedMessageReflectionTest, SwapFieldsAll) {
252   unittest::TestAllTypes message1;
253   unittest::TestAllTypes message2;
254 
255   TestUtil::SetAllFields(&message2);
256 
257   std::vector<const FieldDescriptor*> fields;
258   const Reflection* reflection = message1.GetReflection();
259   reflection->ListFields(message2, &fields);
260   reflection->SwapFields(&message1, &message2, fields);
261 
262   TestUtil::ExpectAllFieldsSet(message1);
263   TestUtil::ExpectClear(message2);
264 }
265 
TEST(GeneratedMessageReflectionTest,SwapFieldsAllExtension)266 TEST(GeneratedMessageReflectionTest, SwapFieldsAllExtension) {
267   unittest::TestAllExtensions message1;
268   unittest::TestAllExtensions message2;
269 
270   TestUtil::SetAllExtensions(&message1);
271 
272   std::vector<const FieldDescriptor*> fields;
273   const Reflection* reflection = message1.GetReflection();
274   reflection->ListFields(message1, &fields);
275   reflection->SwapFields(&message1, &message2, fields);
276 
277   TestUtil::ExpectExtensionsClear(message1);
278   TestUtil::ExpectAllExtensionsSet(message2);
279 }
280 
TEST(GeneratedMessageReflectionTest,SwapOneof)281 TEST(GeneratedMessageReflectionTest, SwapOneof) {
282   unittest::TestOneof2 message1, message2;
283   TestUtil::SetOneof1(&message1);
284 
285   const Reflection* reflection = message1.GetReflection();
286   reflection->Swap(&message1, &message2);
287 
288   TestUtil::ExpectOneofClear(message1);
289   TestUtil::ExpectOneofSet1(message2);
290 }
291 
TEST(GeneratedMessageReflectionTest,SwapOneofBothSet)292 TEST(GeneratedMessageReflectionTest, SwapOneofBothSet) {
293   unittest::TestOneof2 message1, message2;
294   TestUtil::SetOneof1(&message1);
295   TestUtil::SetOneof2(&message2);
296 
297   const Reflection* reflection = message1.GetReflection();
298   reflection->Swap(&message1, &message2);
299 
300   TestUtil::ExpectOneofSet2(message1);
301   TestUtil::ExpectOneofSet1(message2);
302 }
303 
TEST(GeneratedMessageReflectionTest,SwapFieldsOneof)304 TEST(GeneratedMessageReflectionTest, SwapFieldsOneof) {
305   unittest::TestOneof2 message1, message2;
306   TestUtil::SetOneof1(&message1);
307 
308   std::vector<const FieldDescriptor*> fields;
309   const Descriptor* descriptor = message1.GetDescriptor();
310   for (int i = 0; i < descriptor->field_count(); i++) {
311     fields.push_back(descriptor->field(i));
312   }
313   const Reflection* reflection = message1.GetReflection();
314   reflection->SwapFields(&message1, &message2, fields);
315 
316   TestUtil::ExpectOneofClear(message1);
317   TestUtil::ExpectOneofSet1(message2);
318 }
319 
TEST(GeneratedMessageReflectionTest,RemoveLast)320 TEST(GeneratedMessageReflectionTest, RemoveLast) {
321   unittest::TestAllTypes message;
322   TestUtil::ReflectionTester reflection_tester(
323       unittest::TestAllTypes::descriptor());
324 
325   TestUtil::SetAllFields(&message);
326 
327   reflection_tester.RemoveLastRepeatedsViaReflection(&message);
328 
329   TestUtil::ExpectLastRepeatedsRemoved(message);
330 }
331 
TEST(GeneratedMessageReflectionTest,RemoveLastExtensions)332 TEST(GeneratedMessageReflectionTest, RemoveLastExtensions) {
333   unittest::TestAllExtensions message;
334   TestUtil::ReflectionTester reflection_tester(
335       unittest::TestAllExtensions::descriptor());
336 
337   TestUtil::SetAllExtensions(&message);
338 
339   reflection_tester.RemoveLastRepeatedsViaReflection(&message);
340 
341   TestUtil::ExpectLastRepeatedExtensionsRemoved(message);
342 }
343 
TEST(GeneratedMessageReflectionTest,ReleaseLast)344 TEST(GeneratedMessageReflectionTest, ReleaseLast) {
345   unittest::TestAllTypes message;
346   const Descriptor* descriptor = message.GetDescriptor();
347   TestUtil::ReflectionTester reflection_tester(descriptor);
348 
349   TestUtil::SetAllFields(&message);
350 
351   reflection_tester.ReleaseLastRepeatedsViaReflection(&message, false);
352 
353   TestUtil::ExpectLastRepeatedsReleased(message);
354 
355   // Now test that we actually release the right message.
356   message.Clear();
357   TestUtil::SetAllFields(&message);
358   ASSERT_EQ(2, message.repeated_foreign_message_size());
359   const protobuf_unittest::ForeignMessage* expected =
360       message.mutable_repeated_foreign_message(1);
361   std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
362       &message, descriptor->FindFieldByName("repeated_foreign_message")));
363   EXPECT_EQ(expected, released.get());
364 }
365 
TEST(GeneratedMessageReflectionTest,ReleaseLastExtensions)366 TEST(GeneratedMessageReflectionTest, ReleaseLastExtensions) {
367   unittest::TestAllExtensions message;
368   const Descriptor* descriptor = message.GetDescriptor();
369   TestUtil::ReflectionTester reflection_tester(descriptor);
370 
371   TestUtil::SetAllExtensions(&message);
372 
373   reflection_tester.ReleaseLastRepeatedsViaReflection(&message, true);
374 
375   TestUtil::ExpectLastRepeatedExtensionsReleased(message);
376 
377   // Now test that we actually release the right message.
378   message.Clear();
379   TestUtil::SetAllExtensions(&message);
380   ASSERT_EQ(
381       2, message.ExtensionSize(unittest::repeated_foreign_message_extension));
382   const protobuf_unittest::ForeignMessage* expected =
383       message.MutableExtension(unittest::repeated_foreign_message_extension, 1);
384   std::unique_ptr<Message> released(message.GetReflection()->ReleaseLast(
385       &message, descriptor->file()->FindExtensionByName(
386                     "repeated_foreign_message_extension")));
387   EXPECT_EQ(expected, released.get());
388 }
389 
TEST(GeneratedMessageReflectionTest,SwapRepeatedElements)390 TEST(GeneratedMessageReflectionTest, SwapRepeatedElements) {
391   unittest::TestAllTypes message;
392   TestUtil::ReflectionTester reflection_tester(
393       unittest::TestAllTypes::descriptor());
394 
395   TestUtil::SetAllFields(&message);
396 
397   // Swap and test that fields are all swapped.
398   reflection_tester.SwapRepeatedsViaReflection(&message);
399   TestUtil::ExpectRepeatedsSwapped(message);
400 
401   // Swap back and test that fields are all back to original values.
402   reflection_tester.SwapRepeatedsViaReflection(&message);
403   TestUtil::ExpectAllFieldsSet(message);
404 }
405 
TEST(GeneratedMessageReflectionTest,SwapRepeatedElementsExtension)406 TEST(GeneratedMessageReflectionTest, SwapRepeatedElementsExtension) {
407   unittest::TestAllExtensions message;
408   TestUtil::ReflectionTester reflection_tester(
409       unittest::TestAllExtensions::descriptor());
410 
411   TestUtil::SetAllExtensions(&message);
412 
413   // Swap and test that fields are all swapped.
414   reflection_tester.SwapRepeatedsViaReflection(&message);
415   TestUtil::ExpectRepeatedExtensionsSwapped(message);
416 
417   // Swap back and test that fields are all back to original values.
418   reflection_tester.SwapRepeatedsViaReflection(&message);
419   TestUtil::ExpectAllExtensionsSet(message);
420 }
421 
TEST(GeneratedMessageReflectionTest,Extensions)422 TEST(GeneratedMessageReflectionTest, Extensions) {
423   // Set every extension to a unique value then go back and check all those
424   // values.
425   unittest::TestAllExtensions message;
426   TestUtil::ReflectionTester reflection_tester(
427       unittest::TestAllExtensions::descriptor());
428 
429   reflection_tester.SetAllFieldsViaReflection(&message);
430   TestUtil::ExpectAllExtensionsSet(message);
431   reflection_tester.ExpectAllFieldsSetViaReflection(message);
432 
433   reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
434   TestUtil::ExpectRepeatedExtensionsModified(message);
435 }
436 
TEST(GeneratedMessageReflectionTest,FindExtensionTypeByNumber)437 TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
438   const Reflection* reflection =
439       unittest::TestAllExtensions::default_instance().GetReflection();
440 
441   const FieldDescriptor* extension1 =
442       unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
443           "optional_int32_extension");
444   const FieldDescriptor* extension2 =
445       unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
446           "repeated_string_extension");
447 
448   EXPECT_EQ(extension1,
449             reflection->FindKnownExtensionByNumber(extension1->number()));
450   EXPECT_EQ(extension2,
451             reflection->FindKnownExtensionByNumber(extension2->number()));
452 
453   // Non-existent extension.
454   EXPECT_TRUE(reflection->FindKnownExtensionByNumber(62341) == NULL);
455 
456   // Extensions of TestAllExtensions should not show up as extensions of
457   // other types.
458   EXPECT_TRUE(unittest::TestAllTypes::default_instance()
459                   .GetReflection()
460                   ->FindKnownExtensionByNumber(extension1->number()) == NULL);
461 }
462 
TEST(GeneratedMessageReflectionTest,FindKnownExtensionByName)463 TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
464   const Reflection* reflection =
465       unittest::TestAllExtensions::default_instance().GetReflection();
466 
467   const FieldDescriptor* extension1 =
468       unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
469           "optional_int32_extension");
470   const FieldDescriptor* extension2 =
471       unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
472           "repeated_string_extension");
473 
474   EXPECT_EQ(extension1,
475             reflection->FindKnownExtensionByName(extension1->full_name()));
476   EXPECT_EQ(extension2,
477             reflection->FindKnownExtensionByName(extension2->full_name()));
478 
479   // Non-existent extension.
480   EXPECT_TRUE(reflection->FindKnownExtensionByName("no_such_ext") == NULL);
481 
482   // Extensions of TestAllExtensions should not show up as extensions of
483   // other types.
484   EXPECT_TRUE(unittest::TestAllTypes::default_instance()
485                   .GetReflection()
486                   ->FindKnownExtensionByName(extension1->full_name()) == NULL);
487 }
488 
TEST(GeneratedMessageReflectionTest,SetAllocatedMessageTest)489 TEST(GeneratedMessageReflectionTest, SetAllocatedMessageTest) {
490   unittest::TestAllTypes from_message1;
491   unittest::TestAllTypes from_message2;
492   unittest::TestAllTypes to_message;
493   TestUtil::ReflectionTester reflection_tester(
494       unittest::TestAllTypes::descriptor());
495   reflection_tester.SetAllFieldsViaReflection(&from_message1);
496   reflection_tester.SetAllFieldsViaReflection(&from_message2);
497 
498   // Before moving fields, we expect the nested messages to be NULL.
499   reflection_tester.ExpectMessagesReleasedViaReflection(
500       &to_message, TestUtil::ReflectionTester::IS_NULL);
501 
502   // After fields are moved we should get non-NULL releases.
503   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
504       &from_message1, &to_message);
505   reflection_tester.ExpectMessagesReleasedViaReflection(
506       &to_message, TestUtil::ReflectionTester::NOT_NULL);
507 
508   // Another move to make sure that we can SetAllocated several times.
509   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
510       &from_message2, &to_message);
511   reflection_tester.ExpectMessagesReleasedViaReflection(
512       &to_message, TestUtil::ReflectionTester::NOT_NULL);
513 
514   // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
515   // releases to be NULL again.
516   reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
517       &to_message);
518   reflection_tester.ExpectMessagesReleasedViaReflection(
519       &to_message, TestUtil::ReflectionTester::IS_NULL);
520 }
521 
TEST(GeneratedMessageReflectionTest,SetAllocatedMessageOnArenaTest)522 TEST(GeneratedMessageReflectionTest, SetAllocatedMessageOnArenaTest) {
523   unittest::TestAllTypes from_message1;
524   unittest::TestAllTypes from_message2;
525   Arena arena;
526   unittest::TestAllTypes* to_message =
527       Arena::CreateMessage<unittest::TestAllTypes>(&arena);
528   TestUtil::ReflectionTester reflection_tester(
529       unittest::TestAllTypes::descriptor());
530   reflection_tester.SetAllFieldsViaReflection(&from_message1);
531   reflection_tester.SetAllFieldsViaReflection(&from_message2);
532 
533   // Before moving fields, we expect the nested messages to be NULL.
534   reflection_tester.ExpectMessagesReleasedViaReflection(
535       to_message, TestUtil::ReflectionTester::IS_NULL);
536 
537   // After fields are moved we should get non-NULL releases.
538   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
539       &from_message1, to_message);
540   reflection_tester.ExpectMessagesReleasedViaReflection(
541       to_message, TestUtil::ReflectionTester::NOT_NULL);
542 
543   // Another move to make sure that we can SetAllocated several times.
544   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
545       &from_message2, to_message);
546   reflection_tester.ExpectMessagesReleasedViaReflection(
547       to_message, TestUtil::ReflectionTester::NOT_NULL);
548 
549   // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
550   // releases to be NULL again.
551   reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
552       to_message);
553   reflection_tester.ExpectMessagesReleasedViaReflection(
554       to_message, TestUtil::ReflectionTester::IS_NULL);
555 }
556 
TEST(GeneratedMessageReflectionTest,SetAllocatedExtensionMessageTest)557 TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageTest) {
558   unittest::TestAllExtensions from_message1;
559   unittest::TestAllExtensions from_message2;
560   unittest::TestAllExtensions to_message;
561   TestUtil::ReflectionTester reflection_tester(
562       unittest::TestAllExtensions::descriptor());
563   reflection_tester.SetAllFieldsViaReflection(&from_message1);
564   reflection_tester.SetAllFieldsViaReflection(&from_message2);
565 
566   // Before moving fields, we expect the nested messages to be NULL.
567   reflection_tester.ExpectMessagesReleasedViaReflection(
568       &to_message, TestUtil::ReflectionTester::IS_NULL);
569 
570   // After fields are moved we should get non-NULL releases.
571   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
572       &from_message1, &to_message);
573   reflection_tester.ExpectMessagesReleasedViaReflection(
574       &to_message, TestUtil::ReflectionTester::NOT_NULL);
575 
576   // Another move to make sure that we can SetAllocated several times.
577   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
578       &from_message2, &to_message);
579   reflection_tester.ExpectMessagesReleasedViaReflection(
580       &to_message, TestUtil::ReflectionTester::NOT_NULL);
581 
582   // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
583   // releases to be NULL again.
584   reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
585       &to_message);
586   reflection_tester.ExpectMessagesReleasedViaReflection(
587       &to_message, TestUtil::ReflectionTester::IS_NULL);
588 }
589 
TEST(GeneratedMessageReflectionTest,SetAllocatedExtensionMessageOnArenaTest)590 TEST(GeneratedMessageReflectionTest, SetAllocatedExtensionMessageOnArenaTest) {
591   Arena arena;
592   unittest::TestAllExtensions* to_message =
593       Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
594   unittest::TestAllExtensions from_message1;
595   unittest::TestAllExtensions from_message2;
596   TestUtil::ReflectionTester reflection_tester(
597       unittest::TestAllExtensions::descriptor());
598   reflection_tester.SetAllFieldsViaReflection(&from_message1);
599   reflection_tester.SetAllFieldsViaReflection(&from_message2);
600 
601   // Before moving fields, we expect the nested messages to be NULL.
602   reflection_tester.ExpectMessagesReleasedViaReflection(
603       to_message, TestUtil::ReflectionTester::IS_NULL);
604 
605   // After fields are moved we should get non-NULL releases.
606   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
607       &from_message1, to_message);
608   reflection_tester.ExpectMessagesReleasedViaReflection(
609       to_message, TestUtil::ReflectionTester::NOT_NULL);
610 
611   // Another move to make sure that we can SetAllocated several times.
612   reflection_tester.SetAllocatedOptionalMessageFieldsToMessageViaReflection(
613       &from_message2, to_message);
614   reflection_tester.ExpectMessagesReleasedViaReflection(
615       to_message, TestUtil::ReflectionTester::NOT_NULL);
616 
617   // After SetAllocatedOptionalMessageFieldsToNullViaReflection() we expect the
618   // releases to be NULL again.
619   reflection_tester.SetAllocatedOptionalMessageFieldsToNullViaReflection(
620       to_message);
621   reflection_tester.ExpectMessagesReleasedViaReflection(
622       to_message, TestUtil::ReflectionTester::IS_NULL);
623 }
624 
TEST(GeneratedMessageReflectionTest,AddRepeatedMessage)625 TEST(GeneratedMessageReflectionTest, AddRepeatedMessage) {
626   unittest::TestAllTypes message;
627 
628   const Reflection* reflection = message.GetReflection();
629   const Reflection* nested_reflection =
630       unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
631 
632   const FieldDescriptor* nested_bb =
633       unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
634           "bb");
635 
636   Message* nested =
637       reflection->AddMessage(&message, F("repeated_nested_message"));
638   nested_reflection->SetInt32(nested, nested_bb, 11);
639 
640   EXPECT_EQ(11, message.repeated_nested_message(0).bb());
641 }
642 
TEST(GeneratedMessageReflectionTest,MutableRepeatedMessage)643 TEST(GeneratedMessageReflectionTest, MutableRepeatedMessage) {
644   unittest::TestAllTypes message;
645 
646   const Reflection* reflection = message.GetReflection();
647   const Reflection* nested_reflection =
648       unittest::TestAllTypes::NestedMessage::default_instance().GetReflection();
649 
650   const FieldDescriptor* nested_bb =
651       unittest::TestAllTypes::NestedMessage::descriptor()->FindFieldByName(
652           "bb");
653 
654   message.add_repeated_nested_message()->set_bb(12);
655 
656   Message* nested = reflection->MutableRepeatedMessage(
657       &message, F("repeated_nested_message"), 0);
658   EXPECT_EQ(12, nested_reflection->GetInt32(*nested, nested_bb));
659   nested_reflection->SetInt32(nested, nested_bb, 13);
660   EXPECT_EQ(13, message.repeated_nested_message(0).bb());
661 }
662 
TEST(GeneratedMessageReflectionTest,AddAllocatedMessage)663 TEST(GeneratedMessageReflectionTest, AddAllocatedMessage) {
664   unittest::TestAllTypes message;
665 
666   const Reflection* reflection = message.GetReflection();
667 
668   unittest::TestAllTypes::NestedMessage* nested =
669       new unittest::TestAllTypes::NestedMessage();
670   nested->set_bb(11);
671   reflection->AddAllocatedMessage(&message, F("repeated_nested_message"),
672                                   nested);
673   EXPECT_EQ(1, message.repeated_nested_message_size());
674   EXPECT_EQ(11, message.repeated_nested_message(0).bb());
675 }
676 
TEST(GeneratedMessageReflectionTest,ListFieldsOneOf)677 TEST(GeneratedMessageReflectionTest, ListFieldsOneOf) {
678   unittest::TestOneof2 message;
679   TestUtil::SetOneof1(&message);
680 
681   const Reflection* reflection = message.GetReflection();
682   std::vector<const FieldDescriptor*> fields;
683   reflection->ListFields(message, &fields);
684   EXPECT_EQ(4, fields.size());
685 }
686 
TEST(GeneratedMessageReflectionTest,Oneof)687 TEST(GeneratedMessageReflectionTest, Oneof) {
688   unittest::TestOneof2 message;
689   const Descriptor* descriptor = message.GetDescriptor();
690   const Reflection* reflection = message.GetReflection();
691 
692   // Check default values.
693   EXPECT_EQ(
694       0, reflection->GetInt32(message, descriptor->FindFieldByName("foo_int")));
695   EXPECT_EQ("", reflection->GetString(
696                     message, descriptor->FindFieldByName("foo_string")));
697   EXPECT_EQ("", reflection->GetString(message,
698                                       descriptor->FindFieldByName("foo_cord")));
699   EXPECT_EQ("", reflection->GetString(
700                     message, descriptor->FindFieldByName("foo_string_piece")));
701   EXPECT_EQ("", reflection->GetString(
702                     message, descriptor->FindFieldByName("foo_bytes")));
703   EXPECT_EQ(
704       unittest::TestOneof2::FOO,
705       reflection->GetEnum(message, descriptor->FindFieldByName("foo_enum"))
706           ->number());
707   EXPECT_EQ(&unittest::TestOneof2::NestedMessage::default_instance(),
708             &reflection->GetMessage(
709                 message, descriptor->FindFieldByName("foo_message")));
710   EXPECT_EQ(&unittest::TestOneof2::FooGroup::default_instance(),
711             &reflection->GetMessage(message,
712                                     descriptor->FindFieldByName("foogroup")));
713   EXPECT_NE(&unittest::TestOneof2::FooGroup::default_instance(),
714             &reflection->GetMessage(
715                 message, descriptor->FindFieldByName("foo_lazy_message")));
716   EXPECT_EQ(
717       5, reflection->GetInt32(message, descriptor->FindFieldByName("bar_int")));
718   EXPECT_EQ("STRING", reflection->GetString(
719                           message, descriptor->FindFieldByName("bar_string")));
720   EXPECT_EQ("CORD", reflection->GetString(
721                         message, descriptor->FindFieldByName("bar_cord")));
722   EXPECT_EQ("SPIECE",
723             reflection->GetString(
724                 message, descriptor->FindFieldByName("bar_string_piece")));
725   EXPECT_EQ("BYTES", reflection->GetString(
726                          message, descriptor->FindFieldByName("bar_bytes")));
727   EXPECT_EQ(
728       unittest::TestOneof2::BAR,
729       reflection->GetEnum(message, descriptor->FindFieldByName("bar_enum"))
730           ->number());
731 
732   // Check Set functions.
733   reflection->SetInt32(&message, descriptor->FindFieldByName("foo_int"), 123);
734   EXPECT_EQ(123, reflection->GetInt32(message,
735                                       descriptor->FindFieldByName("foo_int")));
736   reflection->SetString(&message, descriptor->FindFieldByName("foo_string"),
737                         "abc");
738   EXPECT_EQ("abc", reflection->GetString(
739                        message, descriptor->FindFieldByName("foo_string")));
740   reflection->SetString(&message, descriptor->FindFieldByName("foo_bytes"),
741                         "bytes");
742   EXPECT_EQ("bytes", reflection->GetString(
743                          message, descriptor->FindFieldByName("foo_bytes")));
744   reflection->SetString(&message, descriptor->FindFieldByName("bar_cord"),
745                         "change_cord");
746   EXPECT_EQ(
747       "change_cord",
748       reflection->GetString(message, descriptor->FindFieldByName("bar_cord")));
749   reflection->SetString(&message,
750                         descriptor->FindFieldByName("bar_string_piece"),
751                         "change_spiece");
752   EXPECT_EQ("change_spiece",
753             reflection->GetString(
754                 message, descriptor->FindFieldByName("bar_string_piece")));
755 
756   message.clear_foo();
757   message.clear_bar();
758   TestUtil::ExpectOneofClear(message);
759 }
760 
TEST(GeneratedMessageReflectionTest,SetAllocatedOneofMessageTest)761 TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageTest) {
762   unittest::TestOneof2 from_message1;
763   unittest::TestOneof2 from_message2;
764   unittest::TestOneof2 to_message;
765   const Descriptor* descriptor = unittest::TestOneof2::descriptor();
766   const Reflection* reflection = to_message.GetReflection();
767 
768   Message* released = reflection->ReleaseMessage(
769       &to_message, descriptor->FindFieldByName("foo_lazy_message"));
770   EXPECT_TRUE(released == NULL);
771   released = reflection->ReleaseMessage(
772       &to_message, descriptor->FindFieldByName("foo_message"));
773   EXPECT_TRUE(released == NULL);
774 
775   TestUtil::ReflectionTester::SetOneofViaReflection(&from_message1);
776   TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1);
777 
778   TestUtil::ReflectionTester::
779       SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1,
780                                                               &to_message);
781   const Message& sub_message = reflection->GetMessage(
782       to_message, descriptor->FindFieldByName("foo_lazy_message"));
783   released = reflection->ReleaseMessage(
784       &to_message, descriptor->FindFieldByName("foo_lazy_message"));
785   EXPECT_TRUE(released != NULL);
786   EXPECT_EQ(&sub_message, released);
787   delete released;
788 
789   TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2);
790 
791   reflection->MutableMessage(&from_message2,
792                              descriptor->FindFieldByName("foo_message"));
793 
794   TestUtil::ReflectionTester::
795       SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2,
796                                                               &to_message);
797 
798   const Message& sub_message2 = reflection->GetMessage(
799       to_message, descriptor->FindFieldByName("foo_message"));
800   released = reflection->ReleaseMessage(
801       &to_message, descriptor->FindFieldByName("foo_message"));
802   EXPECT_TRUE(released != NULL);
803   EXPECT_EQ(&sub_message2, released);
804   delete released;
805 }
806 
TEST(GeneratedMessageReflectionTest,SetAllocatedOneofMessageOnArenaTest)807 TEST(GeneratedMessageReflectionTest, SetAllocatedOneofMessageOnArenaTest) {
808   unittest::TestOneof2 from_message1;
809   unittest::TestOneof2 from_message2;
810   Arena arena;
811   unittest::TestOneof2* to_message =
812       Arena::CreateMessage<unittest::TestOneof2>(&arena);
813   const Descriptor* descriptor = unittest::TestOneof2::descriptor();
814   const Reflection* reflection = to_message->GetReflection();
815 
816   Message* released = reflection->ReleaseMessage(
817       to_message, descriptor->FindFieldByName("foo_lazy_message"));
818   EXPECT_TRUE(released == NULL);
819   released = reflection->ReleaseMessage(
820       to_message, descriptor->FindFieldByName("foo_message"));
821   EXPECT_TRUE(released == NULL);
822 
823   TestUtil::ReflectionTester::SetOneofViaReflection(&from_message1);
824   TestUtil::ReflectionTester::ExpectOneofSetViaReflection(from_message1);
825 
826   TestUtil::ReflectionTester::
827       SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message1,
828                                                               to_message);
829   const Message& sub_message = reflection->GetMessage(
830       *to_message, descriptor->FindFieldByName("foo_lazy_message"));
831   released = reflection->ReleaseMessage(
832       to_message, descriptor->FindFieldByName("foo_lazy_message"));
833   EXPECT_TRUE(released != NULL);
834   // Since sub_message is arena allocated, releasing it results in copying it
835   // into new heap-allocated memory.
836   EXPECT_NE(&sub_message, released);
837   delete released;
838 
839   TestUtil::ReflectionTester::SetOneofViaReflection(&from_message2);
840 
841   reflection->MutableMessage(&from_message2,
842                              descriptor->FindFieldByName("foo_message"));
843 
844   TestUtil::ReflectionTester::
845       SetAllocatedOptionalMessageFieldsToMessageViaReflection(&from_message2,
846                                                               to_message);
847 
848   const Message& sub_message2 = reflection->GetMessage(
849       *to_message, descriptor->FindFieldByName("foo_message"));
850   released = reflection->ReleaseMessage(
851       to_message, descriptor->FindFieldByName("foo_message"));
852   EXPECT_TRUE(released != NULL);
853   // Since sub_message2 is arena allocated, releasing it results in copying it
854   // into new heap-allocated memory.
855   EXPECT_NE(&sub_message2, released);
856   delete released;
857 }
858 
TEST(GeneratedMessageReflectionTest,ReleaseMessageTest)859 TEST(GeneratedMessageReflectionTest, ReleaseMessageTest) {
860   unittest::TestAllTypes message;
861   TestUtil::ReflectionTester reflection_tester(
862       unittest::TestAllTypes::descriptor());
863 
864   // When nothing is set, we expect all released messages to be NULL.
865   reflection_tester.ExpectMessagesReleasedViaReflection(
866       &message, TestUtil::ReflectionTester::IS_NULL);
867 
868   // After fields are set we should get non-NULL releases.
869   reflection_tester.SetAllFieldsViaReflection(&message);
870   reflection_tester.ExpectMessagesReleasedViaReflection(
871       &message, TestUtil::ReflectionTester::NOT_NULL);
872 
873   // After Clear() we may or may not get a message from ReleaseMessage().
874   // This is implementation specific.
875   reflection_tester.SetAllFieldsViaReflection(&message);
876   message.Clear();
877   reflection_tester.ExpectMessagesReleasedViaReflection(
878       &message, TestUtil::ReflectionTester::CAN_BE_NULL);
879 
880   // Test a different code path for setting after releasing.
881   TestUtil::SetAllFields(&message);
882   TestUtil::ExpectAllFieldsSet(message);
883 }
884 
TEST(GeneratedMessageReflectionTest,ReleaseExtensionMessageTest)885 TEST(GeneratedMessageReflectionTest, ReleaseExtensionMessageTest) {
886   unittest::TestAllExtensions message;
887   TestUtil::ReflectionTester reflection_tester(
888       unittest::TestAllExtensions::descriptor());
889 
890   // When nothing is set, we expect all released messages to be NULL.
891   reflection_tester.ExpectMessagesReleasedViaReflection(
892       &message, TestUtil::ReflectionTester::IS_NULL);
893 
894   // After fields are set we should get non-NULL releases.
895   reflection_tester.SetAllFieldsViaReflection(&message);
896   reflection_tester.ExpectMessagesReleasedViaReflection(
897       &message, TestUtil::ReflectionTester::NOT_NULL);
898 
899   // After Clear() we may or may not get a message from ReleaseMessage().
900   // This is implementation specific.
901   reflection_tester.SetAllFieldsViaReflection(&message);
902   message.Clear();
903   reflection_tester.ExpectMessagesReleasedViaReflection(
904       &message, TestUtil::ReflectionTester::CAN_BE_NULL);
905 
906   // Test a different code path for setting after releasing.
907   TestUtil::SetAllExtensions(&message);
908   TestUtil::ExpectAllExtensionsSet(message);
909 }
910 
TEST(GeneratedMessageReflectionTest,ReleaseOneofMessageTest)911 TEST(GeneratedMessageReflectionTest, ReleaseOneofMessageTest) {
912   unittest::TestOneof2 message;
913   TestUtil::ReflectionTester::SetOneofViaReflection(&message);
914 
915   const Descriptor* descriptor = unittest::TestOneof2::descriptor();
916   const Reflection* reflection = message.GetReflection();
917   const Message& sub_message = reflection->GetMessage(
918       message, descriptor->FindFieldByName("foo_lazy_message"));
919   Message* released = reflection->ReleaseMessage(
920       &message, descriptor->FindFieldByName("foo_lazy_message"));
921 
922   EXPECT_TRUE(released != NULL);
923   EXPECT_EQ(&sub_message, released);
924   delete released;
925 
926   released = reflection->ReleaseMessage(
927       &message, descriptor->FindFieldByName("foo_lazy_message"));
928   EXPECT_TRUE(released == NULL);
929 }
930 
TEST(GeneratedMessageReflectionTest,ArenaReleaseMessageTest)931 TEST(GeneratedMessageReflectionTest, ArenaReleaseMessageTest) {
932   Arena arena;
933   unittest::TestAllTypes* message =
934       Arena::CreateMessage<unittest::TestAllTypes>(&arena);
935   TestUtil::ReflectionTester reflection_tester(
936       unittest::TestAllTypes::descriptor());
937 
938   // When nothing is set, we expect all released messages to be NULL.
939   reflection_tester.ExpectMessagesReleasedViaReflection(
940       message, TestUtil::ReflectionTester::IS_NULL);
941 
942   // After fields are set we should get non-NULL releases.
943   reflection_tester.SetAllFieldsViaReflection(message);
944   reflection_tester.ExpectMessagesReleasedViaReflection(
945       message, TestUtil::ReflectionTester::NOT_NULL);
946 
947   // After Clear() we may or may not get a message from ReleaseMessage().
948   // This is implementation specific.
949   reflection_tester.SetAllFieldsViaReflection(message);
950   message->Clear();
951   reflection_tester.ExpectMessagesReleasedViaReflection(
952       message, TestUtil::ReflectionTester::CAN_BE_NULL);
953 }
954 
TEST(GeneratedMessageReflectionTest,ArenaReleaseExtensionMessageTest)955 TEST(GeneratedMessageReflectionTest, ArenaReleaseExtensionMessageTest) {
956   Arena arena;
957   unittest::TestAllExtensions* message =
958       Arena::CreateMessage<unittest::TestAllExtensions>(&arena);
959   TestUtil::ReflectionTester reflection_tester(
960       unittest::TestAllExtensions::descriptor());
961 
962   // When nothing is set, we expect all released messages to be NULL.
963   reflection_tester.ExpectMessagesReleasedViaReflection(
964       message, TestUtil::ReflectionTester::IS_NULL);
965 
966   // After fields are set we should get non-NULL releases.
967   reflection_tester.SetAllFieldsViaReflection(message);
968   reflection_tester.ExpectMessagesReleasedViaReflection(
969       message, TestUtil::ReflectionTester::NOT_NULL);
970 
971   // After Clear() we may or may not get a message from ReleaseMessage().
972   // This is implementation specific.
973   reflection_tester.SetAllFieldsViaReflection(message);
974   message->Clear();
975   reflection_tester.ExpectMessagesReleasedViaReflection(
976       message, TestUtil::ReflectionTester::CAN_BE_NULL);
977 }
978 
TEST(GeneratedMessageReflectionTest,ArenaReleaseOneofMessageTest)979 TEST(GeneratedMessageReflectionTest, ArenaReleaseOneofMessageTest) {
980   Arena arena;
981   unittest::TestOneof2* message =
982       Arena::CreateMessage<unittest::TestOneof2>(&arena);
983   TestUtil::ReflectionTester::SetOneofViaReflection(message);
984 
985   const Descriptor* descriptor = unittest::TestOneof2::descriptor();
986   const Reflection* reflection = message->GetReflection();
987   Message* released = reflection->ReleaseMessage(
988       message, descriptor->FindFieldByName("foo_lazy_message"));
989 
990   EXPECT_TRUE(released != NULL);
991   delete released;
992 
993   released = reflection->ReleaseMessage(
994       message, descriptor->FindFieldByName("foo_lazy_message"));
995   EXPECT_TRUE(released == NULL);
996 }
997 
998 #ifdef PROTOBUF_HAS_DEATH_TEST
999 
TEST(GeneratedMessageReflectionTest,UsageErrors)1000 TEST(GeneratedMessageReflectionTest, UsageErrors) {
1001   unittest::TestAllTypes message;
1002   const Reflection* reflection = message.GetReflection();
1003   const Descriptor* descriptor = message.GetDescriptor();
1004 
1005 #define f(NAME) descriptor->FindFieldByName(NAME)
1006 
1007   // Testing every single failure mode would be too much work.  Let's just
1008   // check a few.
1009   EXPECT_DEATH(
1010       reflection->GetInt32(message,
1011                            descriptor->FindFieldByName("optional_int64")),
1012       "Protocol Buffer reflection usage error:\n"
1013       "  Method      : google::protobuf::Reflection::GetInt32\n"
1014       "  Message type: protobuf_unittest\\.TestAllTypes\n"
1015       "  Field       : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
1016       "  Problem     : Field is not the right type for this message:\n"
1017       "    Expected  : CPPTYPE_INT32\n"
1018       "    Field type: CPPTYPE_INT64");
1019   EXPECT_DEATH(reflection->GetInt32(
1020                    message, descriptor->FindFieldByName("repeated_int32")),
1021                "Protocol Buffer reflection usage error:\n"
1022                "  Method      : google::protobuf::Reflection::GetInt32\n"
1023                "  Message type: protobuf_unittest.TestAllTypes\n"
1024                "  Field       : protobuf_unittest.TestAllTypes.repeated_int32\n"
1025                "  Problem     : Field is repeated; the method requires a "
1026                "singular field.");
1027   EXPECT_DEATH(
1028       reflection->GetInt32(
1029           message,
1030           unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
1031       "Protocol Buffer reflection usage error:\n"
1032       "  Method      : google::protobuf::Reflection::GetInt32\n"
1033       "  Message type: protobuf_unittest.TestAllTypes\n"
1034       "  Field       : protobuf_unittest.ForeignMessage.c\n"
1035       "  Problem     : Field does not match message type.");
1036   EXPECT_DEATH(
1037       reflection->HasField(
1038           message,
1039           unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
1040       "Protocol Buffer reflection usage error:\n"
1041       "  Method      : google::protobuf::Reflection::HasField\n"
1042       "  Message type: protobuf_unittest.TestAllTypes\n"
1043       "  Field       : protobuf_unittest.ForeignMessage.c\n"
1044       "  Problem     : Field does not match message type.");
1045 
1046 #undef f
1047 }
1048 
1049 #endif  // PROTOBUF_HAS_DEATH_TEST
1050 
1051 
1052 }  // namespace
1053 }  // namespace protobuf
1054 }  // namespace google
1055