public void Should_be_valid_when_value_is_greater_using_instance_property()
        {
            var validator = new GreaterThanValidator<Person, int>(p => p.Age);

            var person = new Person {Age = 18};
            Assert.That(validator.Validate(person, 21), Is.True);
        }
Exemplo n.º 2
0
        public void DefaultErrorMessage_GreaterThanValidator()
        {
            var validator = new GreaterThanValidator(1);

            Assert.That(validator.ErrorMessageSource, Is.TypeOf(typeof(LocalizedStringSource)));
            Assert.That(validator.ErrorMessageSource.GetString(), Is.EqualTo(Messages.greaterthan_error));
        }
Exemplo n.º 3
0
        public void IsValidShouldReturnFalseIfTheSpecifiedValueIsSmaller()
        {
            GreaterThanValidator validator = new GreaterThanValidator(5);

            Assert.IsFalse(validator.IsValid(5));
            Assert.IsFalse(validator.IsValid(-5));
        }
Exemplo n.º 4
0
        public void IsValidShouldReturnTrueIfTheSpecifiedValueIsBigger()
        {
            GreaterThanValidator validator = new GreaterThanValidator(-5);

            Assert.IsTrue(validator.IsValid(1));
            Assert.IsTrue(validator.IsValid(-4));
        }
Exemplo n.º 5
0
        public void IsValidShouldReturnFalseIfTheSpecifiedValueIsNotIComparable()
        {
            GreaterThanValidator validator = new GreaterThanValidator(5);

            Assert.IsFalse(validator.IsValid(new Dictionary <string, object>()));
            Assert.IsFalse(validator.IsValid(new List()));
        }
        public void Has_default_negated_message()
        {
            var validator = new GreaterThanValidator<Person, int>(10);

            var message = validator.DefaultNegatedErrorMessage;
            Console.WriteLine(message);
            Assert.That(message, Is.Not.Null & Is.Not.Empty);
        }
Exemplo n.º 7
0
        public void IsValidShouldReturnFalseIfTheSpecifiedValueIsNotSameType()
        {
            GreaterThanValidator validator = new GreaterThanValidator(4);

            Assert.IsFalse(validator.IsValid("0"));
            Assert.IsFalse(validator.IsValid(5M));
            Assert.IsFalse(validator.IsValid(5F));
        }
        public void Has_default_negated_message()
        {
            var validator = new GreaterThanValidator <Person, int>(10);

            var message = validator.DefaultNegatedErrorMessage;

            Console.WriteLine(message);
            Assert.That(message, Is.Not.Null & Is.Not.Empty);
        }
        public void Should_be_invalid_when_value_is_lower_using_instance_property()
        {
            var validator = new GreaterThanValidator <Person, int>(p => p.Age);

            var person = new Person {
                Age = 18
            };

            Assert.That(validator.Validate(person, 16), Is.False);
        }
        public void Should_create_greaterthanadapter_for_greaterthanvalidator()
        {
            // Given
            var validator = new GreaterThanValidator(1);

            // When
            var result = factory.Create(this.rule, validator);

            // Then
            result.ShouldBeOfType <GreaterThanAdapter>();
        }
        public void GetValidator()
        {
            const int valueToCompare = 10;
            GreaterThanValidationAttribute greaterThanValidationAttribute = new GreaterThanValidationAttribute(valueToCompare);

            Assert.IsInstanceOf(typeof(GreaterThanValidator), greaterThanValidationAttribute.GetValidator());

            GreaterThanValidator greaterThanValidator = (GreaterThanValidator)greaterThanValidationAttribute.GetValidator();

            Assert.AreEqual(valueToCompare, greaterThanValidator.ValueToCompare);
        }
        private ValidationMessage GreaterThan <TProp>(
            TProp value,
            TProp valueToCompare,
            IComparer comparer = null,
            ValidationMessageType validationMessageType = ValidationMessageType.Error)
            where TProp : IComparable <TProp>
        {
            var greaterThanValidator = new GreaterThanValidator <TestValidatableObject, TProp>(_ => valueToCompare, comparer, validationMessageType);
            var context           = new ValidationContext <TestValidatableObject, TProp>(null, nameof(TestValidatableObject.Number), null, value);
            var validationMessage = greaterThanValidator.ValidateProperty(context).FirstOrDefault();

            return(validationMessage);
        }
        public void Format_IComparisonValidators()
        {
            var validator1 = new EqualValidator(5);
            var validator2 = new NotEqualValidator(10);
            var validator3 = new GreaterThanValidator(8);
            var validator4 = new GreaterThanOrEqualValidator(7);
            var validator5 = new LessThanValidator(2);
            var validator6 = new LessThanOrEqualValidator(1);
            var validator7 = new EqualValidator(o => o, typeof(Customer).GetProperty("UserName"));

            Assert.That(_formatter.Format(validator1, _typeNameFormatter), Is.EqualTo("EqualValidator { ValueToCompare = '5' }"));
            Assert.That(_formatter.Format(validator2, _typeNameFormatter), Is.EqualTo("NotEqualValidator { ValueToCompare = '10' }"));
            Assert.That(_formatter.Format(validator3, _typeNameFormatter), Is.EqualTo("GreaterThanValidator { ValueToCompare = '8' }"));
            Assert.That(_formatter.Format(validator4, _typeNameFormatter), Is.EqualTo("GreaterThanOrEqualValidator { ValueToCompare = '7' }"));
            Assert.That(_formatter.Format(validator5, _typeNameFormatter), Is.EqualTo("LessThanValidator { ValueToCompare = '2' }"));
            Assert.That(_formatter.Format(validator6, _typeNameFormatter), Is.EqualTo("LessThanOrEqualValidator { ValueToCompare = '1' }"));
            Assert.That(_formatter.Format(validator7, _typeNameFormatter), Is.EqualTo("EqualValidator { MemberToCompare = 'Customer.UserName\r\n' }"));
        }
