Example #1
0
        public void SuppliesAppropriateParametersToMessageTemplate()
        {
            ValueAccess valueAccess = new MockValueAccess(5, "referenced key");
            Validator   validator   = new ValueAccessComparisonValidator(valueAccess, ComparisonOperator.Equal);

            validator.MessageTemplate = "{0}|{1}|{2}|{3}|{4}|{5}";
            validator.Tag             = "tag";
            object target = 6;
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];

            System.Text.RegularExpressions.Match match = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);
            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual(target.ToString(), match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("5", match.Groups["param3"].Value);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual("referenced key", match.Groups["param4"].Value);
            Assert.IsTrue(match.Groups["param5"].Success);
            Assert.AreEqual("Equal", match.Groups["param5"].Value);
        }
Example #2
0
        public void SuppliesAppropriateParametersToDefaultNegatedMessage()
        {
            StringLengthValidator validator = new StringLengthValidator(10, RangeBoundaryType.Exclusive, 20, RangeBoundaryType.Inclusive, true);

            validator.Tag = "tag";
            object target = "not so short";
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsFalse(match.Groups["param0"].Success);
            Assert.IsFalse(match.Groups["param1"].Success);
            Assert.IsFalse(match.Groups["param2"].Success);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("10", match.Groups["param3"].Value);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual("Exclusive", match.Groups["param4"].Value);
            Assert.IsTrue(match.Groups["param5"].Success);
            Assert.AreEqual("20", match.Groups["param5"].Value);
            Assert.IsTrue(match.Groups["param6"].Success);
            Assert.AreEqual("Inclusive", match.Groups["param6"].Value);
        }
Example #3
0
        public void SuppliesAppropriateParametersToMessageTemplate()
        {
            RelativeDateTimeValidator validator = new RelativeDateTimeValidator(-10, DateTimeUnit.Day, 20, DateTimeUnit.Year);

            validator.MessageTemplate = "{0}|{1}|{2}|{3}|{4}|{5}|{6}";
            validator.Tag             = "tag";
            object target = DateTime.Now.AddDays(-20);
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];

            System.Text.RegularExpressions.Match match = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);
            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual(target.ToString(), match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("-10", match.Groups["param3"].Value);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual("Day", match.Groups["param4"].Value);
            Assert.IsTrue(match.Groups["param5"].Success);
            Assert.AreEqual("20", match.Groups["param5"].Value);
            Assert.IsTrue(match.Groups["param6"].Success);
            Assert.AreEqual("Year", match.Groups["param6"].Value);
        }
