Example #1
0
        public void Should_CreateValidator_ReturnValidatorInitializedWithSpecificationAndSettings()
        {
            var holderInfo = new HolderInfo(typeof(ObjectSpecificationAndSettingsHolder), typeof(CustomClass));

            var createdValidator = holderInfo.CreateValidator();

            createdValidator.Should().NotBeNull();
            createdValidator.Should().BeOfType <Validator <CustomClass> >();

            var validator = (Validator <CustomClass>)createdValidator;

            var case1 = new CustomClass()
            {
                CustomValue = "@b"
            };

            var case2 = new CustomClass()
            {
                CustomValue = "abcdef"
            };

            var case3 = new CustomClass()
            {
                CustomValue = "@bcdef"
            };

            var case4 = new CustomClass()
            {
                CustomValue = "a"
            };

            validator.Validate(case1).ToString().ShouldResultToStringHaveLines(
                ToStringContentType.Messages,
                "CustomValue: Must not contain @");

            validator.Validate(case2).ToString().ShouldResultToStringHaveLines(
                ToStringContentType.Messages,
                "CustomValue: MUST HAVE MAX 3 CHARACTERS");

            validator.Validate(case3).ToString().ShouldResultToStringHaveLines(
                ToStringContentType.Messages,
                "CustomValue: MUST HAVE MAX 3 CHARACTERS",
                "CustomValue: Must not contain @");

            validator.IsValid(case4).Should().BeTrue();
        }
Example #2
0
        public void Should_CreateValidator_ReturnValidatorInitializedWithSpecificationAndSettings_When_HolderHoldsMultipleSpecifications()
        {
            var intHolderInfo = new HolderInfo(typeof(MultipleSpecificationAndSettingsHolder), typeof(int));

            var createdIntValidator = intHolderInfo.CreateValidator();

            createdIntValidator.Should().NotBeNull();
            createdIntValidator.Should().BeOfType <Validator <int> >();

            var intValidator = (Validator <int>)createdIntValidator;

            intValidator.IsValid(5).Should().BeTrue();
            intValidator.IsValid(9).Should().BeTrue();

            intValidator.Validate(0).ToString().ShouldResultToStringHaveLines(
                ToStringContentType.Messages,
                "Only values 1-10 are valid");

            intValidator.Validate(11).ToString().ShouldResultToStringHaveLines(
                ToStringContentType.Messages,
                "Only values 1-10 are valid");

            var stringHolderInfo = new HolderInfo(typeof(MultipleSpecificationAndSettingsHolder), typeof(string));

            var createdStringValidator = stringHolderInfo.CreateValidator();

            createdStringValidator.Should().NotBeNull();
            createdStringValidator.Should().BeOfType <Validator <string> >();

            var stringValidator = (Validator <string>)createdStringValidator;

            stringValidator.IsValid("abc").Should().BeTrue();
            stringValidator.IsValid("abcdefgh").Should().BeTrue();

            stringValidator.Validate("").ToString().ShouldResultToStringHaveLines(
                ToStringContentType.Messages,
                "Only values with length 1-10 are valid");

            stringValidator.Validate("01234567890987654321").ToString().ShouldResultToStringHaveLines(
                ToStringContentType.Messages,
                "Only values with length 1-10 are valid");
        }