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 mergedErrors = errors.GetErrors(GoodErrorKey); Assert.IsNotNull(mergedErrors); Assert.AreEqual(2, mergedErrors.Count); Assert.AreEqual(ErrorMessageOne, mergedErrors[0]); Assert.AreEqual(ErrorMessageTwo, mergedErrors[1]); IList otherErrorsForKey = errors.GetErrors(anotherKey); Assert.IsNotNull(otherErrorsForKey); Assert.AreEqual(1, otherErrorsForKey.Count); Assert.AreEqual(ErrorMessageTwo, otherErrorsForKey[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); }
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); }
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); }
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); }
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); }
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); }
public void WhenAllValidatorsReturnFalse() { ValidatorGroup vg = new ValidatorGroup(); 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 ContainsNoErrorsDirectlyAfterInstantiation() { IValidationErrors errors = new ValidationErrors(); Assert.IsTrue(errors.IsEmpty); Assert.IsNotNull(errors.GetErrors(GoodErrorKey)); }
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); }
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); }
public void WhenAllValidatorsReturnTrue() { ValidatorGroup vg = new ValidatorGroup("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); }
public void WhenAllValidatorsReturnFalse() { ValidatorGroup vg = new ValidatorGroup(); 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 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); }
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); }
public void ContainsNoErrorsDirectlyAfterInstantiation() { IValidationErrors errors = new ValidationErrors(); Assert.IsTrue(errors.IsEmpty); Assert.IsNotNull(errors.GetErrors(GoodErrorKey)); }
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]); }
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); }
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); }
public void HandledTypeConversionExceptionTargetToSource() { BaseBindingManager dbm = new BaseBindingManager(); IValidationErrors errors = new ValidationErrors(); Inventor st = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); st.Inventions = new string[] {"Invention One", "Invention Two"}; SimpleExpressionBinding binding = new SimpleExpressionBinding("pob", "DOB"); binding.SetErrorMessage("error", "errors"); dbm.AddBinding(binding); dbm.BindTargetToSource(st, st, errors); Assert.IsFalse(binding.IsValid(errors)); Assert.IsFalse(errors.IsEmpty); Assert.AreEqual(1, errors.GetErrors("errors").Count); // make sure that the old value doesn't override current invalid value dbm.BindSourceToTarget(st, st, errors); Assert.AreEqual(new DateTime(1856, 7, 9), st.DOB); }
public void HandledTypeConversionExceptionSourceToTarget() { BaseBindingManager dbm = new BaseBindingManager(); IValidationErrors errors = new ValidationErrors(); Hashtable source = new Hashtable(); source["boolValue"] = false; Inventor target = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); SimpleExpressionBinding binding = new SimpleExpressionBinding("['boolValue']", "DOB"); binding.SetErrorMessage("error", "errors"); dbm.AddBinding(binding); dbm.BindSourceToTarget(source, target, errors); Assert.IsFalse(binding.IsValid(errors)); Assert.IsFalse(errors.IsEmpty); Assert.AreEqual(1, errors.GetErrors("errors").Count); // make sure that the old value doesn't override current invalid value dbm.BindTargetToSource(source, target, errors); Assert.AreEqual(false, source["boolValue"]); }
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 ); }
public void WhenGroupIsNotValidatedBecauseWhenExpressionReturnsFalse() { ExclusiveValidatorGroup vg = new ExclusiveValidatorGroup("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); }
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 ); }
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); }
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); }