diff options
author | Jon Skeet <jonskeet@google.com> | 2015-06-30 13:16:20 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2015-06-30 13:20:31 +0100 |
commit | f34d37a3d4d64621bc87aa0a65a05cab64062399 (patch) | |
tree | 416cceb9b343b21004b030deea069553644928d3 /csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs | |
parent | b9d1d3891f4e68886398bbf0caf40229275a448a (diff) | |
download | protobuf-f34d37a3d4d64621bc87aa0a65a05cab64062399.tar.gz protobuf-f34d37a3d4d64621bc87aa0a65a05cab64062399.tar.bz2 protobuf-f34d37a3d4d64621bc87aa0a65a05cab64062399.zip |
Tidying up and extra tests.
This is mostly just making things internal instead of public, removing and reordering a bunch of code in CodedInputStream/CodedOutputStream, and generally tidying up.
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs b/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs index 50141621..a14040d1 100644 --- a/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs +++ b/csharp/src/ProtocolBuffers.Test/FieldCodecTest.cs @@ -63,15 +63,21 @@ namespace Google.Protobuf }; [Test, TestCaseSource("Codecs")] - public void RoundTrip(ICodecTestData codec) + public void RoundTripWithTag(ICodecTestData codec) { - codec.TestRoundTrip(); + codec.TestRoundTripWithTag(); + } + + [Test, TestCaseSource("Codecs")] + public void RoundTripRaw(ICodecTestData codec) + { + codec.TestRoundTripRaw(); } [Test, TestCaseSource("Codecs")] public void CalculateSize(ICodecTestData codec) { - codec.TestCalculateSize(); + codec.TestCalculateSizeWithTag(); } [Test, TestCaseSource("Codecs")] @@ -82,8 +88,9 @@ namespace Google.Protobuf public interface ICodecTestData { - void TestRoundTrip(); - void TestCalculateSize(); + void TestRoundTripRaw(); + void TestRoundTripWithTag(); + void TestCalculateSizeWithTag(); void TestDefaultValue(); } @@ -100,7 +107,19 @@ namespace Google.Protobuf this.name = name; } - public void TestRoundTrip() + public void TestRoundTripRaw() + { + var stream = new MemoryStream(); + var codedOutput = CodedOutputStream.CreateInstance(stream); + codec.ValueWriter(codedOutput, sampleValue); + codedOutput.Flush(); + stream.Position = 0; + var codedInput = CodedInputStream.CreateInstance(stream); + Assert.AreEqual(sampleValue, codec.ValueReader(codedInput)); + Assert.IsTrue(codedInput.IsAtEnd); + } + + public void TestRoundTripWithTag() { var stream = new MemoryStream(); var codedOutput = CodedOutputStream.CreateInstance(stream); @@ -108,14 +127,12 @@ namespace Google.Protobuf codedOutput.Flush(); stream.Position = 0; var codedInput = CodedInputStream.CreateInstance(stream); - uint tag; - Assert.IsTrue(codedInput.ReadTag(out tag)); - Assert.AreEqual(codec.Tag, tag); + codedInput.AssertNextTag(codec.Tag); Assert.AreEqual(sampleValue, codec.Read(codedInput)); Assert.IsTrue(codedInput.IsAtEnd); } - public void TestCalculateSize() + public void TestCalculateSizeWithTag() { var stream = new MemoryStream(); var codedOutput = CodedOutputStream.CreateInstance(stream); @@ -126,6 +143,7 @@ namespace Google.Protobuf public void TestDefaultValue() { + // WriteTagAndValue ignores default values var stream = new MemoryStream(); var codedOutput = CodedOutputStream.CreateInstance(stream); codec.WriteTagAndValue(codedOutput, codec.DefaultValue); @@ -136,9 +154,20 @@ namespace Google.Protobuf { Assert.AreEqual(default(T), codec.DefaultValue); } - } - public string Description { get { return name; } } + // The plain ValueWriter/ValueReader delegates don't. + if (codec.DefaultValue != null) // This part isn't appropriate for message types. + { + codedOutput = CodedOutputStream.CreateInstance(stream); + codec.ValueWriter(codedOutput, codec.DefaultValue); + codedOutput.Flush(); + Assert.AreNotEqual(0, stream.Position); + Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue)); + stream.Position = 0; + var codedInput = CodedInputStream.CreateInstance(stream); + Assert.AreEqual(codec.DefaultValue, codec.ValueReader(codedInput)); + } + } public override string ToString() { |