예제 #1
0
        public void InvalidLengthShouldThrowTestTwo()
        {
            List <SomeDemoObject> inValues = new List <SomeDemoObject>()
            {
                new SomeDemoObject()
                {
                    Integer = 1,
                    String  = "a"
                },
                new SomeDemoObject()
                {
                    Integer = 2,
                    String  = "ab"
                },
                new SomeDemoObject()
                {
                    Integer = 3,
                    String  = "cb"
                }
            };

            MaxLengthTestObject obj = new MaxLengthTestObject();

            obj.InvalidLengthDefinedForList.AddRange(inValues);

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "InvalidLengthDefinedForList";

            Action act = () => Validator.ValidateProperty(obj.InvalidLengthDefinedForList, ctx);

            act.ShouldThrowExactly <InvalidOperationException>("MaxLength is a negative value.");
        }
예제 #2
0
        public void InvalidItemPropertyName()
        {
            List <SomeDemoObject> inValues = new List <SomeDemoObject>()
            {
                new SomeDemoObject()
                {
                    Integer = 1,
                    String  = "a"
                },
                new SomeDemoObject()
                {
                    Integer = 2,
                    String  = "ab"
                },
                new SomeDemoObject()
                {
                    Integer = 3,
                    String  = "cb"
                }
            };

            MaxLengthTestObject obj = new MaxLengthTestObject();

            obj.InvalidPropertyNameList.AddRange(inValues);

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "InvalidPropertyNameList";

            Action act = () => Validator.ValidateProperty(obj.InvalidPropertyNameList, ctx);

            act.ShouldThrowExactly <ValidationException>("Named property not found from item.");
        }
예제 #3
0
        public void NullItemPropertyValueShouldntMakeTheWholeListValid()
        {
            // COMMENT: if there is a null value in the list item property, currently makes the whole list values valid

            List <SomeDemoObject> inValues = new List <SomeDemoObject>()
            {
                new SomeDemoObject()
                {
                    Integer = 1,
                    String  = "a"
                },
                new SomeDemoObject()
                {
                    Integer = 2,
                    String  = "ab"
                },
                new SomeDemoObject()
                {
                    Integer = 3,
                    String  = "cb"
                },
                new SomeDemoObject()
                {
                    Integer = 3,
                    String  = null // this makes whole list valid
                },
                new SomeDemoObject()
                {
                    Integer = 4,
                    String  = "too long string"
                }
            };

            MaxLengthTestObject obj = new MaxLengthTestObject(inValues);

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "ValuesList";

            Action act = () => Validator.ValidateProperty(obj.ValuesList, ctx);

            act.ShouldThrowExactly <ValidationException>().WithMessage("Maximum length of property 'String' must be '2'.", "Null property value in a single item shouldn't make all list items valid.");
        }
예제 #4
0
        public void PublicIsValidAndProtectedIsValidShouldWorkTheSame()
        {
            // COMMENT: background, for example .Net framework MaxLengthAttribute is implemneted in public IsValid method
            // out inherited attribute was implemented in protected IsValid so that means we have two different implementations that work differently
            // public IsValid(object) and protected IsValid(object, validationContext) should result in same

            // public isvalid validates string length or collection count, custom implementation validate list objects string length

            ListPropertyMaxLengthAttribute attr = new ListPropertyMaxLengthAttribute(2, "String");

            List <SomeDemoObject> inValues = new List <SomeDemoObject>()
            {
                new SomeDemoObject()
                {
                    Integer = 1,
                    String  = "a"
                },
                new SomeDemoObject()
                {
                    Integer = 2,
                    String  = "ab"
                },
                new SomeDemoObject()
                {
                    Integer = 3,
                    String  = "cb"
                }
            };

            // this call actually currently checks that there are at maximum 2 items in the list and returns fale because there are 3 items in the list
            bool publicIsValid = attr.IsValid(inValues);

            MaxLengthTestObject obj = new MaxLengthTestObject(inValues);

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "ValuesList";

            List <ValidationResult> validationResults = new List <ValidationResult>();

            Validator.TryValidateProperty(inValues, ctx, validationResults).Should().Be(publicIsValid, "Public and protected IsValid methods should result to the same outcome.");
        }
예제 #5
0
        public void ItemTypeValueNullShouldntThrow()
        {
            // validator tries to validate items which Type property value is: TypeValueTest
            // list items have null value, so these should be skipped by the loop
            // currently causes nullreferenceexception because code tries to do item.Type.ToString() and the Type value is null

            List <SomeDemoObject> inValues = new List <SomeDemoObject>()
            {
                new SomeDemoObject()
                {
                    Integer = 1,
                    String  = "a"
                },
                new SomeDemoObject()
                {
                    Integer = 2,
                    String  = "ab"
                },
                new SomeDemoObject()
                {
                    Integer = 3,
                    String  = "cb"
                }
            };

            MaxLengthTestObject obj = new MaxLengthTestObject();

            obj.ItemTypeValueNullList.AddRange(inValues);

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "ItemTypeValueNullList";

            Action act = () => Validator.ValidateProperty(obj.ItemTypeValueNullList, ctx);

            act.ShouldNotThrow();
        }
예제 #6
0
        public void InvalidValueInValuesList()
        {
            List <SomeDemoObject> inValues = new List <SomeDemoObject>()
            {
                new SomeDemoObject()
                {
                    Integer = 1,
                    String  = "a"
                },
                new SomeDemoObject()
                {
                    Integer = 2,
                    String  = "ab"
                },
                new SomeDemoObject()
                {
                    Integer = 3,
                    String  = "cb"
                },
                new SomeDemoObject()
                {
                    Integer = 4,
                    String  = "too long string"
                }
            };

            MaxLengthTestObject obj = new MaxLengthTestObject(inValues);

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "ValuesList";

            Action act = () => Validator.ValidateProperty(obj.ValuesList, ctx);

            act.ShouldThrowExactly <ValidationException>().WithMessage("Maximum length of property 'String' must be '2'.");
        }