[c#] Fix Reflection.IsBonded for custom impls

Reflection.IsBonded now correctly returns true for custom IBonded
implementations. In addition, corrected reversed logic in test utility
code that was likely related to the incorrect predicate.
This commit is contained in:
Chad Walters 2017-07-11 13:42:30 -07:00 коммит произвёл Christopher Warrington
Родитель 0236301fe8
Коммит f49eba5247
5 изменённых файлов: 73 добавлений и 13 удалений

Просмотреть файл

@ -11,11 +11,24 @@ tag versions. The Bond compiler (`gbc`) and
different versioning scheme, following the Haskell community's
[package versioning policy](https://wiki.haskell.org/Package_versioning_policy).
## Unreleased ##
* `gbc` & compiler library: TBD
* IDL core version: TBD
* IDL comm version: TBD
* C++ version: TBD
* C# NuGet version: TBD (bug fix bump needed)
* C# Comm NuGet version: TBD
### C# ###
* Reflection.IsBonded now recognizes custom IBonded
implementations.
## 6.0.0: 2017-06-29 ##
* `gbc` & compiler library: 0.10.0.0
* IDL core version: 2.0
* IDL comm version: 1.2
* C++ version: TBD 6.0.0
* C++ version: 6.0.0
* C# NuGet version: 6.0.0
* C# Comm NuGet version: 0.12.0

Просмотреть файл

@ -110,7 +110,9 @@ namespace Bond
if (type.IsGenericType())
{
var definition = type.GetGenericTypeDefinition();
return definition == typeof(IBonded<>) || definition == typeof(Tag.bonded<>);
return definition == typeof(IBonded<>)
|| definition == typeof(Tag.bonded<>)
|| definition.GetTypeInfo().ImplementedInterfaces.Contains(typeof(IBonded));
}
return false;

Просмотреть файл

@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Bond;
using NUnit.Framework;
@ -301,6 +302,7 @@
public void AliasBonded()
{
var from = new BondedAlias {lazy = new Lazy<Foo>(UnitTest.Random.Init<Foo>())};
Assert.IsTrue(Reflection.IsBonded(typeof(Lazy<Foo>)));
TestTypeAliases(from);
}
@ -308,6 +310,7 @@
public void AliasGenericBonded()
{
var from = new GenericBondedAlias<Foo> { lazy = new Lazy<Foo>(UnitTest.Random.Init<Foo>()) };
Assert.IsTrue(Reflection.IsBonded(typeof(Lazy<Foo>)));
TestTypeAliases(from);
}

Просмотреть файл

@ -996,7 +996,7 @@ namespace UnitTest
streamTranscode(SerializeSP, TranscodeSPFB<From>, DeserializeFB<To>);
// Pull parser doesn't support bonded<T>
if (AnyField<From>(Reflection.IsBonded))
if (!AnyField<From>(Reflection.IsBonded))
{
streamTranscode(SerializeSP, TranscodeSPXml<From>, DeserializeXml<To>);
@ -1017,8 +1017,8 @@ namespace UnitTest
streamMarshalSchema(MarshalSP);
}
// Pull parser doesn't supprot bonded<T>
if (AnyField<From>(Reflection.IsBonded))
// Pull parser doesn't support bonded<T>
if (!AnyField<From>(Reflection.IsBonded))
{
streamRoundtrip(SerializeXml, DeserializeXml<To>);
streamTranscode(SerializeCB, TranscodeCBXml<From>, DeserializeXml<To>);

Просмотреть файл

@ -4,9 +4,11 @@
using System.Collections.Generic;
using System.Linq;
using Bond;
using NUnit.Framework;
using Bond.Tag;
using Bond.Internal.Reflection;
using Bond.IO.Safe;
using Bond.Protocols;
using Bond.Tag;
using NUnit.Framework;
[TestFixture]
public class ReflectionTests
@ -24,7 +26,7 @@
}
[Test]
public void GenericSchemaType()
public void Reflection_GenericSchemaType()
{
GenericSchemaStructTest<int>();
GenericSchemaStructTest<float>();
@ -33,21 +35,21 @@
}
[Test]
public void FindMethodFromObject()
public void Reflection_FindMethodFromObject()
{
Assert.AreEqual("ReadStructBegin", ReflectionExtensions.FindMethod(typeof(ReaderA), "ReadStructBegin", new Type[0]).Name);
Assert.AreEqual(typeof(ReaderA), ReflectionExtensions.FindMethod(typeof(ReaderA), "ReadStructBegin", new Type[0]).DeclaringType);
}
[Test]
public void FindMethodFromInterface()
public void Reflection_FindMethodFromInterface()
{
Assert.AreEqual("ReadStructBegin", ReflectionExtensions.FindMethod(typeof(IReaderA), "ReadStructBegin", new Type[0]).Name);
Assert.AreEqual(typeof(IReaderA), ReflectionExtensions.FindMethod(typeof(IReaderA), "ReadStructBegin", new Type[0]).DeclaringType);
}
[Test]
public void MultipleMethodsImplementedException()
public void Reflection_MultipleMethodsImplementedException()
{
Assert.That(() => ReflectionExtensions.FindMethod(typeof(IReaderAB), "ReadStructBegin", new Type[0]),
Throws.TypeOf<System.Reflection.AmbiguousMatchException>()
@ -57,7 +59,7 @@
// We test on the SchemaFields instead of the RuntimeSchema, because, for now, the list sub
// type is not part of Bond.TypeDef
[Test]
public void DifferentiateBetweenListAndNullable()
public void Reflection_DifferentiateBetweenListAndNullable()
{
var schemaFields = typeof(ListVsNullable).GetSchemaFields();
@ -90,10 +92,24 @@
}
}
[Test]
public void Reflection_IsBonded()
{
Assert.IsTrue(Reflection.IsBonded(typeof(Bonded<>)));
Assert.IsTrue(Reflection.IsBonded(typeof(Bonded<BasicTypes>)));
Assert.IsTrue(Reflection.IsBonded(typeof(BondedVoid<>)));
Assert.IsTrue(Reflection.IsBonded(typeof(BondedVoid<CompactBinaryReader<InputBuffer>>)));
Assert.IsTrue(Reflection.IsBonded(typeof(CustomBonded<>)));
Assert.IsTrue(Reflection.IsBonded(typeof(CustomBonded<BasicTypes>)));
Assert.IsFalse(Reflection.IsBonded(typeof(int)));
Assert.IsFalse(Reflection.IsBonded(typeof(BasicTypes)));
}
// We test on the SchemaFields instead of the RuntimeSchema, because, for now, the list sub
// type is not part of Bond.TypeDef
[Test]
public void EnsureUnknownSeqIDLType()
public void Reflection_EnsureUnknownSeqIDLType()
{
var schemaFields = typeof(BasicTypes).GetSchemaFields();
@ -216,5 +232,31 @@
throw new NotImplementedException();
}
}
class CustomBonded<T> : IBonded
{
readonly IBonded<T> _instance;
public CustomBonded(IBonded<T> instance)
{
_instance = instance;
}
public void Serialize<W>(W writer)
{
_instance.Serialize<W>(writer);
}
public U Deserialize<U>()
{
return _instance.Deserialize<U>();
}
public IBonded<U> Convert<U>()
{
return _instance.Convert<U>();
}
}
}
}