public async Task For_Validate_When_ThereIsValidationRuleForAsyncRule_Then_ValidationErrorIsReturned()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            Func <string, Task <bool> > validationPredicate = async x =>
            {
                await Task.CompletedTask;
                return(x != "x");
            };
            const string message = "SOME MESSAGE";
            const string code    = "SOME CODE";

            validator.AddRule(validationPredicate, message, code);

            var toValidate = "x";

            // Act:
            var validationErrors = await validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            var validationError = validationErrors.First();

            validationError.ErrorMessage.Should().Be(message);
            validationError.ErrorCode.Should().Be(code);
            validationError.PropertyPath.ToString().Should().Be("SomeProperty");
            validationError.ProvidedValue.Should().Be(toValidate);
        }
        public async Task For_Validate_WhenRuleHasSetToStopValidationAfterFailure_Then_ValidationStopsWhenErrorOccurs()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            Func <string, Task <bool> > validationPredicate = async x =>
            {
                await Task.CompletedTask;
                return(x != "x");
            };
            const string message = "SOME MESSAGE";
            const string code    = "SOME CODE";

            validator.AddRule(validationPredicate, message, code);
            validator.StopValidationAfterFailure();
            validator.AddRule(x => x == null, "must be null", "MUST_BE_NULL");

            var toValidate = "x";

            // Act:
            var validationErrors = await validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(1);
            validationErrors[0].ErrorMessage.Should().Be("SOME MESSAGE");
            validationErrors[0].ErrorCode.Should().Be("SOME CODE");
            validationErrors[0].PropertyPath.ToString().Should().Be("SomeProperty");
            validationErrors[0].ProvidedValue.Should().Be(toValidate);
        }
Ejemplo n.º 3
0
        public void For_Validate_When_ObjectToValidateIsNotValidFor2Rules_Then_ValidationErrorsAreReturned()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new SyncPropertyValidator <string>(memberInfo);

            validator.AddRule(x => x != "x", "cannot be x", "NOT_X");
            validator.AddRule(x => x == null, "must be null", "CANNOT_BE_NULL");
            var toValidate = "x";

            // Act:
            var result = validator.Validate(toValidate);

            // Assert:
            result.Count.Should().Be(2);
            result[0].PropertyPath.IsEmpty.Should().Be(false);
            result[0].ProvidedValue.Should().Be("x");
            result[0].PropertyPath.ToString().Should().Be("SomeProperty");
            result[0].ErrorMessage.Should().Be("cannot be x");
            result[0].ErrorCode.Should().Be("NOT_X");

            result[1].PropertyPath.IsEmpty.Should().Be(false);
            result[1].ProvidedValue.Should().Be("x");
            result[1].PropertyPath.ToString().Should().Be("SomeProperty");
            result[1].ErrorMessage.Should().Be("must be null");
            result[1].ErrorCode.Should().Be("CANNOT_BE_NULL");
        }
        public void For_SetValidatorAsync_When_ProvidedValidatorIsNull_Then_ExceptionIsThrown()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);

            // Act & Assert:
            Assert.Throws <ArgumentNullException>(() => validator.SetValidator(null));
        }
        public void For_ValidateObj_When_ProvidedObjectCanBeCastedToTPropType_Then_ValidationForTPropTypeIsCalled()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            var toValidate = (object)"x";

            // Act & Assert:
            Assert.DoesNotThrowAsync(() => validator.Validate(toValidate));
        }
        public void For_SetValidatorAsync_When_PassedValidatorReferenceConcernsTargetValidator_Then_ExceptionIsThrown()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);

            // Act & Assert:
            var exception = Assert.Throws <ArgumentException>(() => validator.SetValidator(validator));

            exception.Message.Should().Be("Detected circular reference");
        }
        public void For_AddRuleAsync_When_PredicateIsNull_Then_ExceptionIsThrown()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            Func <string, Task <bool> > validationPredicate = null;

            // Act & Assert:
            Assert.Throws <ArgumentNullException>(() =>
                                                  validator.AddRule(validationPredicate, It.IsAny <string>()));
        }
        public void For_ValidateObj_When_ProvidedObjectCannotBeCastedToTPropType_Then_ExceptionIsThrown()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            var toValidate = 10;

            // Act & Assert:
            var exception = Assert.ThrowsAsync <ArgumentException>(() => validator.Validate(toValidate));

            exception.Message.Should().Be("'obj' is not String type");
        }
