From 1076e735697d393c7b10654f8dc7d9a91c7c13e1 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Fri, 23 Sep 2016 08:56:29 +0200 Subject: [XamlC] supports enum and consts in x:Static (#369) --- .../StaticExtensionTests.cs | 72 ---------------------- Xamarin.Forms.Xaml.UnitTests/XStatic.xaml | 8 ++- Xamarin.Forms.Xaml.UnitTests/XStatic.xaml.cs | 56 ++++++++++++++--- Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml | 8 +++ .../XStaticException.xaml.cs | 55 +++++++++++++++++ .../Xamarin.Forms.Xaml.UnitTests.csproj | 7 ++- 6 files changed, 125 insertions(+), 81 deletions(-) delete mode 100644 Xamarin.Forms.Xaml.UnitTests/StaticExtensionTests.cs create mode 100644 Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml.cs (limited to 'Xamarin.Forms.Xaml.UnitTests') diff --git a/Xamarin.Forms.Xaml.UnitTests/StaticExtensionTests.cs b/Xamarin.Forms.Xaml.UnitTests/StaticExtensionTests.cs deleted file mode 100644 index 86e02fe7..00000000 --- a/Xamarin.Forms.Xaml.UnitTests/StaticExtensionTests.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using NUnit.Framework; -using System.Xml; - -using Xamarin.Forms.Core.UnitTests; -using System.Reflection; - -namespace Xamarin.Forms.Xaml.UnitTests -{ - [TestFixture] - public class StaticExtensionTests : BaseTestFixture - { - IXamlTypeResolver typeResolver; - - [SetUp] - public override void Setup () - { - base.Setup (); - var nsManager = new XmlNamespaceManager (new NameTable ()); - nsManager.AddNamespace ("local", "clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests"); - nsManager.AddNamespace ("x", "http://schemas.microsoft.com/winfx/2006/xaml"); - - typeResolver = new Internals.XamlTypeResolver (nsManager, XamlParser.GetElementType, Assembly.GetCallingAssembly ()); - } - - [Test] - public void TestxStatic () - { - //{x:Static Member=prefix:typeName.staticMemberName} - //{x:Static prefix:typeName.staticMemberName} - - //The code entity that is referenced must be one of the following: - // - A constant - // - A static property - // - A field - // - An enumeration value - // All other cases should throw - - var serviceProvider = new Internals.XamlServiceProvider (null, null) { - IXamlTypeResolver = typeResolver, - }; - - //Static property - var markupString = @"{x:Static Member=""local:MockxStatic.MockStaticProperty""}"; - Assert.AreEqual ("Property", (new MarkupExtensionParser ()).ParseExpression (ref markupString, serviceProvider)); - - //constant - markupString = @"{x:Static Member=""local:MockxStatic.MockConstant""}"; - Assert.AreEqual ("Constant", (new MarkupExtensionParser ()).ParseExpression (ref markupString, serviceProvider)); - - //field - markupString = @"{x:Static Member=""local:MockxStatic.MockField""}"; - Assert.AreEqual ("Field", (new MarkupExtensionParser ()).ParseExpression (ref markupString, serviceProvider)); - - //enum - markupString = @"{x:Static Member=""local:MockEnum.Second""}"; - Assert.AreEqual (MockEnum.Second, (new MarkupExtensionParser ()).ParseExpression (ref markupString, serviceProvider)); - - //throw on InstanceProperty - markupString = @"{x:Static Member=""local:MockxStatic.InstanceProperty""}"; - Assert.Throws (()=> (new MarkupExtensionParser ()).ParseExpression (ref markupString, serviceProvider)); - - //quotes are optional - markupString = @"{x:Static Member=local:MockxStatic.MockStaticProperty}"; - Assert.AreEqual ("Property", (new MarkupExtensionParser ()).ParseExpression (ref markupString, serviceProvider)); - - //Member is optional - markupString = @"{x:Static local:MockxStatic.MockStaticProperty}"; - Assert.AreEqual ("Property", (new MarkupExtensionParser ()).ParseExpression (ref markupString, serviceProvider)); - } - } -} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml b/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml index 7c2910cd..32d2945e 100644 --- a/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml +++ b/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml.cs index ff1f0199..d8a466e9 100644 --- a/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml.cs +++ b/Xamarin.Forms.Xaml.UnitTests/XStatic.xaml.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; - -using Xamarin.Forms; - -using NUnit.Framework; +using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; namespace Xamarin.Forms.Xaml.UnitTests { @@ -16,7 +12,7 @@ namespace Xamarin.Forms.Xaml.UnitTests public static readonly Color BackgroundColor = Color.Fuchsia; } - public enum MockEnum + public enum MockEnum : long { First, Second, @@ -37,6 +33,28 @@ namespace Xamarin.Forms.Xaml.UnitTests [TestFixture] public class Tests { + //{x:Static Member=prefix:typeName.staticMemberName} + //{x:Static prefix:typeName.staticMemberName} + + //The code entity that is referenced must be one of the following: + // - A constant + // - A static property + // - A field + // - An enumeration value + // All other cases should throw + + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + [TestCase (false)] [TestCase (true)] public void StaticProperty (bool useCompiledXaml) @@ -60,6 +78,30 @@ namespace Xamarin.Forms.Xaml.UnitTests var layout = new XStatic (useCompiledXaml); Assert.AreEqual (Color.Fuchsia, layout.color.TextColor); } + + [TestCase(false)] + [TestCase(true)] + public void Constant(bool useCompiledXaml) + { + var layout = new XStatic(useCompiledXaml); + Assert.AreEqual("Constant", layout.constant.Text); + } + + [TestCase(false)] + [TestCase(true)] + public void Field(bool useCompiledXaml) + { + var layout = new XStatic(useCompiledXaml); + Assert.AreEqual("Field", layout.field.Text); + } + + [TestCase(false)] + [TestCase(true)] + public void Enum(bool useCompiledXaml) + { + var layout = new XStatic(useCompiledXaml); + Assert.AreEqual(ScrollOrientation.Both, layout.enuM.Orientation); + } } } } \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml b/Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml new file mode 100644 index 00000000..58d3cb10 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml @@ -0,0 +1,8 @@ + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml.cs new file mode 100644 index 00000000..261de06a --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/XStaticException.xaml.cs @@ -0,0 +1,55 @@ +using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + [XamlCompilation(XamlCompilationOptions.Skip)] + public partial class XStaticException : ContentPage + { + public XStaticException() + { + InitializeComponent(); + } + + public XStaticException(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + public class Tests + { + //{x:Static Member=prefix:typeName.staticMemberName} + //{x:Static prefix:typeName.staticMemberName} + + //The code entity that is referenced must be one of the following: + // - A constant + // - A static property + // - A field + // - An enumeration value + // All other cases should throw + + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + + [TestCase(false)] + [TestCase(true)] + public void ThrowOnInstanceProperty(bool useCompiledXaml) + { + if (!useCompiledXaml) + Assert.Throws(new XamlParseExceptionConstraint(7, 6), () => new XStaticException(useCompiledXaml)); + else + Assert.Throws(new XamlParseExceptionConstraint(7, 6), () => MockCompiler.Compile(typeof(XStaticException))); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index 16f54e88..b2f5e0ac 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -96,7 +96,6 @@ - @@ -366,6 +365,9 @@ NativeViewsAndBindings.xaml + + XStaticException.xaml + @@ -648,6 +650,9 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + -- cgit v1.2.3