public void Populate_WithRegularAndComplexClass_Works() { // Let's pretend these are the values our properties are currently bound to from our View FromEmail.Value = "LOL_I_AM_NOT_AN_EMAIL!"; // bad ToName.Value = "X"; // bad MessageHtml.Value = "NOPE"; // bad Rating.Value = 5; // good var testClassInstance = ValidatableProps.Populate <TestClass>(); Assert.AreEqual("LOL_I_AM_NOT_AN_EMAIL!", testClassInstance.FromEmail); Assert.AreEqual("X", testClassInstance.ToName); Assert.AreEqual("NOPE", testClassInstance.MessageHtml); Assert.AreEqual(5, testClassInstance.Rating); var complexClassValidatable1 = new Validatable <TestClass>(nameof(TestComplexClass.TestClassInstance)); complexClassValidatable1.Value = testClassInstance; var complexClassValidatable2 = new Validatable <string>(nameof(TestComplexClass.MyName)); complexClassValidatable2.Value = "Test Class!"; var complexValidatables = new Validatables(complexClassValidatable1, complexClassValidatable2); var testComplexClassInstance = complexValidatables.Populate <TestComplexClass>(); Assert.AreEqual("Test Class!", testComplexClassInstance.MyName); Assert.AreEqual(testClassInstance, testComplexClassInstance.TestClassInstance); }
public void Populate_WithExistingClass_Works() { // Let's pretend these are the values our properties are currently bound to from our View FromEmail.Value = "OLD VALUE"; // bad ToName.Value = "OLD VALUE"; // bad MessageHtml.Value = "OLD VALUE"; // bad Rating.Value = 5; // good var testClassInstance = ValidatableProps.Populate <TestClass>(); var subsetOfValidatableProps = new Validatables(FromEmail, ToName); FromEmail.Value = "NEW VALUE"; ToName.Value = "NEW VALUE"; subsetOfValidatableProps.Populate(testClassInstance); Assert.AreEqual("NEW VALUE", testClassInstance.FromEmail); Assert.AreEqual("NEW VALUE", testClassInstance.ToName); Assert.AreEqual("OLD VALUE", testClassInstance.MessageHtml); Assert.AreEqual(5, testClassInstance.Rating); }
public void Clear_SinglePropViaGroupLeavingOneBad_ErrorsUpdated() { // Let's pretend these are the values our properties are currently bound to from our View ToName.Value = "XX"; // bad Rating.Value = 0; // bad var testClassInstance = new TestClass { ToName = ToName.Value, Rating = Rating.Value }; _ = Validator.Validate(testClassInstance).ApplyResultsTo(ValidatableProps); Assert.IsFalse(ValidatableProps.AreValid); Assert.AreEqual(5, ValidatableProps.Errors.Count); Assert.IsFalse(Rating.IsValid); Assert.AreEqual(1, Rating.Errors.Count); ValidatableProps.Clear(forClassPropertyNames: nameof(TestClass.ToName)); Assert.AreEqual("XX", ToName.Value); Assert.IsTrue(ToName.IsValid); Assert.IsEmpty(ToName.Errors); Assert.IsEmpty(ToName.FirstError); Assert.IsFalse(Rating.IsValid); Assert.AreEqual(1, Rating.Errors.Count); Assert.IsNotEmpty(Rating.FirstError); Assert.IsFalse(ValidatableProps.AreValid); Assert.AreEqual(3, ValidatableProps.Errors.Count); // count drops to 3 failed errors only ValidatableProps.Clear(onlyValidation: false); Assert.IsEmpty(ToName.Value); Assert.IsNull(Rating.Value); }
public void ApplyResultsToThenClear_MultipleProps_ResultAccuracy() { // Let's pretend these are the values our properties are currently bound to from our View FromEmail.Value = "LOL_I_AM_NOT_AN_EMAIL!"; // bad ToName.Value = "X"; // bad MessageHtml.Value = "NOPE"; // bad Rating.Value = 5; // good var testClassInstance = new TestClass { FromEmail = FromEmail.Value, ToName = ToName.Value, MessageHtml = MessageHtml.Value, Rating = Rating.Value }; var result = Validator.Validate(testClassInstance).ApplyResultsTo(ValidatableProps); Assert.IsFalse(result.IsValidOverall); Assert.AreEqual(4, result.AllErrors.Count); Assert.IsTrue(result.IsValidForNonSplitErrors); Assert.AreEqual(0, result.NonSplitErrors.Count); Assert.IsFalse(ValidatableProps.AreValid); Assert.AreEqual(4, ValidatableProps.Errors.Count); Assert.IsFalse(FromEmail.IsValid); Assert.AreEqual(1, FromEmail.Errors.Count); Assert.IsFalse(ToName.IsValid); Assert.AreEqual(2, ToName.Errors.Count); Assert.IsFalse(MessageHtml.IsValid); Assert.AreEqual(1, MessageHtml.Errors.Count); // only 1 error expected because of CascadeMode set to StopOnFirstFailure Assert.IsTrue(Rating.IsValid); Assert.AreEqual(0, Rating.Errors.Count); ValidatableProps.Clear(); Assert.IsTrue(ValidatableProps.AreValid); Assert.IsEmpty(ValidatableProps.Errors); Assert.IsEmpty(ValidatableProps.FirstError); Assert.AreEqual("LOL_I_AM_NOT_AN_EMAIL!", FromEmail.Value); Assert.IsTrue(FromEmail.IsValid); Assert.IsEmpty(FromEmail.Errors); Assert.IsEmpty(FromEmail.FirstError); Assert.AreEqual("X", ToName.Value); Assert.IsTrue(ToName.IsValid); Assert.IsEmpty(ToName.Errors); Assert.IsEmpty(ToName.FirstError); Assert.AreEqual("NOPE", MessageHtml.Value); Assert.IsTrue(MessageHtml.IsValid); Assert.IsEmpty(MessageHtml.Errors); Assert.IsEmpty(MessageHtml.FirstError); Assert.AreEqual(5, Rating.Value); Assert.IsTrue(Rating.IsValid); Assert.IsEmpty(Rating.Errors); Assert.IsEmpty(Rating.FirstError); ValidatableProps.Clear(onlyValidation: false); Assert.IsEmpty(FromEmail.Value); Assert.IsEmpty(ToName.Value); Assert.IsEmpty(MessageHtml.Value); Assert.IsNull(Rating.Value); }