diff --git a/CalcBinding/CalcBinding.cs b/CalcBinding/CalcBinding.cs index 7c82cdf..d18a649 100644 --- a/CalcBinding/CalcBinding.cs +++ b/CalcBinding/CalcBinding.cs @@ -295,10 +295,12 @@ private string GetExpressionTemplate(string path, List 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); } /// diff --git a/Tests.Core/zTests.Core.csproj b/Tests.Core/zTests.Core.csproj index 90a8d32..ba6b37b 100644 --- a/Tests.Core/zTests.Core.csproj +++ b/Tests.Core/zTests.Core.csproj @@ -2,6 +2,7 @@ netcoreapp3.0 + false false true diff --git a/Tests/BaseSystemTests.cs b/Tests/BaseSystemTests.cs index 6254ba8..c56c5f5 100644 --- a/Tests/BaseSystemTests.cs +++ b/Tests/BaseSystemTests.cs @@ -194,6 +194,37 @@ public void BindingAssert( Assert.AreEqual(targetValue2, realValue2); } + public void MulitpleBindingAssert(List bindingInputs, + INotifyPropertyChanged source, + List> testCases, + Dictionary 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 @@ -318,4 +349,36 @@ public void BindingBackAssert( #endregion } + + public class BindingTestCase + { + public BindingTestCase(Action sourcePropertySetter, + Func targetPropertyGetter, + TTargetProperty targetValue) + { + SourcePropertySetter = sourcePropertySetter; + TargetPropertyGetter = targetPropertyGetter; + TargetValue = targetValue; + } + + public Action SourcePropertySetter { get; set; } + public Func 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; } + + } } diff --git a/Tests/EnumTests.cs b/Tests/EnumTests.cs index 597d643..ae855d7 100644 --- a/Tests/EnumTests.cs +++ b/Tests/EnumTests.cs @@ -3,6 +3,7 @@ using WpfExample; using System.Collections.Generic; using System.Windows; +using System.Windows.Controls; namespace Tests { @@ -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() + { + new BindingTestInput("EnumValue == local:Enum2.Value1", chb1, CheckBox.IsCheckedProperty), + new BindingTestInput("Enum3Value == local:Enum3.Value11", chb2, CheckBox.IsCheckedProperty), + }; + + var outputs = new List>() + { + new BindingTestCase(() => exampleViewModel.EnumValue = Enum2.Value1, () => chb1.IsChecked, true), + new BindingTestCase(() => exampleViewModel.EnumValue = Enum2.Value2, () => chb1.IsChecked, false), + new BindingTestCase(() => exampleViewModel.Enum3Value = Enum3.Value11, () => chb2.IsChecked, true), + new BindingTestCase(() => exampleViewModel.Enum3Value = Enum3.Value22, () => chb2.IsChecked, false), + }; + + var resolvedTypes = new Dictionary() + { + { "local:Enum2", typeof(Enum2) }, + { "local:Enum3", typeof(Enum3) } + }; + + MulitpleBindingAssert(inputs, exampleViewModel, outputs, resolvedTypes); + } } } diff --git a/WpfExample/ExampleViewModel.cs b/WpfExample/ExampleViewModel.cs index 948fde7..9fadc44 100644 --- a/WpfExample/ExampleViewModel.cs +++ b/WpfExample/ExampleViewModel.cs @@ -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 { @@ -607,7 +621,7 @@ public static Enum2 EnumValue enumValue = value; RaiseStaticPropertyChanged(() => EnumValue); } - } + } private static Visibility visibility; public static Visibility Visibility @@ -728,4 +742,10 @@ public enum Enum2 Value1, Value2 } + + public enum Enum3 + { + Value11, + Value22 + } }