Commit 29141f48 authored by Sydney Acksman's avatar Sydney Acksman

Fix default extension values being unused in FieldCodec

parent 47f2017c
using Google.Protobuf.TestProtos.Proto2;
using Proto2 = Google.Protobuf.TestProtos.Proto2;
using NUnit.Framework;
using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
namespace Google.Protobuf
{
/// <summary>
......@@ -24,7 +27,7 @@ namespace Google.Protobuf
Assert.IsNull(message.OptionalImportMessage);
Assert.AreEqual(0, message.OptionalInt32);
Assert.AreEqual(0L, message.OptionalInt64);
Assert.AreEqual(TestAllTypes.Types.NestedEnum.Foo, message.OptionalNestedEnum);
Assert.AreEqual(Proto2.TestAllTypes.Types.NestedEnum.Foo, message.OptionalNestedEnum);
Assert.IsNull(message.OptionalNestedMessage);
Assert.IsNull(message.OptionalPublicImportMessage);
Assert.AreEqual(0, message.OptionalSfixed32);
......@@ -57,7 +60,7 @@ namespace Google.Protobuf
Assert.AreEqual(0, message.RepeatedUint64.Count);
// Oneof fields
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
Assert.AreEqual(Proto2.TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
Assert.AreEqual(0, message.OneofUint32);
Assert.AreEqual("", message.OneofString);
Assert.AreEqual(ByteString.Empty, message.OneofBytes);
......@@ -74,7 +77,7 @@ namespace Google.Protobuf
Assert.AreEqual(ImportEnum.ImportBar, message.DefaultImportEnum);
Assert.AreEqual(41, message.DefaultInt32);
Assert.AreEqual(42, message.DefaultInt64);
Assert.AreEqual(TestAllTypes.Types.NestedEnum.Bar, message.DefaultNestedEnum);
Assert.AreEqual(Proto2.TestAllTypes.Types.NestedEnum.Bar, message.DefaultNestedEnum);
Assert.AreEqual(49, message.DefaultSfixed32);
Assert.AreEqual(-50, message.DefaultSfixed64);
Assert.AreEqual(-45, message.DefaultSint32);
......@@ -106,6 +109,103 @@ namespace Google.Protobuf
Assert.False(message.HasDefaultUint64);
}
[Test]
public void DefaultExtensionValues()
{
var message = new TestAllExtensions();
Assert.AreEqual(false, message.GetExtension(OptionalBoolExtension));
Assert.AreEqual(ByteString.Empty, message.GetExtension(OptionalBytesExtension));
Assert.AreEqual(0.0, message.GetExtension(OptionalDoubleExtension));
Assert.AreEqual(0, message.GetExtension(OptionalFixed32Extension));
Assert.AreEqual(0L, message.GetExtension(OptionalFixed64Extension));
Assert.AreEqual(0.0f, message.GetExtension(OptionalFloatExtension));
Assert.AreEqual(ForeignEnum.ForeignFoo, message.GetExtension(OptionalForeignEnumExtension));
Assert.IsNull(message.GetExtension(OptionalForeignMessageExtension));
Assert.AreEqual(ImportEnum.ImportFoo, message.GetExtension(OptionalImportEnumExtension));
Assert.IsNull(message.GetExtension(OptionalImportMessageExtension));
Assert.AreEqual(0, message.GetExtension(OptionalInt32Extension));
Assert.AreEqual(0L, message.GetExtension(OptionalInt64Extension));
Assert.AreEqual(Proto2.TestAllTypes.Types.NestedEnum.Foo, message.GetExtension(OptionalNestedEnumExtension));
Assert.IsNull(message.GetExtension(OptionalNestedMessageExtension));
Assert.IsNull(message.GetExtension(OptionalPublicImportMessageExtension));
Assert.AreEqual(0, message.GetExtension(OptionalSfixed32Extension));
Assert.AreEqual(0L, message.GetExtension(OptionalSfixed64Extension));
Assert.AreEqual(0, message.GetExtension(OptionalSint32Extension));
Assert.AreEqual(0L, message.GetExtension(OptionalSint64Extension));
Assert.AreEqual("", message.GetExtension(OptionalStringExtension));
Assert.AreEqual(0U, message.GetExtension(OptionalUint32Extension));
Assert.AreEqual(0UL, message.GetExtension(OptionalUint64Extension));
// Repeated fields
Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
Assert.IsNull(message.GetExtension(RepeatedBytesExtension));
Assert.IsNull(message.GetExtension(RepeatedDoubleExtension));
Assert.IsNull(message.GetExtension(RepeatedFixed32Extension));
Assert.IsNull(message.GetExtension(RepeatedFixed64Extension));
Assert.IsNull(message.GetExtension(RepeatedFloatExtension));
Assert.IsNull(message.GetExtension(RepeatedForeignEnumExtension));
Assert.IsNull(message.GetExtension(RepeatedForeignMessageExtension));
Assert.IsNull(message.GetExtension(RepeatedImportEnumExtension));
Assert.IsNull(message.GetExtension(RepeatedImportMessageExtension));
Assert.IsNull(message.GetExtension(RepeatedNestedEnumExtension));
Assert.IsNull(message.GetExtension(RepeatedNestedMessageExtension));
Assert.IsNull(message.GetExtension(RepeatedSfixed32Extension));
Assert.IsNull(message.GetExtension(RepeatedSfixed64Extension));
Assert.IsNull(message.GetExtension(RepeatedSint32Extension));
Assert.IsNull(message.GetExtension(RepeatedSint64Extension));
Assert.IsNull(message.GetExtension(RepeatedStringExtension));
Assert.IsNull(message.GetExtension(RepeatedUint32Extension));
Assert.IsNull(message.GetExtension(RepeatedUint64Extension));
// Oneof fields
Assert.AreEqual(0, message.GetExtension(OneofUint32Extension));
Assert.AreEqual("", message.GetExtension(OneofStringExtension));
Assert.AreEqual(ByteString.Empty, message.GetExtension(OneofBytesExtension));
Assert.IsNull(message.GetExtension(OneofNestedMessageExtension));
Assert.AreEqual(true, message.GetExtension(DefaultBoolExtension));
Assert.AreEqual(ByteString.CopyFromUtf8("world"), message.GetExtension(DefaultBytesExtension));
Assert.AreEqual("123", message.GetExtension(DefaultCordExtension));
Assert.AreEqual(52e3, message.GetExtension(DefaultDoubleExtension));
Assert.AreEqual(47, message.GetExtension(DefaultFixed32Extension));
Assert.AreEqual(48, message.GetExtension(DefaultFixed64Extension));
Assert.AreEqual(51.5, message.GetExtension(DefaultFloatExtension));
Assert.AreEqual(ForeignEnum.ForeignBar, message.GetExtension(DefaultForeignEnumExtension));
Assert.AreEqual(ImportEnum.ImportBar, message.GetExtension(DefaultImportEnumExtension));
Assert.AreEqual(41, message.GetExtension(DefaultInt32Extension));
Assert.AreEqual(42, message.GetExtension(DefaultInt64Extension));
Assert.AreEqual(Proto2.TestAllTypes.Types.NestedEnum.Bar, message.GetExtension(DefaultNestedEnumExtension));
Assert.AreEqual(49, message.GetExtension(DefaultSfixed32Extension));
Assert.AreEqual(-50, message.GetExtension(DefaultSfixed64Extension));
Assert.AreEqual(-45, message.GetExtension(DefaultSint32Extension));
Assert.AreEqual(46, message.GetExtension(DefaultSint64Extension));
Assert.AreEqual("hello", message.GetExtension(DefaultStringExtension));
Assert.AreEqual("abc", message.GetExtension(DefaultStringPieceExtension));
Assert.AreEqual(43, message.GetExtension(DefaultUint32Extension));
Assert.AreEqual(44, message.GetExtension(DefaultUint64Extension));
Assert.False(message.HasExtension(DefaultBoolExtension));
Assert.False(message.HasExtension(DefaultBytesExtension));
Assert.False(message.HasExtension(DefaultCordExtension));
Assert.False(message.HasExtension(DefaultDoubleExtension));
Assert.False(message.HasExtension(DefaultFixed32Extension));
Assert.False(message.HasExtension(DefaultFixed64Extension));
Assert.False(message.HasExtension(DefaultFloatExtension));
Assert.False(message.HasExtension(DefaultForeignEnumExtension));
Assert.False(message.HasExtension(DefaultImportEnumExtension));
Assert.False(message.HasExtension(DefaultInt32Extension));
Assert.False(message.HasExtension(DefaultInt64Extension));
Assert.False(message.HasExtension(DefaultNestedEnumExtension));
Assert.False(message.HasExtension(DefaultSfixed32Extension));
Assert.False(message.HasExtension(DefaultSfixed64Extension));
Assert.False(message.HasExtension(DefaultSint32Extension));
Assert.False(message.HasExtension(DefaultSint64Extension));
Assert.False(message.HasExtension(DefaultStringExtension));
Assert.False(message.HasExtension(DefaultStringPieceExtension));
Assert.False(message.HasExtension(DefaultUint32Extension));
Assert.False(message.HasExtension(DefaultUint64Extension));
}
[Test]
public void FieldPresence()
{
......@@ -233,7 +333,7 @@ namespace Google.Protobuf
};
byte[] bytes = message.ToByteArray();
TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes);
TestAllTypes parsed = Proto2.TestAllTypes.Parser.ParseFrom(bytes);
Assert.AreEqual(message, parsed);
}
......
......@@ -218,7 +218,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<string> ForString(uint tag, string defaultValue)
{
return new FieldCodec<string>(input => input.ReadString(), (output, value) => output.WriteString(value), CodedOutputStream.ComputeStringSize, tag);
return new FieldCodec<string>(input => input.ReadString(), (output, value) => output.WriteString(value), CodedOutputStream.ComputeStringSize, tag, defaultValue);
}
/// <summary>
......@@ -229,7 +229,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<ByteString> ForBytes(uint tag, ByteString defaultValue)
{
return new FieldCodec<ByteString>(input => input.ReadBytes(), (output, value) => output.WriteBytes(value), CodedOutputStream.ComputeBytesSize, tag);
return new FieldCodec<ByteString>(input => input.ReadBytes(), (output, value) => output.WriteBytes(value), CodedOutputStream.ComputeBytesSize, tag, defaultValue);
}
/// <summary>
......@@ -240,7 +240,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<bool> ForBool(uint tag, bool defaultValue)
{
return new FieldCodec<bool>(input => input.ReadBool(), (output, value) => output.WriteBool(value), CodedOutputStream.BoolSize, tag);
return new FieldCodec<bool>(input => input.ReadBool(), (output, value) => output.WriteBool(value), CodedOutputStream.BoolSize, tag, defaultValue);
}
/// <summary>
......@@ -251,7 +251,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<int> ForInt32(uint tag, int defaultValue)
{
return new FieldCodec<int>(input => input.ReadInt32(), (output, value) => output.WriteInt32(value), CodedOutputStream.ComputeInt32Size, tag);
return new FieldCodec<int>(input => input.ReadInt32(), (output, value) => output.WriteInt32(value), CodedOutputStream.ComputeInt32Size, tag, defaultValue);
}
/// <summary>
......@@ -262,7 +262,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<int> ForSInt32(uint tag, int defaultValue)
{
return new FieldCodec<int>(input => input.ReadSInt32(), (output, value) => output.WriteSInt32(value), CodedOutputStream.ComputeSInt32Size, tag);
return new FieldCodec<int>(input => input.ReadSInt32(), (output, value) => output.WriteSInt32(value), CodedOutputStream.ComputeSInt32Size, tag, defaultValue);
}
/// <summary>
......@@ -273,7 +273,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<uint> ForFixed32(uint tag, uint defaultValue)
{
return new FieldCodec<uint>(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), 4, tag);
return new FieldCodec<uint>(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), 4, tag, defaultValue);
}
/// <summary>
......@@ -284,7 +284,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<int> ForSFixed32(uint tag, int defaultValue)
{
return new FieldCodec<int>(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), 4, tag);
return new FieldCodec<int>(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), 4, tag, defaultValue);
}
/// <summary>
......@@ -295,7 +295,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<uint> ForUInt32(uint tag, uint defaultValue)
{
return new FieldCodec<uint>(input => input.ReadUInt32(), (output, value) => output.WriteUInt32(value), CodedOutputStream.ComputeUInt32Size, tag);
return new FieldCodec<uint>(input => input.ReadUInt32(), (output, value) => output.WriteUInt32(value), CodedOutputStream.ComputeUInt32Size, tag, defaultValue);
}
/// <summary>
......@@ -306,7 +306,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<long> ForInt64(uint tag, long defaultValue)
{
return new FieldCodec<long>(input => input.ReadInt64(), (output, value) => output.WriteInt64(value), CodedOutputStream.ComputeInt64Size, tag);
return new FieldCodec<long>(input => input.ReadInt64(), (output, value) => output.WriteInt64(value), CodedOutputStream.ComputeInt64Size, tag, defaultValue);
}
/// <summary>
......@@ -317,7 +317,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<long> ForSInt64(uint tag, long defaultValue)
{
return new FieldCodec<long>(input => input.ReadSInt64(), (output, value) => output.WriteSInt64(value), CodedOutputStream.ComputeSInt64Size, tag);
return new FieldCodec<long>(input => input.ReadSInt64(), (output, value) => output.WriteSInt64(value), CodedOutputStream.ComputeSInt64Size, tag, defaultValue);
}
/// <summary>
......@@ -328,7 +328,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<ulong> ForFixed64(uint tag, ulong defaultValue)
{
return new FieldCodec<ulong>(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), 8, tag);
return new FieldCodec<ulong>(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), 8, tag, defaultValue);
}
/// <summary>
......@@ -339,7 +339,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<long> ForSFixed64(uint tag, long defaultValue)
{
return new FieldCodec<long>(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), 8, tag);
return new FieldCodec<long>(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), 8, tag, defaultValue);
}
/// <summary>
......@@ -350,7 +350,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<ulong> ForUInt64(uint tag, ulong defaultValue)
{
return new FieldCodec<ulong>(input => input.ReadUInt64(), (output, value) => output.WriteUInt64(value), CodedOutputStream.ComputeUInt64Size, tag);
return new FieldCodec<ulong>(input => input.ReadUInt64(), (output, value) => output.WriteUInt64(value), CodedOutputStream.ComputeUInt64Size, tag, defaultValue);
}
/// <summary>
......@@ -361,7 +361,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<float> ForFloat(uint tag, float defaultValue)
{
return new FieldCodec<float>(input => input.ReadFloat(), (output, value) => output.WriteFloat(value), CodedOutputStream.FloatSize, tag);
return new FieldCodec<float>(input => input.ReadFloat(), (output, value) => output.WriteFloat(value), CodedOutputStream.FloatSize, tag, defaultValue);
}
/// <summary>
......@@ -372,7 +372,7 @@ namespace Google.Protobuf
/// <returns>A codec for the given tag.</returns>
public static FieldCodec<double> ForDouble(uint tag, double defaultValue)
{
return new FieldCodec<double>(input => input.ReadDouble(), (output, value) => output.WriteDouble(value), CodedOutputStream.DoubleSize, tag);
return new FieldCodec<double>(input => input.ReadDouble(), (output, value) => output.WriteDouble(value), CodedOutputStream.DoubleSize, tag, defaultValue);
}
// Enums are tricky. We can probably use expression trees to build these delegates automatically,
......@@ -391,7 +391,7 @@ namespace Google.Protobuf
return new FieldCodec<T>(input => fromInt32(
input.ReadEnum()),
(output, value) => output.WriteEnum(toInt32(value)),
value => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag);
value => CodedOutputStream.ComputeEnumSize(toInt32(value)), tag, defaultValue);
}
/// <summary>
......@@ -689,7 +689,8 @@ namespace Google.Protobuf
Func<CodedInputStream, T> reader,
Action<CodedOutputStream, T> writer,
int fixedSize,
uint tag) : this(reader, writer, _ => fixedSize, tag)
uint tag,
T defaultValue) : this(reader, writer, _ => fixedSize, tag, defaultValue)
{
FixedSize = fixedSize;
}
......@@ -699,7 +700,7 @@ namespace Google.Protobuf
Action<CodedOutputStream, T> writer,
Func<T, int> sizeCalculator,
uint tag,
uint endTag = 0) : this(reader, writer, (CodedInputStream i, ref T v) => v = reader(i), (ref T v, T v2) => { v = v2; return true; }, sizeCalculator, tag, endTag, DefaultDefault)
T defaultValue) : this(reader, writer, (CodedInputStream i, ref T v) => v = reader(i), (ref T v, T v2) => { v = v2; return true; }, sizeCalculator, tag, 0, defaultValue)
{
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment