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