aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/ProtocolBuffers.Test
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2015-07-17 06:41:46 +0100
committerGravatar Jon Skeet <jonskeet@google.com>2015-07-17 06:41:46 +0100
commit34878cb14eba4578d1d67d2dc93250729d492774 (patch)
tree6764c5a6f20e7ac4f2c7205638222d7cbbe4a90f /csharp/src/ProtocolBuffers.Test
parent3da90e9f54f9648a76a0172e5af94f4a0a00a5e8 (diff)
Fixes from PR review.
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test')
-rw-r--r--csharp/src/ProtocolBuffers.Test/WellKnownTypes/WrappersTest.cs71
1 files changed, 68 insertions, 3 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/WellKnownTypes/WrappersTest.cs b/csharp/src/ProtocolBuffers.Test/WellKnownTypes/WrappersTest.cs
index 6c706014..ad88c4eb 100644
--- a/csharp/src/ProtocolBuffers.Test/WellKnownTypes/WrappersTest.cs
+++ b/csharp/src/ProtocolBuffers.Test/WellKnownTypes/WrappersTest.cs
@@ -34,6 +34,7 @@ using System;
using Google.Protobuf.TestProtos;
using NUnit.Framework;
using System.Collections;
+using System.IO;
namespace Google.Protobuf.WellKnownTypes
{
@@ -232,9 +233,44 @@ namespace Google.Protobuf.WellKnownTypes
Assert.IsTrue(dictionary.Contains(3));
}
- // Merging is odd with wrapper types, due to the way that default values aren't emitted in
- // the binary stream. In fact we cheat a little bit - a message with an explicitly present default
- // value will have that default value ignored.
+ [Test]
+ public void Oneof()
+ {
+ var message = new OneofWellKnownTypes { EmptyField = new Empty() };
+ // Start off with a non-wrapper
+ Assert.AreEqual(OneofWellKnownTypes.OneofFieldOneofCase.EmptyField, message.OneofFieldCase);
+ AssertOneofRoundTrip(message);
+
+ message.StringField = "foo";
+ Assert.AreEqual(OneofWellKnownTypes.OneofFieldOneofCase.StringField, message.OneofFieldCase);
+ AssertOneofRoundTrip(message);
+
+ message.StringField = "foo";
+ Assert.AreEqual(OneofWellKnownTypes.OneofFieldOneofCase.StringField, message.OneofFieldCase);
+ AssertOneofRoundTrip(message);
+
+ message.DoubleField = 0.0f;
+ Assert.AreEqual(OneofWellKnownTypes.OneofFieldOneofCase.DoubleField, message.OneofFieldCase);
+ AssertOneofRoundTrip(message);
+
+ message.DoubleField = 1.0f;
+ Assert.AreEqual(OneofWellKnownTypes.OneofFieldOneofCase.DoubleField, message.OneofFieldCase);
+ AssertOneofRoundTrip(message);
+
+ message.ClearOneofField();
+ Assert.AreEqual(OneofWellKnownTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
+ AssertOneofRoundTrip(message);
+ }
+
+ private void AssertOneofRoundTrip(OneofWellKnownTypes message)
+ {
+ // Normal roundtrip, but explicitly checking the case...
+ var bytes = message.ToByteArray();
+ var parsed = OneofWellKnownTypes.Parser.ParseFrom(bytes);
+ Assert.AreEqual(message, parsed);
+ Assert.AreEqual(message.OneofFieldCase, parsed.OneofFieldCase);
+ }
+
[Test]
[TestCase("x", "y", "y")]
[TestCase("x", "", "x")]
@@ -257,5 +293,34 @@ namespace Google.Protobuf.WellKnownTypes
originalMessage.MergeFrom(mergingMessage.ToByteArray());
Assert.AreEqual(expected, originalMessage.StringField);
}
+
+ // Merging is odd with wrapper types, due to the way that default values aren't emitted in
+ // the binary stream. In fact we cheat a little bit - a message with an explicitly present default
+ // value will have that default value ignored.
+ [Test]
+ public void MergingCornerCase()
+ {
+ var message = new TestWellKnownTypes { Int32Field = 5 };
+
+ // Create a byte array which has the data of an Int32Value explicitly containing a value of 0.
+ // This wouldn't normally happen.
+ byte[] bytes;
+ var wrapperTag = WireFormat.MakeTag(TestWellKnownTypes.Int32FieldFieldNumber, WireFormat.WireType.LengthDelimited);
+ var valueTag = WireFormat.MakeTag(Int32Value.ValueFieldNumber, WireFormat.WireType.Varint);
+ using (var stream = new MemoryStream())
+ {
+ var coded = CodedOutputStream.CreateInstance(stream);
+ coded.WriteTag(wrapperTag);
+ coded.WriteLength(2); // valueTag + a value 0, each one byte
+ coded.WriteTag(valueTag);
+ coded.WriteInt32(0);
+ coded.Flush();
+ bytes = stream.ToArray();
+ }
+
+ message.MergeFrom(bytes);
+ // A normal implementation would have 0 now, as the explicit default would have been overwritten the 5.
+ Assert.AreEqual(5, message.Int32Field);
+ }
}
}