diff options
author | Jon Skeet <jonskeet@google.com> | 2018-01-03 09:57:58 +0000 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2018-01-15 02:53:45 -0500 |
commit | 47b7d2c7cadf74ceec90fc5042232819cd0dd557 (patch) | |
tree | 1ca71757c1550c134fa0fd55952b4d0ff63df3ed /csharp/src/Google.Protobuf.Test | |
parent | 9f80df026933901883da1d556b38292e14836612 (diff) | |
download | protobuf-47b7d2c7cadf74ceec90fc5042232819cd0dd557.tar.gz protobuf-47b7d2c7cadf74ceec90fc5042232819cd0dd557.tar.bz2 protobuf-47b7d2c7cadf74ceec90fc5042232819cd0dd557.zip |
Add DiscardUnknownFields support for C#
By default, unknown fields are preserved when parsing. To discard
them, use a parser configured to do so:
var parser = MyMessage.Parser.WithDiscardUnknownFields(true);
Diffstat (limited to 'csharp/src/Google.Protobuf.Test')
-rw-r--r-- | csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs b/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs index 1edd6fba..ddf62321 100644 --- a/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs +++ b/csharp/src/Google.Protobuf.Test/UnknownFieldSetTest.cs @@ -31,6 +31,7 @@ #endregion using System; +using System.IO; using Google.Protobuf.TestProtos; using NUnit.Framework; @@ -124,5 +125,52 @@ namespace Google.Protobuf Assert.AreEqual(message.CalculateSize(), otherEmptyMessage.CalculateSize()); Assert.AreEqual(message.ToByteArray(), otherEmptyMessage.ToByteArray()); } + + [Test] + public void TestDiscardUnknownFields() + { + var message = SampleMessages.CreateFullTestAllTypes(); + var goldenEmptyMessage = new TestEmptyMessage(); + byte[] data = message.ToByteArray(); + int fullSize = message.CalculateSize(); + + Action<IMessage> assertEmpty = msg => + { + Assert.AreEqual(0, msg.CalculateSize()); + Assert.AreEqual(goldenEmptyMessage, msg); + }; + + Action<IMessage> assertFull = msg => Assert.AreEqual(fullSize, msg.CalculateSize()); + + // Test the behavior of the parsers with and without discarding, both generic and non-generic. + MessageParser<TestEmptyMessage> retainingParser1 = TestEmptyMessage.Parser; + MessageParser retainingParser2 = retainingParser1; + MessageParser<TestEmptyMessage> discardingParser1 = retainingParser1.WithDiscardUnknownFields(true); + MessageParser discardingParser2 = retainingParser2.WithDiscardUnknownFields(true); + + // Test parse from byte[] + assertFull(retainingParser1.ParseFrom(data)); + assertFull(retainingParser2.ParseFrom(data)); + assertEmpty(discardingParser1.ParseFrom(data)); + assertEmpty(discardingParser2.ParseFrom(data)); + + // Test parse from byte[] with offset + assertFull(retainingParser1.ParseFrom(data, 0, data.Length)); + assertFull(retainingParser2.ParseFrom(data, 0, data.Length)); + assertEmpty(discardingParser1.ParseFrom(data, 0, data.Length)); + assertEmpty(discardingParser2.ParseFrom(data, 0, data.Length)); + + // Test parse from CodedInputStream + assertFull(retainingParser1.ParseFrom(new CodedInputStream(data))); + assertFull(retainingParser2.ParseFrom(new CodedInputStream(data))); + assertEmpty(discardingParser1.ParseFrom(new CodedInputStream(data))); + assertEmpty(discardingParser2.ParseFrom(new CodedInputStream(data))); + + // Test parse from Stream + assertFull(retainingParser1.ParseFrom(new MemoryStream(data))); + assertFull(retainingParser2.ParseFrom(new MemoryStream(data))); + assertEmpty(discardingParser1.ParseFrom(new MemoryStream(data))); + assertEmpty(discardingParser2.ParseFrom(new MemoryStream(data))); + } } } |