aboutsummaryrefslogtreecommitdiff
path: root/csharp/ProtocolBuffers.Test/GeneratedMessageTest.cs
blob: f51f97551755ef74eaebe4d0d992cafa4316e8e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.
// http://code.google.com/p/protobuf/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;

namespace Google.ProtocolBuffers {
  [TestFixture]
  public class GeneratedMessageTest {
    ReflectionTester reflectionTester;
    ReflectionTester extensionsReflectionTester;
    
    [SetUp]
    public void SetUp() {
      reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
      extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
    }

    [Test]
    public void RepeatedAddPrimitiveBeforeBuild() {
      TestAllTypes message = new TestAllTypes.Builder { RepeatedInt32List = { 1, 2, 3 } }.Build();
      TestUtil.AssertEqual(new int[]{1, 2, 3}, message.RepeatedInt32List);
    }

    [Test]
    public void AddPrimitiveFailsAfterBuild() {
      TestAllTypes.Builder builder = new TestAllTypes.Builder();
      IList<int> list = builder.RepeatedInt32List;
      list.Add(1); // Fine
      builder.Build();

      try {
        list.Add(2);
        Assert.Fail("List should be frozen");
      } catch (NotSupportedException) {
        // Expected
      }
    }

    [Test]
    public void RepeatedAddMessageBeforeBuild() {
      TestAllTypes message = new TestAllTypes.Builder {
          RepeatedNestedMessageList = { new TestAllTypes.Types.NestedMessage.Builder { Bb = 10 }.Build() } }.Build();
      Assert.AreEqual(1, message.RepeatedNestedMessageCount);
      Assert.AreEqual(10, message.RepeatedNestedMessageList[0].Bb);
    }

    [Test]
    public void AddMessageFailsAfterBuild() {
      TestAllTypes.Builder builder = new TestAllTypes.Builder();
      IList<TestAllTypes.Types.NestedMessage> list = builder.RepeatedNestedMessageList;
      builder.Build();

      try {
        list.Add(new TestAllTypes.Types.NestedMessage.Builder { Bb = 10 }.Build());
        Assert.Fail("List should be frozen");
      } catch (NotSupportedException) {
        // Expected
      }
    }

    [Test]
    public void DefaultInstance() {
      Assert.AreSame(TestAllTypes.DefaultInstance, TestAllTypes.DefaultInstance.DefaultInstanceForType);
      Assert.AreSame(TestAllTypes.DefaultInstance, TestAllTypes.CreateBuilder().DefaultInstanceForType);
    }

    [Test]
    public void Accessors() {
      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
      TestUtil.SetAllFields(builder);
      TestAllTypes message = builder.Build();
      TestUtil.AssertAllFieldsSet(message);
    }
    
    [Test]
    public void RepeatedSetters() {
      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
      TestUtil.SetAllFields(builder);
      TestUtil.ModifyRepeatedFields(builder);
      TestAllTypes message = builder.Build();
      TestUtil.AssertRepeatedFieldsModified(message);
    }

    [Test]
    public void RepeatedAppend() {
      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();

      builder.AddRangeRepeatedInt32(new int[]{1, 2, 3, 4});
      builder.AddRangeRepeatedForeignEnum((new ForeignEnum[] { ForeignEnum.FOREIGN_BAZ }));

      ForeignMessage foreignMessage = ForeignMessage.CreateBuilder().SetC(12).Build();
      builder.AddRangeRepeatedForeignMessage(new ForeignMessage[] {foreignMessage});

      TestAllTypes message = builder.Build();
      TestUtil.AssertEqual(message.RepeatedInt32List, new int[]{1, 2, 3, 4});
      TestUtil.AssertEqual(message.RepeatedForeignEnumList, new ForeignEnum[] {ForeignEnum.FOREIGN_BAZ});
      Assert.AreEqual(1, message.RepeatedForeignMessageCount);
      Assert.AreEqual(12, message.GetRepeatedForeignMessage(0).C);
    }

    [Test]
    public void SettingForeignMessageUsingBuilder() {
      TestAllTypes message = TestAllTypes.CreateBuilder()
          // Pass builder for foreign message instance.
          .SetOptionalForeignMessage(ForeignMessage.CreateBuilder().SetC(123))
          .Build();
      TestAllTypes expectedMessage = TestAllTypes.CreateBuilder()
          // Create expected version passing foreign message instance explicitly.
          .SetOptionalForeignMessage(ForeignMessage.CreateBuilder().SetC(123).Build())
          .Build();
      Assert.AreEqual(expectedMessage, message);
    }