Exemplo n.º 14
0
        public static IRuleBuilder <T> GreaterThan <T>(this IRuleBuilder <T> ruleBuilder, dynamic valueToComare) where T : class
        {
            ruleBuilder.AddRule(GreaterThanValidator <T> .New(ruleBuilder, valueToComare));

            return(ruleBuilder);
        }
        public void Should_create_greaterthanadapter_for_greaterthanvalidator()
        {
            // Given
            var validator = new GreaterThanValidator(1);

            // When
            var result = factory.Create(this.rule, validator);

            // Then
            result.ShouldBeOfType<GreaterThanAdapter>();
        }
Exemplo n.º 16
0
        public void IsValidShouldReturnTrueIfTheSpecifiedValueIsNull()
        {
            GreaterThanValidator validator = new GreaterThanValidator(5);

            Assert.IsTrue(validator.IsValid(null));
        }
Exemplo n.º 17
0
        public override IEnumerable <ModelClientValidationRule> GetClientValidationRules()
        {
            if (!ShouldGenerateClientSideRules())
            {
                yield break;
            }

            string comparisionValuePlaceHolder = "ComparisonValue";

            // Convert validator
            GreaterThanValidator greaterThanValidator = (GreaterThanValidator)Validator;

            // Initialize client rule
            ModelClientValidationRule rule = new ModelClientValidationRule();

            rule.ValidationType = "greaterthan";

            MessageFormatter formatter = new MessageFormatter()
                                         .AppendPropertyName(Metadata.DisplayName ?? Rule.PropertyName);

            if (greaterThanValidator.MemberToCompare != null)
            {
                // If memeber has been selected to compare create rule for field

                // Append comparision member to message
                formatter.AppendArgument(
                    comparisionValuePlaceHolder,
                    greaterThanValidator.MemberToCompare.GetDisplayName());

                // Append '*.' to the name of field for prefix merging
                rule.ValidationParameters["field"] = string.Format("*.{0}",
                                                                   greaterThanValidator.MemberToCompare.Name);
            }
            else if (greaterThanValidator.ValueToCompare != null)
            {
                // If value has been set to compare create rule for value
                string valueToCompare = null;

                if (greaterThanValidator.ValueToCompare is DateTime)
                {
                    // If value is of type DateTime convert it to DateTime
                    // and format as yyyy-MM-dd to be able to parse it at client side
                    // easily using jQuery.
                    DateTime dateValueToCompare = (DateTime)greaterThanValidator.ValueToCompare;
                    valueToCompare = dateValueToCompare.ToString("yyyy-MM-dd");
                }
                else
                {
                    valueToCompare = greaterThanValidator.ValueToCompare.ToString();
                }

                // Append comparision value to  message
                formatter.AppendArgument(comparisionValuePlaceHolder, valueToCompare);
                // Set value to compare
                rule.ValidationParameters["value"] = valueToCompare;
            }

            // Set error message of rule
            rule.ErrorMessage = formatter.BuildMessage(Validator.ErrorMessageSource.GetString(null));

            yield return(rule);
        }
Exemplo n.º 18
0
        public void Should_be_valid_when_value_is_greater()
        {
            var validator = new GreaterThanValidator <Person, int>(18);

            Assert.That(validator.Validate(null, 21), Is.True);
        }
Exemplo n.º 19
0
        public void Should_be_invalid_when_value_is_lower()
        {
            var validator = new GreaterThanValidator <Person, int>(18);

            Assert.That(validator.Validate(null, 16), Is.False);
        }
Exemplo n.º 20
0
        public void Should_be_valid_when_value_is_greater()
        {
            var validator = new GreaterThanValidator<Person, int>(18);

            Assert.That(validator.Validate(null, 21), Is.True);
        }
Exemplo n.º 21
0
        public void Should_be_invalid_when_value_is_lower()
        {
            var validator = new GreaterThanValidator<Person, int>(18);

            Assert.That(validator.Validate(null, 16), Is.False);
        }