Example #4
0
        public void SuppliesAppropriateParametersToMessageTemplate()
        {
            DomainValidator <int> validator = new DomainValidator <int>(false, 1, 2, 3, 4);

            validator.MessageTemplate = "{0}-{1}-{2}";
            validator.Tag             = "tag";
            object target = 24;
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual(target.ToString(), match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
        }
Example #5
0
        public void SuppliesAppropriateParametersToMessageTemplate()
        {
            NotNullValidator validator = new NotNullValidator();

            validator.MessageTemplate = "{0}|{1}|{2}";
            validator.Tag             = "tag";
            object target = null;
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];

            System.Text.RegularExpressions.Match match = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);
            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual("", match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
        }
        public void ReturnsCompositeFailureAfterQueryingAllValidatorsIfAllValidatorsReturnFailure()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(true, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(true, "validator3");
            Validator validator = new OrCompositeValidator(subValidator1, subValidator2, subValidator3);

            validator.MessageTemplate = "composite validator";
            string target = new string('a', 10); // just any string, but a new one

            ValidationResults validationResults = validator.Validate(target);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("composite validator", resultsList[0].Message);
            Assert.AreEqual(null, resultsList[0].Tag);
            IList <ValidationResult> nestedResultsList = ValidationTestHelper.GetResultsList(resultsList[0].NestedValidationResults);

            Assert.AreEqual(3, nestedResultsList.Count);
            Assert.AreEqual("validator1", nestedResultsList[0].Message);
            Assert.AreEqual("validator2", nestedResultsList[1].Message);
            Assert.AreEqual("validator3", nestedResultsList[2].Message);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
Example #7
0
        public void SuppliesAppropriateParametersToMessageTemplate()
        {
            RangeValidator validator = new RangeValidator(10, RangeBoundaryType.Exclusive, 20, RangeBoundaryType.Inclusive);

            validator.MessageTemplate = "{0}|{1}|{2}|{3}|{4}|{5}|{6}";
            validator.Tag             = "tag";
            object target = 24;
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual(target.ToString(), match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("10", match.Groups["param3"].Value);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual("Exclusive", match.Groups["param4"].Value);
            Assert.IsTrue(match.Groups["param5"].Success);
            Assert.AreEqual("20", match.Groups["param5"].Value);
            Assert.IsTrue(match.Groups["param6"].Success);
            Assert.AreEqual("Inclusive", match.Groups["param6"].Value);
        }
        public void CanPerformValidationWithSuppliedValidationResults()
        {
            MockValidator <string> subValidator1 = new MockValidator <string>(true, "validator1");
            MockValidator <string> subValidator2 = new MockValidator <string>(true, "validator2");
            MockValidator <string> subValidator3 = new MockValidator <string>(true, "validator3");
            Validator validator = new OrCompositeValidator(subValidator1, subValidator2, subValidator3);

            validator.Tag = "tag";
            string            target            = new string('a', 10); // just any string, but a new one
            ValidationResults validationResults = new ValidationResults();

            validator.Validate(target, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.IsTrue(TemplateStringTester.IsMatch(Resources.OrCompositeValidatorDefaultMessageTemplate, resultsList[0].Message));
            Assert.AreEqual("tag", resultsList[0].Tag);
            IList <ValidationResult> nestedResultsList = ValidationTestHelper.GetResultsList(resultsList[0].NestedValidationResults);

            Assert.AreEqual(3, nestedResultsList.Count);
            Assert.AreEqual("validator1", nestedResultsList[0].Message);
            Assert.AreEqual("validator2", nestedResultsList[1].Message);
            Assert.AreEqual("validator3", nestedResultsList[2].Message);
            Assert.AreSame(target, subValidator1.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator2.ValidatedTargets[0]);
            Assert.AreSame(target, subValidator3.ValidatedTargets[0]);
        }
        public void SuppliesAppropriateParametersToMessageTemplate()
        {
            TypeConversionValidator validator = new TypeConversionValidator(typeof(int));

            validator.MessageTemplate = "{0}-{1}-{2}-{3}";
            validator.Tag             = "tag";
            object target = "not an int";
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual(target.ToString(), match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("System.Int32", match.Groups["param3"].Value);
        }
Example #10
0
        public void SuppliesAppropriateParametersToMessageTemplate()
        {
            EnumConversionValidator validator = new EnumConversionValidator(typeof(MockEnumValidator));

            validator.MessageTemplate = "{0}|{1}|{2}|{3}";
            validator.Tag             = "tag";
            object target = "blah";
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsTrue(match.Groups["param0"].Success);
            Assert.AreEqual(target, match.Groups["param0"].Value);
            Assert.IsTrue(match.Groups["param1"].Success);
            Assert.AreEqual(key, match.Groups["param1"].Value);
            Assert.IsTrue(match.Groups["param2"].Success);
            Assert.AreEqual(validator.Tag, match.Groups["param2"].Value);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual(typeof(MockEnumValidator).Name, match.Groups["param3"].Value);
        }
        public void SuppliesAppropriateParametersToDefaultNegatedMessage()
        {
            RegexValidator validator = new RegexValidator("^[ab]+$", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace, true);

            validator.Tag = "tag";
            object target = "ab";
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsFalse(match.Groups["param0"].Success);
            Assert.IsFalse(match.Groups["param1"].Success);
            Assert.IsFalse(match.Groups["param2"].Success);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("^[ab]+$", match.Groups["param3"].Value);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual((RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace).ToString(), match.Groups["param4"].Value);
        }
        public void CanValidateInstanceWithOwnValidator()
        {
            ValidationProvider validationProvider = new ValidationProvider();

            validationProvider.SourceTypeName = typeof(ValidatedControlItemFixtureTestClass).AssemblyQualifiedName;
            MockControl control = new MockControl();

            control.Text = "value";
            control.IntControlProperty = 5;
            ValidatedControlItem validatedControlItem
                = new ValidatedControlItem(validationProvider, control);

            validatedControlItem.SourcePropertyName = "StringProperty";

            Validator validator = validatedControlItem.Validator;

            Assert.IsNotNull(validator);

            ValidationResults validationResults = validator.Validate(validatedControlItem);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("StringProperty message", resultsList[0].Message);
            Assert.AreEqual("value", MockValidator <object> .CreatedValidators[0].ValidatedTargets[0]);
        }
Example #13
0
        public void CanValidateThroughValueAccessValidatorUsingValueAccess()
        {
            ValidationProvider validationProvider = new ValidationProvider();

            validationProvider.SourceTypeName = typeof(PropertyMappedControlValueAccessFixtureTestClass).AssemblyQualifiedName;
            MockControl control = new MockControl();

            control.Text = "control text";
            validationProvider.SetSourcePropertyName(control, "StringProperty");

            MockValidator valueValidator = new MockValidator(true, "message");
            Validator     validator      = new ValueAccessValidator(new PropertyMappedControlValueAccess("StringProperty"), valueValidator);

            ValidationResults validationResults
                = validator.Validate(validationProvider.GetExistingValidatedControlItem(control));

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("StringProperty", resultsList[0].Key);
            Assert.AreEqual("message", resultsList[0].Message);
            Assert.AreEqual(null, resultsList[0].Tag);
            Assert.AreSame(validationProvider.GetExistingValidatedControlItem(control), resultsList[0].Target);
            Assert.AreSame(valueValidator, resultsList[0].Validator);
            Assert.AreEqual("control text", valueValidator.ValidatedTargets[0]);
        }
        public void SuppliesAppropriateParametersToDefaultNegatedMessage()
        {
            ContainsCharactersValidator validator = new ContainsCharactersValidator("abc", ContainsCharacters.All, true);

            validator.Tag = "tag";
            object target = "blahc";
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsFalse(match.Groups["param0"].Success);
            Assert.IsFalse(match.Groups["param1"].Success);
            Assert.IsFalse(match.Groups["param2"].Success);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("abc", match.Groups["param3"].Value);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual("All", match.Groups["param4"].Value);
        }
Example #15
0
        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);
        }
Example #16
0
        public void SuppliesAppropriateParametersToDefaultNegatedMessage()
        {
            RelativeDateTimeValidator validator = new RelativeDateTimeValidator(-10, DateTimeUnit.Day, 20, DateTimeUnit.Year, true);

            validator.Tag = "tag";
            object target = DateTime.Now.AddDays(-2);
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsFalse(match.Groups["param0"].Success);
            Assert.IsFalse(match.Groups["param1"].Success);
            Assert.IsFalse(match.Groups["param2"].Success);
            Assert.IsTrue(match.Groups["param3"].Success);
            Assert.AreEqual("-10", match.Groups["param3"].Value);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual("Day", match.Groups["param4"].Value);
            Assert.IsTrue(match.Groups["param5"].Success);
            Assert.AreEqual("20", match.Groups["param5"].Value);
            Assert.IsTrue(match.Groups["param6"].Success);
            Assert.AreEqual("Year", match.Groups["param6"].Value);
        }
Example #17
0
        public void SuppliesAppropriateParametersToDefaultNegatedMessage()
        {
            ValueAccess valueAccess = new MockValueAccess(5, "referenced key");
            Validator   validator   = new ValueAccessComparisonValidator(valueAccess, ComparisonOperator.NotEqual, null, true);

            validator.Tag = "tag";
            object target = 6;
            string key    = "key";

            ValidationResults validationResults = new ValidationResults();

            validator.DoValidate(target, null, key, validationResults);

            Assert.IsFalse(validationResults.IsValid);
            ValidationResult validationResult = ValidationTestHelper.GetResultsList(validationResults)[0];
            Match            match            = TemplateStringTester.Match(validator.MessageTemplate, validationResult.Message);

            Assert.IsTrue(match.Success);
            Assert.IsFalse(match.Groups["param0"].Success);
            Assert.IsFalse(match.Groups["param1"].Success);
            Assert.IsFalse(match.Groups["param2"].Success);
            Assert.IsFalse(match.Groups["param3"].Success);
            Assert.IsTrue(match.Groups["param4"].Success);
            Assert.AreEqual("referenced key", match.Groups["param4"].Value);
            Assert.IsTrue(match.Groups["param5"].Success);
            Assert.AreEqual("NotEqual", match.Groups["param5"].Value);
        }
Example #18
0
        public void ComparingWithValueAccessReturningAccessFailureReturnsFailure()
        {
            ValueAccess valueAccess = new MockValueAccess(null, null, true);
            Validator   validator   = new ValueAccessComparisonValidator(valueAccess, ComparisonOperator.Equal);

            ValidationResults validationResults = validator.Validate("non null");

            Assert.IsFalse(validationResults.IsValid);
            Assert.AreEqual(1, ValidationTestHelper.GetResultsList(validationResults).Count);
        }
        public void CanValidateObjectWithRuleSet()
        {
            ValidationResults validationResults
                = Validation.ValidateFromAttributes(new TestObjectWithFailingAttributesOnProperties(), "RuleA");

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("message1-RuleA", resultsList[0].Message);
        }
Example #20
0
        public void NonNegatedValidatorReturnsFailureForNullReference()
        {
            Validator validator = new NotNullValidator();

            ValidationResults validationResults = validator.Validate(null);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual(Resources.NonNullNonNegatedValidatorDefaultMessageTemplate, resultsList[0].Message);
        }
        public void LogsOwnValidationFailureIfValidationMethodThrows()
        {
            Validator validator = new SelfValidationValidator(GetType().GetMethod("ThrowingSelfValidationMethod", BindingFlags.NonPublic | BindingFlags.Instance));
            ValidationResults validationResults = new ValidationResults();

            validator.Validate(this, validationResults);

            Assert.IsNull(suppliedValidationResults);
            IList<ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);
            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual(Resources.SelfValidationMethodThrownMessage, resultsList[0].Message);
        }
Example #22
0
        public void ReturnsFailureForValueOutsideRange()
        {
            Validator <string> validator = new RangeValidator <string>("aaaa", RangeBoundaryType.Exclusive, "zzzz", RangeBoundaryType.Inclusive);

            ValidationResults validationResults = validator.Validate("0000");

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.IsTrue(TemplateStringTester.IsMatch(Resources.RangeValidatorNonNegatedDefaultMessageTemplate, resultsList[0].Message));
        }
Example #23
0
        public void ReturnsFailureForStringLongerThanMaxLengthForMaxLengthOnlyValidator()
        {
            Validator <string> validator = new StringLengthValidator(10);

            ValidationResults validationResults = validator.Validate(new StringBuilder().Append('a', 11).ToString());

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.IsTrue(TemplateStringTester.IsMatch(Resources.StringLengthValidatorNonNegatedDefaultMessageTemplate, resultsList[0].Message));
        }
        public void NegatedReturnFailureForNullString()
        {
            Validator <string> validator = new RegexValidator(RegexPattern, true);

            ValidationResults validationResults = validator.Validate(null);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.IsTrue(TemplateStringTester.IsMatch(Resources.RegexValidatorNegatedDefaultMessageTemplate, resultsList[0].Message));
        }
        public void ReturnFailureForNonMatchingStringUsingResourceRegex()
        {
            Validator <string> validator = new RegexValidator(RegexResourceName1, typeof(Properties.Resources));

            ValidationResults validationResults = validator.Validate("asdfg");

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.IsTrue(TemplateStringTester.IsMatch(Resources.RegexValidatorNonNegatedDefaultMessageTemplate, resultsList[0].Message));
        }
Example #26
0
        public void WhenValidatingRulesFromRuleSet_ThenValidationResultsOnlyContainExpectedRuleFailures()
        {
            Validator <TestObjectWithFailingAttributesOnProperties> validator
                = validationFactory.CreateValidator <TestObjectWithFailingAttributesOnProperties>("RuleA");

            ValidationResults validationResults = validator.Validate(new TestObjectWithFailingAttributesOnProperties());

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("message1-RuleA", resultsList[0].Message);
        }
Example #27
0
        public void CreatedValidatorIsBasedOnValidationAttributesAndProvidedRuleset()
        {
            Validator <TestObjectWithFailingAttributesOnProperties> validator
                = ValidationFactory.CreateValidatorFromAttributes <TestObjectWithFailingAttributesOnProperties>("RuleA");

            ValidationResults validationResults = validator.Validate(new TestObjectWithFailingAttributesOnProperties());

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual("message1-RuleA", resultsList[0].Message);
        }
Example #28
0
        public void WhenValidatingAttributeRulesThatFail_ThenReceiveCorrectFailedValidationResults()
        {
            Validator <TestObjectWithFailingAttributesOnProperties> validator
                = validationFactory.CreateValidator <TestObjectWithFailingAttributesOnProperties>();

            ValidationResults validationResults = validator.Validate(new TestObjectWithFailingAttributesOnProperties());

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(2, resultsList.Count);
            Assert.AreEqual("message1", resultsList[0].Message);
            Assert.AreEqual("message2", resultsList[1].Message);
        }
Example #29
0
        // TODO fix with templated message.
        public void ReturnsFailureWithOverridenMessageForValueOutsideRange()
        {
            string message = "overrinde message";

            Validator <string> validator = new RangeValidator <string>("aaaa", RangeBoundaryType.Exclusive, "zzzz", RangeBoundaryType.Inclusive, message);

            ValidationResults validationResults = validator.Validate("0000");

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual(message, resultsList[0].Message);
        }
Example #30
0
        public void ReturnsFailureWithOverriddenMessageForNullReference()
        {
            string message = "overriden message";

            Validator validator = new NotNullValidator(message);

            ValidationResults validationResults = validator.Validate(null);

            Assert.IsFalse(validationResults.IsValid);
            IList <ValidationResult> resultsList = ValidationTestHelper.GetResultsList(validationResults);

            Assert.AreEqual(1, resultsList.Count);
            Assert.AreEqual(message, resultsList[0].Message);
        }