    [Test]
    public void SettingRepeatedForeignMessageUsingBuilder() {
      TestAllTypes message = TestAllTypes.CreateBuilder()
          // Pass builder for foreign message instance.
          .AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(456))
          .Build();
      TestAllTypes expectedMessage = TestAllTypes.CreateBuilder()
          // Create expected version passing foreign message instance explicitly.
          .AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(456).Build())
          .Build();
      Assert.AreEqual(expectedMessage, message);
    }

    
    [Test]
    public void Defaults() {
      TestUtil.AssertClear(TestAllTypes.DefaultInstance);
      TestUtil.AssertClear(TestAllTypes.CreateBuilder().Build());

      Assert.AreEqual("\u1234", TestExtremeDefaultValues.DefaultInstance.Utf8String);
    }

    [Test]
    public void ReflectionGetters() {
      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
      TestUtil.SetAllFields(builder);
      TestAllTypes message = builder.Build();
      reflectionTester.AssertAllFieldsSetViaReflection(message);
    }

    [Test]
    public void ReflectionSetters() {
      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
      reflectionTester.SetAllFieldsViaReflection(builder);
      TestAllTypes message = builder.Build();
      TestUtil.AssertAllFieldsSet(message);
    }

    [Test]
    public void ReflectionRepeatedSetters() {
      TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
      reflectionTester.SetAllFieldsViaReflection(builder);
      reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
      TestAllTypes message = builder.Build();
      TestUtil.AssertRepeatedFieldsModified(message);
    }

    [Test]
    public void ReflectionDefaults() {
      reflectionTester.AssertClearViaReflection(TestAllTypes.DefaultInstance);
      reflectionTester.AssertClearViaReflection(TestAllTypes.CreateBuilder().Build());
    }
    // =================================================================
    // Extensions.

    [Test]
    public void ExtensionAccessors() {
      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
      TestUtil.SetAllExtensions(builder);
      TestAllExtensions message = builder.Build();
      TestUtil.AssertAllExtensionsSet(message);
    }

    [Test]
    public void ExtensionRepeatedSetters() {
      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
      TestUtil.SetAllExtensions(builder);
      TestUtil.ModifyRepeatedExtensions(builder);
      TestAllExtensions message = builder.Build();
      TestUtil.AssertRepeatedExtensionsModified(message);
    }

    [Test]
    public void ExtensionDefaults() {
      TestUtil.AssertExtensionsClear(TestAllExtensions.DefaultInstance);
      TestUtil.AssertExtensionsClear(TestAllExtensions.CreateBuilder().Build());
    }

    [Test]
    public void ExtensionReflectionGetters() {
      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
      TestUtil.SetAllExtensions(builder);
      TestAllExtensions message = builder.Build();
      extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
    }

    [Test]
    public void ExtensionReflectionSetters() {
      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
      extensionsReflectionTester.SetAllFieldsViaReflection(builder);
      TestAllExtensions message = builder.Build();
      TestUtil.AssertAllExtensionsSet(message);
    }

    [Test]
    public void ExtensionReflectionRepeatedSetters() {
      TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
      extensionsReflectionTester.SetAllFieldsViaReflection(builder);
      extensionsReflectionTester.ModifyRepeatedFieldsViaReflection(builder);
      TestAllExtensions message = builder.Build();
      TestUtil.AssertRepeatedExtensionsModified(message);
    }

    [Test]
    public void ExtensionReflectionDefaults() {
      extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
      extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
    }    
    
    [Test]
    public void ClearExtension() {
      // ClearExtension() is not actually used in TestUtil, so try it manually.
      Assert.IsFalse(TestAllExtensions.CreateBuilder()
          .SetExtension(UnitTestProtoFile.OptionalInt32Extension, 1)
          .ClearExtension(UnitTestProtoFile.OptionalInt32Extension)
          .HasExtension(UnitTestProtoFile.OptionalInt32Extension));
      Assert.AreEqual(0, TestAllExtensions.CreateBuilder()
          .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 1)
          .ClearExtension(UnitTestProtoFile.RepeatedInt32Extension)
          .GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
    }

    [Test]
    public void MultipleFilesOption() {
      // We mostly just want to check that things compile.
      MessageWithNoOuter message = MessageWithNoOuter.CreateBuilder()
          .SetNested(MessageWithNoOuter.Types.NestedMessage.CreateBuilder().SetI(1))
          .AddForeign(TestAllTypes.CreateBuilder().SetOptionalInt32(1))
          .SetNestedEnum(MessageWithNoOuter.Types.NestedEnum.BAZ)
          .SetForeignEnum(EnumWithNoOuter.BAR)
          .Build();
      Assert.AreEqual(message, MessageWithNoOuter.ParseFrom(message.ToByteString()));

      Assert.AreEqual(MultiFileProto.Descriptor, MessageWithNoOuter.Descriptor.File);

      FieldDescriptor field = MessageWithNoOuter.Descriptor.FindDescriptor<FieldDescriptor>("foreign_enum");
      Assert.AreEqual(MultiFileProto.Descriptor.FindTypeByName<EnumDescriptor>("EnumWithNoOuter")
        .FindValueByNumber((int)EnumWithNoOuter.BAR), message[field]);

      Assert.AreEqual(MultiFileProto.Descriptor, ServiceWithNoOuter.Descriptor.File);

      Assert.IsFalse(TestAllExtensions.DefaultInstance.HasExtension(MultiFileProto.ExtensionWithOuter));
    }
  }
}