Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers.Test/GeneratedMessageTest.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: 19.9 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 System.Collections.Generic;
37using Google.ProtocolBuffers.Collections;
38using Google.ProtocolBuffers.TestProtos;
39using NUnit.Framework;
40
41namespace Google.ProtocolBuffers {
42  [TestFixture]
43  public class GeneratedMessageTest {
44    ReflectionTester reflectionTester;
45    ReflectionTester extensionsReflectionTester;
46   
47    [SetUp]
48    public void SetUp() {
49      reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
50      extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
51    }
52
53    [Test]
54    public void RepeatedAddPrimitiveBeforeBuild() {
55      TestAllTypes message = new TestAllTypes.Builder { RepeatedInt32List = { 1, 2, 3 } }.Build();
56      TestUtil.AssertEqual(new int[]{1, 2, 3}, message.RepeatedInt32List);
57    }
58
59    [Test]
60    public void AddPrimitiveFailsAfterBuild() {
61      TestAllTypes.Builder builder = new TestAllTypes.Builder();
62      IList<int> list = builder.RepeatedInt32List;
63      list.Add(1); // Fine
64      builder.Build();
65
66      try {
67        list.Add(2);
68        Assert.Fail("List should be frozen");
69      } catch (NotSupportedException) {
70        // Expected
71      }
72    }
73
74    [Test]
75    public void RepeatedAddMessageBeforeBuild() {
76      TestAllTypes message = new TestAllTypes.Builder {
77          RepeatedNestedMessageList = { new TestAllTypes.Types.NestedMessage.Builder { Bb = 10 }.Build() } }.Build();
78      Assert.AreEqual(1, message.RepeatedNestedMessageCount);
79      Assert.AreEqual(10, message.RepeatedNestedMessageList[0].Bb);
80    }
81
82    [Test]
83    public void AddMessageFailsAfterBuild() {
84      TestAllTypes.Builder builder = new TestAllTypes.Builder();
85      IList<TestAllTypes.Types.NestedMessage> list = builder.RepeatedNestedMessageList;
86      builder.Build();
87
88      try {
89        list.Add(new TestAllTypes.Types.NestedMessage.Builder { Bb = 10 }.Build());
90        Assert.Fail("List should be frozen");
91      } catch (NotSupportedException) {
92        // Expected
93      }
94    }
95
96    [Test]
97    public void DoubleBuildError() {
98      TestAllTypes.Builder builder = new TestAllTypes.Builder();
99      builder.Build();
100      try {
101        builder.Build();
102        Assert.Fail("Should have thrown exception.");
103      } catch (InvalidOperationException) {
104        // Success.
105      }
106    }
107
108    [Test]
109    public void DefaultInstance() {
110      Assert.AreSame(TestAllTypes.DefaultInstance, TestAllTypes.DefaultInstance.DefaultInstanceForType);
111      Assert.AreSame(TestAllTypes.DefaultInstance, TestAllTypes.CreateBuilder().DefaultInstanceForType);
112    }
113
114    [Test]
115    public void Accessors() {
116      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
117      TestUtil.SetAllFields(builder);
118      TestAllTypes message = builder.Build();
119      TestUtil.AssertAllFieldsSet(message);
120    }
121
122    [Test]
123    public void SettersRejectNull() {
124      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
125      TestUtil.AssertArgumentNullException(() => builder.SetOptionalString(null));
126      TestUtil.AssertArgumentNullException(() => builder.SetOptionalBytes(null));
127      TestUtil.AssertArgumentNullException(() => builder.SetOptionalNestedMessage((TestAllTypes.Types.NestedMessage)null));
128      TestUtil.AssertArgumentNullException(() => builder.SetOptionalNestedMessage((TestAllTypes.Types.NestedMessage.Builder)null));
129      TestUtil.AssertArgumentNullException(() => builder.AddRepeatedString(null));
130      TestUtil.AssertArgumentNullException(() => builder.AddRepeatedBytes(null));
131      TestUtil.AssertArgumentNullException(() => builder.AddRepeatedNestedMessage((TestAllTypes.Types.NestedMessage)null));
132      TestUtil.AssertArgumentNullException(() => builder.AddRepeatedNestedMessage((TestAllTypes.Types.NestedMessage.Builder)null));
133    }
134
135    [Test]
136    public void RepeatedSetters() {
137      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
138      TestUtil.SetAllFields(builder);
139      TestUtil.ModifyRepeatedFields(builder);
140      TestAllTypes message = builder.Build();
141      TestUtil.AssertRepeatedFieldsModified(message);
142    }
143
144    [Test]
145    public void RepeatedAppend() {
146      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
147
148      builder.AddRangeRepeatedInt32(new int[]{1, 2, 3, 4});
149      builder.AddRangeRepeatedForeignEnum((new ForeignEnum[] { ForeignEnum.FOREIGN_BAZ }));
150
151      ForeignMessage foreignMessage = ForeignMessage.CreateBuilder().SetC(12).Build();
152      builder.AddRangeRepeatedForeignMessage(new ForeignMessage[] {foreignMessage});
153
154      TestAllTypes message = builder.Build();
155      TestUtil.AssertEqual(message.RepeatedInt32List, new int[]{1, 2, 3, 4});
156      TestUtil.AssertEqual(message.RepeatedForeignEnumList, new ForeignEnum[] {ForeignEnum.FOREIGN_BAZ});
157      Assert.AreEqual(1, message.RepeatedForeignMessageCount);
158      Assert.AreEqual(12, message.GetRepeatedForeignMessage(0).C);
159    }
160
161    [Test]
162    public void RepeatedAppendRejectsNull() {
163      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
164
165      ForeignMessage foreignMessage = ForeignMessage.CreateBuilder().SetC(12).Build();
166      TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedForeignMessage(new[] { foreignMessage, null }));
167      TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedForeignMessage(null));
168      TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedForeignEnum(null));
169      TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedString(new[] { "one", null }));
170      TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedBytes(new[] { TestUtil.ToBytes("one"), null }));
171    }
172
173    [Test]
174    public void SettingForeignMessageUsingBuilder() {
175      TestAllTypes message = TestAllTypes.CreateBuilder()
176          // Pass builder for foreign message instance.
177          .SetOptionalForeignMessage(ForeignMessage.CreateBuilder().SetC(123))
178          .Build();
179      TestAllTypes expectedMessage = TestAllTypes.CreateBuilder()
180          // Create expected version passing foreign message instance explicitly.
181          .SetOptionalForeignMessage(ForeignMessage.CreateBuilder().SetC(123).Build())
182          .Build();
183      Assert.AreEqual(expectedMessage, message);
184    }
185
186    [Test]
187    public void SettingRepeatedForeignMessageUsingBuilder() {
188      TestAllTypes message = TestAllTypes.CreateBuilder()
189          // Pass builder for foreign message instance.
190          .AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(456))
191          .Build();
192      TestAllTypes expectedMessage = TestAllTypes.CreateBuilder()
193          // Create expected version passing foreign message instance explicitly.
194          .AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(456).Build())
195          .Build();
196      Assert.AreEqual(expectedMessage, message);
197    }
198
199    [Test]
200    public void SettingRepeatedValuesUsingRangeInCollectionInitializer() {
201      int[] values = { 1, 2, 3 };
202      TestAllTypes message = new TestAllTypes.Builder {
203        RepeatedSint32List = { values }
204      }.Build();
205      Assert.IsTrue(Lists.Equals(values, message.RepeatedSint32List));
206    }
207
208    [Test]
209    public void SettingRepeatedValuesUsingIndividualValuesInCollectionInitializer() {
210      TestAllTypes message = new TestAllTypes.Builder {
211        RepeatedSint32List = { 6, 7 }
212      }.Build();
213      Assert.IsTrue(Lists.Equals(new int[] { 6, 7 }, message.RepeatedSint32List));
214    }
215   
216    [Test]
217    public void Defaults() {
218      TestUtil.AssertClear(TestAllTypes.DefaultInstance);
219      TestUtil.AssertClear(TestAllTypes.CreateBuilder().Build());
220
221      Assert.AreEqual("\u1234", TestExtremeDefaultValues.DefaultInstance.Utf8String);
222    }
223
224    [Test]
225    public void ReflectionGetters() {
226      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
227      TestUtil.SetAllFields(builder);
228      TestAllTypes message = builder.Build();
229      reflectionTester.AssertAllFieldsSetViaReflection(message);
230    }
231
232    [Test]
233    public void ReflectionSetters() {
234      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
235      reflectionTester.SetAllFieldsViaReflection(builder);
236      TestAllTypes message = builder.Build();
237      TestUtil.AssertAllFieldsSet(message);
238    }
239
240    [Test]
241    public void ReflectionSettersRejectNull() {
242      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
243      reflectionTester.AssertReflectionSettersRejectNull(builder);
244    }
245    [Test]
246    public void ReflectionRepeatedSetters() {
247      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
248      reflectionTester.SetAllFieldsViaReflection(builder);
249      reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
250      TestAllTypes message = builder.Build();
251      TestUtil.AssertRepeatedFieldsModified(message);
252    }
253
254    [Test]
255    public void TestReflectionRepeatedSettersRejectNull() {
256      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
257      reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
258    }
259
260    [Test]
261    public void ReflectionDefaults() {
262      TestUtil.TestInMultipleCultures(() => {
263        reflectionTester.AssertClearViaReflection(TestAllTypes.DefaultInstance);
264        reflectionTester.AssertClearViaReflection(TestAllTypes.CreateBuilder().Build());
265      });
266    }
267    // =================================================================
268    // Extensions.
269
270    [Test]
271    public void ExtensionAccessors() {
272      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
273      TestUtil.SetAllExtensions(builder);
274      TestAllExtensions message = builder.Build();
275      TestUtil.AssertAllExtensionsSet(message);
276    }
277
278    [Test]
279    public void ExtensionRepeatedSetters() {
280      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
281      TestUtil.SetAllExtensions(builder);
282      TestUtil.ModifyRepeatedExtensions(builder);
283      TestAllExtensions message = builder.Build();
284      TestUtil.AssertRepeatedExtensionsModified(message);
285    }
286
287    [Test]
288    public void ExtensionDefaults() {
289      TestUtil.AssertExtensionsClear(TestAllExtensions.DefaultInstance);
290      TestUtil.AssertExtensionsClear(TestAllExtensions.CreateBuilder().Build());
291    }
292
293    [Test]
294    public void ExtensionReflectionGetters() {
295      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
296      TestUtil.SetAllExtensions(builder);
297      TestAllExtensions message = builder.Build();
298      extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
299    }
300
301    [Test]
302    public void ExtensionReflectionSetters() {
303      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
304      extensionsReflectionTester.SetAllFieldsViaReflection(builder);
305      TestAllExtensions message = builder.Build();
306      TestUtil.AssertAllExtensionsSet(message);
307    }
308
309    [Test]
310    public void ExtensionReflectionSettersRejectNull() {
311      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
312      extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
313    }
314
315    [Test]
316    public void ExtensionReflectionRepeatedSetters() {
317      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
318      extensionsReflectionTester.SetAllFieldsViaReflection(builder);
319      extensionsReflectionTester.ModifyRepeatedFieldsViaReflection(builder);
320      TestAllExtensions message = builder.Build();
321      TestUtil.AssertRepeatedExtensionsModified(message);
322    }
323
324    [Test]
325    public void ExtensionReflectionRepeatedSettersRejectNull() {
326      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
327      extensionsReflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
328    }
329
330    [Test]
331    public void ExtensionReflectionDefaults() {
332      TestUtil.TestInMultipleCultures(() => {
333        extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
334        extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
335      });
336    }   
337   
338    [Test]
339    public void ClearExtension() {
340      // ClearExtension() is not actually used in TestUtil, so try it manually.
341      Assert.IsFalse(TestAllExtensions.CreateBuilder()
342          .SetExtension(UnitTestProtoFile.OptionalInt32Extension, 1)
343          .ClearExtension(UnitTestProtoFile.OptionalInt32Extension)
344          .HasExtension(UnitTestProtoFile.OptionalInt32Extension));
345      Assert.AreEqual(0, TestAllExtensions.CreateBuilder()
346          .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 1)
347          .ClearExtension(UnitTestProtoFile.RepeatedInt32Extension)
348          .GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
349    }
350
351    [Test]
352    public void ExtensionMergeFrom() {
353      TestAllExtensions original = TestAllExtensions.CreateBuilder()
354          .SetExtension(UnitTestProtoFile.OptionalInt32Extension, 1).Build();
355      TestAllExtensions merged =
356          TestAllExtensions.CreateBuilder().MergeFrom(original).Build();
357      Assert.IsTrue((merged.HasExtension(UnitTestProtoFile.OptionalInt32Extension)));
358      Assert.AreEqual(1, (int)merged.GetExtension(UnitTestProtoFile.OptionalInt32Extension));
359    }   
360
361    /* Removed multiple files option for the moment
362    [Test]
363    public void MultipleFilesOption() {
364      // We mostly just want to check that things compile.
365      MessageWithNoOuter message = MessageWithNoOuter.CreateBuilder()
366          .SetNested(MessageWithNoOuter.Types.NestedMessage.CreateBuilder().SetI(1))
367          .AddForeign(TestAllTypes.CreateBuilder().SetOptionalInt32(1))
368          .SetNestedEnum(MessageWithNoOuter.Types.NestedEnum.BAZ)
369          .SetForeignEnum(EnumWithNoOuter.BAR)
370          .Build();
371      Assert.AreEqual(message, MessageWithNoOuter.ParseFrom(message.ToByteString()));
372
373      Assert.AreEqual(MultiFileProto.Descriptor, MessageWithNoOuter.Descriptor.File);
374
375      FieldDescriptor field = MessageWithNoOuter.Descriptor.FindDescriptor<FieldDescriptor>("foreign_enum");
376      Assert.AreEqual(MultiFileProto.Descriptor.FindTypeByName<EnumDescriptor>("EnumWithNoOuter")
377        .FindValueByNumber((int)EnumWithNoOuter.BAR), message[field]);
378
379      Assert.AreEqual(MultiFileProto.Descriptor, ServiceWithNoOuter.Descriptor.File);
380
381      Assert.IsFalse(TestAllExtensions.DefaultInstance.HasExtension(MultiFileProto.ExtensionWithOuter));
382    }*/
383
384    [Test]
385    public void OptionalFieldWithRequiredSubfieldsOptimizedForSize() {     
386      TestOptionalOptimizedForSize message = TestOptionalOptimizedForSize.DefaultInstance;
387      Assert.IsTrue(message.IsInitialized);
388
389      message = TestOptionalOptimizedForSize.CreateBuilder().SetO(
390          TestRequiredOptimizedForSize.CreateBuilder().BuildPartial()
391          ).BuildPartial();
392      Assert.IsFalse(message.IsInitialized);
393
394      message = TestOptionalOptimizedForSize.CreateBuilder().SetO(
395          TestRequiredOptimizedForSize.CreateBuilder().SetX(5).BuildPartial()
396          ).BuildPartial();
397      Assert.IsTrue(message.IsInitialized);
398    }
399
400    [Test]
401    public void OptimizedForSizeMergeUsesAllFieldsFromTarget() {
402      TestOptimizedForSize withFieldSet = new TestOptimizedForSize.Builder { I = 10 }.Build();
403      TestOptimizedForSize.Builder builder = new TestOptimizedForSize.Builder();
404      builder.MergeFrom(withFieldSet);
405      TestOptimizedForSize built = builder.Build();
406      Assert.AreEqual(10, built.I);
407    }
408
409    [Test]
410    public void UninitializedExtensionInOptimizedForSizeMakesMessageUninitialized() {
411      TestOptimizedForSize.Builder builder = new TestOptimizedForSize.Builder();
412      builder.SetExtension(TestOptimizedForSize.TestExtension2,
413          new TestRequiredOptimizedForSize.Builder().BuildPartial());
414      Assert.IsFalse(builder.IsInitialized);
415      Assert.IsFalse(builder.BuildPartial().IsInitialized);
416
417      builder = new TestOptimizedForSize.Builder();
418      builder.SetExtension(TestOptimizedForSize.TestExtension2,
419          new TestRequiredOptimizedForSize.Builder { X = 10 }.BuildPartial());
420      Assert.IsTrue(builder.IsInitialized);
421      Assert.IsTrue(builder.BuildPartial().IsInitialized);
422    }
423
424    [Test]
425    public void ToBuilder() {
426      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
427      TestUtil.SetAllFields(builder);
428      TestAllTypes message = builder.Build();
429      TestUtil.AssertAllFieldsSet(message.ToBuilder().Build());
430    }
431
432    [Test]
433    public void FieldConstantValues() {
434      Assert.AreEqual(TestAllTypes.Types.NestedMessage.BbFieldNumber, 1);
435      Assert.AreEqual(TestAllTypes.OptionalInt32FieldNumber, 1);
436      Assert.AreEqual(TestAllTypes.OptionalGroupFieldNumber, 16);
437      Assert.AreEqual(TestAllTypes.OptionalNestedMessageFieldNumber, 18);
438      Assert.AreEqual(TestAllTypes.OptionalNestedEnumFieldNumber, 21);
439      Assert.AreEqual(TestAllTypes.RepeatedInt32FieldNumber, 31);
440      Assert.AreEqual(TestAllTypes.RepeatedGroupFieldNumber, 46);
441      Assert.AreEqual(TestAllTypes.RepeatedNestedMessageFieldNumber, 48);
442      Assert.AreEqual(TestAllTypes.RepeatedNestedEnumFieldNumber, 51);
443    }
444
445    [Test]
446    public void ExtensionConstantValues() {
447      Assert.AreEqual(TestRequired.SingleFieldNumber, 1000);
448      Assert.AreEqual(TestRequired.MultiFieldNumber, 1001);
449      Assert.AreEqual(UnitTestProtoFile.OptionalInt32ExtensionFieldNumber, 1);
450      Assert.AreEqual(UnitTestProtoFile.OptionalGroupExtensionFieldNumber, 16);
451      Assert.AreEqual(UnitTestProtoFile.OptionalNestedMessageExtensionFieldNumber, 18);
452      Assert.AreEqual(UnitTestProtoFile.OptionalNestedEnumExtensionFieldNumber, 21);
453      Assert.AreEqual(UnitTestProtoFile.RepeatedInt32ExtensionFieldNumber, 31);
454      Assert.AreEqual(UnitTestProtoFile.RepeatedGroupExtensionFieldNumber, 46);
455      Assert.AreEqual(UnitTestProtoFile.RepeatedNestedMessageExtensionFieldNumber, 48);
456      Assert.AreEqual(UnitTestProtoFile.RepeatedNestedEnumExtensionFieldNumber, 51);
457    }
458
459    [Test]
460    public void EmptyPackedValue() {
461      TestPackedTypes empty = new TestPackedTypes.Builder().Build();
462      Assert.AreEqual(0, empty.SerializedSize);
463    }
464  }
465}
Note: See TracBrowser for help on using the repository browser.