Beispiel #1
0
        public void MergeErrors()
        {
            ValidationErrors otherErrors = new ValidationErrors();
            const string     anotherKey  = "anotherKey";

            otherErrors.AddError(anotherKey, ErrorMessageTwo);
            otherErrors.AddError(GoodErrorKey, ErrorMessageTwo);


            IValidationErrors errors = new ValidationErrors();

            errors.AddError(GoodErrorKey, ErrorMessageOne);
            errors.MergeErrors(otherErrors);

            Assert.IsFalse(errors.IsEmpty);
            IList <ErrorMessage> mergedErrors = errors.GetErrors(GoodErrorKey);

            Assert.IsNotNull(mergedErrors);
            Assert.AreEqual(2, mergedErrors.Count);
            Assert.AreEqual(ErrorMessageOne, mergedErrors[0]);
            Assert.AreEqual(ErrorMessageTwo, mergedErrors[1]);

            IList <ErrorMessage> otherErrorsForKey = errors.GetErrors(anotherKey);

            Assert.IsNotNull(otherErrorsForKey);
            Assert.AreEqual(1, otherErrorsForKey.Count);
            Assert.AreEqual(ErrorMessageTwo, otherErrorsForKey[0]);
        }
Beispiel #2
0
        public void WhenSingleValidatorReturnsTrue()
        {
            ExclusiveValidatorGroup vg = new ExclusiveValidatorGroup(Expression.Parse("true"));

            vg.Actions.Add(new ErrorMessageAction("exclusiveError", "exclusiveErrors"));

            vg.Validators.Add(new FalseValidator());
            vg.Validators.Add(new TrueValidator());
            vg.Validators.Add(new FalseValidator());

            IValidationErrors errors = new ValidationErrors();

            errors.AddError("existingErrors", new ErrorMessage("error", null));

            bool valid = vg.Validate(new object(), errors);

            Assert.IsTrue(valid, "Validation should succeed when single inner validator returns true.");
            Assert.AreEqual(0, errors.GetErrors("errors").Count);
            Assert.AreEqual(0, errors.GetErrors("exclusiveErrors").Count);
            Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);

            // ALL validators are called
            Assert.IsTrue(((BaseTestValidator)vg.Validators[0]).WasCalled);
            Assert.IsTrue(((BaseTestValidator)vg.Validators[1]).WasCalled);
            Assert.IsTrue(((BaseTestValidator)vg.Validators[2]).WasCalled);
        }
Beispiel #3
0
        public void AddErrorSunnyDay()
        {
            IValidationErrors errors = new ValidationErrors();

            errors.AddError(GoodErrorKey, ErrorMessageOne);
            Assert.IsFalse(errors.IsEmpty);
            Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
            Assert.AreEqual(1, errors.GetErrors(GoodErrorKey).Count);
        }
Beispiel #4
0
        public void MergeErrorsWithNull()
        {
            IValidationErrors errors = new ValidationErrors();

            errors.AddError(GoodErrorKey, ErrorMessageOne);
            errors.AddError(GoodErrorKey, ErrorMessageTwo);
            errors.MergeErrors(null);

            // must be unchanged with no Exception thrown...
            Assert.IsFalse(errors.IsEmpty);
            Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
            Assert.AreEqual(2, errors.GetErrors(GoodErrorKey).Count);
        }
Beispiel #5
0
        private static void WhenSingleValidatorReturnsTrue(AnyValidatorGroup vg)
        {
            vg.Validators.Add(new FalseValidator());
            vg.Validators.Add(new TrueValidator());
            vg.Validators.Add(new FalseValidator());

            IValidationErrors errors = new ValidationErrors();

            errors.AddError("existingErrors", new ErrorMessage("error", null));

            bool valid = vg.Validate(new object(), errors);

            Assert.IsTrue(valid, "Validation should succeed when single inner validator returns true.");
            Assert.AreEqual(0, errors.GetErrors("errors").Count);
            Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
        }
Beispiel #6
0
        public void ContainsNoErrorsDirectlyAfterInstantiation()
        {
            IValidationErrors errors = new ValidationErrors();

            Assert.IsTrue(errors.IsEmpty);
            Assert.IsNotNull(errors.GetErrors(GoodErrorKey));
        }
