public void ValidateMethodShouldCallNotifyEvent()
        {
            ThreadManager.ImmediateInvokeAsync           = true;
            ThreadManager.ImmediateInvokeOnUiThreadAsync = true;
            ThreadManager.ImmediateInvokeOnUiThread      = true;

            int countInvoke = 0;
            ValidatableViewModel viewModel       = GetValidatableViewModel();
            INotifyDataErrorInfo notifyDataError = viewModel;

            notifyDataError.ErrorsChanged += (sender, args) =>
            {
                args.PropertyName.ShouldEqual(PropToValidate1);
                countInvoke++;
            };
            var validator = viewModel.AddValidator <SpyValidator>(new object());

            notifyDataError.HasErrors.ShouldBeFalse();
            validator.SetErrors(PropToValidate1, PropToValidate1);
            countInvoke.ShouldEqual(1);
            validator.SetErrors(PropToValidate1, PropToValidate2);
            countInvoke.ShouldEqual(2);
            notifyDataError.HasErrors.ShouldBeTrue();

            //NOTE: Event is not invoked, if errors is not changed.

            /*viewModel.ValidateAsync(PropToValidate1);
             * countInvoke.ShouldEqual(2);*/
        }
Exemplo n.º 2
0
        public void AddChildValidatableCollection_ChildCollectionIsNullOrEmpty_NoErrorsAreAdded()
        {
            TestUtils.ExecuteWithDispatcher((uiThreadDispatcher, completedAction) =>
            {
                // ARRANGE
                var parent = new ValidatableViewModel();

                parent.Validator.AddChildValidatableCollection(() => parent.Children);

                // ACT
                parent.Validator.ValidateAllAsync().ContinueWith(r1 =>
                {
                    // VERIFY
                    Assert.True(r1.Result.IsValid, "Validation must not fail");

                    // ARRANGE
                    parent.Children = new List <IValidatable>();

                    // ACT
                    parent.Validator.ValidateAllAsync().ContinueWith(r2 =>
                    {
                        // VERIFY
                        Assert.True(r2.Result.IsValid, "Validation must not fail.");

                        completedAction();
                    });
                });
            });
        }
Exemplo n.º 3
0
 internal ValidationContainer(
     ValidatableViewModel <T> viewModel,
     IReadOnlyCollection <PropertyValidation> propertyValidations)
 {
     _viewModel           = viewModel;
     _propertyValidations = propertyValidations;
 }
Exemplo n.º 4
0
        public void AddChildValidatableCollection_AddsRuleThatExecutedValidationOnAllValidatableChildren()
        {
            TestUtils.ExecuteWithDispatcher((uiThreadDispatcher, completedAction) =>
            {
                // ARRANGE
                var parent      = new ValidatableViewModel();
                var child1      = new ValidatableViewModel();
                var child2      = new ValidatableViewModel();
                parent.Children = new List <IValidatable>
                {
                    child1,
                    child2
                };

                child1.Validator.AddRequiredRule(() => child1.Foo, "Error1");
                child2.Validator.AddRule(RuleResult.Valid);

                parent.Validator.AddChildValidatableCollection(() => parent.Children);

                // ACT
                parent.Validator.ValidateAllAsync().ContinueWith(result =>
                {
                    // VERIFY
                    Assert.False(result.Result.IsValid, "Validation must fail");
                    Assert.Equal("Error1", result.Result.ErrorList[0].ErrorText);

                    completedAction();
                });
            });
        }
Exemplo n.º 5
0
        public void AddChildValidatableCollection_MoreThan64Items_DoesNotFail()
        {
            TestUtils.ExecuteWithDispatcher((uiThreadDispatcher, completedAction) =>
            {
                // ARRANGE
                var parent = new ValidatableViewModel();

                var children = new List <IValidatable>();

                for (int i = 0; i < 65; i++)
                {
                    var child = new ValidatableViewModel();
                    child.Validator.AddRule(RuleResult.Valid);

                    children.Add(child);
                }

                parent.Children = children;

                parent.Validator.AddChildValidatableCollection(() => parent.Children);

                // ACT
                parent.Validator.ValidateAllAsync().ContinueWith(result =>
                {
                    // VERIFY
                    uiThreadDispatcher.BeginInvoke(new Action(() =>
                    {
                        Assert.Null(result.Exception);
                        Assert.True(result.Result.IsValid, "Validation must pass");

                        completedAction();
                    }));
                });
            });
        }
