aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf.Test
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2015-07-20 19:24:31 +0100
committerJon Skeet <jonskeet@google.com>2015-07-21 12:59:40 +0100
commit53c399a1d65df65e9f83a70b55041a01cf8d7489 (patch)
treebf3f738dd30295dc8ceb65478b9071d6d654e144 /csharp/src/Google.Protobuf.Test
parent2ee4b5665520fe3245eb5e15df8bd35e0c539a07 (diff)
downloadprotobuf-53c399a1d65df65e9f83a70b55041a01cf8d7489.tar.gz
protobuf-53c399a1d65df65e9f83a70b55041a01cf8d7489.tar.bz2
protobuf-53c399a1d65df65e9f83a70b55041a01cf8d7489.zip
Revamp to reflection.
Changes in brief: 1. Descriptor is now the entry point for all reflection. 2. IReflectedMessage has gone; there's now a Descriptor property in IMessage, which is explicitly implemented (due to the static property). 3. FieldAccessorTable has gone away 4. IFieldAccessor and OneofFieldAccessor still exist; we *could* put the functionality straight into FieldDescriptor and OneofDescriptor... I'm unsure about that. 5. There's a temporary property MessageDescriptor.FieldAccessorsByFieldNumber to make the test changes small - we probably want this to go away 6. Discovery for delegates is now via attributes applied to properties and the Clear method of a oneof I'm happy with 1-3. 4 I'm unsure about - feedback welcome. 5 will go away 6 I'm unsure about, both in design and implementation. Should we have a ProtobufMessageAttribute too? Should we find all the relevant attributes in MessageDescriptor and pass them down, to avoid an O(N^2) scenario? Generated code changes coming in the next commit.
Diffstat (limited to 'csharp/src/Google.Protobuf.Test')
-rw-r--r--csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs47
-rw-r--r--csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs1
-rw-r--r--csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs6
3 files changed, 28 insertions, 26 deletions
diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs
index acb20b15..180976d1 100644
--- a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs
+++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs
@@ -604,7 +604,7 @@ namespace Google.Protobuf
public void Reflection_GetValue()
{
var message = SampleMessages.CreateFullTestAllTypes();
- var fields = ((IReflectedMessage) message).Fields;
+ var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber;
Assert.AreEqual(message.SingleBool, fields[TestAllTypes.SingleBoolFieldNumber].GetValue(message));
Assert.AreEqual(message.SingleBytes, fields[TestAllTypes.SingleBytesFieldNumber].GetValue(message));
Assert.AreEqual(message.SingleDouble, fields[TestAllTypes.SingleDoubleFieldNumber].GetValue(message));
@@ -639,7 +639,7 @@ namespace Google.Protobuf
// Just a single map field, for the same reason
var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } };
- fields = ((IReflectedMessage) mapMessage).Fields;
+ fields = TestMap.Descriptor.FieldAccessorsByFieldNumber;
var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].GetValue(mapMessage);
Assert.AreEqual(mapMessage.MapStringString, dictionary);
Assert.AreEqual("value1", dictionary["key1"]);
@@ -648,8 +648,8 @@ namespace Google.Protobuf
[Test]
public void Reflection_Clear()
{
- IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
- var fields = message.Fields;
+ var message = SampleMessages.CreateFullTestAllTypes();
+ var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber;
fields[TestAllTypes.SingleBoolFieldNumber].Clear(message);
fields[TestAllTypes.SingleInt32FieldNumber].Clear(message);
fields[TestAllTypes.SingleStringFieldNumber].Clear(message);
@@ -673,7 +673,7 @@ namespace Google.Protobuf
// Separately, maps.
var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } };
- fields = ((IReflectedMessage) mapMessage).Fields;
+ fields = TestMap.Descriptor.FieldAccessorsByFieldNumber;
fields[TestMap.MapStringStringFieldNumber].Clear(mapMessage);
Assert.AreEqual(0, mapMessage.MapStringString.Count);
}
@@ -682,8 +682,8 @@ namespace Google.Protobuf
public void Reflection_SetValue_SingleFields()
{
// Just a sample (primitives, messages, enums, strings, byte strings)
- IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
- var fields = message.Fields;
+ var message = SampleMessages.CreateFullTestAllTypes();
+ var fields = TestAllTypes.Descriptor.FieldAccessorsByFieldNumber;
fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, false);
fields[TestAllTypes.SingleInt32FieldNumber].SetValue(message, 500);
fields[TestAllTypes.SingleStringFieldNumber].SetValue(message, "It's a string");
@@ -709,51 +709,52 @@ namespace Google.Protobuf
[Test]
public void Reflection_SetValue_SingleFields_WrongType()
{
- IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
- var fields = message.Fields;
+ IMessage message = SampleMessages.CreateFullTestAllTypes();
+ var fields = message.Descriptor.FieldAccessorsByFieldNumber;
Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].SetValue(message, "This isn't a bool"));
}
[Test]
public void Reflection_SetValue_MapFields()
{
- IReflectedMessage message = new TestMap();
- var fields = message.Fields;
+ IMessage message = new TestMap();
+ var fields = message.Descriptor.FieldAccessorsByFieldNumber;
Assert.Throws<InvalidOperationException>(() => fields[TestMap.MapStringStringFieldNumber].SetValue(message, new Dictionary<string, string>()));
}
[Test]
public void Reflection_SetValue_RepeatedFields()
{
- IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
- var fields = message.Fields;
+ IMessage message = SampleMessages.CreateFullTestAllTypes();
+ var fields = message.Descriptor.FieldAccessorsByFieldNumber;
Assert.Throws<InvalidOperationException>(() => fields[TestAllTypes.RepeatedDoubleFieldNumber].SetValue(message, new double[10]));
}
[Test]
public void Reflection_GetValue_IncorrectType()
{
- IReflectedMessage message = SampleMessages.CreateFullTestAllTypes();
- Assert.Throws<InvalidCastException>(() => message.Fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap()));
+ IMessage message = SampleMessages.CreateFullTestAllTypes();
+ var fields = message.Descriptor.FieldAccessorsByFieldNumber;
+ Assert.Throws<InvalidCastException>(() => fields[TestAllTypes.SingleBoolFieldNumber].GetValue(new TestMap()));
}
[Test]
public void Reflection_Oneof()
{
var message = new TestAllTypes();
- var fields = ((IReflectedMessage) message).Fields;
- Assert.AreEqual(1, fields.Oneofs.Count);
- var oneof = fields.Oneofs[0];
- Assert.AreEqual("oneof_field", oneof.Descriptor.Name);
- Assert.IsNull(oneof.GetCaseFieldDescriptor(message));
+ var descriptor = TestAllTypes.Descriptor;
+ Assert.AreEqual(1, descriptor.Oneofs.Count);
+ var oneof = descriptor.Oneofs[0];
+ Assert.AreEqual("oneof_field", oneof.Name);
+ Assert.IsNull(oneof.Accessor.GetCaseFieldDescriptor(message));
message.OneofString = "foo";
- Assert.AreSame(fields[TestAllTypes.OneofStringFieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message));
+ Assert.AreSame(descriptor.FieldAccessorsByFieldNumber[TestAllTypes.OneofStringFieldNumber].Descriptor, oneof.Accessor.GetCaseFieldDescriptor(message));
message.OneofUint32 = 10;
- Assert.AreSame(fields[TestAllTypes.OneofUint32FieldNumber].Descriptor, oneof.GetCaseFieldDescriptor(message));
+ Assert.AreSame(descriptor.FieldAccessorsByFieldNumber[TestAllTypes.OneofUint32FieldNumber].Descriptor, oneof.Accessor.GetCaseFieldDescriptor(message));
- oneof.Clear(message);
+ oneof.Accessor.Clear(message);
Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
}
}
diff --git a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs
index 0db01a5e..0aff0a6c 100644
--- a/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs
+++ b/csharp/src/Google.Protobuf.Test/Reflection/DescriptorsTest.cs
@@ -62,6 +62,7 @@ namespace Google.Protobuf.Reflection
Assert.AreEqual(UnittestImportProto3.Descriptor, file.Dependencies[0]);
MessageDescriptor messageType = TestAllTypes.Descriptor;
+ Assert.AreSame(typeof(TestAllTypes), messageType.GeneratedType);
Assert.AreEqual(messageType, file.MessageTypes[0]);
Assert.AreEqual(messageType, file.FindTypeByName<MessageDescriptor>("TestAllTypes"));
Assert.Null(file.FindTypeByName<MessageDescriptor>("NoSuchType"));
diff --git a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
index ad88c4eb..c617db36 100644
--- a/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
+++ b/csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
@@ -192,7 +192,7 @@ namespace Google.Protobuf.WellKnownTypes
Uint32Field = 3,
Uint64Field = 4
};
- var fields = ((IReflectedMessage) message).Fields;
+ var fields = TestWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber;
Assert.AreEqual("x", fields[TestWellKnownTypes.StringFieldFieldNumber].GetValue(message));
Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), fields[TestWellKnownTypes.BytesFieldFieldNumber].GetValue(message));
@@ -216,7 +216,7 @@ namespace Google.Protobuf.WellKnownTypes
{
// Just a single example... note that we can't have a null value here
var message = new RepeatedWellKnownTypes { Int32Field = { 1, 2 } };
- var fields = ((IReflectedMessage) message).Fields;
+ var fields = RepeatedWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber;
var list = (IList) fields[RepeatedWellKnownTypes.Int32FieldFieldNumber].GetValue(message);
CollectionAssert.AreEqual(new[] { 1, 2 }, list);
}
@@ -226,7 +226,7 @@ namespace Google.Protobuf.WellKnownTypes
{
// Just a single example... note that we can't have a null value here
var message = new MapWellKnownTypes { Int32Field = { { 1, 2 }, { 3, null } } };
- var fields = ((IReflectedMessage) message).Fields;
+ var fields = MapWellKnownTypes.Descriptor.FieldAccessorsByFieldNumber;
var dictionary = (IDictionary) fields[MapWellKnownTypes.Int32FieldFieldNumber].GetValue(message);
Assert.AreEqual(2, dictionary[1]);
Assert.IsNull(dictionary[3]);