public void ValidationIsPerformedOnCollectionElementsUsingSuppliedRuleset() { object instance1 = new ObjectCollectionValidatorFixtureReferencedTestClass(); object instance2 = new ObjectCollectionValidatorFixtureReferencedTestClass(); object target = new object[] { instance1, instance2 }; Validator validator = new ObjectCollectionValidator(typeof(ObjectCollectionValidatorFixtureReferencedTestClass), "RuleB"); ValidationResults validationResults = new ValidationResults(); validator.DoValidate(target, this, "key", validationResults); // setting the currentTarget and the key Assert.IsFalse(validationResults.IsValid); IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults); Assert.AreEqual(2, resultsList.Count); Assert.AreEqual("PropertyInReferencedObject-RuleB", resultsList[0].Message); Assert.AreEqual("PropertyInReferencedObject", resultsList[0].Key); Assert.AreEqual(null, resultsList[0].Tag); Assert.AreSame(instance1, resultsList[0].Target); Assert.AreEqual("PropertyInReferencedObject-RuleB", resultsList[1].Message); Assert.AreEqual("PropertyInReferencedObject", resultsList[1].Key); Assert.AreEqual(null, resultsList[1].Tag); Assert.AreSame(instance2, resultsList[1].Target); }
public void ValidationOfCollectionWithNullLogsNoError() { object target = new ObjectCollectionValidatorFixtureReferencedTestClass[] { null }; Validator validator = new ObjectCollectionValidator(typeof(ObjectCollectionValidatorFixtureReferencedTestClass)); ValidationResults validationResults = new ValidationResults(); validator.DoValidate(target, this, "key", validationResults); // setting the currentTarget and the key Assert.IsTrue(validationResults.IsValid); }
public void ValidatesUsingTheRulesForTheActualTypeIfValidatingTheActualType() { // TODO - change to avoid relying on reflection order object instance1 = new ObjectCollectionValidatorFixtureReferencedTestClass(); object instance2 = new DerivedObjectCollectionValidatorFixtureReferencedTestClass(); object target = new object[] { instance1, instance2 }; Validator validator = new ObjectCollectionValidator(); ValidationResults validationResults = new ValidationResults(); validator.DoValidate(target, this, "key", validationResults); // setting the currentTarget and the key Assert.IsFalse(validationResults.IsValid); IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults); Assert.AreEqual(6, resultsList.Count); Assert.AreEqual("ReferencedObject", resultsList[0].Message); Assert.AreEqual(null, resultsList[0].Key); Assert.AreEqual(null, resultsList[0].Tag); Assert.AreSame(instance1, resultsList[0].Target); Assert.AreEqual("PropertyInReferencedObject", resultsList[1].Message); Assert.AreEqual("PropertyInReferencedObject", resultsList[1].Key); Assert.AreEqual(null, resultsList[1].Tag); Assert.AreSame(instance1, resultsList[1].Target); Assert.AreEqual("DerivedReferencedObject", resultsList[2].Message); Assert.AreEqual(null, resultsList[2].Key); Assert.AreEqual(null, resultsList[2].Tag); Assert.AreSame(instance2, resultsList[2].Target); Assert.AreEqual("PropertyInDerivedReferencedObject", resultsList[3].Message); Assert.AreEqual("PropertyInDerivedReferencedObject", resultsList[3].Key); Assert.AreEqual(null, resultsList[3].Tag); Assert.AreSame(instance2, resultsList[3].Target); Assert.AreEqual("PropertyInReferencedObject", resultsList[4].Message); Assert.AreEqual("PropertyInReferencedObject", resultsList[4].Key); Assert.AreEqual(null, resultsList[4].Tag); Assert.AreSame(instance2, resultsList[4].Target); Assert.AreEqual("PropertyInDerivedReferencedObject-DataAnnotations", resultsList[5].Message); Assert.AreEqual("PropertyInDerivedReferencedObject", resultsList[5].Key); Assert.AreEqual(null, resultsList[5].Tag); Assert.AreSame(instance2, resultsList[5].Target); }
public void ValidationOfCollectionWithObjectsOfNonCompatibleTypeLogsError() { object target = new object[] { 10 }; Validator validator = new ObjectCollectionValidator(typeof(ObjectCollectionValidatorFixtureReferencedTestClass)); ValidationResults validationResults = new ValidationResults(); validator.DoValidate(target, this, "key", validationResults); // setting the currentTarget and the key Assert.IsFalse(validationResults.IsValid); IDictionary <string, ValidationResult> resultsMapping = ValidationTestHelper.GetResultsMapping(validationResults); Assert.AreEqual(1, resultsMapping.Count); Assert.IsTrue(resultsMapping.ContainsKey(Resources.ObjectCollectionValidatorIncompatibleElementInTargetCollection)); Assert.AreEqual(null, resultsMapping[Resources.ObjectCollectionValidatorIncompatibleElementInTargetCollection].Key); Assert.AreEqual(null, resultsMapping[Resources.ObjectCollectionValidatorIncompatibleElementInTargetCollection].Tag); Assert.AreEqual(10, resultsMapping[Resources.ObjectCollectionValidatorIncompatibleElementInTargetCollection].Target); Assert.AreSame(validator, resultsMapping[Resources.ObjectCollectionValidatorIncompatibleElementInTargetCollection].Validator); }
public void NullElementsInCollectionAreSkipped() { object instance1 = new ObjectCollectionValidatorFixtureReferencedTestClass(); object instance2 = new ObjectCollectionValidatorFixtureReferencedTestClass(); object target = new object[] { instance1, null, instance2 }; Validator validator = new ObjectCollectionValidator(typeof(ObjectCollectionValidatorFixtureReferencedTestClass)); ValidationResults validationResults = new ValidationResults(); validator.DoValidate(target, this, "key", validationResults); // setting the currentTarget and the key Assert.IsFalse(validationResults.IsValid); IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults); Assert.AreEqual(4, resultsList.Count); Assert.AreEqual("ReferencedObject", resultsList[0].Message); Assert.AreEqual(null, resultsList[0].Key); Assert.AreEqual(null, resultsList[0].Tag); Assert.AreSame(instance1, resultsList[0].Target); Assert.AreEqual("PropertyInReferencedObject", resultsList[1].Message); Assert.AreEqual("PropertyInReferencedObject", resultsList[1].Key); Assert.AreEqual(null, resultsList[1].Tag); Assert.AreSame(instance1, resultsList[1].Target); Assert.AreEqual("ReferencedObject", resultsList[2].Message); Assert.AreEqual(null, resultsList[2].Key); Assert.AreEqual(null, resultsList[2].Tag); Assert.AreSame(instance2, resultsList[2].Target); Assert.AreEqual("PropertyInReferencedObject", resultsList[3].Message); Assert.AreEqual("PropertyInReferencedObject", resultsList[3].Key); Assert.AreEqual(null, resultsList[3].Tag); Assert.AreSame(instance2, resultsList[3].Target); }