diff options
author | Jon Skeet <jonskeet@google.com> | 2015-06-24 17:21:55 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2015-06-25 09:39:28 +0100 |
commit | 0d684d34209f8405106e580af854c45fb7c3f16d (patch) | |
tree | 225fcaa27fdc29b3f454b7a4b29b5b843ee711df /csharp/src/ProtocolBuffers.Test | |
parent | 0698aa973740d2a666fed8281c84a884a4227aa4 (diff) | |
download | protobuf-0d684d34209f8405106e580af854c45fb7c3f16d.tar.gz protobuf-0d684d34209f8405106e580af854c45fb7c3f16d.tar.bz2 protobuf-0d684d34209f8405106e580af854c45fb7c3f16d.zip |
First pass at map support.
More tests required. Generated code in next commit.
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs index 26165428..81e35940 100644 --- a/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs +++ b/csharp/src/ProtocolBuffers.Test/GeneratedMessageTest.cs @@ -1,4 +1,6 @@ using System;
+using System.Configuration;
+using System.IO;
using Google.Protobuf.TestProtos;
using NUnit.Framework;
@@ -147,6 +149,65 @@ namespace Google.Protobuf }
[Test]
+ public void RoundTrip_Maps()
+ {
+ var message = new TestAllTypes
+ {
+ MapBoolToEnum = {
+ { false, TestAllTypes.Types.NestedEnum.BAR},
+ { true, TestAllTypes.Types.NestedEnum.BAZ}
+ },
+ MapInt32ToBytes = {
+ { 5, ByteString.CopyFrom(6, 7, 8) },
+ { 25, ByteString.CopyFrom(1, 2, 3, 4, 5) },
+ { 10, ByteString.Empty }
+ },
+ MapStringToNestedMessage = {
+ { "", new TestAllTypes.Types.NestedMessage { Bb = 10 } },
+ { "null value", null },
+ }
+ };
+
+ byte[] bytes = message.ToByteArray();
+ TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
+ Assert.AreEqual(message, parsed);
+ }
+
+ [Test]
+ public void MapWithEmptyEntry()
+ {
+ var message = new TestAllTypes
+ {
+ MapInt32ToBytes = { { 0, ByteString.Empty } }
+ };
+
+ byte[] bytes = message.ToByteArray();
+ Assert.AreEqual(3, bytes.Length); // Tag for field entry (2 bytes), length of entry (0; 1 byte)
+
+ var parsed = TestAllTypes.Parser.ParseFrom(bytes);
+ Assert.AreEqual(1, parsed.MapInt32ToBytes.Count);
+ Assert.AreEqual(ByteString.Empty, parsed.MapInt32ToBytes[0]);
+ }
+
+ [Test]
+ public void MapWithOnlyValue()
+ {
+ // Hand-craft the stream to contain a single entry with just a value.
+ var memoryStream = new MemoryStream();
+ var output = CodedOutputStream.CreateInstance(memoryStream);
+ output.WriteTag(TestAllTypes.MapStringToNestedMessageFieldNumber, WireFormat.WireType.LengthDelimited);
+ var nestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 20 };
+ // Size of the entry (tag, size written by WriteMessage, data written by WriteMessage)
+ output.WriteRawVarint32((uint)(nestedMessage.CalculateSize() + 3));
+ output.WriteTag(2, WireFormat.WireType.LengthDelimited);
+ output.WriteMessage(nestedMessage);
+ output.Flush();
+
+ var parsed = TestAllTypes.Parser.ParseFrom(memoryStream.ToArray());
+ Assert.AreEqual(nestedMessage, parsed.MapStringToNestedMessage[""]);
+ }
+
+ [Test]
public void CloneSingleNonMessageValues()
{
var original = new TestAllTypes
|