Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers.Test/ReflectionTester.cs @ 4095

Last change on this file since 4095 was 3857, checked in by abeham, 14 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 51.5 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using Google.ProtocolBuffers.Descriptors;
37using Google.ProtocolBuffers.TestProtos;
38using NUnit.Framework;
39
40namespace Google.ProtocolBuffers {
41  /// <summary>
42  /// Performs the same things that the methods of TestUtil do, but
43  /// via the reflection interface.  This is its own class because it needs
44  /// to know what descriptor to use.
45  /// </summary>
46  internal class ReflectionTester {
47    private readonly MessageDescriptor baseDescriptor;
48    private readonly ExtensionRegistry extensionRegistry;
49
50    private readonly FileDescriptor file;
51    private readonly FileDescriptor importFile;
52
53    private readonly MessageDescriptor optionalGroup;
54    private readonly MessageDescriptor repeatedGroup;
55    private readonly MessageDescriptor nestedMessage;
56    private readonly MessageDescriptor foreignMessage;
57    private readonly MessageDescriptor importMessage;
58
59    private readonly FieldDescriptor groupA;
60    private readonly FieldDescriptor repeatedGroupA;
61    private readonly FieldDescriptor nestedB;
62    private readonly FieldDescriptor foreignC;
63    private readonly FieldDescriptor importD;
64
65    private readonly EnumDescriptor nestedEnum;
66    private readonly EnumDescriptor foreignEnum;
67    private readonly EnumDescriptor importEnum;
68
69    private readonly EnumValueDescriptor nestedFoo;
70    private readonly EnumValueDescriptor nestedBar;
71    private readonly EnumValueDescriptor nestedBaz;
72    private readonly EnumValueDescriptor foreignFoo;
73    private readonly EnumValueDescriptor foreignBar;
74    private readonly EnumValueDescriptor foreignBaz;
75    private readonly EnumValueDescriptor importFoo;
76    private readonly EnumValueDescriptor importBar;
77    private readonly EnumValueDescriptor importBaz;
78
79    /// <summary>
80    /// Constructs an instance that will expect messages using the given
81    /// descriptor. Normally <paramref name="baseDescriptor"/> should be
82    /// a descriptor for TestAllTypes. However, if extensionRegistry is non-null,
83    /// then baseDescriptor should be for TestAllExtensions instead, and instead of
84    /// reading and writing normal fields, the tester will read and write extensions.
85    /// All of the TestAllExtensions extensions must be registered in the registry.
86    /// </summary>
87    private ReflectionTester(MessageDescriptor baseDescriptor,
88                            ExtensionRegistry extensionRegistry) {
89      this.baseDescriptor = baseDescriptor;
90      this.extensionRegistry = extensionRegistry;
91
92      this.file = baseDescriptor.File;
93      // TODO(jonskeet): We've got 2 dependencies, not 1 - because of the C# options. Hmm.
94      //      Assert.AreEqual(1, file.Dependencies.Count);
95      // TODO(jonskeet): Find dependency by name instead of number?
96      this.importFile = file.Dependencies[1];
97
98      MessageDescriptor testAllTypes;
99      if (baseDescriptor.Name == "TestAllTypes") {
100        testAllTypes = baseDescriptor;
101      } else {
102        testAllTypes = file.FindTypeByName<MessageDescriptor>("TestAllTypes");
103        Assert.IsNotNull(testAllTypes);
104      }
105
106      if (extensionRegistry == null) {
107        // Use testAllTypes, rather than baseDescriptor, to allow
108        // initialization using TestPackedTypes descriptors. These objects
109        // won't be used by the methods for packed fields.
110        this.optionalGroup =
111          testAllTypes.FindDescriptor<MessageDescriptor>("OptionalGroup");
112        this.repeatedGroup =
113          testAllTypes.FindDescriptor<MessageDescriptor>("RepeatedGroup");
114      } else {
115        this.optionalGroup =
116          file.FindTypeByName<MessageDescriptor>("OptionalGroup_extension");
117        this.repeatedGroup =
118          file.FindTypeByName<MessageDescriptor>("RepeatedGroup_extension");
119      }
120      this.nestedMessage = testAllTypes.FindDescriptor<MessageDescriptor>("NestedMessage");
121      this.foreignMessage = file.FindTypeByName<MessageDescriptor>("ForeignMessage");
122      this.importMessage = importFile.FindTypeByName<MessageDescriptor>("ImportMessage");
123
124      this.nestedEnum = testAllTypes.FindDescriptor<EnumDescriptor>("NestedEnum");
125      this.foreignEnum = file.FindTypeByName<EnumDescriptor>("ForeignEnum");
126      this.importEnum = importFile.FindTypeByName<EnumDescriptor>("ImportEnum");
127
128      Assert.IsNotNull(optionalGroup);
129      Assert.IsNotNull(repeatedGroup);
130      Assert.IsNotNull(nestedMessage);
131      Assert.IsNotNull(foreignMessage);
132      Assert.IsNotNull(importMessage);
133      Assert.IsNotNull(nestedEnum);
134      Assert.IsNotNull(foreignEnum);
135      Assert.IsNotNull(importEnum);
136
137      this.nestedB = nestedMessage.FindDescriptor<FieldDescriptor>("bb");
138      this.foreignC = foreignMessage.FindDescriptor<FieldDescriptor>("c");
139      this.importD = importMessage.FindDescriptor<FieldDescriptor>("d");
140      this.nestedFoo = nestedEnum.FindValueByName("FOO");
141      this.nestedBar = nestedEnum.FindValueByName("BAR");
142      this.nestedBaz = nestedEnum.FindValueByName("BAZ");
143      this.foreignFoo = foreignEnum.FindValueByName("FOREIGN_FOO");
144      this.foreignBar = foreignEnum.FindValueByName("FOREIGN_BAR");
145      this.foreignBaz = foreignEnum.FindValueByName("FOREIGN_BAZ");
146      this.importFoo = importEnum.FindValueByName("IMPORT_FOO");
147      this.importBar = importEnum.FindValueByName("IMPORT_BAR");
148      this.importBaz = importEnum.FindValueByName("IMPORT_BAZ");
149
150      this.groupA = optionalGroup.FindDescriptor<FieldDescriptor>("a");
151      this.repeatedGroupA = repeatedGroup.FindDescriptor<FieldDescriptor>("a");
152
153      Assert.IsNotNull(groupA);
154      Assert.IsNotNull(repeatedGroupA);
155      Assert.IsNotNull(nestedB);
156      Assert.IsNotNull(foreignC);
157      Assert.IsNotNull(importD);
158      Assert.IsNotNull(nestedFoo);
159      Assert.IsNotNull(nestedBar);
160      Assert.IsNotNull(nestedBaz);
161      Assert.IsNotNull(foreignFoo);
162      Assert.IsNotNull(foreignBar);
163      Assert.IsNotNull(foreignBaz);
164      Assert.IsNotNull(importFoo);
165      Assert.IsNotNull(importBar);
166      Assert.IsNotNull(importBaz);
167    }
168
169    /// <summary>
170    /// Creates an instance for the TestAllTypes message, with no extension registry.
171    /// </summary>
172    public static ReflectionTester CreateTestAllTypesInstance() {
173      return new ReflectionTester(TestAllTypes.Descriptor, null);
174    }
175
176    /// <summary>
177    /// Creates an instance for the TestAllExtensions message, with an
178    /// extension registry from TestUtil.CreateExtensionRegistry.
179    /// </summary>
180    public static ReflectionTester CreateTestAllExtensionsInstance() {
181      return new ReflectionTester(TestAllExtensions.Descriptor, TestUtil.CreateExtensionRegistry());
182    }
183
184    /// <summary>
185    /// Creates an instance for the TestPackedTypes message, with no extensions.
186    /// </summary>
187    public static ReflectionTester CreateTestPackedTypesInstance() {
188      return new ReflectionTester(TestPackedTypes.Descriptor, null);
189    }
190
191    /// <summary>
192    /// Shorthand to get a FieldDescriptor for a field of unittest::TestAllTypes.
193    /// </summary>
194    private FieldDescriptor f(String name) {
195      FieldDescriptor result;
196      if (extensionRegistry == null) {
197        result = baseDescriptor.FindDescriptor<FieldDescriptor>(name);
198      } else {
199        result = file.FindTypeByName<FieldDescriptor>(name + "_extension");
200      }
201      Assert.IsNotNull(result);
202      return result;
203    }
204
205    /// <summary>
206    /// Calls parent.CreateBuilderForField() or uses the extension registry
207    /// to find an appropriate builder, depending on what type is being tested.
208    /// </summary>
209    private IBuilder CreateBuilderForField(IBuilder parent, FieldDescriptor field) {
210      if (extensionRegistry == null) {
211        return parent.CreateBuilderForField(field);
212      } else {
213        ExtensionInfo extension = extensionRegistry[field.ContainingType, field.FieldNumber];
214        Assert.IsNotNull(extension);
215        Assert.IsNotNull(extension.DefaultInstance);
216        return extension.DefaultInstance.WeakCreateBuilderForType();
217      }
218    }
219
220    /// <summary>
221    /// Sets every field of the message to the values expected by
222    /// AssertAllFieldsSet, using the reflection interface.
223    /// </summary>
224    /// <param name="message"></param>
225    internal void SetAllFieldsViaReflection(IBuilder message) {
226      message[f("optional_int32")] = 101;
227      message[f("optional_int64")] = 102L;
228      message[f("optional_uint32")] = 103U;
229      message[f("optional_uint64")] = 104UL;
230      message[f("optional_sint32")] = 105;
231      message[f("optional_sint64")] = 106L;
232      message[f("optional_fixed32")] = 107U;
233      message[f("optional_fixed64")] = 108UL;
234      message[f("optional_sfixed32")] = 109;
235      message[f("optional_sfixed64")] = 110L;
236      message[f("optional_float")] = 111F;
237      message[f("optional_double")] = 112D;
238      message[f("optional_bool")] = true;
239      message[f("optional_string")] = "115";
240      message[f("optional_bytes")] = TestUtil.ToBytes("116");
241
242      message[f("optionalgroup")] = CreateBuilderForField(message, f("optionalgroup")).SetField(groupA, 117).WeakBuild();
243      message[f("optional_nested_message")] = CreateBuilderForField(message, f("optional_nested_message")).SetField(nestedB, 118).WeakBuild();
244      message[f("optional_foreign_message")] = CreateBuilderForField(message, f("optional_foreign_message")).SetField(foreignC, 119).WeakBuild();
245      message[f("optional_import_message")] = CreateBuilderForField(message, f("optional_import_message")).SetField(importD, 120).WeakBuild();
246
247      message[f("optional_nested_enum")] = nestedBaz;
248      message[f("optional_foreign_enum")] = foreignBaz;
249      message[f("optional_import_enum")] = importBaz;
250
251      message[f("optional_string_piece")] = "124";
252      message[f("optional_cord")] = "125";
253
254      // -----------------------------------------------------------------
255
256      message.WeakAddRepeatedField(f("repeated_int32"), 201);
257      message.WeakAddRepeatedField(f("repeated_int64"), 202L);
258      message.WeakAddRepeatedField(f("repeated_uint32"), 203U);
259      message.WeakAddRepeatedField(f("repeated_uint64"), 204UL);
260      message.WeakAddRepeatedField(f("repeated_sint32"), 205);
261      message.WeakAddRepeatedField(f("repeated_sint64"), 206L);
262      message.WeakAddRepeatedField(f("repeated_fixed32"), 207U);
263      message.WeakAddRepeatedField(f("repeated_fixed64"), 208UL);
264      message.WeakAddRepeatedField(f("repeated_sfixed32"), 209);
265      message.WeakAddRepeatedField(f("repeated_sfixed64"), 210L);
266      message.WeakAddRepeatedField(f("repeated_float"), 211F);
267      message.WeakAddRepeatedField(f("repeated_double"), 212D);
268      message.WeakAddRepeatedField(f("repeated_bool"), true);
269      message.WeakAddRepeatedField(f("repeated_string"), "215");
270      message.WeakAddRepeatedField(f("repeated_bytes"), TestUtil.ToBytes("216"));
271
272
273      message.WeakAddRepeatedField(f("repeatedgroup"), CreateBuilderForField(message, f("repeatedgroup")).SetField(repeatedGroupA, 217).WeakBuild());
274      message.WeakAddRepeatedField(f("repeated_nested_message"), CreateBuilderForField(message, f("repeated_nested_message")).SetField(nestedB, 218).WeakBuild());
275      message.WeakAddRepeatedField(f("repeated_foreign_message"), CreateBuilderForField(message, f("repeated_foreign_message")).SetField(foreignC, 219).WeakBuild());
276      message.WeakAddRepeatedField(f("repeated_import_message"), CreateBuilderForField(message, f("repeated_import_message")).SetField(importD, 220).WeakBuild());
277
278      message.WeakAddRepeatedField(f("repeated_nested_enum"), nestedBar);
279      message.WeakAddRepeatedField(f("repeated_foreign_enum"), foreignBar);
280      message.WeakAddRepeatedField(f("repeated_import_enum"), importBar);
281
282      message.WeakAddRepeatedField(f("repeated_string_piece"), "224");
283      message.WeakAddRepeatedField(f("repeated_cord"), "225");
284
285      // Add a second one of each field.
286      message.WeakAddRepeatedField(f("repeated_int32"), 301);
287      message.WeakAddRepeatedField(f("repeated_int64"), 302L);
288      message.WeakAddRepeatedField(f("repeated_uint32"), 303U);
289      message.WeakAddRepeatedField(f("repeated_uint64"), 304UL);
290      message.WeakAddRepeatedField(f("repeated_sint32"), 305);
291      message.WeakAddRepeatedField(f("repeated_sint64"), 306L);
292      message.WeakAddRepeatedField(f("repeated_fixed32"), 307U);
293      message.WeakAddRepeatedField(f("repeated_fixed64"), 308UL);
294      message.WeakAddRepeatedField(f("repeated_sfixed32"), 309);
295      message.WeakAddRepeatedField(f("repeated_sfixed64"), 310L);
296      message.WeakAddRepeatedField(f("repeated_float"), 311F);
297      message.WeakAddRepeatedField(f("repeated_double"), 312D);
298      message.WeakAddRepeatedField(f("repeated_bool"), false);
299      message.WeakAddRepeatedField(f("repeated_string"), "315");
300      message.WeakAddRepeatedField(f("repeated_bytes"), TestUtil.ToBytes("316"));
301
302      message.WeakAddRepeatedField(f("repeatedgroup"),
303        CreateBuilderForField(message, f("repeatedgroup"))
304               .SetField(repeatedGroupA, 317).WeakBuild());
305      message.WeakAddRepeatedField(f("repeated_nested_message"),
306        CreateBuilderForField(message, f("repeated_nested_message"))
307               .SetField(nestedB, 318).WeakBuild());
308      message.WeakAddRepeatedField(f("repeated_foreign_message"),
309        CreateBuilderForField(message, f("repeated_foreign_message"))
310               .SetField(foreignC, 319).WeakBuild());
311      message.WeakAddRepeatedField(f("repeated_import_message"),
312        CreateBuilderForField(message, f("repeated_import_message"))
313               .SetField(importD, 320).WeakBuild());
314
315      message.WeakAddRepeatedField(f("repeated_nested_enum"), nestedBaz);
316      message.WeakAddRepeatedField(f("repeated_foreign_enum"), foreignBaz);
317      message.WeakAddRepeatedField(f("repeated_import_enum"), importBaz);
318
319      message.WeakAddRepeatedField(f("repeated_string_piece"), "324");
320      message.WeakAddRepeatedField(f("repeated_cord"), "325");
321
322      // -----------------------------------------------------------------
323
324      message[f("default_int32")] = 401;
325      message[f("default_int64")] = 402L;
326      message[f("default_uint32")] = 403U;
327      message[f("default_uint64")] = 404UL;
328      message[f("default_sint32")] = 405;
329      message[f("default_sint64")] = 406L;
330      message[f("default_fixed32")] = 407U;
331      message[f("default_fixed64")] = 408UL;
332      message[f("default_sfixed32")] = 409;
333      message[f("default_sfixed64")] = 410L;
334      message[f("default_float")] = 411F;
335      message[f("default_double")] = 412D;
336      message[f("default_bool")] = false;
337      message[f("default_string")] = "415";
338      message[f("default_bytes")] = TestUtil.ToBytes("416");
339
340      message[f("default_nested_enum")] = nestedFoo;
341      message[f("default_foreign_enum")] = foreignFoo;
342      message[f("default_import_enum")] = importFoo;
343
344      message[f("default_string_piece")] = "424";
345      message[f("default_cord")] = "425";
346    }
347
348    // -------------------------------------------------------------------
349
350    /// <summary>
351    /// Modify the repeated fields of the specified message to contain the
352    /// values expected by AssertRepeatedFieldsModified, using the IBuilder
353    /// reflection interface.
354    /// </summary>
355    internal void ModifyRepeatedFieldsViaReflection(IBuilder message) {
356      message[f("repeated_int32"), 1] = 501;
357      message[f("repeated_int64"), 1] = 502L;
358      message[f("repeated_uint32"), 1] = 503U;
359      message[f("repeated_uint64"), 1] = 504UL;
360      message[f("repeated_sint32"), 1] = 505;
361      message[f("repeated_sint64"), 1] = 506L;
362      message[f("repeated_fixed32"), 1] = 507U;
363      message[f("repeated_fixed64"), 1] = 508UL;
364      message[f("repeated_sfixed32"), 1] = 509;
365      message[f("repeated_sfixed64"), 1] = 510L;
366      message[f("repeated_float"), 1] = 511F;
367      message[f("repeated_double"), 1] = 512D;
368      message[f("repeated_bool"), 1] = true;
369      message[f("repeated_string"), 1] = "515";
370      message.SetRepeatedField(f("repeated_bytes"), 1, TestUtil.ToBytes("516"));
371
372      message.SetRepeatedField(f("repeatedgroup"), 1, CreateBuilderForField(message, f("repeatedgroup")).SetField(repeatedGroupA, 517).WeakBuild());
373      message.SetRepeatedField(f("repeated_nested_message"), 1, CreateBuilderForField(message, f("repeated_nested_message")).SetField(nestedB, 518).WeakBuild());
374      message.SetRepeatedField(f("repeated_foreign_message"), 1, CreateBuilderForField(message, f("repeated_foreign_message")).SetField(foreignC, 519).WeakBuild());
375      message.SetRepeatedField(f("repeated_import_message"), 1, CreateBuilderForField(message, f("repeated_import_message")).SetField(importD, 520).WeakBuild());
376
377      message[f("repeated_nested_enum"), 1] = nestedFoo;
378      message[f("repeated_foreign_enum"), 1] = foreignFoo;
379      message[f("repeated_import_enum"), 1] = importFoo;
380
381      message[f("repeated_string_piece"), 1] = "524";
382      message[f("repeated_cord"), 1] = "525";
383    }
384
385    // -------------------------------------------------------------------
386
387    /// <summary>
388    /// Asserts that all fields of the specified message are set to the values
389    /// assigned by SetAllFields, using the IMessage reflection interface.
390    /// </summary>
391    public void AssertAllFieldsSetViaReflection(IMessage message) {
392      Assert.IsTrue(message.HasField(f("optional_int32")));
393      Assert.IsTrue(message.HasField(f("optional_int64")));
394      Assert.IsTrue(message.HasField(f("optional_uint32")));
395      Assert.IsTrue(message.HasField(f("optional_uint64")));
396      Assert.IsTrue(message.HasField(f("optional_sint32")));
397      Assert.IsTrue(message.HasField(f("optional_sint64")));
398      Assert.IsTrue(message.HasField(f("optional_fixed32")));
399      Assert.IsTrue(message.HasField(f("optional_fixed64")));
400      Assert.IsTrue(message.HasField(f("optional_sfixed32")));
401      Assert.IsTrue(message.HasField(f("optional_sfixed64")));
402      Assert.IsTrue(message.HasField(f("optional_float")));
403      Assert.IsTrue(message.HasField(f("optional_double")));
404      Assert.IsTrue(message.HasField(f("optional_bool")));
405      Assert.IsTrue(message.HasField(f("optional_string")));
406      Assert.IsTrue(message.HasField(f("optional_bytes")));
407
408      Assert.IsTrue(message.HasField(f("optionalgroup")));
409      Assert.IsTrue(message.HasField(f("optional_nested_message")));
410      Assert.IsTrue(message.HasField(f("optional_foreign_message")));
411      Assert.IsTrue(message.HasField(f("optional_import_message")));
412
413      Assert.IsTrue(((IMessage)message[f("optionalgroup")]).HasField(groupA));
414      Assert.IsTrue(((IMessage)message[f("optional_nested_message")]).HasField(nestedB));
415      Assert.IsTrue(((IMessage)message[f("optional_foreign_message")]).HasField(foreignC));
416      Assert.IsTrue(((IMessage)message[f("optional_import_message")]).HasField(importD));
417
418      Assert.IsTrue(message.HasField(f("optional_nested_enum")));
419      Assert.IsTrue(message.HasField(f("optional_foreign_enum")));
420      Assert.IsTrue(message.HasField(f("optional_import_enum")));
421
422      Assert.IsTrue(message.HasField(f("optional_string_piece")));
423      Assert.IsTrue(message.HasField(f("optional_cord")));
424
425      Assert.AreEqual(101, message[f("optional_int32")]);
426      Assert.AreEqual(102L, message[f("optional_int64")]);
427      Assert.AreEqual(103U, message[f("optional_uint32")]);
428      Assert.AreEqual(104UL, message[f("optional_uint64")]);
429      Assert.AreEqual(105, message[f("optional_sint32")]);
430      Assert.AreEqual(106L, message[f("optional_sint64")]);
431      Assert.AreEqual(107U, message[f("optional_fixed32")]);
432      Assert.AreEqual(108UL, message[f("optional_fixed64")]);
433      Assert.AreEqual(109, message[f("optional_sfixed32")]);
434      Assert.AreEqual(110L, message[f("optional_sfixed64")]);
435      Assert.AreEqual(111F, message[f("optional_float")]);
436      Assert.AreEqual(112D, message[f("optional_double")]);
437      Assert.AreEqual(true, message[f("optional_bool")]);
438      Assert.AreEqual("115", message[f("optional_string")]);
439      Assert.AreEqual(TestUtil.ToBytes("116"), message[f("optional_bytes")]);
440
441      Assert.AreEqual(117, ((IMessage)message[f("optionalgroup")])[groupA]);
442      Assert.AreEqual(118, ((IMessage)message[f("optional_nested_message")])[nestedB]);
443      Assert.AreEqual(119, ((IMessage)message[f("optional_foreign_message")])[foreignC]);
444      Assert.AreEqual(120, ((IMessage)message[f("optional_import_message")])[importD]);
445
446      Assert.AreEqual(nestedBaz, message[f("optional_nested_enum")]);
447      Assert.AreEqual(foreignBaz, message[f("optional_foreign_enum")]);
448      Assert.AreEqual(importBaz, message[f("optional_import_enum")]);
449
450      Assert.AreEqual("124", message[f("optional_string_piece")]);
451      Assert.AreEqual("125", message[f("optional_cord")]);
452
453      // -----------------------------------------------------------------
454
455      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_int32")));
456      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_int64")));
457      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_uint32")));
458      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_uint64")));
459      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sint32")));
460      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sint64")));
461      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_fixed32")));
462      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_fixed64")));
463      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sfixed32")));
464      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sfixed64")));
465      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_float")));
466      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_double")));
467      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_bool")));
468      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_string")));
469      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_bytes")));
470
471      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeatedgroup")));
472      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_nested_message")));
473      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_foreign_message")));
474      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_import_message")));
475      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_nested_enum")));
476      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_foreign_enum")));
477      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_import_enum")));
478
479      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_string_piece")));
480      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_cord")));
481
482      Assert.AreEqual(201, message[f("repeated_int32"), 0]);
483      Assert.AreEqual(202L, message[f("repeated_int64"), 0]);
484      Assert.AreEqual(203U, message[f("repeated_uint32"), 0]);
485      Assert.AreEqual(204UL, message[f("repeated_uint64"), 0]);
486      Assert.AreEqual(205, message[f("repeated_sint32"), 0]);
487      Assert.AreEqual(206L, message[f("repeated_sint64"), 0]);
488      Assert.AreEqual(207U, message[f("repeated_fixed32"), 0]);
489      Assert.AreEqual(208UL, message[f("repeated_fixed64"), 0]);
490      Assert.AreEqual(209, message[f("repeated_sfixed32"), 0]);
491      Assert.AreEqual(210L, message[f("repeated_sfixed64"), 0]);
492      Assert.AreEqual(211F, message[f("repeated_float"), 0]);
493      Assert.AreEqual(212D, message[f("repeated_double"), 0]);
494      Assert.AreEqual(true, message[f("repeated_bool"), 0]);
495      Assert.AreEqual("215", message[f("repeated_string"), 0]);
496      Assert.AreEqual(TestUtil.ToBytes("216"), message[f("repeated_bytes"), 0]);
497
498      Assert.AreEqual(217, ((IMessage)message[f("repeatedgroup"), 0])[repeatedGroupA]);
499      Assert.AreEqual(218, ((IMessage)message[f("repeated_nested_message"), 0])[nestedB]);
500      Assert.AreEqual(219, ((IMessage)message[f("repeated_foreign_message"), 0])[foreignC]);
501      Assert.AreEqual(220, ((IMessage)message[f("repeated_import_message"), 0])[importD]);
502
503      Assert.AreEqual(nestedBar, message[f("repeated_nested_enum"), 0]);
504      Assert.AreEqual(foreignBar, message[f("repeated_foreign_enum"), 0]);
505      Assert.AreEqual(importBar, message[f("repeated_import_enum"), 0]);
506
507      Assert.AreEqual("224", message[f("repeated_string_piece"), 0]);
508      Assert.AreEqual("225", message[f("repeated_cord"), 0]);
509
510      Assert.AreEqual(301, message[f("repeated_int32"), 1]);
511      Assert.AreEqual(302L, message[f("repeated_int64"), 1]);
512      Assert.AreEqual(303U, message[f("repeated_uint32"), 1]);
513      Assert.AreEqual(304UL, message[f("repeated_uint64"), 1]);
514      Assert.AreEqual(305, message[f("repeated_sint32"), 1]);
515      Assert.AreEqual(306L, message[f("repeated_sint64"), 1]);
516      Assert.AreEqual(307U, message[f("repeated_fixed32"), 1]);
517      Assert.AreEqual(308UL, message[f("repeated_fixed64"), 1]);
518      Assert.AreEqual(309, message[f("repeated_sfixed32"), 1]);
519      Assert.AreEqual(310L, message[f("repeated_sfixed64"), 1]);
520      Assert.AreEqual(311F, message[f("repeated_float"), 1]);
521      Assert.AreEqual(312D, message[f("repeated_double"), 1]);
522      Assert.AreEqual(false, message[f("repeated_bool"), 1]);
523      Assert.AreEqual("315", message[f("repeated_string"), 1]);
524      Assert.AreEqual(TestUtil.ToBytes("316"), message[f("repeated_bytes"), 1]);
525
526      Assert.AreEqual(317, ((IMessage)message[f("repeatedgroup"), 1])[repeatedGroupA]);
527      Assert.AreEqual(318, ((IMessage)message[f("repeated_nested_message"), 1])[nestedB]);
528      Assert.AreEqual(319, ((IMessage)message[f("repeated_foreign_message"), 1])[foreignC]);
529      Assert.AreEqual(320, ((IMessage)message[f("repeated_import_message"), 1])[importD]);
530
531      Assert.AreEqual(nestedBaz, message[f("repeated_nested_enum"), 1]);
532      Assert.AreEqual(foreignBaz, message[f("repeated_foreign_enum"), 1]);
533      Assert.AreEqual(importBaz, message[f("repeated_import_enum"), 1]);
534
535      Assert.AreEqual("324", message[f("repeated_string_piece"), 1]);
536      Assert.AreEqual("325", message[f("repeated_cord"), 1]);
537
538      // -----------------------------------------------------------------
539
540      Assert.IsTrue(message.HasField(f("default_int32")));
541      Assert.IsTrue(message.HasField(f("default_int64")));
542      Assert.IsTrue(message.HasField(f("default_uint32")));
543      Assert.IsTrue(message.HasField(f("default_uint64")));
544      Assert.IsTrue(message.HasField(f("default_sint32")));
545      Assert.IsTrue(message.HasField(f("default_sint64")));
546      Assert.IsTrue(message.HasField(f("default_fixed32")));
547      Assert.IsTrue(message.HasField(f("default_fixed64")));
548      Assert.IsTrue(message.HasField(f("default_sfixed32")));
549      Assert.IsTrue(message.HasField(f("default_sfixed64")));
550      Assert.IsTrue(message.HasField(f("default_float")));
551      Assert.IsTrue(message.HasField(f("default_double")));
552      Assert.IsTrue(message.HasField(f("default_bool")));
553      Assert.IsTrue(message.HasField(f("default_string")));
554      Assert.IsTrue(message.HasField(f("default_bytes")));
555
556      Assert.IsTrue(message.HasField(f("default_nested_enum")));
557      Assert.IsTrue(message.HasField(f("default_foreign_enum")));
558      Assert.IsTrue(message.HasField(f("default_import_enum")));
559
560      Assert.IsTrue(message.HasField(f("default_string_piece")));
561      Assert.IsTrue(message.HasField(f("default_cord")));
562
563      Assert.AreEqual(401, message[f("default_int32")]);
564      Assert.AreEqual(402L, message[f("default_int64")]);
565      Assert.AreEqual(403U, message[f("default_uint32")]);
566      Assert.AreEqual(404UL, message[f("default_uint64")]);
567      Assert.AreEqual(405, message[f("default_sint32")]);
568      Assert.AreEqual(406L, message[f("default_sint64")]);
569      Assert.AreEqual(407U, message[f("default_fixed32")]);
570      Assert.AreEqual(408UL, message[f("default_fixed64")]);
571      Assert.AreEqual(409, message[f("default_sfixed32")]);
572      Assert.AreEqual(410L, message[f("default_sfixed64")]);
573      Assert.AreEqual(411F, message[f("default_float")]);
574      Assert.AreEqual(412D, message[f("default_double")]);
575      Assert.AreEqual(false, message[f("default_bool")]);
576      Assert.AreEqual("415", message[f("default_string")]);
577      Assert.AreEqual(TestUtil.ToBytes("416"), message[f("default_bytes")]);
578
579      Assert.AreEqual(nestedFoo, message[f("default_nested_enum")]);
580      Assert.AreEqual(foreignFoo, message[f("default_foreign_enum")]);
581      Assert.AreEqual(importFoo, message[f("default_import_enum")]);
582
583      Assert.AreEqual("424", message[f("default_string_piece")]);
584      Assert.AreEqual("425", message[f("default_cord")]);
585    }
586
587    /// <summary>
588    /// Assert that all fields of the message are cleared, and that
589    /// getting the fields returns their default values, using the reflection interface.
590    /// </summary>
591    public void AssertClearViaReflection(IMessage message) {
592      // has_blah() should initially be false for all optional fields.
593      Assert.IsFalse(message.HasField(f("optional_int32")));
594      Assert.IsFalse(message.HasField(f("optional_int64")));
595      Assert.IsFalse(message.HasField(f("optional_uint32")));
596      Assert.IsFalse(message.HasField(f("optional_uint64")));
597      Assert.IsFalse(message.HasField(f("optional_sint32")));
598      Assert.IsFalse(message.HasField(f("optional_sint64")));
599      Assert.IsFalse(message.HasField(f("optional_fixed32")));
600      Assert.IsFalse(message.HasField(f("optional_fixed64")));
601      Assert.IsFalse(message.HasField(f("optional_sfixed32")));
602      Assert.IsFalse(message.HasField(f("optional_sfixed64")));
603      Assert.IsFalse(message.HasField(f("optional_float")));
604      Assert.IsFalse(message.HasField(f("optional_double")));
605      Assert.IsFalse(message.HasField(f("optional_bool")));
606      Assert.IsFalse(message.HasField(f("optional_string")));
607      Assert.IsFalse(message.HasField(f("optional_bytes")));
608
609      Assert.IsFalse(message.HasField(f("optionalgroup")));
610      Assert.IsFalse(message.HasField(f("optional_nested_message")));
611      Assert.IsFalse(message.HasField(f("optional_foreign_message")));
612      Assert.IsFalse(message.HasField(f("optional_import_message")));
613
614      Assert.IsFalse(message.HasField(f("optional_nested_enum")));
615      Assert.IsFalse(message.HasField(f("optional_foreign_enum")));
616      Assert.IsFalse(message.HasField(f("optional_import_enum")));
617
618      Assert.IsFalse(message.HasField(f("optional_string_piece")));
619      Assert.IsFalse(message.HasField(f("optional_cord")));
620
621      // Optional fields without defaults are set to zero or something like it.
622      Assert.AreEqual(0, message[f("optional_int32")]);
623      Assert.AreEqual(0L, message[f("optional_int64")]);
624      Assert.AreEqual(0U, message[f("optional_uint32")]);
625      Assert.AreEqual(0UL, message[f("optional_uint64")]);
626      Assert.AreEqual(0, message[f("optional_sint32")]);
627      Assert.AreEqual(0L, message[f("optional_sint64")]);
628      Assert.AreEqual(0U, message[f("optional_fixed32")]);
629      Assert.AreEqual(0UL, message[f("optional_fixed64")]);
630      Assert.AreEqual(0, message[f("optional_sfixed32")]);
631      Assert.AreEqual(0L, message[f("optional_sfixed64")]);
632      Assert.AreEqual(0F, message[f("optional_float")]);
633      Assert.AreEqual(0D, message[f("optional_double")]);
634      Assert.AreEqual(false, message[f("optional_bool")]);
635      Assert.AreEqual("", message[f("optional_string")]);
636      Assert.AreEqual(ByteString.Empty, message[f("optional_bytes")]);
637
638      // Embedded messages should also be clear.
639      Assert.IsFalse(((IMessage)message[f("optionalgroup")]).HasField(groupA));
640      Assert.IsFalse(((IMessage)message[f("optional_nested_message")])
641                         .HasField(nestedB));
642      Assert.IsFalse(((IMessage)message[f("optional_foreign_message")])
643                         .HasField(foreignC));
644      Assert.IsFalse(((IMessage)message[f("optional_import_message")])
645                         .HasField(importD));
646
647      Assert.AreEqual(0, ((IMessage)message[f("optionalgroup")])[groupA]);
648      Assert.AreEqual(0, ((IMessage)message[f("optional_nested_message")])[nestedB]);
649      Assert.AreEqual(0, ((IMessage)message[f("optional_foreign_message")])[foreignC]);
650      Assert.AreEqual(0, ((IMessage)message[f("optional_import_message")])[importD]);
651
652      // Enums without defaults are set to the first value in the enum.
653      Assert.AreEqual(nestedFoo, message[f("optional_nested_enum")]);
654      Assert.AreEqual(foreignFoo, message[f("optional_foreign_enum")]);
655      Assert.AreEqual(importFoo, message[f("optional_import_enum")]);
656
657      Assert.AreEqual("", message[f("optional_string_piece")]);
658      Assert.AreEqual("", message[f("optional_cord")]);
659
660      // Repeated fields are empty.
661      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_int32")));
662      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_int64")));
663      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_uint32")));
664      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_uint64")));
665      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sint32")));
666      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sint64")));
667      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_fixed32")));
668      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_fixed64")));
669      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sfixed32")));
670      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_sfixed64")));
671      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_float")));
672      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_double")));
673      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_bool")));
674      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_string")));
675      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_bytes")));
676
677      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeatedgroup")));
678      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_nested_message")));
679      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_foreign_message")));
680      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_import_message")));
681      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_nested_enum")));
682      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_foreign_enum")));
683      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_import_enum")));
684
685      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_string_piece")));
686      Assert.AreEqual(0, message.GetRepeatedFieldCount(f("repeated_cord")));
687
688      // has_blah() should also be false for all default fields.
689      Assert.IsFalse(message.HasField(f("default_int32")));
690      Assert.IsFalse(message.HasField(f("default_int64")));
691      Assert.IsFalse(message.HasField(f("default_uint32")));
692      Assert.IsFalse(message.HasField(f("default_uint64")));
693      Assert.IsFalse(message.HasField(f("default_sint32")));
694      Assert.IsFalse(message.HasField(f("default_sint64")));
695      Assert.IsFalse(message.HasField(f("default_fixed32")));
696      Assert.IsFalse(message.HasField(f("default_fixed64")));
697      Assert.IsFalse(message.HasField(f("default_sfixed32")));
698      Assert.IsFalse(message.HasField(f("default_sfixed64")));
699      Assert.IsFalse(message.HasField(f("default_float")));
700      Assert.IsFalse(message.HasField(f("default_double")));
701      Assert.IsFalse(message.HasField(f("default_bool")));
702      Assert.IsFalse(message.HasField(f("default_string")));
703      Assert.IsFalse(message.HasField(f("default_bytes")));
704
705      Assert.IsFalse(message.HasField(f("default_nested_enum")));
706      Assert.IsFalse(message.HasField(f("default_foreign_enum")));
707      Assert.IsFalse(message.HasField(f("default_import_enum")));
708
709      Assert.IsFalse(message.HasField(f("default_string_piece")));
710      Assert.IsFalse(message.HasField(f("default_cord")));
711
712      // Fields with defaults have their default values (duh).
713      Assert.AreEqual(41, message[f("default_int32")]);
714      Assert.AreEqual(42L, message[f("default_int64")]);
715      Assert.AreEqual(43U, message[f("default_uint32")]);
716      Assert.AreEqual(44UL, message[f("default_uint64")]);
717      Assert.AreEqual(-45, message[f("default_sint32")]);
718      Assert.AreEqual(46L, message[f("default_sint64")]);
719      Assert.AreEqual(47U, message[f("default_fixed32")]);
720      Assert.AreEqual(48UL, message[f("default_fixed64")]);
721      Assert.AreEqual(49, message[f("default_sfixed32")]);
722      Assert.AreEqual(-50L, message[f("default_sfixed64")]);
723      Assert.AreEqual(51.5F, message[f("default_float")]);
724      Assert.AreEqual(52e3D, message[f("default_double")]);
725      Assert.AreEqual(true, message[f("default_bool")]);
726      Assert.AreEqual("hello", message[f("default_string")]);
727      Assert.AreEqual(TestUtil.ToBytes("world"), message[f("default_bytes")]);
728
729      Assert.AreEqual(nestedBar, message[f("default_nested_enum")]);
730      Assert.AreEqual(foreignBar, message[f("default_foreign_enum")]);
731      Assert.AreEqual(importBar, message[f("default_import_enum")]);
732
733      Assert.AreEqual("abc", message[f("default_string_piece")]);
734      Assert.AreEqual("123", message[f("default_cord")]);
735    }
736
737    // ---------------------------------------------------------------
738
739    internal void AssertRepeatedFieldsModifiedViaReflection(IMessage message) {
740      // ModifyRepeatedFields only sets the second repeated element of each
741      // field.  In addition to verifying this, we also verify that the first
742      // element and size were *not* modified.
743      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_int32")));
744      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_int64")));
745      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_uint32")));
746      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_uint64")));
747      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sint32")));
748      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sint64")));
749      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_fixed32")));
750      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_fixed64")));
751      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sfixed32")));
752      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_sfixed64")));
753      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_float")));
754      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_double")));
755      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_bool")));
756      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_string")));
757      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_bytes")));
758
759      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeatedgroup")));
760      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_nested_message")));
761      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_foreign_message")));
762      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_import_message")));
763      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_nested_enum")));
764      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_foreign_enum")));
765      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_import_enum")));
766
767      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_string_piece")));
768      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("repeated_cord")));
769
770      Assert.AreEqual(201, message[f("repeated_int32"), 0]);
771      Assert.AreEqual(202L, message[f("repeated_int64"), 0]);
772      Assert.AreEqual(203U, message[f("repeated_uint32"), 0]);
773      Assert.AreEqual(204UL, message[f("repeated_uint64"), 0]);
774      Assert.AreEqual(205, message[f("repeated_sint32"), 0]);
775      Assert.AreEqual(206L, message[f("repeated_sint64"), 0]);
776      Assert.AreEqual(207U, message[f("repeated_fixed32"), 0]);
777      Assert.AreEqual(208UL, message[f("repeated_fixed64"), 0]);
778      Assert.AreEqual(209, message[f("repeated_sfixed32"), 0]);
779      Assert.AreEqual(210L, message[f("repeated_sfixed64"), 0]);
780      Assert.AreEqual(211F, message[f("repeated_float"), 0]);
781      Assert.AreEqual(212D, message[f("repeated_double"), 0]);
782      Assert.AreEqual(true, message[f("repeated_bool"), 0]);
783      Assert.AreEqual("215", message[f("repeated_string"), 0]);
784      Assert.AreEqual(TestUtil.ToBytes("216"), message[f("repeated_bytes"), 0]);
785
786      Assert.AreEqual(217, ((IMessage)message[f("repeatedgroup"), 0])[repeatedGroupA]);
787      Assert.AreEqual(218, ((IMessage)message[f("repeated_nested_message"), 0])[nestedB]);
788      Assert.AreEqual(219, ((IMessage)message[f("repeated_foreign_message"), 0])[foreignC]);
789      Assert.AreEqual(220, ((IMessage)message[f("repeated_import_message"), 0])[importD]);
790
791      Assert.AreEqual(nestedBar, message[f("repeated_nested_enum"), 0]);
792      Assert.AreEqual(foreignBar, message[f("repeated_foreign_enum"), 0]);
793      Assert.AreEqual(importBar, message[f("repeated_import_enum"), 0]);
794
795      Assert.AreEqual("224", message[f("repeated_string_piece"), 0]);
796      Assert.AreEqual("225", message[f("repeated_cord"), 0]);
797
798      Assert.AreEqual(501, message[f("repeated_int32"), 1]);
799      Assert.AreEqual(502L, message[f("repeated_int64"), 1]);
800      Assert.AreEqual(503U, message[f("repeated_uint32"), 1]);
801      Assert.AreEqual(504UL, message[f("repeated_uint64"), 1]);
802      Assert.AreEqual(505, message[f("repeated_sint32"), 1]);
803      Assert.AreEqual(506L, message[f("repeated_sint64"), 1]);
804      Assert.AreEqual(507U, message[f("repeated_fixed32"), 1]);
805      Assert.AreEqual(508UL, message[f("repeated_fixed64"), 1]);
806      Assert.AreEqual(509, message[f("repeated_sfixed32"), 1]);
807      Assert.AreEqual(510L, message[f("repeated_sfixed64"), 1]);
808      Assert.AreEqual(511F, message[f("repeated_float"), 1]);
809      Assert.AreEqual(512D, message[f("repeated_double"), 1]);
810      Assert.AreEqual(true, message[f("repeated_bool"), 1]);
811      Assert.AreEqual("515", message[f("repeated_string"), 1]);
812      Assert.AreEqual(TestUtil.ToBytes("516"), message[f("repeated_bytes"), 1]);
813
814      Assert.AreEqual(517, ((IMessage)message[f("repeatedgroup"), 1])[repeatedGroupA]);
815      Assert.AreEqual(518, ((IMessage)message[f("repeated_nested_message"), 1])[nestedB]);
816      Assert.AreEqual(519, ((IMessage)message[f("repeated_foreign_message"), 1])[foreignC]);
817      Assert.AreEqual(520, ((IMessage)message[f("repeated_import_message"), 1])[importD]);
818
819      Assert.AreEqual(nestedFoo, message[f("repeated_nested_enum"), 1]);
820      Assert.AreEqual(foreignFoo, message[f("repeated_foreign_enum"), 1]);
821      Assert.AreEqual(importFoo, message[f("repeated_import_enum"), 1]);
822
823      Assert.AreEqual("524", message[f("repeated_string_piece"), 1]);
824      Assert.AreEqual("525", message[f("repeated_cord"), 1]);
825    }
826
827    /// <summary>
828    /// Verifies that the reflection setters for the given Builder object throw an
829    /// ArgumentNullException if they are passed a null value.
830    /// </summary>
831    public void AssertReflectionSettersRejectNull(IBuilder builder) {
832      TestUtil.AssertArgumentNullException(() => builder[f("optional_string")] = null);
833      TestUtil.AssertArgumentNullException(() => builder[f("optional_bytes")] = null);
834      TestUtil.AssertArgumentNullException(() => builder[f("optional_nested_enum")] = null);
835      TestUtil.AssertArgumentNullException(() => builder[f("optional_nested_message")] = null);
836      TestUtil.AssertArgumentNullException(() => builder[f("optional_nested_message")] = null);
837      TestUtil.AssertArgumentNullException(() => builder.WeakAddRepeatedField(f("repeated_string"), null));
838      TestUtil.AssertArgumentNullException(() => builder.WeakAddRepeatedField(f("repeated_bytes"), null));
839      TestUtil.AssertArgumentNullException(() => builder.WeakAddRepeatedField(f("repeated_nested_enum"), null));
840      TestUtil.AssertArgumentNullException(() => builder.WeakAddRepeatedField(f("repeated_nested_message"), null));
841    }
842
843    /// <summary>
844    /// Verifies that the reflection repeated setters for the given Builder object throw an
845    /// ArgumentNullException if they are passed a null value.
846    /// </summary>
847    public void AssertReflectionRepeatedSettersRejectNull(IBuilder builder) {
848      builder.WeakAddRepeatedField(f("repeated_string"), "one");
849      TestUtil.AssertArgumentNullException(() => builder.SetRepeatedField(f("repeated_string"), 0, null));
850      builder.WeakAddRepeatedField(f("repeated_bytes"), TestUtil.ToBytes("one"));
851      TestUtil.AssertArgumentNullException(() => builder.SetRepeatedField(f("repeated_bytes"), 0, null));
852      builder.WeakAddRepeatedField(f("repeated_nested_enum"), nestedBaz);
853      TestUtil.AssertArgumentNullException(() => builder.SetRepeatedField(f("repeated_nested_enum"), 0, null));
854      builder.WeakAddRepeatedField(f("repeated_nested_message"),
855          new TestAllTypes.Types.NestedMessage.Builder { Bb = 218 }.Build());
856      TestUtil.AssertArgumentNullException(() => builder.SetRepeatedField(f("repeated_nested_message"), 0, null));
857    }
858
859    public void SetPackedFieldsViaReflection(IBuilder message) {
860      message.WeakAddRepeatedField(f("packed_int32"), 601);
861      message.WeakAddRepeatedField(f("packed_int64"), 602L);
862      message.WeakAddRepeatedField(f("packed_uint32"), 603U);
863      message.WeakAddRepeatedField(f("packed_uint64"), 604UL);
864      message.WeakAddRepeatedField(f("packed_sint32"), 605);
865      message.WeakAddRepeatedField(f("packed_sint64"), 606L);
866      message.WeakAddRepeatedField(f("packed_fixed32"), 607U);
867      message.WeakAddRepeatedField(f("packed_fixed64"), 608UL);
868      message.WeakAddRepeatedField(f("packed_sfixed32"), 609);
869      message.WeakAddRepeatedField(f("packed_sfixed64"), 610L);
870      message.WeakAddRepeatedField(f("packed_float"), 611F);
871      message.WeakAddRepeatedField(f("packed_double"), 612D);
872      message.WeakAddRepeatedField(f("packed_bool"), true);
873      message.WeakAddRepeatedField(f("packed_enum"), foreignBar);
874      // Add a second one of each field.
875      message.WeakAddRepeatedField(f("packed_int32"), 701);
876      message.WeakAddRepeatedField(f("packed_int64"), 702L);
877      message.WeakAddRepeatedField(f("packed_uint32"), 703U);
878      message.WeakAddRepeatedField(f("packed_uint64"), 704UL);
879      message.WeakAddRepeatedField(f("packed_sint32"), 705);
880      message.WeakAddRepeatedField(f("packed_sint64"), 706L);
881      message.WeakAddRepeatedField(f("packed_fixed32"), 707U);
882      message.WeakAddRepeatedField(f("packed_fixed64"), 708UL);
883      message.WeakAddRepeatedField(f("packed_sfixed32"), 709);
884      message.WeakAddRepeatedField(f("packed_sfixed64"), 710L);
885      message.WeakAddRepeatedField(f("packed_float"), 711F);
886      message.WeakAddRepeatedField(f("packed_double"), 712D);
887      message.WeakAddRepeatedField(f("packed_bool"), false);
888      message.WeakAddRepeatedField(f("packed_enum"), foreignBaz);
889    }
890
891    public void AssertPackedFieldsSetViaReflection(IMessage message) {
892      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_int32")));
893      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_int64")));
894      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_uint32")));
895      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_uint64")));
896      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_sint32")));
897      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_sint64")));
898      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_fixed32")));
899      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_fixed64")));
900      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_sfixed32")));
901      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_sfixed64")));
902      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_float")));
903      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_double")));
904      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_bool")));
905      Assert.AreEqual(2, message.GetRepeatedFieldCount(f("packed_enum")));
906      Assert.AreEqual(601, message[f("packed_int32"), 0]);
907      Assert.AreEqual(602L, message[f("packed_int64"), 0]);
908      Assert.AreEqual(603, message[f("packed_uint32"), 0]);
909      Assert.AreEqual(604L, message[f("packed_uint64"), 0]);
910      Assert.AreEqual(605, message[f("packed_sint32"), 0]);
911      Assert.AreEqual(606L, message[f("packed_sint64"), 0]);
912      Assert.AreEqual(607, message[f("packed_fixed32"), 0]);
913      Assert.AreEqual(608L, message[f("packed_fixed64"), 0]);
914      Assert.AreEqual(609, message[f("packed_sfixed32"), 0]);
915      Assert.AreEqual(610L, message[f("packed_sfixed64"), 0]);
916      Assert.AreEqual(611F, message[f("packed_float"), 0]);
917      Assert.AreEqual(612D, message[f("packed_double"), 0]);
918      Assert.AreEqual(true, message[f("packed_bool"), 0]);
919      Assert.AreEqual(foreignBar, message[f("packed_enum"), 0]);
920      Assert.AreEqual(701, message[f("packed_int32"), 1]);
921      Assert.AreEqual(702L, message[f("packed_int64"), 1]);
922      Assert.AreEqual(703, message[f("packed_uint32"), 1]);
923      Assert.AreEqual(704L, message[f("packed_uint64"), 1]);
924      Assert.AreEqual(705, message[f("packed_sint32"), 1]);
925      Assert.AreEqual(706L, message[f("packed_sint64"), 1]);
926      Assert.AreEqual(707, message[f("packed_fixed32"), 1]);
927      Assert.AreEqual(708L, message[f("packed_fixed64"), 1]);
928      Assert.AreEqual(709, message[f("packed_sfixed32"), 1]);
929      Assert.AreEqual(710L, message[f("packed_sfixed64"), 1]);
930      Assert.AreEqual(711F, message[f("packed_float"), 1]);
931      Assert.AreEqual(712D, message[f("packed_double"), 1]);
932      Assert.AreEqual(false, message[f("packed_bool"), 1]);
933      Assert.AreEqual(foreignBaz, message[f("packed_enum"), 1]);
934    }
935  }
936}
Note: See TracBrowser for help on using the repository browser.