public void IsValidWhenAllValidatorsAreValid() { var validatableObject = new ValidatableTestObject(); var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .Required("Error occurred!"); var anotherValidator = PropertyValidator .For(validatableObject, o => o.AnotherStringProperty) .Required("Error occurred!"); var modelValidator = new ModelValidator(validator, anotherValidator); Assert.IsFalse(modelValidator.IsValid); validatableObject.SomeStringProperty = "valid"; Assert.IsFalse(modelValidator.IsValid); validatableObject.AnotherStringProperty = "valid"; Assert.IsTrue(modelValidator.IsValid); validatableObject.AnotherStringProperty = ""; Assert.IsFalse(modelValidator.IsValid); }
public void ValidationResultReturnsUnvalidatedIfNoValidators() { var validatableObject = new ValidatableTestObject(); var validator = PropertyValidator.For(validatableObject, o => o.SomeStringProperty); var result = validator.ValidationResult; Assert.AreEqual(PropertyValidationResult.Unvalidated, result); }
public void DoesNotShowErrorWhenUnvalidated() { var viewModel = new ValidatableTestObject(); var testValidator = PropertyValidator.For(viewModel, x => x.SomeStringProperty) .Required("Please enter some text"); var validationMessage = new ValidationMessage(); validationMessage.Validator = testValidator; Assert.IsFalse(validationMessage.ShowError); }
public void RequiredIsUnvalidatedAndNotValidIfPropertyNeverChanges() { var validatableObject = new ValidatableTestObject(); var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .Required("Please provide a value for SomeStringProperty!"); var result = validator.ValidationResult; Assert.IsFalse(result.IsValid); Assert.AreEqual(PropertyValidationResult.Unvalidated, result); }
public void ValidationResultReturnsSuccessIfValidatorsPass() { var validatableObject = new ValidatableTestObject(); // Validator validates that the property is equal to the string "Inigo Montoya". var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .ValidIfTrue(value => value == "Inigo Montoya", "Error occurred!"); validatableObject.SomeStringProperty = "Inigo Montoya"; var result = validator.ValidationResult; Assert.IsTrue(result.IsValid); }
public void RequiredValidatesPropertyNotEmpty() { var validatableObject = new ValidatableTestObject(); var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .Required("Please provide a value for SomeStringProperty!"); validatableObject.SomeStringProperty = ""; var result = validator.ValidationResult; Assert.IsFalse(result.IsValid); Assert.AreEqual("Please provide a value for SomeStringProperty!", result.Message); }
public void ShowsErrorWhenValidationResultIsInvalid() { var viewModel = new ValidatableTestObject(); var testValidator = PropertyValidator.For(viewModel, x => x.SomeStringProperty) .Required("Please enter some text"); var validationMessage = new ValidationMessage(); validationMessage.Validator = testValidator; viewModel.SomeStringProperty = "valid"; viewModel.SomeStringProperty = ""; Assert.AreEqual(ValidationStatus.Invalid, testValidator.ValidationResult.Status); Assert.IsTrue(validationMessage.ShowError); }
public void ValidationResultReturnsFailureIfAnyValidatorsFail() { var validatableObject = new ValidatableTestObject(); var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .Required("String may not be null or empty") .ValidIfTrue(value => value == "Inigo Montoya", "Error occurred!") .ValidIfTrue(value => value == "My name is not Inigo Montoya", "Doh!"); validatableObject.SomeStringProperty = "My name is not Inigo Montoya"; var result = validator.ValidationResult; Assert.IsFalse(result.IsValid); Assert.AreEqual("Error occurred!", result.Message); }
public void ValidatorsRunInOrderAndStopWhenInvalid() { List<int> results = new List<int>(); var validatableObject = new ValidatableTestObject(); var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .ValidIfTrue(value => { results.Add(0); return true; }, "Error occurred!") .ValidIfTrue(value => { results.Add(1); return true; }, "Error occurred!") .ValidIfTrue(value => { results.Add(2); return true; }, "Error occurred!") .ValidIfTrue(value => { results.Add(3); return false; }, "Error occurred!") .ValidIfTrue(value => { results.Add(4); return true; }, "Error occurred!"); validatableObject.SomeStringProperty = "My name is not Inigo Montoya"; var result = validator.ValidationResult; Assert.IsFalse(result.IsValid); Assert.AreEqual("Error occurred!", result.Message); Assert.AreEqual(4, results.Count); for (int i = 0; i < 4; i++) Assert.AreEqual(i, results[i]); }
public void ValidationResultNotifiesWhenValidationStateChanges() { var validatableObject = new ValidatableTestObject(); // Validator validates that the property is equal to the string "Inigo Montoya". var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .ValidIfTrue(value => value == "Inigo Montoya", "Error occurred!"); PropertyValidationResult validationResult = null; validator.PropertyChanged += (s, e) => { if(e.PropertyName == nameof(validator.ValidationResult)) validationResult = validator.ValidationResult; }; Assert.IsNull(validationResult); // Precondition validatableObject.SomeStringProperty = "not empty"; Assert.AreEqual(validationResult, validator.ValidationResult); Assert.IsFalse(validationResult.IsValid); }
public void EndToEndTestShowsErrorMessageWhenReactiveValidatorIsNotValid() { var textBox = new TextBox(); var viewModel = new ValidatableTestObject(); var testValidator = PropertyValidator.For(viewModel, x => x.SomeStringProperty) .Required("Please enter some text"); var validationMessage = new ValidationMessage(); BindControlToViewModel(viewModel, nameof(viewModel.SomeStringProperty), textBox, TextBox.TextProperty); validationMessage.Validator = testValidator; Assert.IsFalse(validationMessage.ShowError); textBox.Text = "x"; Assert.AreEqual("x", viewModel.SomeStringProperty); textBox.Text = ""; Assert.AreEqual("", viewModel.SomeStringProperty); Assert.IsFalse(testValidator.ValidationResult.IsValid); Assert.IsTrue(validationMessage.ShowError); }
public void ValidationResultNotifiesWhenValidationStateChanges() { var validatableObject = new ValidatableTestObject(); // Validator validates that the property is equal to the string "Inigo Montoya". var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .ValidIfTrue(value => value == "Inigo Montoya", "Error occurred!"); PropertyValidationResult validationResult = null; validator.PropertyChanged += (s, e) => { if (e.PropertyName == nameof(validator.ValidationResult)) { validationResult = validator.ValidationResult; } }; Assert.IsNull(validationResult); // Precondition validatableObject.SomeStringProperty = "not empty"; Assert.AreEqual(validationResult, validator.ValidationResult); Assert.IsFalse(validationResult.IsValid); }
public void ValidatorsRunInOrderAndStopWhenInvalid() { List <int> results = new List <int>(); var validatableObject = new ValidatableTestObject(); var validator = PropertyValidator .For(validatableObject, o => o.SomeStringProperty) .ValidIfTrue(value => { results.Add(0); return(true); }, "Error occurred!") .ValidIfTrue(value => { results.Add(1); return(true); }, "Error occurred!") .ValidIfTrue(value => { results.Add(2); return(true); }, "Error occurred!") .ValidIfTrue(value => { results.Add(3); return(false); }, "Error occurred!") .ValidIfTrue(value => { results.Add(4); return(true); }, "Error occurred!"); validatableObject.SomeStringProperty = "My name is not Inigo Montoya"; var result = validator.ValidationResult; Assert.IsFalse(result.IsValid); Assert.AreEqual("Error occurred!", result.Message); Assert.AreEqual(4, results.Count); for (int i = 0; i < 4; i++) { Assert.AreEqual(i, results[i]); } }