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 ValidationOfNonCollectionLogsError()
        {
            object target = 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.ObjectCollectionValidatorTargetNotCollection));
            Assert.AreEqual("key", resultsMapping[Resources.ObjectCollectionValidatorTargetNotCollection].Key);
            Assert.AreEqual(null, resultsMapping[Resources.ObjectCollectionValidatorTargetNotCollection].Tag);
            Assert.AreSame(this, resultsMapping[Resources.ObjectCollectionValidatorTargetNotCollection].Target);
            Assert.AreSame(validator, resultsMapping[Resources.ObjectCollectionValidatorTargetNotCollection].Validator);
        }
        public void ValidatesUsingTheRulesForTheActualTypeWithASpecifiedValidatorFactoryIfValidatingTheActualType()
        {
            // 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(new AttributeValidatorFactory());

            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(5, 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);
        }
        public void ValidatesUsingTheRulesForTheBaseTypeIfNotValidatingTheActualType()
        {
            object instance1 = new ObjectCollectionValidatorFixtureReferencedTestClass();
            object instance2 = new DerivedObjectCollectionValidatorFixtureReferencedTestClass();
            object target = new object[] { instance1, 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);
        }
        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);
        }