Skip to content

Commit

Permalink
Merge pull request #25 from thenderson21/master
Browse files Browse the repository at this point in the history
Added validation property to SetProperty<T>()
  • Loading branch information
jamesmontemagno authored Feb 5, 2018
2 parents ff4e24d + f0653cd commit 8d5622a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
50 changes: 49 additions & 1 deletion MvvmHelpers.Tests/ObservableObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public void OnDidntChange()
person.FirstName = "James";



Assert.IsNull(updated, "Property changed was raised, but shouldn't have been");
}

Expand All @@ -64,6 +63,55 @@ public void OnChangedEvent()

Assert.IsTrue(triggered, "OnChanged didn't raise");
}

[Test()]
public void ValidateEvent()
{
var contol = "Motz";
var triggered = false;
person.Validate = (oldValue, newValue) =>
{
triggered = true;
return oldValue != newValue;
};

person.FirstName = contol;

Assert.IsTrue(triggered, "ValidateValue didn't raise");
Assert.AreEqual(person.FirstName, contol, "Value was not set correctly.");

}

[Test()]
public void NotValidateEvent()
{
var contol = person.FirstName;
var triggered = false;
person.Validate = (oldValue, newValue) =>
{
triggered = true;
return false;
};

person.FirstName = "Motz";

Assert.IsTrue(triggered, "ValidateValue didn't raise");
Assert.AreEqual(person.FirstName, contol, "Value should not have been set.");

}

[Test()]
public void ValidateEventException()
{
person.Validate = (oldValue, newValue) =>
{
throw new ArgumentOutOfRangeException();
return false;
};

Assert.Throws<ArgumentOutOfRangeException>(() => person.FirstName = "Motz", "Should throw ArgumentOutOfRangeException");

}
}
}

13 changes: 9 additions & 4 deletions MvvmHelpers.Tests/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ public class PersonViewModel : BaseViewModel

}

public class Person : ObservableObject
{
public class Person : ObservableObject
{
public Action Changed { get; set; }

public Func<string, string, bool> Validate {get;set;}

string firstName;
public string FirstName
{
get { return firstName; }
set
{
SetProperty(ref firstName, value, onChanged: Changed);
SetProperty(ref firstName, value, onChanged: Changed, validateValue: Validate);
}
}
string lastName;
Expand All @@ -27,9 +30,11 @@ public string LastName
get { return lastName; }
set
{
SetProperty(ref lastName, value, onChanged: Changed);
SetProperty(ref lastName, value, onChanged: Changed, validateValue: Validate);
}
}


public string SortName
{
get { return FirstName[0].ToString().ToUpperInvariant(); }
Expand Down
16 changes: 10 additions & 6 deletions MvvmHelpers/ObservableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ public class ObservableObject : INotifyPropertyChanged
/// <returns><c>true</c>, if property was set, <c>false</c> otherwise.</returns>
/// <param name="backingStore">Backing store.</param>
/// <param name="value">Value.</param>
/// <param name="validateValue">Validates value.</param>
/// <param name="propertyName">Property name.</param>
/// <param name="onChanged">On changed.</param>
/// <typeparam name="T">The 1st type parameter.</typeparam>
protected virtual bool SetProperty<T>(
ref T backingStore, T value,
[CallerMemberName]string propertyName = "",
Action onChanged = null)
Action onChanged = null,
Func<T, T, bool> validateValue = null)
{
if (EqualityComparer<T>.Default.Equals(backingStore, value))
if ((validateValue == null && EqualityComparer<T>.Default.Equals(backingStore, value)) ||
(validateValue != null && !validateValue(backingStore, value)))
return false;

backingStore = value;
Expand All @@ -33,10 +36,11 @@ protected virtual bool SetProperty<T>(
return true;
}

/// <summary>
/// Occurs when property changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Occurs when property changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;

/// <summary>
/// Raises the property changed event.
Expand Down

0 comments on commit 8d5622a

Please sign in to comment.