Пример #1
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");
        }
        protected SyncPropertyValidationBuilder <TObject, TProp> RuleFor <TProp>(Expression <Func <TObject, TProp> > expression)
        {
            var property          = ExtractProperty(expression);
            var propertyValidator = new SyncPropertyValidator <TObject, TProp>(property);

            InnerPropertyValidators.Add(propertyValidator);

            return(new SyncPropertyValidationBuilder <TObject, TProp>(propertyValidator));
        }
Пример #3
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));
        }
Пример #4
0
        public void For_Validate_When_ProvidedObjectCannotBeCastedToTPropType_Then_ExceptionIsThrown()
        {
            // Arrange:
            var memberInfo = PropertyValidatorTestsCommon.CreateTestMemberInfo();
            var validator  = new SyncPropertyValidator <string>(memberInfo);

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

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

            exception.Message.Should().Be("'obj' is not String type");
        }
Пример #5
0
        public SyncPropertyValidator <TProp> Setup <TProp>(Expression <Func <TObject, TProp> > expression)
        {
            var memberInfo = PropertyExtractor.Extract(expression);

            if (PropertyValidators.TryGetValue(memberInfo.Name, out var propertyValidator))
            {
                return((SyncPropertyValidator <TProp>)propertyValidator);
            }

            var newPropertyValidator = new SyncPropertyValidator <TProp>(memberInfo);

            PropertyValidators.Add(memberInfo.Name, newPropertyValidator);

            return(newPropertyValidator);
        }
Пример #6
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");
        }
Пример #7
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");
        }