mirror of https://github.com/AMT-Cheif/Stylet.git
Add unit tests for ActionExtension
This commit is contained in:
parent
f3fdd86a03
commit
3592a75723
|
@ -26,12 +26,12 @@ namespace Stylet
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// View to grab the View.ActionTarget from
|
/// View to grab the View.ActionTarget from
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private FrameworkElement subject;
|
public FrameworkElement Subject { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Method name. E.g. if someone's gone Buttom Command="{s:Action MyMethod}", this is MyMethod.
|
/// Method name. E.g. if someone's gone Buttom Command="{s:Action MyMethod}", this is MyMethod.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private string methodName;
|
public string MethodName { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generated accessor to grab the value of the guard property, or null if there is none
|
/// Generated accessor to grab the value of the guard property, or null if there is none
|
||||||
|
@ -52,23 +52,23 @@ namespace Stylet
|
||||||
/// <param name="methodName">Method name. the MyMethod in Buttom Command="{s:Action MyMethod}".</param>
|
/// <param name="methodName">Method name. the MyMethod in Buttom Command="{s:Action MyMethod}".</param>
|
||||||
public CommandAction(FrameworkElement subject, string methodName)
|
public CommandAction(FrameworkElement subject, string methodName)
|
||||||
{
|
{
|
||||||
this.subject = subject;
|
this.Subject = subject;
|
||||||
this.methodName = methodName;
|
this.MethodName = methodName;
|
||||||
|
|
||||||
this.UpdateGuardAndMethod();
|
this.UpdateGuardAndMethod();
|
||||||
|
|
||||||
// Observe the View.ActionTarget for changes, and re-bind the guard property and MethodInfo if it changes
|
// 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
|
private string GuardName
|
||||||
{
|
{
|
||||||
get { return "Can" + this.methodName; }
|
get { return "Can" + this.MethodName; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateGuardAndMethod()
|
private void UpdateGuardAndMethod()
|
||||||
{
|
{
|
||||||
var newTarget = View.GetActionTarget(this.subject);
|
var newTarget = View.GetActionTarget(this.Subject);
|
||||||
MethodInfo targetMethodInfo = null;
|
MethodInfo targetMethodInfo = null;
|
||||||
|
|
||||||
this.guardPropertyGetter = null;
|
this.guardPropertyGetter = null;
|
||||||
|
@ -84,9 +84,9 @@ namespace Stylet
|
||||||
this.guardPropertyGetter = Expressions.Expression.Lambda<Func<bool>>(propertyAccess).Compile();
|
this.guardPropertyGetter = Expressions.Expression.Lambda<Func<bool>>(propertyAccess).Compile();
|
||||||
}
|
}
|
||||||
|
|
||||||
targetMethodInfo = newTargetType.GetMethod(this.methodName);
|
targetMethodInfo = newTargetType.GetMethod(this.MethodName);
|
||||||
if (targetMethodInfo == null)
|
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;
|
var oldTarget = this.target as INotifyPropertyChanged;
|
||||||
|
|
|
@ -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<IProvideValueTarget> provideValueTarget;
|
||||||
|
private Mock<IServiceProvider> serviceProvider;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
this.actionExtension = new ActionExtension("MethodName");
|
||||||
|
|
||||||
|
this.provideValueTarget = new Mock<IProvideValueTarget>();
|
||||||
|
this.provideValueTarget.Setup(x => x.TargetObject).Returns(new FrameworkElement());
|
||||||
|
|
||||||
|
this.serviceProvider = new Mock<IServiceProvider>();
|
||||||
|
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<CommandAction>(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<RoutedEventHandler>(this.actionExtension.ProvideValue(this.serviceProvider.Object));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ThrowsArgumentExceptionIfTargetObjectNotDependencyPropertyOrEventInfo()
|
||||||
|
{
|
||||||
|
this.provideValueTarget.Setup(x => x.TargetProperty).Returns(5);
|
||||||
|
|
||||||
|
Assert.Throws<ArgumentException>(() => this.actionExtension.ProvideValue(this.serviceProvider.Object));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -32,18 +32,26 @@
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Moq">
|
||||||
|
<HintPath>..\Sandbox\packages\Moq.4.2.1402.2112\lib\net40\Moq.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="nunit.framework">
|
<Reference Include="nunit.framework">
|
||||||
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
|
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="PresentationCore" />
|
||||||
|
<Reference Include="PresentationFramework" />
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Xaml" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
|
<Reference Include="WindowsBase" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="ActionExtensionTests.cs" />
|
||||||
<Compile Include="BindableCollectionTests.cs" />
|
<Compile Include="BindableCollectionTests.cs" />
|
||||||
<Compile Include="EventAggregatorTests.cs" />
|
<Compile Include="EventAggregatorTests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<packages>
|
<packages>
|
||||||
|
<package id="Moq" version="4.2.1402.2112" targetFramework="net45" />
|
||||||
<package id="NUnit" version="2.6.3" targetFramework="net45" />
|
<package id="NUnit" version="2.6.3" targetFramework="net45" />
|
||||||
</packages>
|
</packages>
|
Loading…
Reference in New Issue