OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2015 Google Inc. All rights reserved. | 3 // Copyright 2015 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 public void DefaultValues() | 59 public void DefaultValues() |
60 { | 60 { |
61 // Single fields | 61 // Single fields |
62 var message = new TestAllTypes(); | 62 var message = new TestAllTypes(); |
63 Assert.AreEqual(false, message.SingleBool); | 63 Assert.AreEqual(false, message.SingleBool); |
64 Assert.AreEqual(ByteString.Empty, message.SingleBytes); | 64 Assert.AreEqual(ByteString.Empty, message.SingleBytes); |
65 Assert.AreEqual(0.0, message.SingleDouble); | 65 Assert.AreEqual(0.0, message.SingleDouble); |
66 Assert.AreEqual(0, message.SingleFixed32); | 66 Assert.AreEqual(0, message.SingleFixed32); |
67 Assert.AreEqual(0L, message.SingleFixed64); | 67 Assert.AreEqual(0L, message.SingleFixed64); |
68 Assert.AreEqual(0.0f, message.SingleFloat); | 68 Assert.AreEqual(0.0f, message.SingleFloat); |
69 Assert.AreEqual(ForeignEnum.FOREIGN_UNSPECIFIED, message.SingleForei
gnEnum); | 69 Assert.AreEqual(ForeignEnum.ForeignUnspecified, message.SingleForeig
nEnum); |
70 Assert.IsNull(message.SingleForeignMessage); | 70 Assert.IsNull(message.SingleForeignMessage); |
71 Assert.AreEqual(ImportEnum.IMPORT_ENUM_UNSPECIFIED, message.SingleIm
portEnum); | 71 Assert.AreEqual(ImportEnum.Unspecified, message.SingleImportEnum); |
72 Assert.IsNull(message.SingleImportMessage); | 72 Assert.IsNull(message.SingleImportMessage); |
73 Assert.AreEqual(0, message.SingleInt32); | 73 Assert.AreEqual(0, message.SingleInt32); |
74 Assert.AreEqual(0L, message.SingleInt64); | 74 Assert.AreEqual(0L, message.SingleInt64); |
75 Assert.AreEqual(TestAllTypes.Types.NestedEnum.NESTED_ENUM_UNSPECIFIE
D, message.SingleNestedEnum); | 75 Assert.AreEqual(TestAllTypes.Types.NestedEnum.Unspecified, message.S
ingleNestedEnum); |
76 Assert.IsNull(message.SingleNestedMessage); | 76 Assert.IsNull(message.SingleNestedMessage); |
77 Assert.IsNull(message.SinglePublicImportMessage); | 77 Assert.IsNull(message.SinglePublicImportMessage); |
78 Assert.AreEqual(0, message.SingleSfixed32); | 78 Assert.AreEqual(0, message.SingleSfixed32); |
79 Assert.AreEqual(0L, message.SingleSfixed64); | 79 Assert.AreEqual(0L, message.SingleSfixed64); |
80 Assert.AreEqual(0, message.SingleSint32); | 80 Assert.AreEqual(0, message.SingleSint32); |
81 Assert.AreEqual(0L, message.SingleSint64); | 81 Assert.AreEqual(0L, message.SingleSint64); |
82 Assert.AreEqual("", message.SingleString); | 82 Assert.AreEqual("", message.SingleString); |
83 Assert.AreEqual(0U, message.SingleUint32); | 83 Assert.AreEqual(0U, message.SingleUint32); |
84 Assert.AreEqual(0UL, message.SingleUint64); | 84 Assert.AreEqual(0UL, message.SingleUint64); |
85 | 85 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 public void RoundTrip_SingleValues() | 138 public void RoundTrip_SingleValues() |
139 { | 139 { |
140 var message = new TestAllTypes | 140 var message = new TestAllTypes |
141 { | 141 { |
142 SingleBool = true, | 142 SingleBool = true, |
143 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), | 143 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), |
144 SingleDouble = 23.5, | 144 SingleDouble = 23.5, |
145 SingleFixed32 = 23, | 145 SingleFixed32 = 23, |
146 SingleFixed64 = 1234567890123, | 146 SingleFixed64 = 1234567890123, |
147 SingleFloat = 12.25f, | 147 SingleFloat = 12.25f, |
148 SingleForeignEnum = ForeignEnum.FOREIGN_BAR, | 148 SingleForeignEnum = ForeignEnum.ForeignBar, |
149 SingleForeignMessage = new ForeignMessage { C = 10 }, | 149 SingleForeignMessage = new ForeignMessage { C = 10 }, |
150 SingleImportEnum = ImportEnum.IMPORT_BAZ, | 150 SingleImportEnum = ImportEnum.ImportBaz, |
151 SingleImportMessage = new ImportMessage { D = 20 }, | 151 SingleImportMessage = new ImportMessage { D = 20 }, |
152 SingleInt32 = 100, | 152 SingleInt32 = 100, |
153 SingleInt64 = 3210987654321, | 153 SingleInt64 = 3210987654321, |
154 SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO, | 154 SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo, |
155 SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb
= 35 }, | 155 SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb
= 35 }, |
156 SinglePublicImportMessage = new PublicImportMessage { E = 54 }, | 156 SinglePublicImportMessage = new PublicImportMessage { E = 54 }, |
157 SingleSfixed32 = -123, | 157 SingleSfixed32 = -123, |
158 SingleSfixed64 = -12345678901234, | 158 SingleSfixed64 = -12345678901234, |
159 SingleSint32 = -456, | 159 SingleSint32 = -456, |
160 SingleSint64 = -12345678901235, | 160 SingleSint64 = -12345678901235, |
161 SingleString = "test", | 161 SingleString = "test", |
162 SingleUint32 = uint.MaxValue, | 162 SingleUint32 = uint.MaxValue, |
163 SingleUint64 = ulong.MaxValue | 163 SingleUint64 = ulong.MaxValue |
164 }; | 164 }; |
165 | 165 |
166 byte[] bytes = message.ToByteArray(); | 166 byte[] bytes = message.ToByteArray(); |
167 TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); | 167 TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); |
168 Assert.AreEqual(message, parsed); | 168 Assert.AreEqual(message, parsed); |
169 } | 169 } |
170 | 170 |
171 [Test] | 171 [Test] |
172 public void RoundTrip_RepeatedValues() | 172 public void RoundTrip_RepeatedValues() |
173 { | 173 { |
174 var message = new TestAllTypes | 174 var message = new TestAllTypes |
175 { | 175 { |
176 RepeatedBool = { true, false }, | 176 RepeatedBool = { true, false }, |
177 RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.Co
pyFrom(5, 6) }, | 177 RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.Co
pyFrom(5, 6) }, |
178 RepeatedDouble = { -12.25, 23.5 }, | 178 RepeatedDouble = { -12.25, 23.5 }, |
179 RepeatedFixed32 = { uint.MaxValue, 23 }, | 179 RepeatedFixed32 = { uint.MaxValue, 23 }, |
180 RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, | 180 RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, |
181 RepeatedFloat = { 100f, 12.25f }, | 181 RepeatedFloat = { 100f, 12.25f }, |
182 RepeatedForeignEnum = { ForeignEnum.FOREIGN_FOO, ForeignEnum.FOR
EIGN_BAR }, | 182 RepeatedForeignEnum = { ForeignEnum.ForeignFoo, ForeignEnum.Fore
ignBar }, |
183 RepeatedForeignMessage = { new ForeignMessage(), new ForeignMess
age { C = 10 } }, | 183 RepeatedForeignMessage = { new ForeignMessage(), new ForeignMess
age { C = 10 } }, |
184 RepeatedImportEnum = { ImportEnum.IMPORT_BAZ, ImportEnum.IMPORT_
ENUM_UNSPECIFIED }, | 184 RepeatedImportEnum = { ImportEnum.ImportBaz, ImportEnum.Unspecif
ied }, |
185 RepeatedImportMessage = { new ImportMessage { D = 20 }, new Impo
rtMessage { D = 25 } }, | 185 RepeatedImportMessage = { new ImportMessage { D = 20 }, new Impo
rtMessage { D = 25 } }, |
186 RepeatedInt32 = { 100, 200 }, | 186 RepeatedInt32 = { 100, 200 }, |
187 RepeatedInt64 = { 3210987654321, long.MaxValue }, | 187 RepeatedInt64 = { 3210987654321, long.MaxValue }, |
188 RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.FOO, TestAl
lTypes.Types.NestedEnum.NEG }, | 188 RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAl
lTypes.Types.NestedEnum.Neg }, |
189 RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage {
Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } }, | 189 RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage {
Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } }, |
190 RepeatedPublicImportMessage = { new PublicImportMessage { E = 54
}, new PublicImportMessage { E = -1 } }, | 190 RepeatedPublicImportMessage = { new PublicImportMessage { E = 54
}, new PublicImportMessage { E = -1 } }, |
191 RepeatedSfixed32 = { -123, 123 }, | 191 RepeatedSfixed32 = { -123, 123 }, |
192 RepeatedSfixed64 = { -12345678901234, 12345678901234 }, | 192 RepeatedSfixed64 = { -12345678901234, 12345678901234 }, |
193 RepeatedSint32 = { -456, 100 }, | 193 RepeatedSint32 = { -456, 100 }, |
194 RepeatedSint64 = { -12345678901235, 123 }, | 194 RepeatedSint64 = { -12345678901235, 123 }, |
195 RepeatedString = { "foo", "bar" }, | 195 RepeatedString = { "foo", "bar" }, |
196 RepeatedUint32 = { uint.MaxValue, uint.MinValue }, | 196 RepeatedUint32 = { uint.MaxValue, uint.MinValue }, |
197 RepeatedUint64 = { ulong.MaxValue, uint.MinValue } | 197 RepeatedUint64 = { ulong.MaxValue, uint.MinValue } |
198 }; | 198 }; |
(...skipping 15 matching lines...) Expand all Loading... |
214 { false, true }, | 214 { false, true }, |
215 { true, false } | 215 { true, false } |
216 }, | 216 }, |
217 MapInt32Bytes = { | 217 MapInt32Bytes = { |
218 { 5, ByteString.CopyFrom(6, 7, 8) }, | 218 { 5, ByteString.CopyFrom(6, 7, 8) }, |
219 { 25, ByteString.CopyFrom(1, 2, 3, 4, 5) }, | 219 { 25, ByteString.CopyFrom(1, 2, 3, 4, 5) }, |
220 { 10, ByteString.Empty } | 220 { 10, ByteString.Empty } |
221 }, | 221 }, |
222 MapInt32ForeignMessage = { | 222 MapInt32ForeignMessage = { |
223 { 0, new ForeignMessage { C = 10 } }, | 223 { 0, new ForeignMessage { C = 10 } }, |
224 { 5, null }, | 224 { 5, new ForeignMessage() }, |
225 }, | 225 }, |
226 MapInt32Enum = { | 226 MapInt32Enum = { |
227 { 1, MapEnum.MAP_ENUM_BAR }, | 227 { 1, MapEnum.Bar }, |
228 { 2000, MapEnum.MAP_ENUM_FOO } | 228 { 2000, MapEnum.Foo } |
229 } | 229 } |
230 }; | 230 }; |
231 | 231 |
232 byte[] bytes = message.ToByteArray(); | 232 byte[] bytes = message.ToByteArray(); |
233 TestMap parsed = TestMap.Parser.ParseFrom(bytes); | 233 TestMap parsed = TestMap.Parser.ParseFrom(bytes); |
234 Assert.AreEqual(message, parsed); | 234 Assert.AreEqual(message, parsed); |
235 } | 235 } |
236 | 236 |
237 [Test] | 237 [Test] |
238 public void MapWithEmptyEntry() | 238 public void MapWithEmptyEntry() |
239 { | 239 { |
240 var message = new TestMap | 240 var message = new TestMap |
241 { | 241 { |
242 MapInt32Bytes = { { 0, ByteString.Empty } } | 242 MapInt32Bytes = { { 0, ByteString.Empty } } |
243 }; | 243 }; |
244 | 244 |
245 byte[] bytes = message.ToByteArray(); | 245 byte[] bytes = message.ToByteArray(); |
246 Assert.AreEqual(2, bytes.Length); // Tag for field entry (1 byte), l
ength of entry (0; 1 byte) | 246 Assert.AreEqual(2, bytes.Length); // Tag for field entry (1 byte), l
ength of entry (0; 1 byte) |
247 | 247 |
248 var parsed = TestMap.Parser.ParseFrom(bytes); | 248 var parsed = TestMap.Parser.ParseFrom(bytes); |
249 Assert.AreEqual(1, parsed.MapInt32Bytes.Count); | 249 Assert.AreEqual(1, parsed.MapInt32Bytes.Count); |
250 Assert.AreEqual(ByteString.Empty, parsed.MapInt32Bytes[0]); | 250 Assert.AreEqual(ByteString.Empty, parsed.MapInt32Bytes[0]); |
251 } | 251 } |
252 | 252 |
253 [Test] | 253 [Test] |
254 public void MapWithOnlyValue() | 254 public void MapWithOnlyValue() |
255 { | 255 { |
256 // Hand-craft the stream to contain a single entry with just a value
. | 256 // Hand-craft the stream to contain a single entry with just a value
. |
257 var memoryStream = new MemoryStream(); | 257 var memoryStream = new MemoryStream(); |
258 var output = new CodedOutputStream(memoryStream); | 258 var output = new CodedOutputStream(memoryStream); |
259 output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireForma
t.WireType.LengthDelimited); | 259 output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireForma
t.WireType.LengthDelimited); |
260 var nestedMessage = new ForeignMessage { C = 20 }; | 260 var nestedMessage = new ForeignMessage { C = 20 }; |
261 // Size of the entry (tag, size written by WriteMessage, data writte
n by WriteMessage) | 261 // Size of the entry (tag, size written by WriteMessage, data writte
n by WriteMessage) |
262 output.WriteLength(2 + nestedMessage.CalculateSize()); | 262 output.WriteLength(2 + nestedMessage.CalculateSize()); |
263 output.WriteTag(2, WireFormat.WireType.LengthDelimited); | 263 output.WriteTag(2, WireFormat.WireType.LengthDelimited); |
264 output.WriteMessage(nestedMessage); | 264 output.WriteMessage(nestedMessage); |
265 output.Flush(); | 265 output.Flush(); |
266 | 266 |
267 var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); | 267 var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); |
268 Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]); | 268 Assert.AreEqual(nestedMessage, parsed.MapInt32ForeignMessage[0]); |
269 } | 269 } |
270 | 270 |
271 [Test] | 271 [Test] |
| 272 public void MapWithOnlyKey_PrimitiveValue() |
| 273 { |
| 274 // Hand-craft the stream to contain a single entry with just a key. |
| 275 var memoryStream = new MemoryStream(); |
| 276 var output = new CodedOutputStream(memoryStream); |
| 277 output.WriteTag(TestMap.MapInt32DoubleFieldNumber, WireFormat.WireTy
pe.LengthDelimited); |
| 278 int key = 10; |
| 279 output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key)); |
| 280 output.WriteTag(1, WireFormat.WireType.Varint); |
| 281 output.WriteInt32(key); |
| 282 output.Flush(); |
| 283 |
| 284 var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); |
| 285 Assert.AreEqual(0.0, parsed.MapInt32Double[key]); |
| 286 } |
| 287 |
| 288 [Test] |
| 289 public void MapWithOnlyKey_MessageValue() |
| 290 { |
| 291 // Hand-craft the stream to contain a single entry with just a key. |
| 292 var memoryStream = new MemoryStream(); |
| 293 var output = new CodedOutputStream(memoryStream); |
| 294 output.WriteTag(TestMap.MapInt32ForeignMessageFieldNumber, WireForma
t.WireType.LengthDelimited); |
| 295 int key = 10; |
| 296 output.WriteLength(1 + CodedOutputStream.ComputeInt32Size(key)); |
| 297 output.WriteTag(1, WireFormat.WireType.Varint); |
| 298 output.WriteInt32(key); |
| 299 output.Flush(); |
| 300 |
| 301 var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); |
| 302 Assert.AreEqual(new ForeignMessage(), parsed.MapInt32ForeignMessage[
key]); |
| 303 } |
| 304 |
| 305 [Test] |
272 public void MapIgnoresExtraFieldsWithinEntryMessages() | 306 public void MapIgnoresExtraFieldsWithinEntryMessages() |
273 { | 307 { |
274 // Hand-craft the stream to contain a single entry with three fields | 308 // Hand-craft the stream to contain a single entry with three fields |
275 var memoryStream = new MemoryStream(); | 309 var memoryStream = new MemoryStream(); |
276 var output = new CodedOutputStream(memoryStream); | 310 var output = new CodedOutputStream(memoryStream); |
277 | 311 |
278 output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireTyp
e.LengthDelimited); | 312 output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireTyp
e.LengthDelimited); |
279 | 313 |
280 var key = 10; // Field 1 | 314 var key = 10; // Field 1 |
281 var value = 20; // Field 2 | 315 var value = 20; // Field 2 |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 var original = new TestAllTypes | 442 var original = new TestAllTypes |
409 { | 443 { |
410 SingleBool = true, | 444 SingleBool = true, |
411 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), | 445 SingleBytes = ByteString.CopyFrom(1, 2, 3, 4), |
412 SingleDouble = 23.5, | 446 SingleDouble = 23.5, |
413 SingleFixed32 = 23, | 447 SingleFixed32 = 23, |
414 SingleFixed64 = 1234567890123, | 448 SingleFixed64 = 1234567890123, |
415 SingleFloat = 12.25f, | 449 SingleFloat = 12.25f, |
416 SingleInt32 = 100, | 450 SingleInt32 = 100, |
417 SingleInt64 = 3210987654321, | 451 SingleInt64 = 3210987654321, |
418 SingleNestedEnum = TestAllTypes.Types.NestedEnum.FOO, | 452 SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo, |
419 SingleSfixed32 = -123, | 453 SingleSfixed32 = -123, |
420 SingleSfixed64 = -12345678901234, | 454 SingleSfixed64 = -12345678901234, |
421 SingleSint32 = -456, | 455 SingleSint32 = -456, |
422 SingleSint64 = -12345678901235, | 456 SingleSint64 = -12345678901235, |
423 SingleString = "test", | 457 SingleString = "test", |
424 SingleUint32 = uint.MaxValue, | 458 SingleUint32 = uint.MaxValue, |
425 SingleUint64 = ulong.MaxValue | 459 SingleUint64 = ulong.MaxValue |
426 }; | 460 }; |
427 var clone = original.Clone(); | 461 var clone = original.Clone(); |
428 Assert.AreNotSame(original, clone); | 462 Assert.AreNotSame(original, clone); |
429 Assert.AreEqual(original, clone); | 463 Assert.AreEqual(original, clone); |
430 // Just as a single example | 464 // Just as a single example |
431 clone.SingleInt32 = 150; | 465 clone.SingleInt32 = 150; |
432 Assert.AreNotEqual(original, clone); | 466 Assert.AreNotEqual(original, clone); |
433 } | 467 } |
434 | 468 |
435 [Test] | 469 [Test] |
436 public void CloneRepeatedNonMessageValues() | 470 public void CloneRepeatedNonMessageValues() |
437 { | 471 { |
438 var original = new TestAllTypes | 472 var original = new TestAllTypes |
439 { | 473 { |
440 RepeatedBool = { true, false }, | 474 RepeatedBool = { true, false }, |
441 RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.Co
pyFrom(5, 6) }, | 475 RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.Co
pyFrom(5, 6) }, |
442 RepeatedDouble = { -12.25, 23.5 }, | 476 RepeatedDouble = { -12.25, 23.5 }, |
443 RepeatedFixed32 = { uint.MaxValue, 23 }, | 477 RepeatedFixed32 = { uint.MaxValue, 23 }, |
444 RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, | 478 RepeatedFixed64 = { ulong.MaxValue, 1234567890123 }, |
445 RepeatedFloat = { 100f, 12.25f }, | 479 RepeatedFloat = { 100f, 12.25f }, |
446 RepeatedInt32 = { 100, 200 }, | 480 RepeatedInt32 = { 100, 200 }, |
447 RepeatedInt64 = { 3210987654321, long.MaxValue }, | 481 RepeatedInt64 = { 3210987654321, long.MaxValue }, |
448 RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.FOO, TestAl
lTypes.Types.NestedEnum.NEG }, | 482 RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAl
lTypes.Types.NestedEnum.Neg }, |
449 RepeatedSfixed32 = { -123, 123 }, | 483 RepeatedSfixed32 = { -123, 123 }, |
450 RepeatedSfixed64 = { -12345678901234, 12345678901234 }, | 484 RepeatedSfixed64 = { -12345678901234, 12345678901234 }, |
451 RepeatedSint32 = { -456, 100 }, | 485 RepeatedSint32 = { -456, 100 }, |
452 RepeatedSint64 = { -12345678901235, 123 }, | 486 RepeatedSint64 = { -12345678901235, 123 }, |
453 RepeatedString = { "foo", "bar" }, | 487 RepeatedString = { "foo", "bar" }, |
454 RepeatedUint32 = { uint.MaxValue, uint.MinValue }, | 488 RepeatedUint32 = { uint.MaxValue, uint.MinValue }, |
455 RepeatedUint64 = { ulong.MaxValue, uint.MinValue } | 489 RepeatedUint64 = { ulong.MaxValue, uint.MinValue } |
456 }; | 490 }; |
457 | 491 |
458 var clone = original.Clone(); | 492 var clone = original.Clone(); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 var empty = Empty.Parser.ParseFrom(data); | 663 var empty = Empty.Parser.ParseFrom(data); |
630 Assert.AreEqual(new Empty(), empty); | 664 Assert.AreEqual(new Empty(), empty); |
631 } | 665 } |
632 | 666 |
633 // This was originally seen as a conformance test failure. | 667 // This was originally seen as a conformance test failure. |
634 [Test] | 668 [Test] |
635 public void TruncatedMessageFieldThrows() | 669 public void TruncatedMessageFieldThrows() |
636 { | 670 { |
637 // 130, 3 is the message tag | 671 // 130, 3 is the message tag |
638 // 1 is the data length - but there's no data. | 672 // 1 is the data length - but there's no data. |
639 var data = new byte[] { 130, 3, 1 }; | 673 var data = new byte[] { 130, 3, 1 }; |
640 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseFrom(data)); | 674 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseFrom(data)); |
641 } | 675 } |
642 | 676 |
643 /// <summary> | 677 /// <summary> |
644 /// Demonstrates current behaviour with an extraneous end group tag - se
e issue 688 | 678 /// Demonstrates current behaviour with an extraneous end group tag - se
e issue 688 |
645 /// for details; we may want to change this. | 679 /// for details; we may want to change this. |
646 /// </summary> | 680 /// </summary> |
647 [Test] | 681 [Test] |
648 public void ExtraEndGroupSkipped() | 682 public void ExtraEndGroupThrows() |
649 { | 683 { |
650 var message = SampleMessages.CreateFullTestAllTypes(); | 684 var message = SampleMessages.CreateFullTestAllTypes(); |
651 var stream = new MemoryStream(); | 685 var stream = new MemoryStream(); |
652 var output = new CodedOutputStream(stream); | 686 var output = new CodedOutputStream(stream); |
653 | 687 |
654 output.WriteTag(100, WireFormat.WireType.EndGroup); | |
655 output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.Wi
reType.Fixed32); | 688 output.WriteTag(TestAllTypes.SingleFixed32FieldNumber, WireFormat.Wi
reType.Fixed32); |
656 output.WriteFixed32(123); | 689 output.WriteFixed32(123); |
| 690 output.WriteTag(100, WireFormat.WireType.EndGroup); |
657 | 691 |
658 output.Flush(); | 692 output.Flush(); |
659 | 693 |
660 stream.Position = 0; | 694 stream.Position = 0; |
661 var parsed = TestAllTypes.Parser.ParseFrom(stream); | 695 Assert.Throws<InvalidProtocolBufferException>(() => TestAllTypes.Par
ser.ParseFrom(stream)); |
662 Assert.AreEqual(new TestAllTypes { SingleFixed32 = 123 }, parsed); | 696 } |
| 697 |
| 698 [Test] |
| 699 public void CustomDiagnosticMessage_DirectToStringCall() |
| 700 { |
| 701 var message = new ForeignMessage { C = 31 }; |
| 702 Assert.AreEqual("{ \"c\": 31, \"@cInHex\": \"1f\" }", message.ToStri
ng()); |
| 703 Assert.AreEqual("{ \"c\": 31 }", JsonFormatter.Default.Format(messag
e)); |
| 704 } |
| 705 |
| 706 [Test] |
| 707 public void CustomDiagnosticMessage_Nested() |
| 708 { |
| 709 var message = new TestAllTypes { SingleForeignMessage = new ForeignM
essage { C = 16 } }; |
| 710 Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16, \"@cInHex\
": \"10\" } }", message.ToString()); |
| 711 Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16 } }", JsonF
ormatter.Default.Format(message)); |
| 712 } |
| 713 |
| 714 [Test] |
| 715 public void CustomDiagnosticMessage_DirectToTextWriterCall() |
| 716 { |
| 717 var message = new ForeignMessage { C = 31 }; |
| 718 var writer = new StringWriter(); |
| 719 JsonFormatter.Default.Format(message, writer); |
| 720 Assert.AreEqual("{ \"c\": 31 }", writer.ToString()); |
663 } | 721 } |
664 } | 722 } |
665 } | 723 } |
OLD | NEW |