# HG changeset patch # User stevenhollidge # Date 1336302809 -3600 # Node ID db05a55e353698e7ffa8d9cf22ad6d52acb83d91 # Parent 2372846797f0363d801083059845029d4902c118 INPC tests added diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/Libs/Moq.dll Binary file SilverlightValidation/Libs/Moq.dll has changed diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.PL/Data/Factory.cs --- a/SilverlightValidation/SilverlightValidation.PL/Data/Factory.cs Sat May 05 23:04:42 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation.PL/Data/Factory.cs Sun May 06 12:13:29 2012 +0100 @@ -10,11 +10,11 @@ { return new List(5) { - new UserModel() { Username = "StevenH", Password = "Password1", Email = "steven@hotmail.com", DateOfBirth = new DateTime(1977, 09, 01), Description = ""}, - new UserModel() { Username = "RichardJ", Password = "12N456a", Email = "dicky@gmail.com", DateOfBirth = new DateTime(1983, 03, 13), Description = "Loves .Net!"}, - new UserModel() { Username = "BobbyP", Password = "pa33Word", Email = "bob@yahoo.co.uk", DateOfBirth = new DateTime(1992, 08, 30), Description = ""}, - new UserModel() { Username = "DavidM", Password = "poIu789", Email = "daveyboy@marsh.com", DateOfBirth = new DateTime(1965, 06, 21), Description = "Java fan boy"}, - new UserModel() { Username = "JessieJ", Password = "jlkJh567", Email = "jj@apple.co.uk", DateOfBirth = new DateTime(1990, 10, 15), Description = ""} + UserModel.Create("StevenH", "Password1", "steven@hotmail.com", new DateTime(1977, 09, 01), ""), + UserModel.Create("RichardJ", "12N456a", "dicky@gmail.com", new DateTime(1983, 03, 13), "Loves .Net!"), + UserModel.Create("BobbyP", "pa33Word", "bob@yahoo.co.uk", new DateTime(1992, 08, 30), ""), + UserModel.Create("DavidM", "poIu789", "daveyboy@marsh.com", new DateTime(1965, 06, 21), "Java fan boy"), + UserModel.Create("JessieJ", "jlkJh567", "jj@apple.co.uk", new DateTime(1990, 10, 15), "") }; } } diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.PL/Models/UserModel.cs --- a/SilverlightValidation/SilverlightValidation.PL/Models/UserModel.cs Sat May 05 23:04:42 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation.PL/Models/UserModel.cs Sun May 06 12:13:29 2012 +0100 @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; using SilverlightValidation.Interfaces; namespace SilverlightValidation.Models @@ -7,19 +6,32 @@ public class UserModel : IUserModel { public string Username { get; set; } + public string Password { get; set; } public string Email { get; set; } - public string Password { get; set; } public DateTime? DateOfBirth { get; set; } public string Description { get; set; } - public static UserModel Create() + private UserModel() { } + + public static UserModel Create(string username = "", + string password = "", + string email = "", + DateTime? dateOfBirth = null, + string descripton = "") { - return new UserModel() { Username = "", Email = "", Password = "", DateOfBirth = null, Description = "" }; + return new UserModel() + { + Username = "", + Password = "", + Email = "", + DateOfBirth = null, + Description = "" + }; } public IUserModel Clone() { - return (UserModel) this.MemberwiseClone(); + return (UserModel)this.MemberwiseClone(); } } } diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs --- a/SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs Sat May 05 23:04:42 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation.PL/Validators/UserModelValidator.cs Sun May 06 12:13:29 2012 +0100 @@ -6,7 +6,7 @@ { public class UserModelValidator : AbstractValidator { - public UserModelValidator() + private UserModelValidator() { RuleFor(x => x.Username) .Length(3, 8) @@ -32,5 +32,10 @@ return false; return true; } + + public static UserModelValidator Create() + { + return new UserModelValidator(); + } } } diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.PL/ViewModels/UserViewModel.cs --- a/SilverlightValidation/SilverlightValidation.PL/ViewModels/UserViewModel.cs Sat May 05 23:04:42 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation.PL/ViewModels/UserViewModel.cs Sun May 06 12:13:29 2012 +0100 @@ -5,7 +5,6 @@ using System.Windows.Input; using FluentValidation; using SilverlightValidation.Interfaces; -using SilverlightValidation.Validators; using SilverlightValidation.Models; using SilverlightValidation.Commands; using GalaSoft.MvvmLight.Messaging; @@ -17,7 +16,7 @@ { #region Fields - private readonly UserModelValidator _validator; + private readonly AbstractValidator _validator; private UserModel _data; private UserModel _backup; @@ -25,7 +24,7 @@ #region Constructor - public UserViewModel(UserModel model, UserModelValidator validator) + public UserViewModel(UserModel model, AbstractValidator validator) { if (model == null) throw new ArgumentNullException("model"); if (validator == null) throw new ArgumentNullException("validator"); diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.Tests/Fakes/UserModelValidatorFake.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SilverlightValidation/SilverlightValidation.Tests/Fakes/UserModelValidatorFake.cs Sun May 06 12:13:29 2012 +0100 @@ -0,0 +1,38 @@ +using System; +using FluentValidation; +using SilverlightValidation.Interfaces; + +namespace SilverlightValidation.Tests.Fakes +{ + class UserModelValidatorFake : AbstractValidator + { + private UserModelValidatorFake() + { + RuleFor(x => x.Username) + .Length(3, 8) + .WithMessage("Must be between 3-8 characters."); + + RuleFor(x => x.Password) + .Matches(@"^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$") + .WithMessage("Must contain lower, upper and numeric chars."); + + RuleFor(x => x.Email) + .EmailAddress() + .WithMessage("A valid email address is required."); + + RuleFor(x => x.DateOfBirth) + .Must(BeAValidDateOfBirth) + .WithMessage("Must be within 100 years of today."); + } + + private bool BeAValidDateOfBirth(DateTime? dateOfBirth) + { + if (dateOfBirth == null) return false; + if (dateOfBirth.Value > DateTime.Today || dateOfBirth < DateTime.Today.AddYears(-100)) + return false; + return true; + } + + public static UserModelValidatorFake Create() { return new UserModelValidatorFake(); } + } +} diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.Tests/SilverlightValidation.Tests.csproj --- a/SilverlightValidation/SilverlightValidation.Tests/SilverlightValidation.Tests.csproj Sat May 05 23:04:42 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation.Tests/SilverlightValidation.Tests.csproj Sun May 06 12:13:29 2012 +0100 @@ -31,6 +31,9 @@ 4 + + ..\Libs\FakeItEasy.dll + ..\Libs\FluentAssertions.dll @@ -52,10 +55,11 @@ - + + - - + + diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserListViewModelFixture.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserListViewModelFixture.cs Sun May 06 12:13:29 2012 +0100 @@ -0,0 +1,15 @@ +using System; +using NUnit.Framework; + +namespace SilverlightValidation.Tests.ViewModels +{ + [TestFixture] + class UserListViewModelFixture + { + [Test] + public void Given_When_Then() + { + Assert.True(true); + } + } +} diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SilverlightValidation/SilverlightValidation.Tests/ViewModels/UserViewModelFixture.cs Sun May 06 12:13:29 2012 +0100 @@ -0,0 +1,185 @@ +using System; +using FluentAssertions.EventMonitoring; +using NUnit.Framework; +using SilverlightValidation.Models; +using SilverlightValidation.Tests.Fakes; +using SilverlightValidation.Validators; +using SilverlightValidation.ViewModels; + +namespace SilverlightValidation.Tests.ViewModels +{ + [TestFixture] + class UserViewModelFixture + { + #region Constructor + + [Test] + public void WhenConstructed_WithTwoNulls_ThenArgumentNullExceptionForModel() + { + Assert.Throws(() => new UserViewModel(null, null), "model"); + } + + [Test] + public void WhenConstructed_WithNullFirstParam_ThenArgumentNullExceptionForModel() + { + Assert.Throws(() => new UserViewModel(null, UserModelValidator.Create()), "model"); + } + + [Test] + public void WhenConstructed_WithNullSecondParam_ThenArgumentNullExceptionForValidator() + { + Assert.Throws(() => new UserViewModel(UserModel.Create(), null), "validator"); + } + + [Test] + public void WhenConstructed_WithGenericParams_ThenInstantiatesViewModel() + { + var vm = new UserViewModel(UserModel.Create(), UserModelValidator.Create()); + Assert.IsNotNull(vm); + } + + #endregion + + #region INotifyPropertyChanged + + [Test] + public void WhenPropertyChanged_WithValidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.DateOfBirth = new DateTime(1977, 01, 01); + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth); + Assert.IsTrue(vm.IsChanged); + } + + + [Test] + public void WhenPropertyChanged_WithInvalidDateOfBirthUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.DateOfBirth = new DateTime(1800, 01, 01); + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.DateOfBirth); + Assert.IsTrue(vm.IsChanged); + } + + [Test] + public void WhenPropertyChanged_WithDescriptionUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.Description = "New description"; + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.Description); + Assert.IsTrue(vm.IsChanged); + } + + [Test] + public void WhenPropertyChanged_WithValidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.Email = "test@domain.com"; + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.Email); + Assert.IsTrue(vm.IsChanged); + } + + [Test] + public void WhenPropertyChanged_WithInvalidEmailUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.Email = "invalid email"; + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.Email); + Assert.IsTrue(vm.IsChanged); + } + + [Test] + public void WhenPropertyChanged_WithValidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.Username = "dummy"; + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.Username); + Assert.IsTrue(vm.IsChanged); + } + + [Test] + public void WhenPropertyChanged_WithInvalidUsernameUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.Username = "invalidusernametoolong"; + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.Username); + Assert.IsTrue(vm.IsChanged); + } + + [Test] + public void WhenPropertyChanged_WithValidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.Password = "dummy"; + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.Password); + Assert.IsTrue(vm.IsChanged); + } + + [Test] + public void WhenPropertyChanged_WithInvalidPasswordUpdated_ThenFiresChangeEventAndIsChangedEqualsTrue() + { + // arrange + var vm = CreateDefaultUserViewModel(); + + // act + vm.Password = "invalidpasswordtoolong"; + + // assert + vm.ShouldRaisePropertyChangeFor(x => x.Password); + Assert.IsTrue(vm.IsChanged); + } + + #endregion + + #region Helpers + + private static UserViewModel CreateDefaultUserViewModel() + { + var vm = new UserViewModel(UserModel.Create(), UserModelValidatorFake.Create()); + vm.MonitorEvents(); + return vm; + } + + #endregion + } +} diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation.Tests/ViewModels/ViewModelBaseFixture.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SilverlightValidation/SilverlightValidation.Tests/ViewModels/ViewModelBaseFixture.cs Sun May 06 12:13:29 2012 +0100 @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using NUnit.Framework; + +namespace SilverlightValidation.Tests.ViewModels +{ + [TestFixture] + class ViewModelBaseFixture + { + + } +} diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation/Views/UserListView.xaml.cs --- a/SilverlightValidation/SilverlightValidation/Views/UserListView.xaml.cs Sat May 05 23:04:42 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/Views/UserListView.xaml.cs Sun May 06 12:13:29 2012 +0100 @@ -15,7 +15,7 @@ InitializeComponent(); HtmlPage.Document.SetProperty("title", "Silverlight Validation"); - vm = new UserListViewModel(new UserView(), Factory.CreateUserModels(), new UserModelValidator()); + vm = new UserListViewModel(new UserView(), Factory.CreateUserModels(), UserModelValidator.Create()); this.DataContext = vm; } diff -r 2372846797f0 -r db05a55e3536 SilverlightValidation/SilverlightValidation/Views/UserView.xaml.cs --- a/SilverlightValidation/SilverlightValidation/Views/UserView.xaml.cs Sat May 05 23:04:42 2012 +0100 +++ b/SilverlightValidation/SilverlightValidation/Views/UserView.xaml.cs Sun May 06 12:13:29 2012 +0100 @@ -15,7 +15,7 @@ InitializeComponent(); HtmlPage.Document.SetProperty("title", "Silverlight Validation"); - vm = new UserViewModel(UserModel.Create(), new UserModelValidator()); + vm = new UserViewModel(UserModel.Create(), UserModelValidator.Create()); this.DataContext = vm; }