Exemplo n.º 6
0
        public void AddChildValidatableCollection_OneRuleThrowsException_EntireValidationFails()
        {
            TestUtils.ExecuteWithDispatcher((uiThreadDispatcher, completedAction) =>
            {
                // ARRANGE
                var parent = new ValidatableViewModel();
                var child1 = new ValidatableViewModel();
                var child2 = new ValidatableViewModel();

                parent.Children = new List <IValidatable>
                {
                    child1,
                    child2
                };

                parent.Validator.AddAsyncRule(() => TaskEx.FromResult(RuleResult.Valid()));
                child1.Validator.AddAsyncRule(
                    () => Task.Factory.StartNew <RuleResult>(() => { throw new FakeException(); }));

                child2.Validator.AddAsyncRule(() => TaskEx.FromResult(RuleResult.Valid()));

                parent.Validator.AddChildValidatableCollection(() => parent.Children);

                // ACT
                parent.Validator.ValidateAllAsync().ContinueWith(r1 =>
                {
                    uiThreadDispatcher.BeginInvoke(new Action(() =>
                    {
                        Assert.NotNull(r1.Exception);
                        Assert.IsType <FakeException>(ExceptionUtils.UnwrapException(r1.Exception));
                        completedAction();
                    }));
                });
            });
        }
Exemplo n.º 7
0
        public void ValidateInstanceShouldValidateOnlyInstance()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();

            ValidatorProvider.Register <SpyValidator>();
            var instance  = new object();
            var instance2 = new object();

            viewModel.AddInstance(instance);
            viewModel.AddInstance(instance2);
            viewModel.AddValidator <SpyValidator>(instance);
            viewModel.GetValidators().Count.ShouldEqual(4);
            viewModel.GetValidators().OfType <SpyValidator>().ForEach(validator => validator.ValidateAllCount = 0);
            viewModel.ValidateInstanceAsync(instance);

            foreach (SpyValidator result in viewModel.GetValidators().OfType <SpyValidator>())
            {
                if (result.Context.Instance == instance)
                {
                    result.ValidateAllCount.ShouldEqual(1);
                }
                else
                {
                    result.ValidateAllCount.ShouldEqual(0);
                }
            }
        }
Exemplo n.º 8
0
        public void AddInstanceShouldGetValidatorsFromProviderEmpty()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var instance = new object();

            viewModel.AddInstance(instance);
            viewModel.GetValidators().Where(validator => validator != viewModel.Validator).ShouldBeEmpty();
        }
Exemplo n.º 9
0
        public void VmShouldValidateSelfByDefault()
        {
            ValidatorProvider.Register <SpyValidator>();
            ValidatableViewModel validatableViewModel = GetValidatableViewModel();
            var validator = (SpyValidator)validatableViewModel.GetValidators().Single(validator1 => validator1 != validatableViewModel.Validator);

            validator.Context.Instance.ShouldEqual(validatableViewModel);
        }
        public static void Initialize(this ValidatableViewModel validatableViewModel, System.Action initializationAction)
        {
            validatableViewModel.IsNotifying = false;

            initializationAction();

            validatableViewModel.SetIsValid();

            validatableViewModel.IsNotifying = true;
        }
Exemplo n.º 11
0
        public void ClearErrorsShouldRedirectCallToValidators()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var validator1 = viewModel.AddValidator <SpyValidator>(new object());
            var validator2 = viewModel.AddValidator <SpyValidator>(new object());

            viewModel.ClearErrors();
            validator1.ClearAllErrorsCount.ShouldEqual(1);
            validator2.ClearAllErrorsCount.ShouldEqual(1);
        }
Exemplo n.º 12
0
        public void RemoveValidatorGenericShouldBeRemoveFromValidators()
        {
            var o = new object();
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var validator = viewModel.AddValidator <SpyValidator>(o);

            viewModel.GetValidators().Single(validator1 => validator1 != viewModel.Validator).ShouldEqual(validator);
            viewModel.RemoveValidator(validator).ShouldBeTrue();
            viewModel.GetValidators().Where(validator1 => validator1 != viewModel.Validator).ShouldBeEmpty();
        }
Exemplo n.º 13
0
        public void AddedValidatorGenericShouldBeInValidators()
        {
            var o = new object();
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var validator = viewModel.AddValidator <SpyValidator>(o);

            validator.Context.ShouldNotBeNull();
            validator.Context.Instance.ShouldEqual(o);
            viewModel.GetValidators().Single(validator1 => validator1 != viewModel.Validator).ShouldEqual(validator);
        }
