Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CalcBinding/CalcBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,12 @@ private string GetExpressionTemplate(string path, List<PathAppearances> properti
return result;
}

private static int _enumCounter = 0;

private string GetEnumName(int i)
{
// Enum1, Enum2, etc
return string.Format("Enum{0}", ++i);
return string.Format("Enum{0}", ++_enumCounter);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Tests.Core/zTests.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<IsPackable>false</IsPackable>
<UseWPF>true</UseWPF>
</PropertyGroup>
Expand Down
63 changes: 63 additions & 0 deletions Tests/BaseSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,37 @@ public void BindingAssert<TTargetProperty>(
Assert.AreEqual(targetValue2, realValue2);
}

public void MulitpleBindingAssert<TTargetProperty>(List<BindingTestInput> bindingInputs,
INotifyPropertyChanged source,
List<BindingTestCase<TTargetProperty>> testCases,
Dictionary<string, Type> resolvedTypes = null
)
{
// the two loops on the bindingInputs are used to replicate the order things happen in a WPF view. The ProvideValue
// should be called for all bindings before the SetValue is called on the TargetObject. This allows issue #66 to
// be reproduced in a unit test.

foreach (var input in bindingInputs)
{
input.TargetObject.DataContext = source;
var binding = new CalcBinding.Binding(input.Path);
input.BindingExpression = binding.ProvideValue(new ServiceProviderMock(input.TargetObject, input.TargetProperty, resolvedTypes));
}

foreach (var input in bindingInputs)
{
input.TargetObject.SetValue(input.TargetProperty, input.BindingExpression);
}

foreach (var testCase in testCases)
{
testCase.SourcePropertySetter();
var realValue = testCase.TargetPropertyGetter();
Assert.AreEqual(testCase.TargetValue, realValue);
}
}


#endregion


Expand Down Expand Up @@ -318,4 +349,36 @@ public void BindingBackAssert<TTargetProperty>(

#endregion
}

public class BindingTestCase<TTargetProperty>
{
public BindingTestCase(Action sourcePropertySetter,
Func<TTargetProperty> targetPropertyGetter,
TTargetProperty targetValue)
{
SourcePropertySetter = sourcePropertySetter;
TargetPropertyGetter = targetPropertyGetter;
TargetValue = targetValue;
}

public Action SourcePropertySetter { get; set; }
public Func<TTargetProperty> TargetPropertyGetter { get; set; }
public TTargetProperty TargetValue { get; set; }
}

public class BindingTestInput
{
public BindingTestInput(string path, FrameworkElement targetObject, DependencyProperty targetProperty)
{
Path = path;
TargetObject = targetObject;
TargetProperty = targetProperty;
}

public string Path { get; set; }
public FrameworkElement TargetObject {get; set;}
public DependencyProperty TargetProperty {get; set;}
public object BindingExpression { get; set; }

}
}
31 changes: 31 additions & 0 deletions Tests/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using WpfExample;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Tests
{
Expand Down Expand Up @@ -136,5 +137,35 @@ public void BindingToSingleEnumTest()
}
);
}

[TestMethod]
public void MultipleEnumBindingsTest()
{
var exampleViewModel = new ExampleViewModel();
var chb1 = new CheckBox();
var chb2 = new CheckBox();

var inputs = new List<BindingTestInput>()
{
new BindingTestInput("EnumValue == local:Enum2.Value1", chb1, CheckBox.IsCheckedProperty),
new BindingTestInput("Enum3Value == local:Enum3.Value11", chb2, CheckBox.IsCheckedProperty),
};

var outputs = new List<BindingTestCase<bool?>>()
{
new BindingTestCase<bool?>(() => exampleViewModel.EnumValue = Enum2.Value1, () => chb1.IsChecked, true),
new BindingTestCase<bool?>(() => exampleViewModel.EnumValue = Enum2.Value2, () => chb1.IsChecked, false),
new BindingTestCase<bool?>(() => exampleViewModel.Enum3Value = Enum3.Value11, () => chb2.IsChecked, true),
new BindingTestCase<bool?>(() => exampleViewModel.Enum3Value = Enum3.Value22, () => chb2.IsChecked, false),
};

var resolvedTypes = new Dictionary<string, Type>()
{
{ "local:Enum2", typeof(Enum2) },
{ "local:Enum3", typeof(Enum3) }
};

MulitpleBindingAssert(inputs, exampleViewModel, outputs, resolvedTypes);
}
}
}
22 changes: 21 additions & 1 deletion WpfExample/ExampleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,20 @@ public Enum2 EnumValue
}
}

private Enum3 enum3Value;
public Enum3 Enum3Value
{
get
{
return enum3Value;
}
set
{
enum3Value = value;
RaisePropertyChanged(() => Enum3Value);
}
}

private Visibility visibility;
public Visibility Visibility
{
Expand Down Expand Up @@ -607,7 +621,7 @@ public static Enum2 EnumValue
enumValue = value;
RaiseStaticPropertyChanged(() => EnumValue);
}
}
}

private static Visibility visibility;
public static Visibility Visibility
Expand Down Expand Up @@ -728,4 +742,10 @@ public enum Enum2
Value1,
Value2
}

public enum Enum3
{
Value11,
Value22
}
}