Ejemplo n.º 9
0
        public void For_ValidateObj_When_ProvidedObjectToValidateIsObjectType_Then_ItIsCastedAndPassedToFurtherValidation()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new SyncPropertyValidator <string>(memberInfo);

            validator.AddRule(x => x != "x", "cannot be x", "NOT_X");
            var toValidate = (object)"x";

            // Act & Assert:
            Assert.DoesNotThrow(() => validator.Validate(toValidate));
        }
        public void For_SetValidatorAsync_When_OtherValidatorIsAlreadySet_Then_ExceptionIsThrown()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);

            validator.SetValidator(Mock.Of <IAsyncValidator <string> >()); // First .SetValidator() call

            // Act & Assert:
            var exception = Assert.Throws <InvalidOperationException>(() =>
                                                                      validator.SetValidator(Mock.Of <IAsyncValidator <string> >()));

            exception.Message.Should().Be("There is already set other validator");
        }
        public void For_AddRuleAsync_When_MessageIsNull_Then_ExceptionIsThrown()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            Func <string, Task <bool> > validationPredicate = async x =>
            {
                await Task.CompletedTask;
                return(x != "x");
            };

            // Act & Assert:
            Assert.Throws <ArgumentNullException>(() =>
                                                  validator.AddRule(validationPredicate, null));
        }
        public void For_SetValidatorAsync_When_ThereIsNoValidationTaskCorrespondingToOtherValidator_Then_NewValidationTaskForValidatorIsAdded()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);

            // Act:
            validator.SetValidator(Mock.Of <IAsyncValidator <string> >());

            // Assert:
            validator.ValidationTasksAsReadonly.Count.Should().Be(1);
            var validationTask = validator.ValidationTasksAsReadonly.First();

            validationTask.IsOtherValidator.Should().Be(true);
            validationTask.Validator.Should().BeAssignableTo <IAsyncValidator <string> >();
        }
        public void For_AddRuleAsync_When_PassedArgumentsAreValid_Then_NewValidationTaskIsAdded()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            Func <string, Task <bool> > validationPredicate = async x =>
            {
                await Task.CompletedTask;
                return(x != "x");
            };
            const string message = "SOME MESSAGE";

            // Act & Assert:
            Assert.DoesNotThrow(() => validator.AddRule(validationPredicate, message));
            validator.ValidationTasksAsReadonly.Count.Should().Be(1);
            var validationTask = validator.ValidationTasksAsReadonly.First();

            validationTask.Validator.Should().Should().NotBeNull();
            validationTask.Validator.GetType().Should().BeAssignableTo <IAsyncValidator>();
        }
Ejemplo n.º 14
0
        public void For_Validate_WhenRuleHasSetToStopValidationAfterFailure_Then_ValidationStopsWhenErrorOccurs()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new SyncPropertyValidator <string>(memberInfo);

            validator.AddRule(x => x != "x", "cannot be x", "NOT_X");
            validator.StopValidationAfterFailure();
            validator.AddRule(x => x == null, "must be null", "CANNOT_BE_NULL");
            var toValidate = "y";

            // Act:
            var result = validator.Validate(toValidate);

            // Assert:
            result[0].PropertyPath.IsEmpty.Should().Be(false);
            result[0].ProvidedValue.Should().Be("y");
            result[0].PropertyPath.ToString().Should().Be("SomeProperty");
            result[0].ErrorMessage.Should().Be("must be null");
            result[0].ErrorCode.Should().Be("CANNOT_BE_NULL");
        }
Ejemplo n.º 15
0
        public void For_Validate_When_PropertyHasSetMessageAndCodeOverrides_Then_AreAppliedToValidationError()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new SyncPropertyValidator <string>(memberInfo);

            validator.AddRule(x => x != "x", "cannot be x", "NOT_X");
            validator.WithMessage("overridden message");
            validator.WithCode("overridden code");
            var toValidate = "x";

            // Act:
            var result = validator.Validate(toValidate);

            // Assert:
            result.Count.Should().Be(1);
            result[0].PropertyPath.IsEmpty.Should().Be(false);
            result[0].ProvidedValue.Should().Be("x");
            result[0].PropertyPath.ToString().Should().Be("SomeProperty");
            result[0].ErrorMessage.Should().Be("overridden message");
            result[0].ErrorCode.Should().Be("overridden code");
        }
        public async Task For_Validate_When_ThereAreOverridesToApplyToValidationErrors_Then_AreAppliedToValidationErrors()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new AsyncPropertyValidator <string>(memberInfo);
            Func <string, Task <bool> > validationPredicate = async x =>
            {
                await Task.CompletedTask;
                return(x != "x");
            };
            const string message = "SOME MESSAGE";
            const string code    = "SOME CODE";

            validator.AddRule(validationPredicate, message, code);
            validator.WithMessage("Cannot be x");
            validator.WithCode("MUST_NOT_BE_X");
            validator.AddRule(x => x == null, "must be null", "MUST_BE_NULL");
            validator.SetPropertyDisplayName("OverridenPropertyDisplayName");

            var toValidate = "x";

            // Act:
            var validationErrors = await validator.Validate(toValidate);

            // Assert:
            validationErrors.Count.Should().Be(2);

            validationErrors[0].ErrorMessage.Should().Be("Cannot be x");
            validationErrors[0].ErrorCode.Should().Be("MUST_NOT_BE_X");
            validationErrors[0].PropertyPath.ToString().Should().Be("OverridenPropertyDisplayName");
            validationErrors[0].ProvidedValue.Should().Be(toValidate);

            validationErrors[1].ErrorMessage.Should().Be("must be null");
            validationErrors[1].ErrorCode.Should().Be("MUST_BE_NULL");
            validationErrors[1].PropertyPath.ToString().Should().Be("OverridenPropertyDisplayName");
            validationErrors[1].ProvidedValue.Should().Be(toValidate);
        }