Exemplo n.º 14
0
        public void AddedValidatorShouldBeInValidators()
        {
            var o = new object();
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var validator = new SpyValidator();

            validator.Initialize(viewModel.CreateContext(o));
            viewModel.AddValidator(validator);
            validator.Context.ShouldNotBeNull();
            validator.Context.Instance.ShouldEqual(o);
            viewModel.GetValidators().Single(validator1 => validator1 != viewModel.Validator).ShouldEqual(validator);
        }
Exemplo n.º 15
0
        public void AddInstanceShouldGetValidatorsFromProviderNotEmpty()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();

            ValidatorProvider.Register <SpyValidator>();
            var instance = new object();

            viewModel.AddInstance(instance);
            viewModel.GetValidators().Single(validator => validator != viewModel.Validator)
            .Context
            .Instance
            .ShouldEqual(instance);
        }
Exemplo n.º 16
0
        public void ValidateAllShouldRedirectCallToValidators()
        {
            const int            count     = 10;
            ValidatableViewModel viewModel = GetValidatableViewModel();

            ValidatorProvider.Register <SpyValidator>();

            for (int i = 0; i < count; i++)
            {
                viewModel.AddInstance(new object());
            }
            viewModel.GetValidators().Count.ShouldEqual(count + 1);
            viewModel.GetValidators().OfType <SpyValidator>().All(validator => validator.ValidateAllCount == 1).ShouldBeTrue();
        }
Exemplo n.º 17
0
        public void CreateContextShouldCreateContextWithDefaultValuesFromVm()
        {
            var obj = new object();
            ValidatableViewModel viewModel = GetValidatableViewModel();
            IViewModel           vm        = viewModel;

            IValidatorContext validatorContext = viewModel.CreateContext(obj);

            validatorContext.ServiceProvider.ShouldEqual(vm.IocContainer);
            validatorContext.ValidationMetadata.ShouldEqual(viewModel.Settings.Metadata);
            validatorContext.PropertyMappings.ShouldEqual(viewModel.PropertyMappings);
            validatorContext.IgnoreProperties.ShouldEqual(viewModel.IgnoreProperties);
            validatorContext.Instance.ShouldEqual(obj);
            validatorContext.ServiceProvider.ShouldEqual(vm.IocContainer);
        }
Exemplo n.º 18
0
        public void RemoveInstanceShouldRemoveAllValidatorsAssociatedWithInstance()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();

            ValidatorProvider.Register <SpyValidator>();
            var instance  = new object();
            var instance2 = new object();

            viewModel.AddInstance(instance);
            viewModel.AddInstance(instance2);
            viewModel.AddValidator <ManualValidator>(instance);
            viewModel.GetValidators().Count.ShouldEqual(4);

            viewModel.RemoveInstance(instance);
            viewModel.GetValidators().Single(validator => validator != viewModel.Validator).Context.Instance.ShouldEqual(instance2);
        }
Exemplo n.º 19
0
        protected virtual IValidatorAggregator GetValidatorAggregatorInternal()
        {
            ValidatableViewModel viewModel;
            var iocContainer = ServiceProvider as IIocContainer;
            IViewModelProvider viewModelProvider;

            if (iocContainer != null && iocContainer.TryGet(out viewModelProvider))
            {
                viewModel = viewModelProvider.GetViewModel <ValidatableViewModel>();
            }
            else
            {
                viewModel = new ValidatableViewModel();
            }
            viewModel.ValidatorProvider = this;
            return(viewModel);
        }
Exemplo n.º 20
0
        public void IsValidShouldRedirectCallToValidators()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var validator1 = viewModel.AddValidator <SpyValidator>(new object());
            var validator2 = viewModel.AddValidator <SpyValidator>(new object());

            viewModel.IsValid.ShouldBeTrue();
            validator1.SetErrors(PropToValidate1, PropToValidate1);
            validator2.SetErrors(PropToValidate2, PropToValidate2);
            viewModel.IsValid.ShouldBeFalse();

            validator1.SetErrors(PropToValidate1);
            viewModel.IsValid.ShouldBeFalse();

            validator2.SetErrors(PropToValidate2);
            viewModel.IsValid.ShouldBeTrue();
        }
