add more tests

pull/7576/head
Jan Tattermusch 5 years ago
parent ecbb29d354
commit 2bce090f32
  1. 14
      csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs
  2. 19
      csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs

@ -132,6 +132,8 @@ namespace Google.Protobuf
byte[] bytes = message.ToByteArray();
Assert.AreEqual(0, bytes.Length);
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message);
}
@ -164,7 +166,7 @@ namespace Google.Protobuf
SingleUint64 = ulong.MaxValue
};
byte[] bytes = message.ToByteArray();
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message);
}
@ -198,7 +200,7 @@ namespace Google.Protobuf
RepeatedUint64 = { ulong.MaxValue, uint.MinValue }
};
byte[] bytes = message.ToByteArray();
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message);
}
@ -230,7 +232,7 @@ namespace Google.Protobuf
}
};
byte[] bytes = message.ToByteArray();
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestMap.Parser, message);
}
@ -246,6 +248,8 @@ namespace Google.Protobuf
byte[] bytes = message.ToByteArray();
Assert.AreEqual(2, bytes.Length); // Tag for field entry (1 byte), length of entry (0; 1 byte)
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertReadingMessage(
TestMap.Parser,
bytes,
@ -660,6 +664,8 @@ namespace Google.Protobuf
var bytes = message.ToByteArray();
Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - no string!
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message, parsedMessage =>
{
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, parsedMessage.OneofFieldCase);
@ -675,6 +681,8 @@ namespace Google.Protobuf
var bytes = message.ToByteArray();
Assert.AreEqual(3, bytes.Length); // 2 bytes for the tag + 1 for the value - it's still serialized
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestAllTypes.Parser, message, parsedMessage =>
{
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.OneofUint32, parsedMessage.OneofFieldCase);

@ -33,6 +33,7 @@
using NUnit.Framework;
using System;
using System.Buffers;
using Google.Protobuf.Buffers;
namespace Google.Protobuf
{
@ -81,6 +82,11 @@ namespace Google.Protobuf
{
var bytes = message.ToByteArray();
// also serialize using IBufferWriter and check it leads to the same data
var bufferWriter = new ArrayBufferWriter<byte>();
message.WriteTo(bufferWriter);
Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray(), "Both serialization approaches need to result in the same data.");
// Load content as single segment
var parsedBuffer = parser.ParseFrom(new ReadOnlySequence<byte>(bytes));
Assert.AreEqual(message, parsedBuffer);
@ -96,5 +102,18 @@ namespace Google.Protobuf
Assert.AreEqual(message, parsedStream);
additionalAssert?.Invoke(parsedStream);
}
public static void AssertWritingMessage(IMessage message)
{
// serialize using CodedOutputStream
var bytes = message.ToByteArray();
// also serialize using IBufferWriter and check it leads to the same data
var bufferWriter = new ArrayBufferWriter<byte>();
message.WriteTo(bufferWriter);
Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray(), "Both serialization approaches need to result in the same data.");
Assert.AreEqual(message.CalculateSize(), bytes.Length);
}
}
}
Loading…
Cancel
Save