Commit 9163a6f1 authored by Sydney Acksman's avatar Sydney Acksman

Add tests for extension accessor

parent 0c80570a
...@@ -34,6 +34,8 @@ using System; ...@@ -34,6 +34,8 @@ using System;
using Google.Protobuf.TestProtos; using Google.Protobuf.TestProtos;
using Proto2 = Google.Protobuf.TestProtos.Proto2; using Proto2 = Google.Protobuf.TestProtos.Proto2;
using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
namespace Google.Protobuf namespace Google.Protobuf
{ {
/// <summary> /// <summary>
...@@ -61,7 +63,7 @@ namespace Google.Protobuf ...@@ -61,7 +63,7 @@ namespace Google.Protobuf
SingleImportMessage = new ImportMessage { D = 20 }, SingleImportMessage = new ImportMessage { D = 20 },
SingleInt32 = 100, SingleInt32 = 100,
SingleInt64 = 3210987654321, SingleInt64 = 3210987654321,
SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo, SingleNestedEnum = TestProtos.TestAllTypes.Types.NestedEnum.Foo,
SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 }, SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
SinglePublicImportMessage = new PublicImportMessage { E = 54 }, SinglePublicImportMessage = new PublicImportMessage { E = 54 },
SingleSfixed32 = -123, SingleSfixed32 = -123,
...@@ -83,7 +85,7 @@ namespace Google.Protobuf ...@@ -83,7 +85,7 @@ namespace Google.Protobuf
RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } }, RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } },
RepeatedInt32 = { 100, 200 }, RepeatedInt32 = { 100, 200 },
RepeatedInt64 = { 3210987654321, Int64.MaxValue }, RepeatedInt64 = { 3210987654321, Int64.MaxValue },
RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg }, RepeatedNestedEnum = { TestProtos.TestAllTypes.Types.NestedEnum.Foo, TestProtos.TestAllTypes.Types.NestedEnum.Neg },
RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } }, RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } },
RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } }, RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } },
RepeatedSfixed32 = { -123, 123 }, RepeatedSfixed32 = { -123, 123 },
...@@ -149,5 +151,57 @@ namespace Google.Protobuf ...@@ -149,5 +151,57 @@ namespace Google.Protobuf
OneofString = "Oneof string" OneofString = "Oneof string"
}; };
} }
public static Proto2.TestAllExtensions CreateFullTestAllExtensions()
{
var message = new Proto2.TestAllExtensions();
message.SetExtension(OptionalBoolExtension, true);
message.SetExtension(OptionalBytesExtension, ByteString.CopyFrom(1, 2, 3, 4));
message.SetExtension(OptionalDoubleExtension, 23.5);
message.SetExtension(OptionalFixed32Extension, 23u);
message.SetExtension(OptionalFixed64Extension, 1234567890123u);
message.SetExtension(OptionalFloatExtension, 12.25f);
message.SetExtension(OptionalForeignEnumExtension, Proto2.ForeignEnum.ForeignBar);
message.SetExtension(OptionalForeignMessageExtension, new Proto2.ForeignMessage { C = 10 });
message.SetExtension(OptionalImportEnumExtension, Proto2.ImportEnum.ImportBaz);
message.SetExtension(OptionalImportMessageExtension, new Proto2.ImportMessage { D = 20 });
message.SetExtension(OptionalInt32Extension, 100);
message.SetExtension(OptionalInt64Extension, 3210987654321);
message.SetExtension(OptionalNestedEnumExtension, Proto2.TestAllTypes.Types.NestedEnum.Foo);
message.SetExtension(OptionalNestedMessageExtension, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 });
message.SetExtension(OptionalPublicImportMessageExtension, new Proto2.PublicImportMessage { E = 54 });
message.SetExtension(OptionalSfixed32Extension, -123);
message.SetExtension(OptionalSfixed64Extension, -12345678901234);
message.SetExtension(OptionalSint32Extension, -456);
message.SetExtension(OptionalSint64Extension, -12345678901235);
message.SetExtension(OptionalStringExtension, "test");
message.SetExtension(OptionalUint32Extension, UInt32.MaxValue);
message.SetExtension(OptionalUint64Extension, UInt64.MaxValue);
message.SetExtension(OptionalGroupExtension, new Proto2.OptionalGroup_extension { A = 10 });
message.GetOrRegisterExtension(RepeatedBoolExtension).AddRange(new[] { true, false });
message.GetOrRegisterExtension(RepeatedBytesExtension).AddRange(new[] { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) });
message.GetOrRegisterExtension(RepeatedDoubleExtension).AddRange(new[] { -12.25, 23.5 });
message.GetOrRegisterExtension(RepeatedFixed32Extension).AddRange(new[] { UInt32.MaxValue, 23u });
message.GetOrRegisterExtension(RepeatedFixed64Extension).AddRange(new[] { UInt64.MaxValue, 1234567890123ul });
message.GetOrRegisterExtension(RepeatedFloatExtension).AddRange(new[] { 100f, 12.25f });
message.GetOrRegisterExtension(RepeatedForeignEnumExtension).AddRange(new[] { Proto2.ForeignEnum.ForeignFoo, Proto2.ForeignEnum.ForeignBar });
message.GetOrRegisterExtension(RepeatedForeignMessageExtension).AddRange(new[] { new Proto2.ForeignMessage(), new Proto2.ForeignMessage { C = 10 } });
message.GetOrRegisterExtension(RepeatedImportEnumExtension).AddRange(new[] { Proto2.ImportEnum.ImportBaz, Proto2.ImportEnum.ImportFoo });
message.GetOrRegisterExtension(RepeatedImportMessageExtension).AddRange(new[] { new Proto2.ImportMessage { D = 20 }, new Proto2.ImportMessage { D = 25 } });
message.GetOrRegisterExtension(RepeatedInt32Extension).AddRange(new[] { 100, 200 });
message.GetOrRegisterExtension(RepeatedInt64Extension).AddRange(new[] { 3210987654321, Int64.MaxValue });
message.GetOrRegisterExtension(RepeatedNestedEnumExtension).AddRange(new[] { Proto2.TestAllTypes.Types.NestedEnum.Foo, Proto2.TestAllTypes.Types.NestedEnum.Neg });
message.GetOrRegisterExtension(RepeatedNestedMessageExtension).AddRange(new[] { new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 }, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 10 } });
message.GetOrRegisterExtension(RepeatedSfixed32Extension).AddRange(new[] { -123, 123 });
message.GetOrRegisterExtension(RepeatedSfixed64Extension).AddRange(new[] { -12345678901234, 12345678901234 });
message.GetOrRegisterExtension(RepeatedSint32Extension).AddRange(new[] { -456, 100 });
message.GetOrRegisterExtension(RepeatedSint64Extension).AddRange(new[] { -12345678901235, 123 });
message.GetOrRegisterExtension(RepeatedStringExtension).AddRange(new[] { "foo", "bar" });
message.GetOrRegisterExtension(RepeatedUint32Extension).AddRange(new[] { UInt32.MaxValue, UInt32.MinValue });
message.GetOrRegisterExtension(RepeatedUint64Extension).AddRange(new[] { UInt64.MaxValue, UInt32.MinValue });
message.GetOrRegisterExtension(RepeatedGroupExtension).AddRange(new[] { new Proto2.RepeatedGroup_extension { A = 10 }, new Proto2.RepeatedGroup_extension { A = 20 } });
message.SetExtension(OneofStringExtension, "Oneof string");
return message;
}
} }
} }
\ No newline at end of file
...@@ -381,6 +381,11 @@ namespace Google.Protobuf.Reflection ...@@ -381,6 +381,11 @@ namespace Google.Protobuf.Reflection
private IFieldAccessor CreateAccessor() private IFieldAccessor CreateAccessor()
{ {
if (Extension != null)
{
return new ExtensionAccessor(this);
}
// If we're given no property name, that's because we really don't want an accessor. // If we're given no property name, that's because we really don't want an accessor.
// This could be because it's a map message, or it could be that we're loading a FileDescriptor dynamically. // This could be because it's a map message, or it could be that we're loading a FileDescriptor dynamically.
// TODO: Support dynamic messages. // TODO: Support dynamic messages.
...@@ -389,10 +394,6 @@ namespace Google.Protobuf.Reflection ...@@ -389,10 +394,6 @@ namespace Google.Protobuf.Reflection
return null; return null;
} }
if (Extension != null)
{
return new ExtensionAccessor(this);
}
var property = ContainingType.ClrType.GetProperty(propertyName); var property = ContainingType.ClrType.GetProperty(propertyName);
if (property == null) if (property == null)
{ {
......
...@@ -123,7 +123,7 @@ namespace Google.Protobuf.Reflection ...@@ -123,7 +123,7 @@ namespace Google.Protobuf.Reflection
/// the type that declares the method, and the second argument to the first parameter type of the method. /// the type that declares the method, and the second argument to the first parameter type of the method.
/// </summary> /// </summary>
internal static IExtensionReflectionHelper CreateExtensionHelper(Extension extension) => internal static IExtensionReflectionHelper CreateExtensionHelper(Extension extension) =>
(IExtensionReflectionHelper)Activator.CreateInstance(typeof(ExtensionReflectionHelper<,>).MakeGenericType(extension.TargetType, extension.GetType().GenericTypeArguments[1])); (IExtensionReflectionHelper)Activator.CreateInstance(typeof(ExtensionReflectionHelper<,>).MakeGenericType(extension.TargetType, extension.GetType().GenericTypeArguments[1]), extension);
/// <summary> /// <summary>
/// Creates a reflection helper for the given type arguments. Currently these are created on demand /// Creates a reflection helper for the given type arguments. Currently these are created on demand
......
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