Beispiel #7
0
        public void WhenGroupIsNotValidatedBecauseWhenExpressionReturnsFalse()
        {
            AnyValidatorGroup vg = new AnyValidatorGroup("false");

            vg.Validators.Add(new FalseValidator());
            vg.Validators.Add(new FalseValidator());

            IValidationErrors errors = new ValidationErrors();

            errors.AddError("existingErrors", new ErrorMessage("error", null));

            bool valid = vg.Validate(new object(), errors);

            Assert.IsTrue(valid, "Validation should succeed when group validator is not evaluated.");
            Assert.AreEqual(0, errors.GetErrors("errors").Count);
            Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
        }
Beispiel #8
0
        public void WhenAllValidatorsReturnTrue()
        {
            AnyValidatorGroup vg = new AnyValidatorGroup("true");

            vg.Validators.Add(new TrueValidator());
            vg.Validators.Add(new TrueValidator());
            vg.Validators.Add(new TrueValidator());

            IValidationErrors errors = new ValidationErrors();

            errors.AddError("existingErrors", new ErrorMessage("error", null));

            bool valid = vg.Validate(new object(), errors);

            Assert.IsTrue(valid, "Validation should succeed when all inner validators return true.");
            Assert.AreEqual(0, errors.GetErrors("errors").Count);
            Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
        }
Beispiel #9
0
        public void WhenAllValidatorsReturnFalse()
        {
            AnyValidatorGroup vg = new AnyValidatorGroup();

            vg.Validators.Add(new FalseValidator());
            vg.Validators.Add(new FalseValidator());
            vg.Validators.Add(new FalseValidator());

            IValidationErrors errors = new ValidationErrors();

            errors.AddError("existingErrors", new ErrorMessage("error", null));

            bool valid = vg.Validate(new object(), errors);

            Assert.IsFalse(valid, "Validation should fail when all inner validators return false.");
            Assert.AreEqual(3, errors.GetErrors("errors").Count);
            Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
        }
        public void WhenSingleValidatorReturnsTrue()
        {
            ValidatorGroup vg = new ValidatorGroup(Expression.Parse("true"));

            vg.Validators.Add(new FalseValidator());
            vg.Validators.Add(new TrueValidator());
            vg.Validators.Add(new FalseValidator());

            IValidationErrors errors = new ValidationErrors();

            errors.AddError("existingErrors", new ErrorMessage("error", null));

            bool valid = vg.Validate(new object(), errors);

            Assert.IsFalse(valid, "Validation should fail when single inner validator returns true.");
            Assert.AreEqual(2, errors.GetErrors("errors").Count);
            Assert.AreEqual(1, errors.GetErrors("existingErrors").Count);
        }
Beispiel #11
0
        public void EmptyErrorsReturnEmptyCollections()
        {
            IValidationErrors errors = new ValidationErrors();

            IList <ErrorMessage> typedErrors = errors.GetErrors("xyz");

            Assert.IsNotNull(typedErrors);
            Assert.AreEqual(0, typedErrors.Count);

            IList <string> resolvedErrors = errors.GetResolvedErrors("xyz", new NullMessageSource());

            Assert.IsNotNull(resolvedErrors);
            Assert.AreEqual(0, resolvedErrors.Count);
        }
Beispiel #12
0
        public void WhenMultipleValidatorsReturnTrueAndNotFastValidate()
        {
            ExclusiveValidatorGroup vg = new ExclusiveValidatorGroup(Expression.Parse("true"));

            vg.FastValidate = false;
            IValidationErrors errors = new ValidationErrors();

            WhenMultipleValidatorsReturnTrue(vg, errors);
            // ALL validators are called
            Assert.AreEqual(2, errors.GetErrors("errors").Count);
            Assert.IsTrue(((BaseTestValidator)vg.Validators[0]).WasCalled);
            Assert.IsTrue(((BaseTestValidator)vg.Validators[1]).WasCalled);
            Assert.IsTrue(((BaseTestValidator)vg.Validators[2]).WasCalled);
            Assert.IsTrue(((BaseTestValidator)vg.Validators[3]).WasCalled);
        }