blob: b9abb6717db45ce9690d88561a7f10a09fe44c55 [file] [log] [blame]
Brian Silverman9c614bc2016-02-15 20:20:02 -05001// 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
Brian Silverman9c614bc2016-02-15 20:20:02 -050038#include <google/protobuf/unittest.pb.h>
39
Austin Schuh40c16522018-10-28 20:27:54 -070040#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
Brian Silverman9c614bc2016-02-15 20:20:02 -050048namespace google {
49namespace protobuf {
Austin Schuh40c16522018-10-28 20:27:54 -070050// This file doesn't use these declarations, but some .cc files do.
Brian Silverman9c614bc2016-02-15 20:20:02 -050051namespace unittest = ::protobuf_unittest;
Austin Schuh40c16522018-10-28 20:27:54 -070052namespace unittest_import = ::protobuf_unittest_import;
Brian Silverman9c614bc2016-02-15 20:20:02 -050053
Austin Schuh40c16522018-10-28 20:27:54 -070054namespace TestUtil {
55
56class ReflectionTester {
Brian Silverman9c614bc2016-02-15 20:20:02 -050057 public:
Austin Schuh40c16522018-10-28 20:27:54 -070058 // base_descriptor must be a descriptor for TestAllTypes or
59 // TestAllExtensions. In the former case, ReflectionTester fetches from
60 // it the FieldDescriptors needed to use the reflection interface. In
61 // the latter case, ReflectionTester searches for extension fields in
62 // its file.
63 explicit ReflectionTester(const Descriptor* base_descriptor);
Brian Silverman9c614bc2016-02-15 20:20:02 -050064
Austin Schuh40c16522018-10-28 20:27:54 -070065 void SetAllFieldsViaReflection(Message* message);
66 void ModifyRepeatedFieldsViaReflection(Message* message);
67 void ExpectAllFieldsSetViaReflection(const Message& message);
68 void ExpectClearViaReflection(const Message& message);
Brian Silverman9c614bc2016-02-15 20:20:02 -050069
Austin Schuh40c16522018-10-28 20:27:54 -070070 void SetPackedFieldsViaReflection(Message* message);
71 void ModifyPackedFieldsViaReflection(Message* message);
72 void ExpectPackedFieldsSetViaReflection(const Message& message);
73 void ExpectPackedClearViaReflection(const Message& message);
Brian Silverman9c614bc2016-02-15 20:20:02 -050074
Austin Schuh40c16522018-10-28 20:27:54 -070075 void RemoveLastRepeatedsViaReflection(Message* message);
76 void ReleaseLastRepeatedsViaReflection(Message* message,
77 bool expect_extensions_notnull);
78 void SwapRepeatedsViaReflection(Message* message);
79 void SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message);
80 static void SetAllocatedOptionalMessageFieldsToMessageViaReflection(
81 Message* from_message, Message* to_message);
Brian Silverman9c614bc2016-02-15 20:20:02 -050082
Austin Schuh40c16522018-10-28 20:27:54 -070083 enum MessageReleaseState {
84 IS_NULL,
85 CAN_BE_NULL,
86 NOT_NULL,
Brian Silverman9c614bc2016-02-15 20:20:02 -050087 };
Austin Schuh40c16522018-10-28 20:27:54 -070088 void ExpectMessagesReleasedViaReflection(
89 Message* message, MessageReleaseState expected_release_state);
90
91 // Set and check functions for TestOneof2 messages. No need to construct
92 // the ReflectionTester by TestAllTypes nor TestAllExtensions.
93 static void SetOneofViaReflection(Message* message);
94 static void ExpectOneofSetViaReflection(const Message& message);
Brian Silverman9c614bc2016-02-15 20:20:02 -050095
96 private:
Austin Schuh40c16522018-10-28 20:27:54 -070097 const FieldDescriptor* F(const string& name);
98
99 const Descriptor* base_descriptor_;
100
101 const FieldDescriptor* group_a_;
102 const FieldDescriptor* repeated_group_a_;
103 const FieldDescriptor* nested_b_;
104 const FieldDescriptor* foreign_c_;
105 const FieldDescriptor* import_d_;
106 const FieldDescriptor* import_e_;
107
108 const EnumValueDescriptor* nested_foo_;
109 const EnumValueDescriptor* nested_bar_;
110 const EnumValueDescriptor* nested_baz_;
111 const EnumValueDescriptor* foreign_foo_;
112 const EnumValueDescriptor* foreign_bar_;
113 const EnumValueDescriptor* foreign_baz_;
114 const EnumValueDescriptor* import_foo_;
115 const EnumValueDescriptor* import_bar_;
116 const EnumValueDescriptor* import_baz_;
117
118 // We have to split this into three function otherwise it creates a stack
119 // frame so large that it triggers a warning.
120 void ExpectAllFieldsSetViaReflection1(const Message& message);
121 void ExpectAllFieldsSetViaReflection2(const Message& message);
122 void ExpectAllFieldsSetViaReflection3(const Message& message);
123
124 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionTester);
Brian Silverman9c614bc2016-02-15 20:20:02 -0500125};
126
Austin Schuh40c16522018-10-28 20:27:54 -0700127inline TestUtil::ReflectionTester::ReflectionTester(
128 const Descriptor* base_descriptor)
129 : base_descriptor_(base_descriptor) {
130 const DescriptorPool* pool = base_descriptor->file()->pool();
131 string package = base_descriptor->file()->package();
132 const FieldDescriptor* import_descriptor =
133 pool->FindFieldByName(package + ".TestAllTypes.optional_import_message");
134 string import_package = 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.
178inline const FieldDescriptor* TestUtil::ReflectionTester::F(
179 const 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
193inline 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
343inline 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
360inline void TestUtil::ReflectionTester::ExpectOneofSetViaReflection(
361 const Message& message) {
362 const Descriptor* descriptor = message.GetDescriptor();
363 const Reflection* reflection = message.GetReflection();
364 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
396inline 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
432inline 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
441inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection1(
442 const Message& message) {
443 const Reflection* reflection = message.GetReflection();
444 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
565inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection2(
566 const Message& message) {
567 const Reflection* reflection = message.GetReflection();
568 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
740inline void TestUtil::ReflectionTester::ExpectAllFieldsSetViaReflection3(
741 const Message& message) {
742 const Reflection* reflection = message.GetReflection();
743 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
807inline 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
869inline void TestUtil::ReflectionTester::ExpectClearViaReflection(
870 const Message& message) {
871 const Reflection* reflection = message.GetReflection();
872 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
1059inline void TestUtil::ReflectionTester::ExpectPackedClearViaReflection(
1060 const Message& message) {
1061 const Reflection* reflection = message.GetReflection();
1062
1063 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int32")));
1064 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_int64")));
1065 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint32")));
1066 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_uint64")));
1067 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint32")));
1068 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sint64")));
1069 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed32")));
1070 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_fixed64")));
1071 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed32")));
1072 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_sfixed64")));
1073 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_float")));
1074 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_double")));
1075 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_bool")));
1076 EXPECT_EQ(0, reflection->FieldSize(message, F("packed_enum")));
1077}
1078
1079// -------------------------------------------------------------------
1080
1081inline void TestUtil::ReflectionTester::ModifyRepeatedFieldsViaReflection(
1082 Message* message) {
1083 const Reflection* reflection = message->GetReflection();
1084 Message* sub_message;
1085
1086 reflection->SetRepeatedInt32(message, F("repeated_int32"), 1, 501);
1087 reflection->SetRepeatedInt64(message, F("repeated_int64"), 1, 502);
1088 reflection->SetRepeatedUInt32(message, F("repeated_uint32"), 1, 503);
1089 reflection->SetRepeatedUInt64(message, F("repeated_uint64"), 1, 504);
1090 reflection->SetRepeatedInt32(message, F("repeated_sint32"), 1, 505);
1091 reflection->SetRepeatedInt64(message, F("repeated_sint64"), 1, 506);
1092 reflection->SetRepeatedUInt32(message, F("repeated_fixed32"), 1, 507);
1093 reflection->SetRepeatedUInt64(message, F("repeated_fixed64"), 1, 508);
1094 reflection->SetRepeatedInt32(message, F("repeated_sfixed32"), 1, 509);
1095 reflection->SetRepeatedInt64(message, F("repeated_sfixed64"), 1, 510);
1096 reflection->SetRepeatedFloat(message, F("repeated_float"), 1, 511);
1097 reflection->SetRepeatedDouble(message, F("repeated_double"), 1, 512);
1098 reflection->SetRepeatedBool(message, F("repeated_bool"), 1, true);
1099 reflection->SetRepeatedString(message, F("repeated_string"), 1, "515");
1100 reflection->SetRepeatedString(message, F("repeated_bytes"), 1, "516");
1101
1102 sub_message =
1103 reflection->MutableRepeatedMessage(message, F("repeatedgroup"), 1);
1104 sub_message->GetReflection()->SetInt32(sub_message, repeated_group_a_, 517);
1105 sub_message = reflection->MutableRepeatedMessage(
1106 message, F("repeated_nested_message"), 1);
1107 sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 518);
1108 sub_message = reflection->MutableRepeatedMessage(
1109 message, F("repeated_foreign_message"), 1);
1110 sub_message->GetReflection()->SetInt32(sub_message, foreign_c_, 519);
1111 sub_message = reflection->MutableRepeatedMessage(
1112 message, F("repeated_import_message"), 1);
1113 sub_message->GetReflection()->SetInt32(sub_message, import_d_, 520);
1114 sub_message = reflection->MutableRepeatedMessage(
1115 message, F("repeated_lazy_message"), 1);
1116 sub_message->GetReflection()->SetInt32(sub_message, nested_b_, 527);
1117
1118 reflection->SetRepeatedEnum(message, F("repeated_nested_enum"), 1,
1119 nested_foo_);
1120 reflection->SetRepeatedEnum(message, F("repeated_foreign_enum"), 1,
1121 foreign_foo_);
1122 reflection->SetRepeatedEnum(message, F("repeated_import_enum"), 1,
1123 import_foo_);
1124
1125 reflection->SetRepeatedString(message, F("repeated_string_piece"), 1, "524");
1126 reflection->SetRepeatedString(message, F("repeated_cord"), 1, "525");
1127}
1128
1129inline void TestUtil::ReflectionTester::ModifyPackedFieldsViaReflection(
1130 Message* message) {
1131 const Reflection* reflection = message->GetReflection();
1132 reflection->SetRepeatedInt32(message, F("packed_int32"), 1, 801);
1133 reflection->SetRepeatedInt64(message, F("packed_int64"), 1, 802);
1134 reflection->SetRepeatedUInt32(message, F("packed_uint32"), 1, 803);
1135 reflection->SetRepeatedUInt64(message, F("packed_uint64"), 1, 804);
1136 reflection->SetRepeatedInt32(message, F("packed_sint32"), 1, 805);
1137 reflection->SetRepeatedInt64(message, F("packed_sint64"), 1, 806);
1138 reflection->SetRepeatedUInt32(message, F("packed_fixed32"), 1, 807);
1139 reflection->SetRepeatedUInt64(message, F("packed_fixed64"), 1, 808);
1140 reflection->SetRepeatedInt32(message, F("packed_sfixed32"), 1, 809);
1141 reflection->SetRepeatedInt64(message, F("packed_sfixed64"), 1, 810);
1142 reflection->SetRepeatedFloat(message, F("packed_float"), 1, 811);
1143 reflection->SetRepeatedDouble(message, F("packed_double"), 1, 812);
1144 reflection->SetRepeatedBool(message, F("packed_bool"), 1, true);
1145 reflection->SetRepeatedEnum(message, F("packed_enum"), 1, foreign_foo_);
1146}
1147
1148inline void TestUtil::ReflectionTester::RemoveLastRepeatedsViaReflection(
1149 Message* message) {
1150 const Reflection* reflection = message->GetReflection();
1151
1152 std::vector<const FieldDescriptor*> output;
1153 reflection->ListFields(*message, &output);
1154 for (int i = 0; i < output.size(); ++i) {
1155 const FieldDescriptor* field = output[i];
1156 if (!field->is_repeated()) continue;
1157
1158 reflection->RemoveLast(message, field);
1159 }
1160}
1161
1162inline void TestUtil::ReflectionTester::ReleaseLastRepeatedsViaReflection(
1163 Message* message, bool expect_extensions_notnull) {
1164 const Reflection* reflection = message->GetReflection();
1165
1166 std::vector<const FieldDescriptor*> output;
1167 reflection->ListFields(*message, &output);
1168 for (int i = 0; i < output.size(); ++i) {
1169 const FieldDescriptor* field = output[i];
1170 if (!field->is_repeated()) continue;
1171 if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) continue;
1172
1173 Message* released = reflection->ReleaseLast(message, field);
1174 if (!field->is_extension() || expect_extensions_notnull) {
1175 ASSERT_TRUE(released != nullptr)
1176 << "ReleaseLast returned nullptr for: " << field->name();
1177 }
1178 delete released;
1179 }
1180}
1181
1182inline void TestUtil::ReflectionTester::SwapRepeatedsViaReflection(
1183 Message* message) {
1184 const Reflection* reflection = message->GetReflection();
1185
1186 std::vector<const FieldDescriptor*> output;
1187 reflection->ListFields(*message, &output);
1188 for (int i = 0; i < output.size(); ++i) {
1189 const FieldDescriptor* field = output[i];
1190 if (!field->is_repeated()) continue;
1191
1192 reflection->SwapElements(message, field, 0, 1);
1193 }
1194}
1195
1196inline void TestUtil::ReflectionTester::
1197 SetAllocatedOptionalMessageFieldsToNullViaReflection(Message* message) {
1198 const Reflection* reflection = message->GetReflection();
1199
1200 std::vector<const FieldDescriptor*> fields;
1201 reflection->ListFields(*message, &fields);
1202
1203 for (int i = 0; i < fields.size(); ++i) {
1204 const FieldDescriptor* field = fields[i];
1205 if (!field->is_optional() ||
1206 field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
1207 continue;
1208
1209 reflection->SetAllocatedMessage(message, nullptr, field);
1210 }
1211}
1212
1213inline void TestUtil::ReflectionTester::
1214 SetAllocatedOptionalMessageFieldsToMessageViaReflection(
1215 Message* from_message, Message* to_message) {
1216 EXPECT_EQ(from_message->GetDescriptor(), to_message->GetDescriptor());
1217 const Reflection* from_reflection = from_message->GetReflection();
1218 const Reflection* to_reflection = to_message->GetReflection();
1219
1220 std::vector<const FieldDescriptor*> fields;
1221 from_reflection->ListFields(*from_message, &fields);
1222
1223 for (int i = 0; i < fields.size(); ++i) {
1224 const FieldDescriptor* field = fields[i];
1225 if (!field->is_optional() ||
1226 field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE)
1227 continue;
1228
1229 Message* sub_message = from_reflection->ReleaseMessage(from_message, field);
1230 to_reflection->SetAllocatedMessage(to_message, sub_message, field);
1231 }
1232}
1233
1234inline void TestUtil::ReflectionTester::ExpectMessagesReleasedViaReflection(
1235 Message* message,
1236 TestUtil::ReflectionTester::MessageReleaseState expected_release_state) {
1237 const Reflection* reflection = message->GetReflection();
1238
1239 static const char* fields[] = {
1240 "optionalgroup",
1241 "optional_nested_message",
1242 "optional_foreign_message",
1243 "optional_import_message",
1244 };
1245 for (int i = 0; i < GOOGLE_ARRAYSIZE(fields); i++) {
1246 const Message& sub_message = reflection->GetMessage(*message, F(fields[i]));
1247 Message* released = reflection->ReleaseMessage(message, F(fields[i]));
1248 switch (expected_release_state) {
1249 case IS_NULL:
1250 EXPECT_TRUE(released == nullptr);
1251 break;
1252 case NOT_NULL:
1253 EXPECT_TRUE(released != nullptr);
1254 if (message->GetArena() == nullptr) {
1255 // released message must be same as sub_message if source message is
1256 // not on arena.
1257 EXPECT_EQ(&sub_message, released);
1258 }
1259 break;
1260 case CAN_BE_NULL:
1261 break;
1262 }
1263 delete released;
1264 EXPECT_FALSE(reflection->HasField(*message, F(fields[i])));
1265 }
1266}
1267
1268// Check that the passed-in serialization is the canonical serialization we
1269// expect for a TestFieldOrderings message filled in by
1270// SetAllFieldsAndExtensions().
1271inline void ExpectAllFieldsAndExtensionsInOrder(
1272 const string& serialized) {
1273 // We set each field individually, serialize separately, and concatenate all
1274 // the strings in canonical order to determine the expected serialization.
1275 string expected;
1276 unittest::TestFieldOrderings message;
1277 message.set_my_int(1); // Field 1.
1278 message.AppendToString(&expected);
1279 message.Clear();
1280 message.SetExtension(unittest::my_extension_int, 23); // Field 5.
1281 message.AppendToString(&expected);
1282 message.Clear();
1283 message.set_my_string("foo"); // Field 11.
1284 message.AppendToString(&expected);
1285 message.Clear();
1286 message.SetExtension(unittest::my_extension_string, "bar"); // Field 50.
1287 message.AppendToString(&expected);
1288 message.Clear();
1289 message.set_my_float(1.0); // Field 101.
1290 message.AppendToString(&expected);
1291 message.Clear();
1292
1293 // We don't EXPECT_EQ() since we don't want to print raw bytes to stdout.
1294 EXPECT_TRUE(serialized == expected);
1295}
1296
1297} // namespace TestUtil
Brian Silverman9c614bc2016-02-15 20:20:02 -05001298} // namespace protobuf
1299
1300} // namespace google
1301#endif // GOOGLE_PROTOBUF_TEST_UTIL_H__