Exemple #1
0
        public void CanUseAttributeAsValidationAttributeForValidValue()
        {
            ValidationAttribute attribute =
                new StringLengthValidatorAttribute(10)
            {
                MessageTemplate = "template {1}"
            };

            Assert.IsTrue(attribute.IsValid("abcdefg"));
        }
Exemple #2
0
        public void ValidatingWithValidatorAttributeWithARulesetSkipsValidation()
        {
            ValidationAttribute attribute =
                new StringLengthValidatorAttribute(10)
            {
                MessageTemplate = "template {1}",
                Ruleset         = "some ruleset"
            };

            Assert.IsTrue(attribute.IsValid("abcdefghijklm"));
        }
Exemple #3
0
        public void CanUseAttributeAsValidationAttribute()
        {
            ValidationAttribute attribute =
                new StringLengthValidatorAttribute(10)
            {
                MessageTemplate = "template {1}"
            };

            Assert.IsFalse(attribute.IsValid("abcdefghijklm"));
            Assert.AreEqual("template name", attribute.FormatErrorMessage("name"));
        }
Exemple #4
0
        public void AttributeWithUpperBoundOnlyCreatesAppropriateValidator()
        {
            ValidatorAttribute attribute = new StringLengthValidatorAttribute(20);

            Validator validator = ((IValidatorDescriptor)attribute).CreateValidator(null, null, null, null);

            Assert.IsNotNull(validator);

            StringLengthValidator stringLengthValidator = validator as StringLengthValidator;

            Assert.IsNotNull(stringLengthValidator);
            Assert.AreEqual(RangeBoundaryType.Ignore, stringLengthValidator.LowerBoundType);
            Assert.AreEqual(20, stringLengthValidator.UpperBound);
            Assert.AreEqual(RangeBoundaryType.Inclusive, stringLengthValidator.UpperBoundType);
            Assert.AreEqual(Resources.StringLengthValidatorNonNegatedDefaultMessageTemplate, stringLengthValidator.MessageTemplate);
            Assert.AreEqual(false, stringLengthValidator.Negated);
        }
Exemple #5
0
        public void AttributeWithLowerAndUpperBoundsOnlyAndNegatedCreatesAppropriateValidator()
        {
            ValueValidatorAttribute attribute = new StringLengthValidatorAttribute(10, 20);

            attribute.Negated = true;

            Validator validator = ((IValidatorDescriptor)attribute).CreateValidator(null, null, null, null);

            Assert.IsNotNull(validator);

            StringLengthValidator stringLengthValidator = validator as StringLengthValidator;

            Assert.IsNotNull(stringLengthValidator);
            Assert.AreEqual(10, stringLengthValidator.LowerBound);
            Assert.AreEqual(RangeBoundaryType.Inclusive, stringLengthValidator.LowerBoundType);
            Assert.AreEqual(20, stringLengthValidator.UpperBound);
            Assert.AreEqual(RangeBoundaryType.Inclusive, stringLengthValidator.UpperBoundType);
            Assert.AreEqual(Resources.StringLengthValidatorNegatedDefaultMessageTemplate, stringLengthValidator.MessageTemplate);
            Assert.AreEqual(true, stringLengthValidator.Negated);
        }
Exemple #6
0
        public void AttributeWithLowerAndUpperBoundsAndBoundTypesAndMessageTemplateCreatesAppropriateValidator()
        {
            ValidatorAttribute attribute = new StringLengthValidatorAttribute(10, RangeBoundaryType.Exclusive, 0, RangeBoundaryType.Ignore);

            attribute.MessageTemplate = "my message template";

            Validator validator = ((IValidatorDescriptor)attribute).CreateValidator(null, null, null, null);

            Assert.IsNotNull(validator);

            StringLengthValidator stringLengthValidator = validator as StringLengthValidator;

            Assert.IsNotNull(stringLengthValidator);
            Assert.AreEqual(10, stringLengthValidator.LowerBound);
            Assert.AreEqual(RangeBoundaryType.Exclusive, stringLengthValidator.LowerBoundType);
            Assert.AreEqual(0, stringLengthValidator.UpperBound);
            Assert.AreEqual(RangeBoundaryType.Ignore, stringLengthValidator.UpperBoundType);
            Assert.AreEqual("my message template", stringLengthValidator.MessageTemplate);
            Assert.AreEqual(false, stringLengthValidator.Negated);
        }