diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2017-06-21 10:32:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-21 10:32:58 +0200 |
commit | c09603732916be56d27f21cea4ef1370057414bc (patch) | |
tree | 08e63c73ef6b04120e9a9b034a99d9f3d9c469d0 /Xamarin.Forms.Xaml.UnitTests | |
parent | 28b343b2788fa073a3d3a87610275d8f3e756b61 (diff) | |
download | xamarin-forms-c09603732916be56d27f21cea4ef1370057414bc.tar.gz xamarin-forms-c09603732916be56d27f21cea4ef1370057414bc.tar.bz2 xamarin-forms-c09603732916be56d27f21cea4ef1370057414bc.zip |
[XamlC] Allow Properties and BP of generic types (#995)
* [XamlC] Allow Properties and BP of generic types
* try hard to convince the compiler that the variable is initialized
* Update MethodDefinitionExtensions.cs
Diffstat (limited to 'Xamarin.Forms.Xaml.UnitTests')
5 files changed, 167 insertions, 42 deletions
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53350.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53350.xaml new file mode 100644 index 00000000..ad664ba0 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53350.xaml @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" + xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" + xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests" + x:Class="Xamarin.Forms.Xaml.UnitTests.Bz53350"> + <local:Bz53350String x:Name="content" SomeBP="Foo" SomeProperty="Bar" /> +</ContentPage>
\ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53350.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53350.xaml.cs new file mode 100644 index 00000000..f25c018a --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53350.xaml.cs @@ -0,0 +1,63 @@ +using System; +using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public class Bz53350Generic<T> : ContentView + { + public static readonly BindableProperty SomeBPProperty = + BindableProperty.Create("SomeBP", typeof(T), typeof(Bz53350Generic<T>), default(T)); + + public T SomeBP + { + get { return (T)GetValue(SomeBPProperty); } + set { SetValue(SomeBPProperty, value); } + } + + public T SomeProperty { get; set; } + } + + public class Bz53350String : Bz53350Generic<string> + { + + } + + public partial class Bz53350 + { + public Bz53350() + { + } + + public Bz53350(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Application.Current = null; + Device.PlatformServices = null; + } + + [TestCase(true)] + [TestCase(false)] + public void PropertiesWithGenericType(bool useCompiledXaml) + { + var layout = new Bz53350(useCompiledXaml); + Assert.That(layout.content.SomeBP, Is.EqualTo("Foo")); + Assert.That(layout.content.SomeProperty, Is.EqualTo("Bar")); + } + } + } +} diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index d528379e..ba470e60 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -491,6 +491,10 @@ <Compile Include="Issues\Bz56852.xaml.cs"> <DependentUpon>Bz56852.xaml</DependentUpon> </Compile> + <Compile Include="Issues\Bz53350.xaml.cs"> + <DependentUpon>Bz53350.xaml</DependentUpon> + </Compile> + <Compile Include="XamlC\MethodDefinitionExtensionsTests.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" /> @@ -899,6 +903,9 @@ <EmbeddedResource Include="Issues\Bz56852.xaml"> <Generator>MSBuild:UpdateDesignTimeXaml</Generator> </EmbeddedResource> + <EmbeddedResource Include="Issues\Bz53350.xaml"> + <Generator>MSBuild:UpdateDesignTimeXaml</Generator> + </EmbeddedResource> </ItemGroup> <ItemGroup> <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> diff --git a/Xamarin.Forms.Xaml.UnitTests/XamlC/MethodDefinitionExtensionsTests.cs b/Xamarin.Forms.Xaml.UnitTests/XamlC/MethodDefinitionExtensionsTests.cs new file mode 100644 index 00000000..6133d727 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/XamlC/MethodDefinitionExtensionsTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Linq; + +using Mono.Cecil; + +using Xamarin.Forms.Build.Tasks; + +using NUnit.Framework; +using System.Collections.Generic; + +namespace Xamarin.Forms.Xaml.XamlcUnitTests +{ + [TestFixture] + public class MethodDefinitionExtensionsTests + { + public class NonGenericClass + { + public object Property { get; set; } + } + + public class GenericClass<T, U, V> + { + public object NonGeneric() => default(object); + public T GenericT() => default(T); + public U GenericU() => default(U); + public V GenericV() => default(V); + public IEnumerable<T> EnumerableT() => default(IEnumerable<T>); + public KeyValuePair<V, U> KvpVU() => default(KeyValuePair<V,U>); + } + + ModuleDefinition module; + + [SetUp] + public void SetUp() + { + module = ModuleDefinition.CreateModule("foo", ModuleKind.Dll); + } + + [Test] + public void ResolveGenericReturnType() + { + var type = module.ImportReference(typeof(GenericClass<bool, string, int>)); + + var getter = type.GetMethods(md => md.Name == "NonGeneric", module).Single(); + var returnType = getter.Item1.ResolveGenericReturnType(getter.Item2, module); + Assert.AreEqual("System.Object", returnType.FullName); + + getter = type.GetMethods(md => md.Name == "GenericT", module).Single(); + returnType = getter.Item1.ResolveGenericReturnType(getter.Item2, module); + Assert.AreEqual("System.Boolean", returnType.FullName); + + getter = type.GetMethods(md => md.Name == "GenericU", module).Single(); + returnType = getter.Item1.ResolveGenericReturnType(getter.Item2, module); + Assert.AreEqual("System.String", returnType.FullName); + + getter = type.GetMethods(md => md.Name == "GenericV", module).Single(); + returnType = getter.Item1.ResolveGenericReturnType(getter.Item2, module); + Assert.AreEqual("System.Int32", returnType.FullName); + } + } +}
\ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/XamlC/PropertyDefinitionExtensionsTests.cs b/Xamarin.Forms.Xaml.UnitTests/XamlC/PropertyDefinitionExtensionsTests.cs index b43e4c9d..e8e56afa 100644 --- a/Xamarin.Forms.Xaml.UnitTests/XamlC/PropertyDefinitionExtensionsTests.cs +++ b/Xamarin.Forms.Xaml.UnitTests/XamlC/PropertyDefinitionExtensionsTests.cs @@ -6,6 +6,7 @@ using Mono.Cecil; using Xamarin.Forms.Build.Tasks; using NUnit.Framework; +using System.Collections.Generic; namespace Xamarin.Forms.Xaml.XamlcUnitTests { @@ -17,10 +18,14 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests public object Property { get; set; } } - public class GenericClass<T> + public class GenericClass<T, U, V> { public object Property { get; set; } - public T GenericProperty { get; set; } + public T GenericT { get; set; } + public U GenericU { get; set; } + public V GenericV { get; set; } + public IEnumerable<T> EnumerableT { get; set; } + public KeyValuePair<V, U> KvpVU { get; set; } } ModuleDefinition module; @@ -31,45 +36,27 @@ namespace Xamarin.Forms.Xaml.XamlcUnitTests module = ModuleDefinition.CreateModule ("foo", ModuleKind.Dll); } -// [Test] -// public void ResolveGenericsOnNonGenericPreserveAccessors () -// { -// var type = module.Import (typeof (NonGenericClass)); -// TypeReference declaringTypeReference; -// PropertyDefinition prop = type.GetProperty (fd => fd.Name == "Property", out declaringTypeReference); -// Assert.AreEqual ("System.Object", prop.PropertyType.FullName); -// Assert.AreEqual ("System.Void Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/NonGenericClass::set_Property(System.Object)", prop.SetMethod.FullName); -// Assert.AreEqual ("System.Object Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/NonGenericClass::get_Property()", prop.GetMethod.FullName); -// Assert.AreEqual ("Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/NonGenericClass", prop.DeclaringType.FullName); -// -// prop.ResolveGenericParameters (declaringTypeReference); -// -// Assert.AreEqual ("System.Object", prop.PropertyType.FullName); -// Assert.AreEqual ("System.Void Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/NonGenericClass::set_Property(System.Object)", prop.SetMethod.FullName); -// Assert.AreEqual ("System.Object Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/NonGenericClass::get_Property()", prop.GetMethod.FullName); -// Assert.AreEqual ("Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/NonGenericClass", prop.DeclaringType.FullName); -// -// } -// -// [Test] -// public void NonGenericPropertyOnGenericType () -// { -// var type = module.Import (typeof (GenericClass<bool>)); -// TypeReference declaringTypeReference; -// PropertyDefinition prop = type.GetProperty (fd => fd.Name == "Property", out declaringTypeReference); -// Assert.AreEqual ("System.Object", prop.PropertyType.FullName); -// Assert.AreEqual ("System.Void Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/GenericClass`1::set_Property(System.Object)", prop.SetMethod.FullName); -// Assert.AreEqual ("System.Object Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/GenericClass`1::get_Property()", prop.GetMethod.FullName); -// Assert.AreEqual ("Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/GenericClass`1", prop.DeclaringType.FullName); -// Assert.False (prop.DeclaringType.IsGenericInstance); -// -// prop.ResolveGenericParameters (declaringTypeReference); -// Assert.AreEqual ("System.Object", prop.PropertyType.FullName); -// Assert.AreEqual ("System.Void Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/GenericClass`1::set_Property(System.Object)", prop.SetMethod.FullName); -// Assert.AreEqual ("System.Object Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/GenericClass`1::get_Property()", prop.GetMethod.FullName); -// Assert.AreEqual ("Xamarin.Forms.Xaml.XamlcUnitTests.PropertyDefinitionExtensionsTests/GenericClass`1", prop.DeclaringType.FullName); -// Assert.True (prop.DeclaringType.IsGenericInstance); -// -// } + [Test] + public void ResolveGenericPropertyType () + { + var type = module.ImportReference (typeof (GenericClass<bool, string, int>)); + TypeReference declaringTypeReference; + var prop = type.GetProperty (fd => fd.Name == "Property", out declaringTypeReference); + var propertyType = prop.ResolveGenericPropertyType (declaringTypeReference, module); + Assert.AreEqual ("System.Object", propertyType.FullName); + + prop = type.GetProperty(fd => fd.Name == "GenericT", out declaringTypeReference); + propertyType = prop.ResolveGenericPropertyType(declaringTypeReference, module); + Assert.AreEqual("System.Boolean", propertyType.FullName); + + prop = type.GetProperty(fd => fd.Name == "GenericU", out declaringTypeReference); + propertyType = prop.ResolveGenericPropertyType(declaringTypeReference, module); + Assert.AreEqual("System.String", propertyType.FullName); + + prop = type.GetProperty(fd => fd.Name == "GenericV", out declaringTypeReference); + propertyType = prop.ResolveGenericPropertyType(declaringTypeReference, module); + Assert.AreEqual("System.Int32", propertyType.FullName); + + } } }
\ No newline at end of file |