increase test coverage

pull/7576/head
Jan Tattermusch 5 years ago
parent 7bfaaba534
commit b3cdba1cfd
  1. 6
      csharp/src/Google.Protobuf.Test/GeneratedMessageTest.Proto2.cs
  2. 12
      csharp/src/Google.Protobuf.Test/MessageParsingHelpers.cs
  3. 8
      csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs

@ -344,6 +344,8 @@ namespace Google.Protobuf
} }
}; };
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(Proto2.TestAllTypes.Parser, message); MessageParsingHelpers.AssertRoundtrip(Proto2.TestAllTypes.Parser, message);
} }
@ -359,6 +361,8 @@ namespace Google.Protobuf
new RepeatedGroup_extension { A = 30 } new RepeatedGroup_extension { A = 30 }
}); });
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip( MessageParsingHelpers.AssertRoundtrip(
TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { UnittestExtensions.OptionalGroupExtension, UnittestExtensions.RepeatedGroupExtension }), TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { UnittestExtensions.OptionalGroupExtension, UnittestExtensions.RepeatedGroupExtension }),
message); message);
@ -370,6 +374,8 @@ namespace Google.Protobuf
var message = new TestGroupExtension(); var message = new TestGroupExtension();
message.SetExtension(TestNestedExtension.Extensions.OptionalGroupExtension, new TestNestedExtension.Types.OptionalGroup_extension { A = 10 }); message.SetExtension(TestNestedExtension.Extensions.OptionalGroupExtension, new TestNestedExtension.Types.OptionalGroup_extension { A = 10 });
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip( MessageParsingHelpers.AssertRoundtrip(
TestGroupExtension.Parser.WithExtensionRegistry(new ExtensionRegistry() { TestNestedExtension.Extensions.OptionalGroupExtension }), TestGroupExtension.Parser.WithExtensionRegistry(new ExtensionRegistry() { TestNestedExtension.Extensions.OptionalGroupExtension }),
message); message);

@ -108,12 +108,18 @@ namespace Google.Protobuf
// serialize using CodedOutputStream // serialize using CodedOutputStream
var bytes = message.ToByteArray(); var bytes = message.ToByteArray();
// also serialize using IBufferWriter and check it leads to the same data int messageSize = message.CalculateSize();
Assert.AreEqual(message.CalculateSize(), bytes.Length);
// serialize using IBufferWriter and check it leads to the same output
var bufferWriter = new ArrayBufferWriter<byte>(); var bufferWriter = new ArrayBufferWriter<byte>();
message.WriteTo(bufferWriter); message.WriteTo(bufferWriter);
Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray(), "Both serialization approaches need to result in the same data."); Assert.AreEqual(bytes, bufferWriter.WrittenSpan.ToArray());
Assert.AreEqual(message.CalculateSize(), bytes.Length); // serialize into a single span and check it leads to the same output
var singleSpan = new Span<byte>(new byte[messageSize]);
message.WriteTo(singleSpan);
Assert.AreEqual(bytes, singleSpan.ToArray());
// TODO: also test different chunk sizes for IBufferWriter // TODO: also test different chunk sizes for IBufferWriter
} }

@ -71,6 +71,8 @@ namespace Google.Protobuf.WellKnownTypes
Uint64Field = 4 Uint64Field = 4
}; };
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed => MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed =>
{ {
Assert.AreEqual("x", parsed.StringField); Assert.AreEqual("x", parsed.StringField);
@ -101,6 +103,8 @@ namespace Google.Protobuf.WellKnownTypes
Uint64Field = 0 Uint64Field = 0
}; };
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed => MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed =>
{ {
Assert.AreEqual("", parsed.StringField); Assert.AreEqual("", parsed.StringField);
@ -144,6 +148,8 @@ namespace Google.Protobuf.WellKnownTypes
// Just to test a single value for sanity... // Just to test a single value for sanity...
Assert.AreEqual("Second", message.StringField[1]); Assert.AreEqual("Second", message.StringField[1]);
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(RepeatedWellKnownTypes.Parser, message); MessageParsingHelpers.AssertRoundtrip(RepeatedWellKnownTypes.Parser, message);
} }
@ -196,6 +202,8 @@ namespace Google.Protobuf.WellKnownTypes
// Just to test a single value for sanity... // Just to test a single value for sanity...
Assert.AreEqual("Second", message.StringField[12]); Assert.AreEqual("Second", message.StringField[12]);
MessageParsingHelpers.AssertWritingMessage(message);
MessageParsingHelpers.AssertRoundtrip(MapWellKnownTypes.Parser, message); MessageParsingHelpers.AssertRoundtrip(MapWellKnownTypes.Parser, message);
} }

Loading…
Cancel
Save