From 3592a75723294e3e4d058492e7d74098b1f4fe33 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Wed, 12 Mar 2014 12:54:07 +0000 Subject: [PATCH] Add unit tests for ActionExtension --- Stylet/CommandAction.cs | 18 +++--- StyletUnitTests/ActionExtensionTests.cs | 74 +++++++++++++++++++++++++ StyletUnitTests/StyletUnitTests.csproj | 8 +++ StyletUnitTests/packages.config | 1 + 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 StyletUnitTests/ActionExtensionTests.cs diff --git a/Stylet/CommandAction.cs b/Stylet/CommandAction.cs index e52b8bb..09903e0 100644 --- a/Stylet/CommandAction.cs +++ b/Stylet/CommandAction.cs @@ -26,12 +26,12 @@ namespace Stylet /// /// View to grab the View.ActionTarget from /// - private FrameworkElement subject; + public FrameworkElement Subject { get; private set; } /// /// Method name. E.g. if someone's gone Buttom Command="{s:Action MyMethod}", this is MyMethod. /// - private string methodName; + public string MethodName { get; private set; } /// /// Generated accessor to grab the value of the guard property, or null if there is none @@ -52,23 +52,23 @@ namespace Stylet /// Method name. the MyMethod in Buttom Command="{s:Action MyMethod}". public CommandAction(FrameworkElement subject, string methodName) { - this.subject = subject; - this.methodName = methodName; + this.Subject = subject; + this.MethodName = methodName; this.UpdateGuardAndMethod(); // Observe the View.ActionTarget for changes, and re-bind the guard property and MethodInfo if it changes - DependencyPropertyDescriptor.FromProperty(View.ActionTargetProperty, typeof(View)).AddValueChanged(this.subject, (o, e) => this.UpdateGuardAndMethod()); + DependencyPropertyDescriptor.FromProperty(View.ActionTargetProperty, typeof(View)).AddValueChanged(this.Subject, (o, e) => this.UpdateGuardAndMethod()); } private string GuardName { - get { return "Can" + this.methodName; } + get { return "Can" + this.MethodName; } } private void UpdateGuardAndMethod() { - var newTarget = View.GetActionTarget(this.subject); + var newTarget = View.GetActionTarget(this.Subject); MethodInfo targetMethodInfo = null; this.guardPropertyGetter = null; @@ -84,9 +84,9 @@ namespace Stylet this.guardPropertyGetter = Expressions.Expression.Lambda>(propertyAccess).Compile(); } - targetMethodInfo = newTargetType.GetMethod(this.methodName); + targetMethodInfo = newTargetType.GetMethod(this.MethodName); if (targetMethodInfo == null) - throw new ArgumentException(String.Format("Unable to find method {0} on {1}", this.methodName, newTargetType.Name)); + throw new ArgumentException(String.Format("Unable to find method {0} on {1}", this.MethodName, newTargetType.Name)); } var oldTarget = this.target as INotifyPropertyChanged; diff --git a/StyletUnitTests/ActionExtensionTests.cs b/StyletUnitTests/ActionExtensionTests.cs new file mode 100644 index 0000000..79fb151 --- /dev/null +++ b/StyletUnitTests/ActionExtensionTests.cs @@ -0,0 +1,74 @@ +using Moq; +using NUnit.Framework; +using Stylet; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Markup; + +namespace StyletUnitTests +{ + [TestFixture, RequiresSTA] + public class ActionExtensionTests + { + private ActionExtension actionExtension; + private Mock provideValueTarget; + private Mock serviceProvider; + + [SetUp] + public void SetUp() + { + this.actionExtension = new ActionExtension("MethodName"); + + this.provideValueTarget = new Mock(); + this.provideValueTarget.Setup(x => x.TargetObject).Returns(new FrameworkElement()); + + this.serviceProvider = new Mock(); + serviceProvider.Setup(x => x.GetService(typeof(IProvideValueTarget))).Returns(provideValueTarget.Object); + } + + [Test] + public void ReturnsThisIfTargetObjectIsNotFrameworkElement() + { + this.provideValueTarget.Setup(x => x.TargetObject).Returns(null); + + Assert.AreEqual(this.actionExtension, this.actionExtension.ProvideValue(this.serviceProvider.Object)); + } + + [Test] + public void ReturnsCommandActionIfTargetObjectPropertyTypeIsICommand() + { + // This will be asked for the IViewManager, which we don't care about for now + IoC.GetInstance = (t, k) => null; + this.provideValueTarget.Setup(x => x.TargetProperty).Returns(Button.CommandProperty); + + object value = this.actionExtension.ProvideValue(this.serviceProvider.Object); + Assert.IsInstanceOf(value); + + var action = (CommandAction)value; + + Assert.AreEqual(action.Subject, this.provideValueTarget.Object.TargetObject); + Assert.AreEqual("MethodName", action.MethodName); + } + + [Test] + public void ReturnsEventActionIfTargetObjectPropertyIsEventInfo() + { + this.provideValueTarget.Setup(x => x.TargetProperty).Returns(typeof(Button).GetEvent("Click")); + + Assert.IsInstanceOf(this.actionExtension.ProvideValue(this.serviceProvider.Object)); + } + + [Test] + public void ThrowsArgumentExceptionIfTargetObjectNotDependencyPropertyOrEventInfo() + { + this.provideValueTarget.Setup(x => x.TargetProperty).Returns(5); + + Assert.Throws(() => this.actionExtension.ProvideValue(this.serviceProvider.Object)); + } + } +} diff --git a/StyletUnitTests/StyletUnitTests.csproj b/StyletUnitTests/StyletUnitTests.csproj index e319fe2..19ba153 100644 --- a/StyletUnitTests/StyletUnitTests.csproj +++ b/StyletUnitTests/StyletUnitTests.csproj @@ -32,18 +32,26 @@ 4 + + ..\Sandbox\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll + ..\packages\NUnit.2.6.3\lib\nunit.framework.dll + + + + + diff --git a/StyletUnitTests/packages.config b/StyletUnitTests/packages.config index ad37a52..5cd66f2 100644 --- a/StyletUnitTests/packages.config +++ b/StyletUnitTests/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file