Exemplo n.º 21
0
        public void GetAllErrorsShouldRedirectCallToValidators()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var validator1 = viewModel.AddValidator <SpyValidator>(new object());
            var validator2 = viewModel.AddValidator <SpyValidator>(new object());

            viewModel.GetErrors().ShouldBeEmpty();
            viewModel.GetErrors().ShouldBeEmpty();

            validator1.SetErrors(PropToValidate1, PropToValidate1);
            validator1.SetErrors(PropToValidate2, PropToValidate2);
            validator2.SetErrors(PropToValidate1, PropToValidate2);
            validator2.SetErrors(PropToValidate2, PropToValidate1);

            IDictionary <string, IList <object> > errors = viewModel.GetErrors();

            errors.Count.ShouldEqual(2);

            errors[PropToValidate1].Count.ShouldEqual(2);
            errors[PropToValidate1].Contains(PropToValidate1).ShouldBeTrue();
            errors[PropToValidate1].Contains(PropToValidate2).ShouldBeTrue();

            errors[PropToValidate2].Count.ShouldEqual(2);
            errors[PropToValidate2].Contains(PropToValidate1).ShouldBeTrue();
            errors[PropToValidate2].Contains(PropToValidate2).ShouldBeTrue();

            validator1.SetErrors(PropToValidate1);
            validator1.SetErrors(PropToValidate2);

            errors = viewModel.GetErrors();
            errors.Count.ShouldEqual(2);

            errors[PropToValidate1].Count.ShouldEqual(1);
            errors[PropToValidate1].Contains(PropToValidate2).ShouldBeTrue();

            errors[PropToValidate2].Count.ShouldEqual(1);
            errors[PropToValidate2].Contains(PropToValidate1).ShouldBeTrue();

            validator2.SetErrors(PropToValidate1);
            validator2.SetErrors(PropToValidate2);

            viewModel.GetErrors().ShouldBeEmpty();
            viewModel.GetErrors().ShouldBeEmpty();
        }
Exemplo n.º 22
0
        public void AddChildValidatable_ChildValidatableIsNull_NoErrorsAreAdded()
        {
            TestUtils.ExecuteWithDispatcher((uiThreadDispatcher, completedAction) =>
            {
                // ARRANGE
                var parent = new ValidatableViewModel();

                parent.Validator.AddChildValidatable(() => parent.Child);

                // ACT
                parent.Validator.ValidateAllAsync().ContinueWith(result =>
                {
                    // VERIFY
                    Assert.True(result.Result.IsValid, "Validation must not fail");

                    completedAction();
                });
            });
        }
Exemplo n.º 23
0
        public void NotifyDataErrorInfoShouldGetErrorsFromValidators()
        {
            ValidatableViewModel viewModel       = GetValidatableViewModel();
            INotifyDataErrorInfo notifyDataError = viewModel;

            var validator = viewModel.AddValidator <SpyValidator>(new object());

            notifyDataError.HasErrors.ShouldBeFalse();
            validator.SetErrors(PropToValidate1, PropToValidate1, PropToValidate2);
            notifyDataError.HasErrors.ShouldBeTrue();

            string[] errors = notifyDataError.GetErrors(PropToValidate1).OfType <string>().ToArray();
            errors.Length.ShouldEqual(2);
            errors.Contains(PropToValidate1).ShouldBeTrue();
            errors.Contains(PropToValidate2).ShouldBeTrue();

            validator.SetErrors(PropToValidate1);
            notifyDataError.GetErrors(PropToValidate1).ShouldBeEmpty();
            notifyDataError.HasErrors.ShouldBeFalse();
        }
Exemplo n.º 24
0
        public void GetErrorsShouldRedirectCallToValidators()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();
            var validator1 = viewModel.AddValidator <SpyValidator>(new object());
            var validator2 = viewModel.AddValidator <SpyValidator>(new object());

            viewModel.GetErrors(PropToValidate1).ShouldBeEmpty();
            viewModel.GetErrors(PropToValidate2).ShouldBeEmpty();

            validator1.SetErrors(PropToValidate1, PropToValidate1);
            validator1.SetErrors(PropToValidate2, PropToValidate2);
            validator2.SetErrors(PropToValidate1, PropToValidate2);
            validator2.SetErrors(PropToValidate2, PropToValidate1);

            object[] errors = viewModel.GetErrors(PropToValidate1).OfType <object>().ToArray();
            errors.Length.ShouldEqual(2);
            errors.Contains(PropToValidate1).ShouldBeTrue();
            errors.Contains(PropToValidate2).ShouldBeTrue();

            errors = viewModel.GetErrors(PropToValidate2).OfType <object>().ToArray();
            errors.Length.ShouldEqual(2);
            errors.Contains(PropToValidate1).ShouldBeTrue();
            errors.Contains(PropToValidate2).ShouldBeTrue();

            validator1.SetErrors(PropToValidate1);
            validator1.SetErrors(PropToValidate2);

            errors = viewModel.GetErrors(PropToValidate1).OfType <object>().ToArray();
            errors.Length.ShouldEqual(1);
            errors.Contains(PropToValidate2).ShouldBeTrue();

            errors = viewModel.GetErrors(PropToValidate2).OfType <object>().ToArray();
            errors.Length.ShouldEqual(1);
            errors.Contains(PropToValidate1).ShouldBeTrue();

            validator2.SetErrors(PropToValidate1);
            validator2.SetErrors(PropToValidate2);

            viewModel.GetErrors(PropToValidate1).ShouldBeEmpty();
            viewModel.GetErrors(PropToValidate2).ShouldBeEmpty();
        }
Exemplo n.º 25
0
        public void DataErrorInfoShouldGetErrorsFromValidators()
        {
            ValidatableViewModel viewModel     = GetValidatableViewModel();
            IDataErrorInfo       dataErrorInfo = viewModel;

            var            validator          = viewModel.AddValidator <SpyValidator>(viewModel);
            IDataErrorInfo validatorErrorInfo = validator;

            validator.SetErrors(PropToValidate1, PropToValidate1);
            viewModel.OnPropertyChanged(PropToValidate1, ExecutionType.None);
            string error = dataErrorInfo[PropToValidate1];

            validator.ValidateCount.ShouldEqual(1);
            validator.ValidateProperties.Contains(PropToValidate1).ShouldBeTrue();
            error.ShouldEqual(PropToValidate1);

            validator.SetErrors(PropToValidate1);
            viewModel.OnPropertyChanged(PropToValidate1, ExecutionType.None);
            error = validatorErrorInfo[PropToValidate1];
            validator.ValidateCount.ShouldEqual(2);
            error.ShouldBeNull();
        }
Exemplo n.º 26
0
        public void AddChildValidatable_AddsRuleWithProperTarget()
        {
            TestUtils.ExecuteWithDispatcher((uiThreadDispatcher, completedAction) =>
            {
                // ARRANGE
                var parent   = new ValidatableViewModel();
                var child    = new ValidatableViewModel();
                parent.Child = child;

                child.Validator.AddRequiredRule(() => child.Foo, "Error1");
                parent.Validator.AddChildValidatable(() => parent.Child);

                // ACT
                parent.Validator.ValidateAllAsync().ContinueWith(result =>
                {
                    // VERIFY
                    Assert.False(result.Result.IsValid, "Validation must fail");
                    Assert.Equal("parent.Child", result.Result.ErrorList[0].Target);

                    completedAction();
                });
            });
        }
Exemplo n.º 27
0
 internal ValidationConfigurationBuilder(ValidatableViewModel <T> viewModel)
 {
     _rulesBuilders = new List <PropertyRulesBuilder <T> >();
     _viewModel     = viewModel;
 }
 protected virtual IValidatorAggregator GetValidatorAggregatorInternal()
 {
     ValidatableViewModel viewModel;
     IIocContainer iocContainer = ServiceProvider.IocContainer;
     IViewModelProvider viewModelProvider;
     if (iocContainer != null && iocContainer.TryGet(out viewModelProvider))
         viewModel = viewModelProvider.GetViewModel<ValidatableViewModel>();
     else
         viewModel = new ValidatableViewModel();
     viewModel.ValidatorProvider = this;
     return viewModel;
 }
 public static string YearValidation(ValidatableViewModel viewmodel, string propertyname)
 {
     var propValue = viewmodel.GetType().GetProperty(propertyname).GetValue(viewmodel, null);
     if (propValue != null)
     {
         string stringPropValue = propValue.ToString();
         int result;
         if (int.TryParse(stringPropValue, out result))
         {
             if (result > 1900 && result < 2100)
             {
                 return null;
             }
         }
     }
     return "wrong year!";
 }
Exemplo n.º 30
0
        public void AddNotInitializedValidatorShouldThrowException()
        {
            ValidatableViewModel viewModel = GetValidatableViewModel();

            ShouldThrow <InvalidOperationException>(() => viewModel.AddValidator(new SpyValidator()));
        }