Exemplo n.º 1
0
        public void PropertyName_ThrowsArgumentNullException_WhenThisArgIsNull()
        {
            FakeStringLengthCommand owner = null;
            // ReSharper disable ExpressionIsAlwaysNull
            var exception = Assert.Throws <ArgumentNullException>(() => owner.PropertyName(x => x.StringProperty));

            // ReSharper restore ExpressionIsAlwaysNull
            exception.ShouldNotBeNull();
            exception.ParamName.ShouldEqual("owner");
        }
Exemplo n.º 2
0
        public void IsValid_WhenString_IsNull()
        {
            var command = new FakeStringLengthCommand { StringProperty = null };
            var validator = new FakeStringMaxLengthValidator(5);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            Func<ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);
            result.Errors.Any(maxLengthError).ShouldBeFalse();
            validator.ShouldNotHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
Exemplo n.º 3
0
        public void IsValid_WhenString_IsNull()
        {
            var command = new FakeStringLengthCommand {
                StringProperty = null
            };
            var validator = new FakeStringMaxLengthValidator(5);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            Func <ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);

            result.Errors.Any(maxLengthError).ShouldBeFalse();
            validator.ShouldNotHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
Exemplo n.º 4
0
        public void IsValid_WhenStringLength_IsLessThanMaxLength()
        {
            var command = new FakeStringLengthCommand {
                StringProperty = Guid.NewGuid().ToString()
            };
            var validator = new FakeStringMaxLengthValidator(command.StringProperty.Length + 1);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeTrue();
            Func <ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);

            result.Errors.Any(maxLengthError).ShouldBeFalse();
            validator.ShouldNotHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
Exemplo n.º 5
0
        public void IsInvalid_WhenStringLength_IsGreaterThanMaxLength()
        {
            var command = new FakeStringLengthCommand { StringProperty = "asdf" };
            var validator = new FakeStringMaxLengthValidator(3);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func<ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);
            result.Errors.Count(maxLengthError).ShouldEqual(1);
            result.Errors.Single(maxLengthError).ErrorMessage.ShouldEqual(Resources.Validation_MaxLength
                .Replace("{PropertyName}", "String Property")
                .Replace("{MaxLength}", "3")
                .Replace("{TotalLength}", command.StringProperty.Length.ToString(CultureInfo.InvariantCulture))
            );
            validator.ShouldHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
Exemplo n.º 6
0
        public void IsInvalid_WhenStringLength_IsGreaterThanMaxLength()
        {
            var command = new FakeStringLengthCommand {
                StringProperty = "asdf"
            };
            var validator = new FakeStringMaxLengthValidator(3);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> maxLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);

            result.Errors.Count(maxLengthError).ShouldEqual(1);
            result.Errors.Single(maxLengthError).ErrorMessage.ShouldEqual(Resources.Validation_MaxLength
                                                                          .Replace("{PropertyName}", "String Property")
                                                                          .Replace("{MaxLength}", "3")
                                                                          .Replace("{TotalLength}", command.StringProperty.Length.ToString(CultureInfo.InvariantCulture))
                                                                          );
            validator.ShouldHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
Exemplo n.º 7
0
        public void IsInvalid_WhenStringLength_IsLessThanMinLength(string value, int minLength)
        {
            var command = new FakeStringLengthCommand { StringProperty = value };
            var validator = new FakeStringMinLengthValidator(minLength);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func<ValidationFailure, bool> minLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);
            result.Errors.Count(minLengthError).ShouldEqual(1);
            result.Errors.Single(minLengthError).ErrorMessage.ShouldEqual(Resources.Validation_MinLength
                .Replace("{PropertyName}", "String Property")
                .Replace("{MinLength}", minLength.ToString(CultureInfo.InvariantCulture))
                .Replace("{TotalLength}", command.StringProperty.Length.ToString(CultureInfo.InvariantCulture))
                .Replace("{Characters}", value.Length == 1
                    ? Resources.Validation_CharacterLower
                    : Resources.Validation_CharactersLower)
            );
            validator.ShouldHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }
Exemplo n.º 8
0
        public void IsInvalid_WhenStringLength_IsLessThanMinLength(string value, int minLength)
        {
            var command = new FakeStringLengthCommand {
                StringProperty = value
            };
            var validator = new FakeStringMinLengthValidator(minLength);

            var result = validator.Validate(command);

            result.IsValid.ShouldBeFalse();
            Func <ValidationFailure, bool> minLengthError = x => x.PropertyName == command.PropertyName(y => y.StringProperty);

            result.Errors.Count(minLengthError).ShouldEqual(1);
            result.Errors.Single(minLengthError).ErrorMessage.ShouldEqual(Resources.Validation_MinLength
                                                                          .Replace("{PropertyName}", "String Property")
                                                                          .Replace("{MinLength}", minLength.ToString(CultureInfo.InvariantCulture))
                                                                          .Replace("{TotalLength}", command.StringProperty.Length.ToString(CultureInfo.InvariantCulture))
                                                                          .Replace("{Characters}", value.Length == 1
                    ? Resources.Validation_CharacterLower
                    : Resources.Validation_CharactersLower)
                                                                          );
            validator.ShouldHaveValidationErrorFor(x => x.StringProperty, command.StringProperty);
        }