public void TestPropertyChangeFiresErrorsChanged()
        {
            var rule = new TestValidationRule
            {
                Result = ValidationRuleResult.Success,
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(nameof(TestViewModel.AProperty), rule);

            List <string> errorChanges =
                CaptureErrorChanges(vm, () =>
            {
                vm.PublicRaisePropertyChanged(nameof(vm.AProperty));
            });

            CollectionAssert.AreEqual(
                new[] { nameof(TestViewModel.AProperty), string.Empty },
                errorChanges
                );
        }
        public void TestHasErrorsTrue()
        {
            var rule = new TestValidationRule
            {
                Result = new ValidationRuleResult(true, "test"),
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(nameof(TestViewModel.AProperty), rule);

            Assert.IsTrue(vm.HasErrors);
        }
        public void TestHasErrorsFalse()
        {
            var rule = new TestValidationRule
            {
                Result = ValidationRuleResult.Success,
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(nameof(TestViewModel.AProperty), rule);

            Assert.IsFalse(vm.HasErrors);
        }
        public void TestExplicitGetErrors()
        {
            var vm = new TestViewModel();

            vm.AddValidationRule <int>(nameof(vm.AProperty), x => new ValidationRuleResult(true, "error"));

            INotifyDataErrorInfo indei = vm;

            object[] indeiErrors = indei
                                   .GetErrors(nameof(vm.AProperty))
                                   .Cast <object>()
                                   .ToArray();

            string[] errors = vm.GetErrors(nameof(vm.AProperty)).ToArray();

            CollectionAssert.AreEqual(errors, indeiErrors);
        }
        public void TestPropertyChangeFiresErrorsChangedAndDoesNotThrowWithNoSubscribers()
        {
            var rule = new TestValidationRule
            {
                Result = ValidationRuleResult.Success,
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(nameof(TestViewModel.AProperty), rule);

            // this line should not throw any exceptions.
            vm.PublicRaisePropertyChanged(nameof(vm.AProperty));
        }
        public void TestValidateEntityUsingNullAndEmptyStringWithSuccess()
        {
            var rule = new TestValidationRule
            {
                Result = new ValidationRuleResult(false, "test"),
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(null, rule);

            string[] errors = vm.GetErrors(string.Empty).ToArray();

            CollectionAssert.AreEqual(Array.Empty <string>(), errors);
        }
        public void TestValidateEntityUsingEmptyAndNullStringWithError()
        {
            var rule = new TestValidationRule
            {
                Result = new ValidationRuleResult(true, "test"),
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(string.Empty, rule);

            string[] errors = vm.GetErrors(null).ToArray();

            CollectionAssert.AreEqual(new string[] { "test" }, errors);
        }
        public void TestAddEntityValidationRuleByDelegate()
        {
            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            int runCount = 0;

            vm.AddValidationRule <TestViewModel>(null, (testVM) =>
            {
                runCount++;
                return(new ValidationRuleResult(true, "test"));
            });

            string[] errors = vm.GetErrors(null).ToArray();

            Assert.AreEqual(1, runCount);
            CollectionAssert.AreEqual(new[] { "test" }, errors);
        }
        public void TestValidatePropertyWithSuccess()
        {
            var rule = new TestValidationRule
            {
                Result = new ValidationRuleResult(false, "test"),
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(nameof(TestViewModel.AProperty), rule);

            string[] errors = vm.GetErrors(nameof(TestViewModel.AProperty)).ToArray();

            Assert.AreEqual(1, rule.RunCount);
            Assert.AreEqual(5, rule.LastRunValue);
            CollectionAssert.AreEqual(Array.Empty <string>(), errors);
        }
        public void TestPropertyChangedNullNameConvertedToEmpty()
        {
            var rule = new TestValidationRule
            {
                Result = ValidationRuleResult.Success,
            };

            var vm = new TestViewModel();

            vm.AddValidationRule(null, rule);

            List <string> errorChanges = CaptureErrorChanges(vm, () =>
            {
                vm.PublicRaisePropertyChanged(null);
            });

            CollectionAssert.AreEqual(
                new[] { string.Empty },
                errorChanges
                );
        }
        public void TestRemoveEntityValidationRule()
        {
            var rule = new TestValidationRule
            {
                Result = new ValidationRuleResult(true, "test"),
            };

            var vm = new TestViewModel
            {
                AProperty = 5,
            };

            vm.AddValidationRule(null, rule);

            vm.RemoveValidationRule(null, rule);

            string[] errors = vm.GetErrors(null).ToArray();

            Assert.AreEqual(0, rule.RunCount);
            CollectionAssert.AreEqual(Array.Empty <string>(), errors);
        }