21 changed files with 398 additions and 201 deletions
@ -0,0 +1,99 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using Perspex.Data; |
|||
using Perspex.Markup.Data.Plugins; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel; |
|||
using System.Linq; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
|
|||
namespace Perspex.Markup.UnitTests.Data |
|||
{ |
|||
public class ExceptionValidatorTests |
|||
{ |
|||
public class Data : INotifyPropertyChanged |
|||
{ |
|||
private int nonValidated; |
|||
|
|||
public int NonValidated |
|||
{ |
|||
get { return nonValidated; } |
|||
set { nonValidated = value; NotifyPropertyChanged(); } |
|||
} |
|||
|
|||
private int mustBePositive; |
|||
|
|||
public int MustBePositive |
|||
{ |
|||
get { return mustBePositive; } |
|||
set |
|||
{ |
|||
if (value <= 0) |
|||
{ |
|||
throw new ArgumentOutOfRangeException(nameof(value)); |
|||
} |
|||
mustBePositive = value; |
|||
} |
|||
} |
|||
|
|||
public event PropertyChangedEventHandler PropertyChanged; |
|||
|
|||
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") |
|||
{ |
|||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Setting_Non_Validating_Triggers_Validation() |
|||
{ |
|||
var inpcAccessorPlugin = new InpcPropertyAccessorPlugin(); |
|||
var validatorPlugin = new ExceptionValidationCheckerPlugin(); |
|||
var data = new Data(); |
|||
var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.NonValidated), _ => { }); |
|||
ValidationStatus status = null; |
|||
var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.NonValidated), accessor, s => status = s); |
|||
|
|||
validator.SetValue(5, BindingPriority.LocalValue); |
|||
|
|||
Assert.NotNull(status); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Setting_Validating_Property_To_Valid_Value_Returns_Successful_ValidationStatus() |
|||
{ |
|||
var inpcAccessorPlugin = new InpcPropertyAccessorPlugin(); |
|||
var validatorPlugin = new ExceptionValidationCheckerPlugin(); |
|||
var data = new Data(); |
|||
var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), _ => { }); |
|||
ValidationStatus status = null; |
|||
var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), accessor, s => status = s); |
|||
|
|||
validator.SetValue(5, BindingPriority.LocalValue); |
|||
|
|||
Assert.True(status.IsValid); |
|||
} |
|||
|
|||
|
|||
|
|||
[Fact] |
|||
public void Setting_Validating_Property_To_Invalid_Value_Returns_Failed_ValidationStatus() |
|||
{ |
|||
var inpcAccessorPlugin = new InpcPropertyAccessorPlugin(); |
|||
var validatorPlugin = new ExceptionValidationCheckerPlugin(); |
|||
var data = new Data(); |
|||
var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), _ => { }); |
|||
ValidationStatus status = null; |
|||
var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), accessor, s => status = s); |
|||
|
|||
validator.SetValue(-5, BindingPriority.LocalValue); |
|||
|
|||
Assert.False(status.IsValid); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,120 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
|
|||
using Perspex.Data; |
|||
using Perspex.Markup.Data.Plugins; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.ComponentModel; |
|||
using System.Linq; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Xunit; |
|||
using System.Collections; |
|||
|
|||
namespace Perspex.Markup.UnitTests.Data |
|||
{ |
|||
public class IndeiValidatorTests |
|||
{ |
|||
public class Data : INotifyPropertyChanged, INotifyDataErrorInfo |
|||
{ |
|||
private int nonValidated; |
|||
|
|||
public int NonValidated |
|||
{ |
|||
get { return nonValidated; } |
|||
set { nonValidated = value; NotifyPropertyChanged(); } |
|||
} |
|||
|
|||
private int mustBePositive; |
|||
|
|||
public int MustBePositive |
|||
{ |
|||
get { return mustBePositive; } |
|||
set |
|||
{ |
|||
mustBePositive = value; |
|||
NotifyErrorsChanged(); |
|||
} |
|||
} |
|||
|
|||
public bool HasErrors |
|||
{ |
|||
get |
|||
{ |
|||
return MustBePositive > 0; |
|||
} |
|||
} |
|||
|
|||
public event PropertyChangedEventHandler PropertyChanged; |
|||
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged; |
|||
|
|||
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") |
|||
{ |
|||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
|||
} |
|||
|
|||
private void NotifyErrorsChanged([CallerMemberName] string propertyName = "") |
|||
{ |
|||
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName)); |
|||
} |
|||
|
|||
public IEnumerable GetErrors(string propertyName) |
|||
{ |
|||
if (propertyName == nameof(MustBePositive) && MustBePositive <= 0) |
|||
{ |
|||
yield return $"{nameof(MustBePositive)} must be positive"; |
|||
} |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Setting_Non_Validating_Does_Not_Trigger_Validation() |
|||
{ |
|||
var inpcAccessorPlugin = new InpcPropertyAccessorPlugin(); |
|||
var validatorPlugin = new IndeiValidationCheckerPlugin(); |
|||
var data = new Data(); |
|||
var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.NonValidated), _ => { }); |
|||
ValidationStatus status = null; |
|||
var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.NonValidated), accessor, s => status = s); |
|||
|
|||
validator.SetValue(5, BindingPriority.LocalValue); |
|||
|
|||
Assert.Null(status); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Setting_Validating_Property_To_Valid_Value_Returns_Successful_ValidationStatus() |
|||
{ |
|||
var inpcAccessorPlugin = new InpcPropertyAccessorPlugin(); |
|||
var validatorPlugin = new IndeiValidationCheckerPlugin(); |
|||
var data = new Data(); |
|||
var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), _ => { }); |
|||
ValidationStatus status = null; |
|||
var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), accessor, s => status = s); |
|||
|
|||
validator.SetValue(5, BindingPriority.LocalValue); |
|||
|
|||
Assert.True(status.IsValid); |
|||
} |
|||
|
|||
|
|||
|
|||
[Fact] |
|||
public void Setting_Validating_Property_To_Invalid_Value_Returns_Failed_ValidationStatus() |
|||
{ |
|||
var inpcAccessorPlugin = new InpcPropertyAccessorPlugin(); |
|||
var validatorPlugin = new IndeiValidationCheckerPlugin(); |
|||
var data = new Data(); |
|||
var accessor = inpcAccessorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), _ => { }); |
|||
ValidationStatus status = null; |
|||
var validator = validatorPlugin.Start(new WeakReference(data), nameof(data.MustBePositive), accessor, s => status = s); |
|||
|
|||
validator.SetValue(-5, BindingPriority.LocalValue); |
|||
|
|||
Assert.False(status.IsValid); |
|||
} |
|||
} |
|||
} |
|||
@ -1,142 +0,0 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using Perspex.Markup.Data.Plugins; |
|||
using System; |
|||
using System.Collections; |
|||
using System.ComponentModel; |
|||
using System.Runtime.CompilerServices; |
|||
using Xunit; |
|||
|
|||
namespace Perspex.Markup.UnitTests.Data |
|||
{ |
|||
public class InpcPluginTests |
|||
{ |
|||
private class InpcTest : INotifyPropertyChanged, INotifyDataErrorInfo |
|||
{ |
|||
private int noValidationTest; |
|||
|
|||
public int NoValidationTest |
|||
{ |
|||
get { return noValidationTest; } |
|||
set |
|||
{ |
|||
noValidationTest = value; |
|||
NotifyPropertyChanged(); |
|||
} |
|||
} |
|||
|
|||
public bool HasErrors |
|||
{ |
|||
get |
|||
{ |
|||
return NonNegative < 0; |
|||
} |
|||
} |
|||
|
|||
private int nonNegative; |
|||
|
|||
public int NonNegative |
|||
{ |
|||
get { return nonNegative; } |
|||
set |
|||
{ |
|||
var old = nonNegative; |
|||
nonNegative = value; |
|||
NotifyPropertyChanged(); |
|||
if (old * value < 0) // If signs are different
|
|||
{ |
|||
NotifyErrorsChanged(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged; |
|||
public event PropertyChangedEventHandler PropertyChanged; |
|||
|
|||
public IEnumerable GetErrors(string propertyName) |
|||
{ |
|||
if (string.IsNullOrEmpty(propertyName) || propertyName == nameof(NonNegative)) |
|||
{ |
|||
if (NonNegative < 0) |
|||
{ |
|||
yield return "Invalid Value"; |
|||
} |
|||
} |
|||
} |
|||
|
|||
private void NotifyPropertyChanged([CallerMemberName] string property = "") |
|||
{ |
|||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); |
|||
} |
|||
|
|||
private void NotifyErrorsChanged([CallerMemberName] string property = "") |
|||
{ |
|||
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(property)); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void Calls_Change_Callback_When_Value_Changes() |
|||
{ |
|||
var plugin = new InpcPropertyAccessorPlugin(); |
|||
var source = new InpcTest { NoValidationTest = 0 }; |
|||
var changeFired = false; |
|||
var accessor = plugin.Start(new WeakReference(source), nameof(InpcTest.NoValidationTest), _ => changeFired = true, _ => { }); |
|||
source.NoValidationTest = 1; |
|||
|
|||
Assert.True(changeFired); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ValidationChanged_Does_Not_Fire_When_NonValidated_Value_Changes() |
|||
{ |
|||
var plugin = new InpcPropertyAccessorPlugin(); |
|||
var source = new InpcTest { NoValidationTest = 0 }; |
|||
var validationFired = false; |
|||
plugin.Start(new WeakReference(source), nameof(InpcTest.NoValidationTest), _ => { }, _ => validationFired = true); |
|||
source.NoValidationTest = 1; |
|||
|
|||
Assert.False(validationFired); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ValidationChanged_Does_Not_Fire_When_Validation_Does_Not_Change() |
|||
{ |
|||
var plugin = new InpcPropertyAccessorPlugin(); |
|||
var source = new InpcTest { NonNegative = 3 }; |
|||
var validationFired = false; |
|||
plugin.Start(new WeakReference(source), nameof(InpcTest.NonNegative), _ => { }, _ => validationFired = true); |
|||
source.NonNegative = 5; |
|||
|
|||
Assert.False(validationFired); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ValidationChanged_Fires_On_Start_If_Has_Errors() |
|||
{ |
|||
var plugin = new InpcPropertyAccessorPlugin(); |
|||
var source = new InpcTest { NonNegative = -5 }; |
|||
|
|||
Assert.True(source.HasErrors); |
|||
|
|||
var validationFired = false; |
|||
plugin.Start(new WeakReference(source), nameof(InpcTest.NonNegative), _ => { }, _ => validationFired = true); |
|||
Assert.True(validationFired); |
|||
} |
|||
|
|||
|
|||
|
|||
[Fact] |
|||
public void ValidationChanged_Fires_When_Validation_Changes() |
|||
{ |
|||
var plugin = new InpcPropertyAccessorPlugin(); |
|||
var source = new InpcTest { NonNegative = 5 }; |
|||
var validationFired = false; |
|||
plugin.Start(new WeakReference(source), nameof(InpcTest.NonNegative), _ => { }, _ => validationFired = true); |
|||
source.NonNegative = -1; |
|||
Assert.True(validationFired); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue