• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: kenton@google.com (Kenton Varda)
9 //  Based on original Protocol Buffers design by
10 //  Sanjay Ghemawat, Jeff Dean, and others.
11 
12 #ifndef GOOGLE_PROTOBUF_TEST_UTIL_H__
13 #define GOOGLE_PROTOBUF_TEST_UTIL_H__
14 
15 #include "absl/strings/string_view.h"
16 #include "google/protobuf/unittest.pb.h"
17 
18 #define UNITTEST ::protobuf_unittest
19 #define UNITTEST_IMPORT ::protobuf_unittest_import
20 // Must be included when the preprocessor symbols above are defined.
21 #include "google/protobuf/test_util.inc"
22 #undef UNITTEST
23 #undef UNITTEST_IMPORT
24 
25 // Must be included last.
26 #include "google/protobuf/port_def.inc"
27 
28 namespace google {
29 namespace protobuf {
30 // This file doesn't use these declarations, but some .cc files do.
31 namespace unittest = ::protobuf_unittest;
32 namespace unittest_import = ::protobuf_unittest_import;
33 
34 namespace TestUtil {
35 
36 class ReflectionTester {
37  public:
38   // base_descriptor must be a descriptor for TestAllTypes or
39   // TestAllExtensions.  In the former case, ReflectionTester fetches from
40   // it the FieldDescriptors needed to use the reflection interface.  In
41   // the latter case, ReflectionTester searches for extension fields in
42   // its file.
43   explicit ReflectionTester(const Descriptor* base_descriptor);
44   ReflectionTester(const ReflectionTester&) = delete;
45   ReflectionTester& operator=(const ReflectionTester&) = delete;
46 
47   void SetAllFieldsViaReflection(Message* message);
48   void ModifyRepeatedFieldsViaReflection(Message* message);
49   void ExpectAllFieldsSetViaReflection(const Message& message);
50   void ExpectClearViaReflection(const Message& message);
51 
52   void SetPackedFieldsViaReflection(Message* message);
53   void ExpectPackedFieldsSetViaReflection(const Message& message);
54 
55   void RemoveLastRepeatedsViaReflection(Message* message);
56   void ReleaseLastRepeatedsViaReflection(Message* message,
57                                          bool expect_extensions_notnull);
58   void SwapRepeatedsViaReflection(Message* message);
59   void SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message);
60   static void SetAllocatedOptionalMessageFieldsToMessageViaReflection(
61       Message* from_message, Message* to_message);
62 
63   enum MessageReleaseState {
64     IS_NULL,
65     CAN_BE_NULL,
66     NOT_NULL,
67   };
68   void ExpectMessagesReleasedViaReflection(
69       Message* message, MessageReleaseState expected_release_state);
70 
71   // Set and check functions for TestOneof2 messages. No need to construct
72   // the ReflectionTester by TestAllTypes nor TestAllExtensions.
73   static void SetOneofViaReflection(Message* message);
74   static void ExpectOneofSetViaReflection(const Message& message);
75 
76  private:
77   const FieldDescriptor* F(const std::string& name);
78 
79   const Descriptor* base_descriptor_;
80 
81   const FieldDescriptor* group_a_;
82   const FieldDescriptor* repeated_group_a_;
83   const FieldDescriptor* nested_b_;
84   const FieldDescriptor* foreign_c_;
85   const FieldDescriptor* import_d_;
86   const FieldDescriptor* import_e_;
87 
88   const EnumValueDescriptor* nested_foo_;
89   const EnumValueDescriptor* nested_bar_;
90   const EnumValueDescriptor* nested_baz_;
91   const EnumValueDescriptor* foreign_foo_;
92   const EnumValueDescriptor* foreign_bar_;
93   const EnumValueDescriptor* foreign_baz_;
94   const EnumValueDescriptor* import_foo_;
95   const EnumValueDescriptor* import_bar_;
96   const EnumValueDescriptor* import_baz_;
97 
98   // We have to split this into three function otherwise it creates a stack
99   // frame so large that it triggers a warning.
100   void ExpectAllFieldsSetViaReflection1(const Message& message);
101   void ExpectAllFieldsSetViaReflection2(const Message& message);
102   void ExpectAllFieldsSetViaReflection3(const Message& message);
103 };
104 
ReflectionTester(const Descriptor * base_descriptor)105 inline TestUtil::ReflectionTester::ReflectionTester(
106     const Descriptor* base_descriptor)
107     : base_descriptor_(base_descriptor) {
108   const DescriptorPool* pool = base_descriptor->file()->pool();
109   const absl::string_view package = base_descriptor->file()->package();
110   const FieldDescriptor* import_descriptor = pool->FindFieldByName(
111       absl::StrCat(package, ".TestAllTypes.optional_import_message"));
112   const absl::string_view import_package =
113       import_descriptor->message_type()->file()->package();
114 
115   nested_b_ = pool->FindFieldByName(
116       absl::StrCat(package, ".TestAllTypes.NestedMessage.bb"));
117   foreign_c_ =
118       pool->FindFieldByName(absl::StrCat(package, ".ForeignMessage.c"));
119   import_d_ =
120       pool->FindFieldByName(absl::StrCat(import_package, ".ImportMessage.d"));
121   import_e_ = pool->FindFieldByName(
122       absl::StrCat(import_package, ".PublicImportMessage.e"));
123   nested_foo_ =
124       pool->FindEnumValueByName(absl::StrCat(package, ".TestAllTypes.FOO"));
125   nested_bar_ =
126       pool->FindEnumValueByName(absl::StrCat(package, ".TestAllTypes.BAR"));
127   nested_baz_ =
128       pool->FindEnumValueByName(absl::StrCat(package, ".TestAllTypes.BAZ"));
129   foreign_foo_ =
130       pool->FindEnumValueByName(absl::StrCat(package, ".FOREIGN_FOO"));
131   foreign_bar_ =
132       pool->FindEnumValueByName(absl::StrCat(package, ".FOREIGN_BAR"));
133   foreign_baz_ =
134       pool->FindEnumValueByName(absl::StrCat(package, ".FOREIGN_BAZ"));
135   import_foo_ =
136       pool->FindEnumValueByName(absl::StrCat(import_package, ".IMPORT_FOO"));
137   import_bar_ =
138       pool->FindEnumValueByName(absl::StrCat(import_package, ".IMPORT_BAR"));
139   import_baz_ =
140       pool->FindEnumValueByName(absl::StrCat(import_package, ".IMPORT_BAZ"));
141 
142   if (base_descriptor_->name() == "TestAllExtensions") {
143     group_a_ = pool->FindFieldByName(
144         absl::StrCat(package, ".OptionalGroup_extension.a"));
145     repeated_group_a_ = pool->FindFieldByName(
146         absl::StrCat(package, ".RepeatedGroup_extension.a"));
147   } else {
148     group_a_ = pool->FindFieldByName(
149         absl::StrCat(package, ".TestAllTypes.OptionalGroup.a"));
150     repeated_group_a_ = pool->FindFieldByName(
151         absl::StrCat(package, ".TestAllTypes.RepeatedGroup.a"));
152   }
153 
154   EXPECT_TRUE(group_a_ != nullptr);
155   EXPECT_TRUE(repeated_group_a_ != nullptr);
156   EXPECT_TRUE(nested_b_ != nullptr);
157   EXPECT_TRUE(foreign_c_ != nullptr);
158   EXPECT_TRUE(import_d_ != nullptr);
159   EXPECT_TRUE(import_e_ != nullptr);
160   EXPECT_TRUE(nested_foo_ != nullptr);
161   EXPECT_TRUE(nested_bar_ != nullptr);
162   EXPECT_TRUE(nested_baz_ != nullptr);
163   EXPECT_TRUE(foreign_foo_ != nullptr);
164   EXPECT_TRUE(foreign_bar_ != nullptr);
165   EXPECT_TRUE(foreign_baz_ != nullptr);
166   EXPECT_TRUE(import_foo_ != nullptr);
167   EXPECT_TRUE(import_bar_ != nullptr);
168   EXPECT_TRUE(import_baz_ != nullptr);
169 }
170 
171 // Shorthand to get a FieldDescriptor for a field of TestAllTypes.
F(const std::string & name)172 inline const FieldDescriptor* TestUtil::ReflectionTester::F(
173     const std::string& name) {
174   const FieldDescriptor* result = nullptr;
175   if (base_descriptor_->name() == "TestAllExtensions" ||
176       base_descriptor_->name() == "TestPackedExtensions") {
177     result = base_descriptor_->file()->FindExtensionByName(
178         absl::StrCat(name, "_extension"));
179   } else {
180     result = base_descriptor_->FindFieldByName(name);
181   }
182   ABSL_CHECK(result != nullptr);
183   return result;
184 }
185 
186 // -------------------------------------------------------------------
187 
SetAllFieldsViaReflection(Message * message)188 inline void TestUtil::ReflectionTester::SetAllFieldsViaReflection(
189     Message* message) {
190   const Reflection* reflection = message->GetReflection();
191   Message* sub_message;
192 
193   reflection->SetInt32(message, F("optional_int32"), 101);
194   reflection->SetInt64(message, F("optional_int64"), 102);
195   reflection->SetUInt32(message, F("optional_uint32"), 103);
196   reflection->SetUInt64(message, F("optional_uint64"), 104);
197   reflection->SetInt32(message, F("optional_sint32"), 105);
198   reflection->SetInt64(message, F("optional_sint64"), 106);
199   reflection->SetUInt32(message, F("optional_fixed32"), 107);
200   reflection->SetUInt64(message, F("optional_fixed64"), 108);
201   reflection->SetInt32(message, F("optional_sfixed32"), 109);
202   reflection->SetInt64(message, F("optional_sfixed64"), 110);
203   reflection->SetFloat(message, F("optional_float"), 111);
204   reflection->SetDouble(message, F("optional_double"), 112);
205   reflection->SetBool(message, F("optional_bool"), true);
206   reflection->SetString(message, F("optional_string"), "115");
207   reflection->SetString(message, F("optional_bytes"), "116");
208 
209   sub_message = reflection->MutableMessage(message, F("optionalgroup"));
210   sub_message->GetReflection()->SetInt32(sub_message, group_a_, 117);
211   sub_message =
212       reflection->MutableMessage(message, F("optional_nested_message"));
213   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 118);
214   sub_message =
215       reflection->MutableMessage(message, F("optional_foreign_message"));
216   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 119);
217   sub_message =
218       reflection->MutableMessage(message, F("optional_import_message"));
219   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 120);
220 
221   reflection->SetEnum(message, F("optional_nested_enum"), nested_baz_);
222   reflection->SetEnum(message, F("optional_foreign_enum"), foreign_baz_);
223   reflection->SetEnum(message, F("optional_import_enum"), import_baz_);
224 
225   reflection->SetString(message, F("optional_string_piece"), "124");
226   reflection->SetString(message, F("optional_cord"), "125");
227   reflection->SetString(message, F("optional_bytes_cord"),
228                         "optional bytes cord");
229 
230   sub_message =
231       reflection->MutableMessage(message, F("optional_public_import_message"));
232   sub_message->GetReflection()->SetInt32(sub_message, import_e_, 126);
233 
234   sub_message = reflection->MutableMessage(message, F("optional_lazy_message"));
235   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 127);
236 
237   sub_message = reflection->MutableMessage(
238       message, F("optional_unverified_lazy_message"));
239   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 128);
240 
241   // -----------------------------------------------------------------
242 
243   reflection->AddInt32(message, F("repeated_int32"), 201);
244   reflection->AddInt64(message, F("repeated_int64"), 202);
245   reflection->AddUInt32(message, F("repeated_uint32"), 203);
246   reflection->AddUInt64(message, F("repeated_uint64"), 204);
247   reflection->AddInt32(message, F("repeated_sint32"), 205);
248   reflection->AddInt64(message, F("repeated_sint64"), 206);
249   reflection->AddUInt32(message, F("repeated_fixed32"), 207);
250   reflection->AddUInt64(message, F("repeated_fixed64"), 208);
251   reflection->AddInt32(message, F("repeated_sfixed32"), 209);
252   reflection->AddInt64(message, F("repeated_sfixed64"), 210);
253   reflection->AddFloat(message, F("repeated_float"), 211);
254   reflection->AddDouble(message, F("repeated_double"), 212);
255   reflection->AddBool(message, F("repeated_bool"), true);
256   reflection->AddString(message, F("repeated_string"), "215");
257   reflection->AddString(message, F("repeated_bytes"), "216");
258 
259   sub_message = reflection->AddMessage(message, F("repeatedgroup"));
260   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 217);
261   sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
262   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 218);
263   sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
264   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 219);
265   sub_message = reflection->AddMessage(message, F("repeated_import_message"));
266   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 220);
267   sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
268   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 227);
269 
270   reflection->AddEnum(message, F("repeated_nested_enum"), nested_bar_);
271   reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_bar_);
272   reflection->AddEnum(message, F("repeated_import_enum"), import_bar_);
273 
274   reflection->AddString(message, F("repeated_string_piece"), "224");
275   reflection->AddString(message, F("repeated_cord"), "225");
276 
277   // Add a second one of each field.
278   reflection->AddInt32(message, F("repeated_int32"), 301);
279   reflection->AddInt64(message, F("repeated_int64"), 302);
280   reflection->AddUInt32(message, F("repeated_uint32"), 303);
281   reflection->AddUInt64(message, F("repeated_uint64"), 304);
282   reflection->AddInt32(message, F("repeated_sint32"), 305);
283   reflection->AddInt64(message, F("repeated_sint64"), 306);
284   reflection->AddUInt32(message, F("repeated_fixed32"), 307);
285   reflection->AddUInt64(message, F("repeated_fixed64"), 308);
286   reflection->AddInt32(message, F("repeated_sfixed32"), 309);
287   reflection->AddInt64(message, F("repeated_sfixed64"), 310);
288   reflection->AddFloat(message, F("repeated_float"), 311);
289   reflection->AddDouble(message, F("repeated_double"), 312);
290   reflection->AddBool(message, F("repeated_bool"), false);
291   reflection->AddString(message, F("repeated_string"), "315");
292   reflection->AddString(message, F("repeated_bytes"), "316");
293 
294   sub_message = reflection->AddMessage(message, F("repeatedgroup"));
295   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 317);
296   sub_message = reflection->AddMessage(message, F("repeated_nested_message"));
297   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 318);
298   sub_message = reflection->AddMessage(message, F("repeated_foreign_message"));
299   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 319);
300   sub_message = reflection->AddMessage(message, F("repeated_import_message"));
301   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 320);
302   sub_message = reflection->AddMessage(message, F("repeated_lazy_message"));
303   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 327);
304 
305   reflection->AddEnum(message, F("repeated_nested_enum"), nested_baz_);
306   reflection->AddEnum(message, F("repeated_foreign_enum"), foreign_baz_);
307   reflection->AddEnum(message, F("repeated_import_enum"), import_baz_);
308 
309   reflection->AddString(message, F("repeated_string_piece"), "324");
310   reflection->AddString(message, F("repeated_cord"), "325");
311 
312   // -----------------------------------------------------------------
313 
314   reflection->SetInt32(message, F("default_int32"), 401);
315   reflection->SetInt64(message, F("default_int64"), 402);
316   reflection->SetUInt32(message, F("default_uint32"), 403);
317   reflection->SetUInt64(message, F("default_uint64"), 404);
318   reflection->SetInt32(message, F("default_sint32"), 405);
319   reflection->SetInt64(message, F("default_sint64"), 406);
320   reflection->SetUInt32(message, F("default_fixed32"), 407);
321   reflection->SetUInt64(message, F("default_fixed64"), 408);
322   reflection->SetInt32(message, F("default_sfixed32"), 409);
323   reflection->SetInt64(message, F("default_sfixed64"), 410);
324   reflection->SetFloat(message, F("default_float"), 411);
325   reflection->SetDouble(message, F("default_double"), 412);
326   reflection->SetBool(message, F("default_bool"), false);
327   reflection->SetString(message, F("default_string"), "415");
328   reflection->SetString(message, F("default_bytes"), "416");
329 
330   reflection->SetEnum(message, F("default_nested_enum"), nested_foo_);
331   reflection->SetEnum(message, F("default_foreign_enum"), foreign_foo_);
332   reflection->SetEnum(message, F("default_import_enum"), import_foo_);
333 
334   reflection->SetString(message, F("default_string_piece"), "424");
335   reflection->SetString(message, F("default_cord"), "425");
336 
337   reflection->SetUInt32(message, F("oneof_uint32"), 601);
338   sub_message = reflection->MutableMessage(message, F("oneof_nested_message"));
339   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 602);
340   reflection->SetString(message, F("oneof_string"), "603");
341   reflection->SetString(message, F("oneof_bytes"), "604");
342 }
343 
SetOneofViaReflection(Message * message)344 inline void TestUtil::ReflectionTester::SetOneofViaReflection(
345     Message* message) {
346   const Descriptor* descriptor = message->GetDescriptor();
347   const Reflection* reflection = message->GetReflection();
348   Message* sub_message = reflection->MutableMessage(
349       message, descriptor->FindFieldByName("foo_lazy_message"));
350   sub_message->GetReflection()->SetInt64(
351       sub_message, sub_message->GetDescriptor()->FindFieldByName("moo_int"),
352       100);
353 
354   reflection->SetString(message, descriptor->FindFieldByName("bar_cord"),
355                         "101");
356   reflection->SetInt32(message, descriptor->FindFieldByName("baz_int"), 102);
357   reflection->SetString(message, descriptor->FindFieldByName("baz_string"),
358                         "103");
359 }
360 
ExpectOneofSetViaReflection(const Message & message)361 inline void TestUtil::ReflectionTester::ExpectOneofSetViaReflection(
362     const Message& message) {
363   const Descriptor* descriptor = message.GetDescriptor();
364   const Reflection* reflection = message.GetReflection();
365   std::string scratch;
366   EXPECT_TRUE(reflection->HasField(
367       message, descriptor->FindFieldByName("foo_lazy_message")));
368   EXPECT_TRUE(
369       reflection->HasField(message, descriptor->FindFieldByName("bar_cord")));
370   EXPECT_TRUE(
371       reflection->HasField(message, descriptor->FindFieldByName("baz_int")));
372   EXPECT_TRUE(
373       reflection->HasField(message, descriptor->FindFieldByName("baz_string")));
374 
375   const Message* sub_message = &reflection->GetMessage(
376       message, descriptor->FindFieldByName("foo_lazy_message"));
377   EXPECT_EQ(100, sub_message->GetReflection()->GetInt64(
378                      *sub_message,
379                      sub_message->GetDescriptor()->FindFieldByName("moo_int")));
380 
381   EXPECT_EQ("101", reflection->GetString(
382                        message, descriptor->FindFieldByName("bar_cord")));
383   EXPECT_EQ("101",
384             reflection->GetStringReference(
385                 message, descriptor->FindFieldByName("bar_cord"), &scratch));
386 
387   EXPECT_EQ(102, reflection->GetInt32(message,
388                                       descriptor->FindFieldByName("baz_int")));
389 
390   EXPECT_EQ("103", reflection->GetString(
391                        message, descriptor->FindFieldByName("baz_string")));
392   EXPECT_EQ("103",
393             reflection->GetStringReference(
394                 message, descriptor->FindFieldByName("baz_string"), &scratch));
395 }
396 
SetPackedFieldsViaReflection(Message * message)397 inline void TestUtil::ReflectionTester::SetPackedFieldsViaReflection(
398     Message* message) {
399   const Reflection* reflection = message->GetReflection();
400   reflection->AddInt32(message, F("packed_int32"), 601);
401   reflection->AddInt64(message, F("packed_int64"), 602);
402   reflection->AddUInt32(message, F("packed_uint32"), 603);
403   reflection->AddUInt64(message, F("packed_uint64"), 604);
404   reflection->AddInt32(message, F("packed_sint32"), 605);
405   reflection->AddInt64(message, F("packed_sint64"), 606);
406   reflection->AddUInt32(message, F("packed_fixed32"), 607);
407   reflection->AddUInt64(message, F("packed_fixed64"), 608);
408   reflection->AddInt32(message, F("packed_sfixed32"), 609);
409   reflection->AddInt64(message, F("packed_sfixed64"), 610);
410   reflection->AddFloat(message, F("packed_float"), 611);
411   reflection->AddDouble(message, F("packed_double"), 612);
412   reflection->AddBool(message, F("packed_bool"), true);
413   reflection->AddEnum(message, F("packed_enum"), foreign_bar_);
414 
415   reflection->AddInt32(message, F("packed_int32"), 701);
416   reflection->AddInt64(message, F("packed_int64"), 702);
417   reflection->AddUInt32(message, F("packed_uint32"), 703);
418   reflection->AddUInt64(message, F("packed_uint64"), 704);
419   reflection->AddInt32(message, F("packed_sint32"), 705);
420   reflection->AddInt64(message, F("packed_sint64"), 706);
421   reflection->AddUInt32(message, F("packed_fixed32"), 707);
422   reflection->AddUInt64(message, F("packed_fixed64"), 708);
423   reflection->AddInt32(message, F("packed_sfixed32"), 709);
424   reflection->AddInt64(message, F("packed_sfixed64"), 710);
425   reflection->AddFloat(message, F("packed_float"), 711);
426   reflection->AddDouble(message, F("packed_double"), 712);
427   reflection->AddBool(message, F("packed_bool"), false);
428   reflection->AddEnum(message, F("packed_enum"), foreign_baz_);
429 }
430 
431 // -------------------------------------------------------------------
432 
ExpectAllFieldsSetViaReflection(const Message & message)433 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection(
434     const Message& message) {
435   // We have to split this into three function otherwise it creates a stack
436   // frame so large that it triggers a warning.
437   ExpectAllFieldsSetViaReflection1(message);
438   ExpectAllFieldsSetViaReflection2(message);
439   ExpectAllFieldsSetViaReflection3(message);
440 }
441 
ExpectAllFieldsSetViaReflection1(const Message & message)442 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection1(
443     const Message& message) {
444   const Reflection* reflection = message.GetReflection();
445   std::string scratch;
446   const Message* sub_message;
447 
448   EXPECT_TRUE(reflection->HasField(message, F("optional_int32")));
449   EXPECT_TRUE(reflection->HasField(message, F("optional_int64")));
450   EXPECT_TRUE(reflection->HasField(message, F("optional_uint32")));
451   EXPECT_TRUE(reflection->HasField(message, F("optional_uint64")));
452   EXPECT_TRUE(reflection->HasField(message, F("optional_sint32")));
453   EXPECT_TRUE(reflection->HasField(message, F("optional_sint64")));
454   EXPECT_TRUE(reflection->HasField(message, F("optional_fixed32")));
455   EXPECT_TRUE(reflection->HasField(message, F("optional_fixed64")));
456   EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed32")));
457   EXPECT_TRUE(reflection->HasField(message, F("optional_sfixed64")));
458   EXPECT_TRUE(reflection->HasField(message, F("optional_float")));
459   EXPECT_TRUE(reflection->HasField(message, F("optional_double")));
460   EXPECT_TRUE(reflection->HasField(message, F("optional_bool")));
461   EXPECT_TRUE(reflection->HasField(message, F("optional_string")));
462   EXPECT_TRUE(reflection->HasField(message, F("optional_bytes")));
463 
464   EXPECT_TRUE(reflection->HasField(message, F("optionalgroup")));
465   EXPECT_TRUE(reflection->HasField(message, F("optional_nested_message")));
466   EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_message")));
467   EXPECT_TRUE(reflection->HasField(message, F("optional_import_message")));
468   EXPECT_TRUE(
469       reflection->HasField(message, F("optional_public_import_message")));
470   EXPECT_TRUE(reflection->HasField(message, F("optional_lazy_message")));
471   EXPECT_TRUE(
472       reflection->HasField(message, F("optional_unverified_lazy_message")));
473 
474   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
475   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
476   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
477   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
478   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
479   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
480   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
481   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
482   sub_message =
483       &reflection->GetMessage(message, F("optional_public_import_message"));
484   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
485   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
486   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
487   sub_message =
488       &reflection->GetMessage(message, F("optional_unverified_lazy_message"));
489   EXPECT_TRUE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
490 
491   EXPECT_TRUE(reflection->HasField(message, F("optional_nested_enum")));
492   EXPECT_TRUE(reflection->HasField(message, F("optional_foreign_enum")));
493   EXPECT_TRUE(reflection->HasField(message, F("optional_import_enum")));
494 
495   EXPECT_TRUE(reflection->HasField(message, F("optional_string_piece")));
496   EXPECT_TRUE(reflection->HasField(message, F("optional_cord")));
497   EXPECT_TRUE(reflection->HasField(message, F("optional_bytes_cord")));
498 
499   EXPECT_EQ(101, reflection->GetInt32(message, F("optional_int32")));
500   EXPECT_EQ(102, reflection->GetInt64(message, F("optional_int64")));
501   EXPECT_EQ(103, reflection->GetUInt32(message, F("optional_uint32")));
502   EXPECT_EQ(104, reflection->GetUInt64(message, F("optional_uint64")));
503   EXPECT_EQ(105, reflection->GetInt32(message, F("optional_sint32")));
504   EXPECT_EQ(106, reflection->GetInt64(message, F("optional_sint64")));
505   EXPECT_EQ(107, reflection->GetUInt32(message, F("optional_fixed32")));
506   EXPECT_EQ(108, reflection->GetUInt64(message, F("optional_fixed64")));
507   EXPECT_EQ(109, reflection->GetInt32(message, F("optional_sfixed32")));
508   EXPECT_EQ(110, reflection->GetInt64(message, F("optional_sfixed64")));
509   EXPECT_EQ(111, reflection->GetFloat(message, F("optional_float")));
510   EXPECT_EQ(112, reflection->GetDouble(message, F("optional_double")));
511   EXPECT_TRUE(reflection->GetBool(message, F("optional_bool")));
512   EXPECT_EQ("115", reflection->GetString(message, F("optional_string")));
513   EXPECT_EQ("116", reflection->GetString(message, F("optional_bytes")));
514 
515   EXPECT_EQ("115", reflection->GetStringReference(message, F("optional_string"),
516                                                   &scratch));
517   EXPECT_EQ("116", reflection->GetStringReference(message, F("optional_bytes"),
518                                                   &scratch));
519 
520   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
521   EXPECT_EQ(117,
522             sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
523   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
524   EXPECT_EQ(118,
525             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
526   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
527   EXPECT_EQ(119,
528             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
529   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
530   EXPECT_EQ(120,
531             sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
532   sub_message =
533       &reflection->GetMessage(message, F("optional_public_import_message"));
534   EXPECT_EQ(126,
535             sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
536   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
537   EXPECT_EQ(127,
538             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
539   sub_message =
540       &reflection->GetMessage(message, F("optional_unverified_lazy_message"));
541   EXPECT_EQ(128,
542             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
543 
544   EXPECT_EQ(nested_baz_,
545             reflection->GetEnum(message, F("optional_nested_enum")));
546   EXPECT_EQ(foreign_baz_,
547             reflection->GetEnum(message, F("optional_foreign_enum")));
548   EXPECT_EQ(import_baz_,
549             reflection->GetEnum(message, F("optional_import_enum")));
550 
551   EXPECT_EQ("124", reflection->GetString(message, F("optional_string_piece")));
552   EXPECT_EQ("124", reflection->GetStringReference(
553                        message, F("optional_string_piece"), &scratch));
554 
555   EXPECT_EQ("125", reflection->GetString(message, F("optional_cord")));
556   EXPECT_EQ("125", reflection->GetStringReference(message, F("optional_cord"),
557                                                   &scratch));
558 
559   EXPECT_EQ("optional bytes cord",
560             reflection->GetString(message, F("optional_bytes_cord")));
561   EXPECT_EQ("optional bytes cord",
562             reflection->GetStringReference(message, F("optional_bytes_cord"),
563                                            &scratch));
564   EXPECT_EQ("optional bytes cord",
565             reflection->GetCord(message, F("optional_bytes_cord")));
566 
567   EXPECT_TRUE(reflection->HasField(message, F("oneof_bytes")));
568   EXPECT_EQ("604", reflection->GetString(message, F("oneof_bytes")));
569 
570   if (base_descriptor_->name() == "TestAllTypes") {
571     EXPECT_FALSE(reflection->HasField(message, F("oneof_uint32")));
572     EXPECT_FALSE(reflection->HasField(message, F("oneof_string")));
573   } else {
574     EXPECT_TRUE(reflection->HasField(message, F("oneof_uint32")));
575     EXPECT_TRUE(reflection->HasField(message, F("oneof_string")));
576     EXPECT_EQ(601, reflection->GetUInt32(message, F("oneof_uint32")));
577     EXPECT_EQ("603", reflection->GetString(message, F("oneof_string")));
578     sub_message = &reflection->GetMessage(message, F("oneof_nested_message"));
579     EXPECT_EQ(602,
580               sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
581   }
582 }
583 
ExpectAllFieldsSetViaReflection2(const Message & message)584 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection2(
585     const Message& message) {
586   const Reflection* reflection = message.GetReflection();
587   std::string scratch;
588   const Message* sub_message;
589 
590   // -----------------------------------------------------------------
591 
592   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int32")));
593   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_int64")));
594   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint32")));
595   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_uint64")));
596   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint32")));
597   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sint64")));
598   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed32")));
599   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_fixed64")));
600   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed32")));
601   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_sfixed64")));
602   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_float")));
603   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_double")));
604   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bool")));
605   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string")));
606   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_bytes")));
607 
608   ASSERT_EQ(2, reflection->FieldSize(message, F("repeatedgroup")));
609   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_message")));
610   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_message")));
611   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_message")));
612   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_lazy_message")));
613   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_nested_enum")));
614   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_foreign_enum")));
615   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_import_enum")));
616 
617   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_string_piece")));
618   ASSERT_EQ(2, reflection->FieldSize(message, F("repeated_cord")));
619 
620   EXPECT_EQ(201, reflection->GetRepeatedInt32(message, F("repeated_int32"), 0));
621   EXPECT_EQ(202, reflection->GetRepeatedInt64(message, F("repeated_int64"), 0));
622   EXPECT_EQ(203,
623             reflection->GetRepeatedUInt32(message, F("repeated_uint32"), 0));
624   EXPECT_EQ(204,
625             reflection->GetRepeatedUInt64(message, F("repeated_uint64"), 0));
626   EXPECT_EQ(205,
627             reflection->GetRepeatedInt32(message, F("repeated_sint32"), 0));
628   EXPECT_EQ(206,
629             reflection->GetRepeatedInt64(message, F("repeated_sint64"), 0));
630   EXPECT_EQ(207,
631             reflection->GetRepeatedUInt32(message, F("repeated_fixed32"), 0));
632   EXPECT_EQ(208,
633             reflection->GetRepeatedUInt64(message, F("repeated_fixed64"), 0));
634   EXPECT_EQ(209,
635             reflection->GetRepeatedInt32(message, F("repeated_sfixed32"), 0));
636   EXPECT_EQ(210,
637             reflection->GetRepeatedInt64(message, F("repeated_sfixed64"), 0));
638   EXPECT_EQ(211, reflection->GetRepeatedFloat(message, F("repeated_float"), 0));
639   EXPECT_EQ(212,
640             reflection->GetRepeatedDouble(message, F("repeated_double"), 0));
641   EXPECT_TRUE(reflection->GetRepeatedBool(message, F("repeated_bool"), 0));
642   EXPECT_EQ("215",
643             reflection->GetRepeatedString(message, F("repeated_string"), 0));
644   EXPECT_EQ("216",
645             reflection->GetRepeatedString(message, F("repeated_bytes"), 0));
646 
647   EXPECT_EQ("215", reflection->GetRepeatedStringReference(
648                        message, F("repeated_string"), 0, &scratch));
649   EXPECT_EQ("216", reflection->GetRepeatedStringReference(
650                        message, F("repeated_bytes"), 0, &scratch));
651 
652   sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 0);
653   EXPECT_EQ(217, sub_message->GetReflection()->GetInt32(*sub_message,
654                                                         repeated_group_a_));
655   sub_message =
656       &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 0);
657   EXPECT_EQ(218,
658             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
659   sub_message = &reflection->GetRepeatedMessage(
660       message, F("repeated_foreign_message"), 0);
661   EXPECT_EQ(219,
662             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
663   sub_message =
664       &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 0);
665   EXPECT_EQ(220,
666             sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
667   sub_message =
668       &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 0);
669   EXPECT_EQ(227,
670             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
671 
672   EXPECT_EQ(nested_bar_,
673             reflection->GetRepeatedEnum(message, F("repeated_nested_enum"), 0));
674   EXPECT_EQ(foreign_bar_, reflection->GetRepeatedEnum(
675                               message, F("repeated_foreign_enum"), 0));
676   EXPECT_EQ(import_bar_,
677             reflection->GetRepeatedEnum(message, F("repeated_import_enum"), 0));
678 
679   EXPECT_EQ("224", reflection->GetRepeatedString(
680                        message, F("repeated_string_piece"), 0));
681   EXPECT_EQ("224", reflection->GetRepeatedStringReference(
682                        message, F("repeated_string_piece"), 0, &scratch));
683 
684   EXPECT_EQ("225",
685             reflection->GetRepeatedString(message, F("repeated_cord"), 0));
686   EXPECT_EQ("225", reflection->GetRepeatedStringReference(
687                        message, F("repeated_cord"), 0, &scratch));
688 
689   EXPECT_EQ(301, reflection->GetRepeatedInt32(message, F("repeated_int32"), 1));
690   EXPECT_EQ(302, reflection->GetRepeatedInt64(message, F("repeated_int64"), 1));
691   EXPECT_EQ(303,
692             reflection->GetRepeatedUInt32(message, F("repeated_uint32"), 1));
693   EXPECT_EQ(304,
694             reflection->GetRepeatedUInt64(message, F("repeated_uint64"), 1));
695   EXPECT_EQ(305,
696             reflection->GetRepeatedInt32(message, F("repeated_sint32"), 1));
697   EXPECT_EQ(306,
698             reflection->GetRepeatedInt64(message, F("repeated_sint64"), 1));
699   EXPECT_EQ(307,
700             reflection->GetRepeatedUInt32(message, F("repeated_fixed32"), 1));
701   EXPECT_EQ(308,
702             reflection->GetRepeatedUInt64(message, F("repeated_fixed64"), 1));
703   EXPECT_EQ(309,
704             reflection->GetRepeatedInt32(message, F("repeated_sfixed32"), 1));
705   EXPECT_EQ(310,
706             reflection->GetRepeatedInt64(message, F("repeated_sfixed64"), 1));
707   EXPECT_EQ(311, reflection->GetRepeatedFloat(message, F("repeated_float"), 1));
708   EXPECT_EQ(312,
709             reflection->GetRepeatedDouble(message, F("repeated_double"), 1));
710   EXPECT_FALSE(reflection->GetRepeatedBool(message, F("repeated_bool"), 1));
711   EXPECT_EQ("315",
712             reflection->GetRepeatedString(message, F("repeated_string"), 1));
713   EXPECT_EQ("316",
714             reflection->GetRepeatedString(message, F("repeated_bytes"), 1));
715 
716   EXPECT_EQ("315", reflection->GetRepeatedStringReference(
717                        message, F("repeated_string"), 1, &scratch));
718   EXPECT_EQ("316", reflection->GetRepeatedStringReference(
719                        message, F("repeated_bytes"), 1, &scratch));
720 
721   sub_message = &reflection->GetRepeatedMessage(message, F("repeatedgroup"), 1);
722   EXPECT_EQ(317, sub_message->GetReflection()->GetInt32(*sub_message,
723                                                         repeated_group_a_));
724   sub_message =
725       &reflection->GetRepeatedMessage(message, F("repeated_nested_message"), 1);
726   EXPECT_EQ(318,
727             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
728   sub_message = &reflection->GetRepeatedMessage(
729       message, F("repeated_foreign_message"), 1);
730   EXPECT_EQ(319,
731             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
732   sub_message =
733       &reflection->GetRepeatedMessage(message, F("repeated_import_message"), 1);
734   EXPECT_EQ(320,
735             sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
736   sub_message =
737       &reflection->GetRepeatedMessage(message, F("repeated_lazy_message"), 1);
738   EXPECT_EQ(327,
739             sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
740 
741   EXPECT_EQ(nested_baz_,
742             reflection->GetRepeatedEnum(message, F("repeated_nested_enum"), 1));
743   EXPECT_EQ(foreign_baz_, reflection->GetRepeatedEnum(
744                               message, F("repeated_foreign_enum"), 1));
745   EXPECT_EQ(import_baz_,
746             reflection->GetRepeatedEnum(message, F("repeated_import_enum"), 1));
747 
748   EXPECT_EQ("324", reflection->GetRepeatedString(
749                        message, F("repeated_string_piece"), 1));
750   EXPECT_EQ("324", reflection->GetRepeatedStringReference(
751                        message, F("repeated_string_piece"), 1, &scratch));
752 
753   EXPECT_EQ("325",
754             reflection->GetRepeatedString(message, F("repeated_cord"), 1));
755   EXPECT_EQ("325", reflection->GetRepeatedStringReference(
756                        message, F("repeated_cord"), 1, &scratch));
757 }
758 
ExpectAllFieldsSetViaReflection3(const Message & message)759 inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection3(
760     const Message& message) {
761   const Reflection* reflection = message.GetReflection();
762   std::string scratch;
763 
764   // -----------------------------------------------------------------
765 
766   EXPECT_TRUE(reflection->HasField(message, F("default_int32")));
767   EXPECT_TRUE(reflection->HasField(message, F("default_int64")));
768   EXPECT_TRUE(reflection->HasField(message, F("default_uint32")));
769   EXPECT_TRUE(reflection->HasField(message, F("default_uint64")));
770   EXPECT_TRUE(reflection->HasField(message, F("default_sint32")));
771   EXPECT_TRUE(reflection->HasField(message, F("default_sint64")));
772   EXPECT_TRUE(reflection->HasField(message, F("default_fixed32")));
773   EXPECT_TRUE(reflection->HasField(message, F("default_fixed64")));
774   EXPECT_TRUE(reflection->HasField(message, F("default_sfixed32")));
775   EXPECT_TRUE(reflection->HasField(message, F("default_sfixed64")));
776   EXPECT_TRUE(reflection->HasField(message, F("default_float")));
777   EXPECT_TRUE(reflection->HasField(message, F("default_double")));
778   EXPECT_TRUE(reflection->HasField(message, F("default_bool")));
779   EXPECT_TRUE(reflection->HasField(message, F("default_string")));
780   EXPECT_TRUE(reflection->HasField(message, F("default_bytes")));
781 
782   EXPECT_TRUE(reflection->HasField(message, F("default_nested_enum")));
783   EXPECT_TRUE(reflection->HasField(message, F("default_foreign_enum")));
784   EXPECT_TRUE(reflection->HasField(message, F("default_import_enum")));
785 
786   EXPECT_TRUE(reflection->HasField(message, F("default_string_piece")));
787   EXPECT_TRUE(reflection->HasField(message, F("default_cord")));
788 
789   EXPECT_EQ(401, reflection->GetInt32(message, F("default_int32")));
790   EXPECT_EQ(402, reflection->GetInt64(message, F("default_int64")));
791   EXPECT_EQ(403, reflection->GetUInt32(message, F("default_uint32")));
792   EXPECT_EQ(404, reflection->GetUInt64(message, F("default_uint64")));
793   EXPECT_EQ(405, reflection->GetInt32(message, F("default_sint32")));
794   EXPECT_EQ(406, reflection->GetInt64(message, F("default_sint64")));
795   EXPECT_EQ(407, reflection->GetUInt32(message, F("default_fixed32")));
796   EXPECT_EQ(408, reflection->GetUInt64(message, F("default_fixed64")));
797   EXPECT_EQ(409, reflection->GetInt32(message, F("default_sfixed32")));
798   EXPECT_EQ(410, reflection->GetInt64(message, F("default_sfixed64")));
799   EXPECT_EQ(411, reflection->GetFloat(message, F("default_float")));
800   EXPECT_EQ(412, reflection->GetDouble(message, F("default_double")));
801   EXPECT_FALSE(reflection->GetBool(message, F("default_bool")));
802   EXPECT_EQ("415", reflection->GetString(message, F("default_string")));
803   EXPECT_EQ("416", reflection->GetString(message, F("default_bytes")));
804 
805   EXPECT_EQ("415", reflection->GetStringReference(message, F("default_string"),
806                                                   &scratch));
807   EXPECT_EQ("416", reflection->GetStringReference(message, F("default_bytes"),
808                                                   &scratch));
809 
810   EXPECT_EQ(nested_foo_,
811             reflection->GetEnum(message, F("default_nested_enum")));
812   EXPECT_EQ(foreign_foo_,
813             reflection->GetEnum(message, F("default_foreign_enum")));
814   EXPECT_EQ(import_foo_,
815             reflection->GetEnum(message, F("default_import_enum")));
816 
817   EXPECT_EQ("424", reflection->GetString(message, F("default_string_piece")));
818   EXPECT_EQ("424", reflection->GetStringReference(
819                        message, F("default_string_piece"), &scratch));
820 
821   EXPECT_EQ("425", reflection->GetString(message, F("default_cord")));
822   EXPECT_EQ("425", reflection->GetStringReference(message, F("default_cord"),
823                                                   &scratch));
824 }
825 
ExpectPackedFieldsSetViaReflection(const Message & message)826 inline void TestUtil::ReflectionTester::ExpectPackedFieldsSetViaReflection(
827     const Message& message) {
828   const Reflection* reflection = message.GetReflection();
829 
830   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int32")));
831   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_int64")));
832   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint32")));
833   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_uint64")));
834   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint32")));
835   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sint64")));
836   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed32")));
837   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_fixed64")));
838   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed32")));
839   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_sfixed64")));
840   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_float")));
841   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_double")));
842   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_bool")));
843   ASSERT_EQ(2, reflection->FieldSize(message, F("packed_enum")));
844 
845   EXPECT_EQ(601, reflection->GetRepeatedInt32(message, F("packed_int32"), 0));
846   EXPECT_EQ(602, reflection->GetRepeatedInt64(message, F("packed_int64"), 0));
847   EXPECT_EQ(603, reflection->GetRepeatedUInt32(message, F("packed_uint32"), 0));
848   EXPECT_EQ(604, reflection->GetRepeatedUInt64(message, F("packed_uint64"), 0));
849   EXPECT_EQ(605, reflection->GetRepeatedInt32(message, F("packed_sint32"), 0));
850   EXPECT_EQ(606, reflection->GetRepeatedInt64(message, F("packed_sint64"), 0));
851   EXPECT_EQ(607,
852             reflection->GetRepeatedUInt32(message, F("packed_fixed32"), 0));
853   EXPECT_EQ(608,
854             reflection->GetRepeatedUInt64(message, F("packed_fixed64"), 0));
855   EXPECT_EQ(609,
856             reflection->GetRepeatedInt32(message, F("packed_sfixed32"), 0));
857   EXPECT_EQ(610,
858             reflection->GetRepeatedInt64(message, F("packed_sfixed64"), 0));
859   EXPECT_EQ(611, reflection->GetRepeatedFloat(message, F("packed_float"), 0));
860   EXPECT_EQ(612, reflection->GetRepeatedDouble(message, F("packed_double"), 0));
861   EXPECT_TRUE(reflection->GetRepeatedBool(message, F("packed_bool"), 0));
862   EXPECT_EQ(foreign_bar_,
863             reflection->GetRepeatedEnum(message, F("packed_enum"), 0));
864 
865   EXPECT_EQ(701, reflection->GetRepeatedInt32(message, F("packed_int32"), 1));
866   EXPECT_EQ(702, reflection->GetRepeatedInt64(message, F("packed_int64"), 1));
867   EXPECT_EQ(703, reflection->GetRepeatedUInt32(message, F("packed_uint32"), 1));
868   EXPECT_EQ(704, reflection->GetRepeatedUInt64(message, F("packed_uint64"), 1));
869   EXPECT_EQ(705, reflection->GetRepeatedInt32(message, F("packed_sint32"), 1));
870   EXPECT_EQ(706, reflection->GetRepeatedInt64(message, F("packed_sint64"), 1));
871   EXPECT_EQ(707,
872             reflection->GetRepeatedUInt32(message, F("packed_fixed32"), 1));
873   EXPECT_EQ(708,
874             reflection->GetRepeatedUInt64(message, F("packed_fixed64"), 1));
875   EXPECT_EQ(709,
876             reflection->GetRepeatedInt32(message, F("packed_sfixed32"), 1));
877   EXPECT_EQ(710,
878             reflection->GetRepeatedInt64(message, F("packed_sfixed64"), 1));
879   EXPECT_EQ(711, reflection->GetRepeatedFloat(message, F("packed_float"), 1));
880   EXPECT_EQ(712, reflection->GetRepeatedDouble(message, F("packed_double"), 1));
881   EXPECT_FALSE(reflection->GetRepeatedBool(message, F("packed_bool"), 1));
882   EXPECT_EQ(foreign_baz_,
883             reflection->GetRepeatedEnum(message, F("packed_enum"), 1));
884 }
885 
886 // -------------------------------------------------------------------
887 
ExpectClearViaReflection(const Message & message)888 inline void TestUtil::ReflectionTester::ExpectClearViaReflection(
889     const Message& message) {
890   const Reflection* reflection = message.GetReflection();
891   std::string scratch;
892   const Message* sub_message;
893 
894   // has_blah() should initially be false for all optional fields.
895   EXPECT_FALSE(reflection->HasField(message, F("optional_int32")));
896   EXPECT_FALSE(reflection->HasField(message, F("optional_int64")));
897   EXPECT_FALSE(reflection->HasField(message, F("optional_uint32")));
898   EXPECT_FALSE(reflection->HasField(message, F("optional_uint64")));
899   EXPECT_FALSE(reflection->HasField(message, F("optional_sint32")));
900   EXPECT_FALSE(reflection->HasField(message, F("optional_sint64")));
901   EXPECT_FALSE(reflection->HasField(message, F("optional_fixed32")));
902   EXPECT_FALSE(reflection->HasField(message, F("optional_fixed64")));
903   EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed32")));
904   EXPECT_FALSE(reflection->HasField(message, F("optional_sfixed64")));
905   EXPECT_FALSE(reflection->HasField(message, F("optional_float")));
906   EXPECT_FALSE(reflection->HasField(message, F("optional_double")));
907   EXPECT_FALSE(reflection->HasField(message, F("optional_bool")));
908   EXPECT_FALSE(reflection->HasField(message, F("optional_string")));
909   EXPECT_FALSE(reflection->HasField(message, F("optional_bytes")));
910 
911   EXPECT_FALSE(reflection->HasField(message, F("optionalgroup")));
912   EXPECT_FALSE(reflection->HasField(message, F("optional_nested_message")));
913   EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_message")));
914   EXPECT_FALSE(reflection->HasField(message, F("optional_import_message")));
915   EXPECT_FALSE(
916       reflection->HasField(message, F("optional_public_import_message")));
917   EXPECT_FALSE(reflection->HasField(message, F("optional_lazy_message")));
918   EXPECT_FALSE(
919       reflection->HasField(message, F("optional_unverified_lazy_message")));
920 
921   EXPECT_FALSE(reflection->HasField(message, F("optional_nested_enum")));
922   EXPECT_FALSE(reflection->HasField(message, F("optional_foreign_enum")));
923   EXPECT_FALSE(reflection->HasField(message, F("optional_import_enum")));
924 
925   EXPECT_FALSE(reflection->HasField(message, F("optional_string_piece")));
926   EXPECT_FALSE(reflection->HasField(message, F("optional_cord")));
927   EXPECT_FALSE(reflection->HasField(message, F("optional_bytes_cord")));
928 
929   // Optional fields without defaults are set to zero or something like it.
930   EXPECT_EQ(0, reflection->GetInt32(message, F("optional_int32")));
931   EXPECT_EQ(0, reflection->GetInt64(message, F("optional_int64")));
932   EXPECT_EQ(0, reflection->GetUInt32(message, F("optional_uint32")));
933   EXPECT_EQ(0, reflection->GetUInt64(message, F("optional_uint64")));
934   EXPECT_EQ(0, reflection->GetInt32(message, F("optional_sint32")));
935   EXPECT_EQ(0, reflection->GetInt64(message, F("optional_sint64")));
936   EXPECT_EQ(0, reflection->GetUInt32(message, F("optional_fixed32")));
937   EXPECT_EQ(0, reflection->GetUInt64(message, F("optional_fixed64")));
938   EXPECT_EQ(0, reflection->GetInt32(message, F("optional_sfixed32")));
939   EXPECT_EQ(0, reflection->GetInt64(message, F("optional_sfixed64")));
940   EXPECT_EQ(0, reflection->GetFloat(message, F("optional_float")));
941   EXPECT_EQ(0, reflection->GetDouble(message, F("optional_double")));
942   EXPECT_FALSE(reflection->GetBool(message, F("optional_bool")));
943   EXPECT_EQ("", reflection->GetString(message, F("optional_string")));
944   EXPECT_EQ("", reflection->GetString(message, F("optional_bytes")));
945 
946   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_string"),
947                                                &scratch));
948   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_bytes"),
949                                                &scratch));
950 
951   // Embedded messages should also be clear.
952   sub_message = &reflection->GetMessage(message, F("optionalgroup"));
953   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, group_a_));
954   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, group_a_));
955   sub_message = &reflection->GetMessage(message, F("optional_nested_message"));
956   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
957   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
958   sub_message = &reflection->GetMessage(message, F("optional_foreign_message"));
959   EXPECT_FALSE(
960       sub_message->GetReflection()->HasField(*sub_message, foreign_c_));
961   EXPECT_EQ(0,
962             sub_message->GetReflection()->GetInt32(*sub_message, foreign_c_));
963   sub_message = &reflection->GetMessage(message, F("optional_import_message"));
964   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_d_));
965   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_d_));
966   sub_message =
967       &reflection->GetMessage(message, F("optional_public_import_message"));
968   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, import_e_));
969   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, import_e_));
970   sub_message = &reflection->GetMessage(message, F("optional_lazy_message"));
971   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
972   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
973   sub_message =
974       &reflection->GetMessage(message, F("optional_unverified_lazy_message"));
975   EXPECT_FALSE(sub_message->GetReflection()->HasField(*sub_message, nested_b_));
976   EXPECT_EQ(0, sub_message->GetReflection()->GetInt32(*sub_message, nested_b_));
977 
978   // Enums without defaults are set to the first value in the enum.
979   EXPECT_EQ(nested_foo_,
980             reflection->GetEnum(message, F("optional_nested_enum")));
981   EXPECT_EQ(foreign_foo_,
982             reflection->GetEnum(message, F("optional_foreign_enum")));
983   EXPECT_EQ(import_foo_,
984             reflection->GetEnum(message, F("optional_import_enum")));
985 
986   EXPECT_EQ("", reflection->GetString(message, F("optional_string_piece")));
987   EXPECT_EQ("", reflection->GetStringReference(
988                     message, F("optional_string_piece"), &scratch));
989 
990   EXPECT_EQ("", reflection->GetString(message, F("optional_cord")));
991   EXPECT_EQ("", reflection->GetStringReference(message, F("optional_cord"),
992                                                &scratch));
993 
994   EXPECT_EQ("", reflection->GetString(message, F("optional_bytes_cord")));
995   EXPECT_EQ("", reflection->GetStringReference(
996                     message, F("optional_bytes_cord"), &scratch));
997   EXPECT_EQ("", reflection->GetCord(message, F("optional_bytes_cord")));
998 
999   // Repeated fields are empty.
1000   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int32")));
1001   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_int64")));
1002   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint32")));
1003   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_uint64")));
1004   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint32")));
1005   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sint64")));
1006   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed32")));
1007   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_fixed64")));
1008   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed32")));
1009   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_sfixed64")));
1010   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_float")));
1011   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_double")));
1012   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bool")));
1013   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string")));
1014   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_bytes")));
1015 
1016   EXPECT_EQ(0, reflection->FieldSize(message, F("repeatedgroup")));
1017   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_message")));
1018   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_message")));
1019   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_message")));
1020   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_lazy_message")));
1021   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_nested_enum")));
1022   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_foreign_enum")));
1023   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_import_enum")));
1024 
1025   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_string_piece")));
1026   EXPECT_EQ(0, reflection->FieldSize(message, F("repeated_cord")));
1027 
1028   // has_blah() should also be false for all default fields.
1029   EXPECT_FALSE(reflection->HasField(message, F("default_int32")));
1030   EXPECT_FALSE(reflection->HasField(message, F("default_int64")));
1031   EXPECT_FALSE(reflection->HasField(message, F("default_uint32")));
1032   EXPECT_FALSE(reflection->HasField(message, F("default_uint64")));
1033   EXPECT_FALSE(reflection->HasField(message, F("default_sint32")));
1034   EXPECT_FALSE(reflection->HasField(message, F("default_sint64")));
1035   EXPECT_FALSE(reflection->HasField(message, F("default_fixed32")));
1036   EXPECT_FALSE(reflection->HasField(message, F("default_fixed64")));
1037   EXPECT_FALSE(reflection->HasField(message, F("default_sfixed32")));
1038   EXPECT_FALSE(reflection->HasField(message, F("default_sfixed64")));
1039   EXPECT_FALSE(reflection->HasField(message, F("default_float")));
1040   EXPECT_FALSE(reflection->HasField(message, F("default_double")));
1041   EXPECT_FALSE(reflection->HasField(message, F("default_bool")));
1042   EXPECT_FALSE(reflection->HasField(message, F("default_string")));
1043   EXPECT_FALSE(reflection->HasField(message, F("default_bytes")));
1044 
1045   EXPECT_FALSE(reflection->HasField(message, F("default_nested_enum")));
1046   EXPECT_FALSE(reflection->HasField(message, F("default_foreign_enum")));
1047   EXPECT_FALSE(reflection->HasField(message, F("default_import_enum")));
1048 
1049   EXPECT_FALSE(reflection->HasField(message, F("default_string_piece")));
1050   EXPECT_FALSE(reflection->HasField(message, F("default_cord")));
1051 
1052   // Fields with defaults have their default values (duh).
1053   EXPECT_EQ(41, reflection->GetInt32(message, F("default_int32")));
1054   EXPECT_EQ(42, reflection->GetInt64(message, F("default_int64")));
1055   EXPECT_EQ(43, reflection->GetUInt32(message, F("default_uint32")));
1056   EXPECT_EQ(44, reflection->GetUInt64(message, F("default_uint64")));
1057   EXPECT_EQ(-45, reflection->GetInt32(message, F("default_sint32")));
1058   EXPECT_EQ(46, reflection->GetInt64(message, F("default_sint64")));
1059   EXPECT_EQ(47, reflection->GetUInt32(message, F("default_fixed32")));
1060   EXPECT_EQ(48, reflection->GetUInt64(message, F("default_fixed64")));
1061   EXPECT_EQ(49, reflection->GetInt32(message, F("default_sfixed32")));
1062   EXPECT_EQ(-50, reflection->GetInt64(message, F("default_sfixed64")));
1063   EXPECT_EQ(51.5, reflection->GetFloat(message, F("default_float")));
1064   EXPECT_EQ(52e3, reflection->GetDouble(message, F("default_double")));
1065   EXPECT_TRUE(reflection->GetBool(message, F("default_bool")));
1066   EXPECT_EQ("hello", reflection->GetString(message, F("default_string")));
1067   EXPECT_EQ("world", reflection->GetString(message, F("default_bytes")));
1068 
1069   EXPECT_EQ("hello", reflection->GetStringReference(
1070                          message, F("default_string"), &scratch));
1071   EXPECT_EQ("world", reflection->GetStringReference(message, F("default_bytes"),
1072                                                     &scratch));
1073 
1074   EXPECT_EQ(nested_bar_,
1075             reflection->GetEnum(message, F("default_nested_enum")));
1076   EXPECT_EQ(foreign_bar_,
1077             reflection->GetEnum(message, F("default_foreign_enum")));
1078   EXPECT_EQ(import_bar_,
1079             reflection->GetEnum(message, F("default_import_enum")));
1080 
1081   EXPECT_EQ("abc", reflection->GetString(message, F("default_string_piece")));
1082   EXPECT_EQ("abc", reflection->GetStringReference(
1083                        message, F("default_string_piece"), &scratch));
1084 
1085   EXPECT_EQ("123", reflection->GetString(message, F("default_cord")));
1086   EXPECT_EQ("123", reflection->GetStringReference(message, F("default_cord"),
1087                                                   &scratch));
1088 }
1089 
1090 // -------------------------------------------------------------------
1091 
ModifyRepeatedFieldsViaReflection(Message * message)1092 inline void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection(
1093     Message* message) {
1094   const Reflection* reflection = message->GetReflection();
1095   Message* sub_message;
1096 
1097   reflection->SetRepeatedInt32(message, F("repeated_int32"), 1, 501);
1098   reflection->SetRepeatedInt64(message, F("repeated_int64"), 1, 502);
1099   reflection->SetRepeatedUInt32(message, F("repeated_uint32"), 1, 503);
1100   reflection->SetRepeatedUInt64(message, F("repeated_uint64"), 1, 504);
1101   reflection->SetRepeatedInt32(message, F("repeated_sint32"), 1, 505);
1102   reflection->SetRepeatedInt64(message, F("repeated_sint64"), 1, 506);
1103   reflection->SetRepeatedUInt32(message, F("repeated_fixed32"), 1, 507);
1104   reflection->SetRepeatedUInt64(message, F("repeated_fixed64"), 1, 508);
1105   reflection->SetRepeatedInt32(message, F("repeated_sfixed32"), 1, 509);
1106   reflection->SetRepeatedInt64(message, F("repeated_sfixed64"), 1, 510);
1107   reflection->SetRepeatedFloat(message, F("repeated_float"), 1, 511);
1108   reflection->SetRepeatedDouble(message, F("repeated_double"), 1, 512);
1109   reflection->SetRepeatedBool(message, F("repeated_bool"), 1, true);
1110   reflection->SetRepeatedString(message, F("repeated_string"), 1, "515");
1111   reflection->SetRepeatedString(message, F("repeated_bytes"), 1, "516");
1112 
1113   sub_message =
1114       reflection->MutableRepeatedMessage(message, F("repeatedgroup"), 1);
1115   sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 517);
1116   sub_message = reflection->MutableRepeatedMessage(
1117       message, F("repeated_nested_message"), 1);
1118   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 518);
1119   sub_message = reflection->MutableRepeatedMessage(
1120       message, F("repeated_foreign_message"), 1);
1121   sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 519);
1122   sub_message = reflection->MutableRepeatedMessage(
1123       message, F("repeated_import_message"), 1);
1124   sub_message->GetReflection()->SetInt32(sub_message, import_d_, 520);
1125   sub_message = reflection->MutableRepeatedMessage(
1126       message, F("repeated_lazy_message"), 1);
1127   sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 527);
1128 
1129   reflection->SetRepeatedEnum(message, F("repeated_nested_enum"), 1,
1130                               nested_foo_);
1131   reflection->SetRepeatedEnum(message, F("repeated_foreign_enum"), 1,
1132                               foreign_foo_);
1133   reflection->SetRepeatedEnum(message, F("repeated_import_enum"), 1,
1134                               import_foo_);
1135 
1136   reflection->SetRepeatedString(message, F("repeated_string_piece"), 1, "524");
1137   reflection->SetRepeatedString(message, F("repeated_cord"), 1, "525");
1138 }
1139 
RemoveLastRepeatedsViaReflection(Message * message)1140 inline void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection(
1141     Message* message) {
1142   const Reflection* reflection = message->GetReflection();
1143 
1144   std::vector<const FieldDescriptor*> output;
1145   reflection->ListFields(*message, &output);
1146   for (int i = 0; i < output.size(); ++i) {
1147     const FieldDescriptor* field = output[i];
1148     if (!field->is_repeated()) continue;
1149 
1150     reflection->RemoveLast(message, field);
1151   }
1152 }
1153 
ReleaseLastRepeatedsViaReflection(Message * message,bool expect_extensions_notnull)1154 inline void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection(
1155     Message* message, bool expect_extensions_notnull) {
1156   const Reflection* reflection = message->GetReflection();
1157 
1158   std::vector<const FieldDescriptor*> output;
1159   reflection->ListFields(*message, &output);
1160   for (int i = 0; i < output.size(); ++i) {
1161     const FieldDescriptor* field = output[i];
1162     if (!field->is_repeated()) continue;
1163     if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
1164 
1165     Message* released = reflection->ReleaseLast(message, field);
1166     if (!field->is_extension() || expect_extensions_notnull) {
1167       ASSERT_TRUE(released != nullptr)
1168           << "ReleaseLast returned nullptr for: " << field->name();
1169     }
1170     delete released;
1171   }
1172 }
1173 
SwapRepeatedsViaReflection(Message * message)1174 inline void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(
1175     Message* message) {
1176   const Reflection* reflection = message->GetReflection();
1177 
1178   std::vector<const FieldDescriptor*> output;
1179   reflection->ListFields(*message, &output);
1180   for (int i = 0; i < output.size(); ++i) {
1181     const FieldDescriptor* field = output[i];
1182     if (!field->is_repeated()) continue;
1183 
1184     reflection->SwapElements(message, field, 0, 1);
1185   }
1186 }
1187 
1188 inline void TestUtil::ReflectionTester::
SetAllocatedOptionalMessageFieldsToNullViaReflection(Message * message)1189     SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message) {
1190   const Reflection* reflection = message->GetReflection();
1191 
1192   std::vector<const FieldDescriptor*> fields;
1193   reflection->ListFields(*message, &fields);
1194 
1195   for (int i = 0; i < fields.size(); ++i) {
1196     const FieldDescriptor* field = fields[i];
1197     if (!field->is_optional() ||
1198         field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
1199       continue;
1200 
1201     reflection->SetAllocatedMessage(message, nullptr, field);
1202   }
1203 }
1204 
1205 inline void TestUtil::ReflectionTester::
SetAllocatedOptionalMessageFieldsToMessageViaReflection(Message * from_message,Message * to_message)1206     SetAllocatedOptionalMessageFieldsToMessageViaReflection(
1207         Message* from_message, Message* to_message) {
1208   EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor());
1209   const Reflection* from_reflection = from_message->GetReflection();
1210   const Reflection* to_reflection = to_message->GetReflection();
1211 
1212   std::vector<const FieldDescriptor*> fields;
1213   from_reflection->ListFields(*from_message, &fields);
1214 
1215   for (int i = 0; i < fields.size(); ++i) {
1216     const FieldDescriptor* field = fields[i];
1217     if (!field->is_optional() ||
1218         field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
1219       continue;
1220 
1221     Message* sub_message = from_reflection->ReleaseMessage(from_message, field);
1222     to_reflection->SetAllocatedMessage(to_message, sub_message, field);
1223   }
1224 }
1225 
ExpectMessagesReleasedViaReflection(Message * message,TestUtil::ReflectionTester::MessageReleaseState expected_release_state)1226 inline void TestUtil::ReflectionTester::ExpectMessagesReleasedViaReflection(
1227     Message* message,
1228     TestUtil::ReflectionTester::MessageReleaseState expected_release_state) {
1229   const Reflection* reflection = message->GetReflection();
1230 
1231   static const char* fields[] = {
1232       "optionalgroup",
1233       "optional_nested_message",
1234       "optional_foreign_message",
1235       "optional_import_message",
1236   };
1237   for (int i = 0; i < ABSL_ARRAYSIZE(fields); i++) {
1238     Message* released = reflection->ReleaseMessage(message, F(fields[i]));
1239     switch (expected_release_state) {
1240       case IS_NULL:
1241         EXPECT_TRUE(released == nullptr);
1242         break;
1243       case NOT_NULL:
1244         EXPECT_TRUE(released != nullptr);
1245         break;
1246       case CAN_BE_NULL:
1247         break;
1248     }
1249     delete released;
1250     EXPECT_FALSE(reflection->HasField(*message, F(fields[i])));
1251   }
1252 }
1253 
1254 // Check that the passed-in serialization is the canonical serialization we
1255 // expect for a TestFieldOrderings message filled in by
1256 // SetAllFieldsAndExtensions().
ExpectAllFieldsAndExtensionsInOrder(const std::string & serialized)1257 inline void ExpectAllFieldsAndExtensionsInOrder(const std::string& serialized) {
1258   // We set each field individually, serialize separately, and concatenate all
1259   // the strings in canonical order to determine the expected serialization.
1260   std::string expected;
1261   unittest::TestFieldOrderings message;
1262   message.set_my_int(1);  // Field 1.
1263   message.AppendToString(&expected);
1264   message.Clear();
1265   message.SetExtension(unittest::my_extension_int, 23);  // Field 5.
1266   message.AppendToString(&expected);
1267   message.Clear();
1268   message.set_my_string("foo");  // Field 11.
1269   message.AppendToString(&expected);
1270   message.Clear();
1271   message.SetExtension(unittest::my_extension_string, "bar");  // Field 50.
1272   message.AppendToString(&expected);
1273   message.Clear();
1274   message.set_my_float(1.0);  // Field 101.
1275   message.AppendToString(&expected);
1276   message.Clear();
1277 
1278   // We don't EXPECT_EQ() since we don't want to print raw bytes to stdout.
1279   EXPECT_TRUE(serialized == expected);
1280 }
1281 
1282 }  // namespace TestUtil
1283 }  // namespace protobuf
1284 }  // namespace google
1285 
1286 #include "google/protobuf/port_undef.inc"
1287 
1288 #endif  // GOOGLE_PROTOBUF_TEST_UTIL_H__
1289