Beispiel #1
0
        public void ShortStringReturnsTrue()
        {
            var rule      = new StringLengthRule(20);
            var candidate = rule.IsValid("valid");

            Assert.IsTrue(candidate);
        }
Beispiel #2
0
        public void LongStringReturnsFalse()
        {
            var rule      = new StringLengthRule(2);
            var candidate = rule.IsValid("valid");

            Assert.IsFalse(candidate);
        }
Beispiel #3
0
        public void Ctor_WithValidValues_InitializesPropertiesCorrectly(int min, int max, string message)
        {
            var rule = new StringLengthRule(min, max, message);

            Assert.Equal(message, rule.ValidationMessage);
            Assert.IsAssignableFrom <IValidationRule <string> >(rule);
        }
Beispiel #4
0
        public void Check_ForCorrectLengthString_ReturnsTrue(int min, int max, string value)
        {
            var rule = new StringLengthRule(min, max, ErrorMessage);

            var isValid = rule.Check(value);

            Assert.True(isValid);
        }
Beispiel #5
0
        private static StringLengthRule AddArgs(this StringLengthRule rule, IEnumerable <RuleArgument> args)
        {
            try
            {
                rule.Min = Convert.ToInt32(args.Where(a => string.Equals(a.Parameter, "minlength", StringComparison.CurrentCultureIgnoreCase))
                                           .Single().Value);
            }
            catch (Exception e)
            {
                throw new ValidationParseException($"Error parsing minimum value for {rule.RuleType.ToString()}:\r\n {e.Message}", e);
            }

            try
            {
                rule.Max = Convert.ToInt32(args.Where(a => string.Equals(a.Parameter, "maxlength", StringComparison.CurrentCultureIgnoreCase))
                                           .Single().Value);
            }
            catch (Exception e)
            {
                throw new ValidationParseException($"Error parsing maximum value for {rule.RuleType.ToString()}:\r\n {e.Message}", e);
            }
            return(rule);
        }
 public void ShortStringReturnsTrue()
 {
     var rule = new StringLengthRule(20);
     var candidate = rule.IsValid("valid");
     Assert.IsTrue(candidate);
 }
 public void LongStringReturnsFalse()
 {
     var rule = new StringLengthRule(2);
     var candidate = rule.IsValid("valid");
     Assert.IsFalse(candidate);
 }
        public void EvaluateShouldReturnSuccessForNullValue()
        {
            // arrange
            var rule = new StringLengthRule( 10 );
            var property = new Property<string>( "Text", null );
            var expected = ValidationResult.Success;

            // act
            var actual = rule.Evaluate( property );

            // assert
            Assert.Equal( expected, actual );
        }
        public void EvaluateWithMinLengthShouldReturnExpectedResultForInvalidValue( int count )
        {
            // arrange
            var value = new string( 'x', count );
            var rule = new StringLengthRule( 1, 10 );
            var property = new Property<string>( "Text", value );

            // act
            var actual = rule.Evaluate( property );

            // assert
            Assert.Equal( "The Text field must be a string with a minimum length of 1 and a maximum length of 10.", actual.ErrorMessage );
            Assert.Equal( 1, actual.MemberNames.Count() );
            Assert.Equal( "Text", actual.MemberNames.Single() );
        }
 private static ModelClientValidationRule GetStringLengthRule(string errorMessage, StringLengthRule stringLengthRule)
 {
     return(new ModelClientValidationStringLengthRule(errorMessage, stringLengthRule.Minimum ?? 0, stringLengthRule.Maximum ?? int.MaxValue));
 }
 private static StringLengthAttribute Convert(this StringLengthRule rule)
 => new StringLengthAttribute(rule.Max)
 {
     MinimumLength = rule.Min
 };