Beispiel #1
0
        public void ListIsNullIsValid()
        {
            // passing null (IList) to isvalid should be ok
            ListPropertyMaxLengthAttribute attr = new ListPropertyMaxLengthAttribute(100, "Value", null);

            attr.IsValid(null).Should().BeTrue();
        }
Beispiel #2
0
        public void UsedOnInvalidTypeShouldThrow()
        {
            ListPropertyMaxLengthAttribute attr = new ListPropertyMaxLengthAttribute(5, "Value", null);

            Action act = () => attr.IsValid("NotAnIListType");

            act.ShouldThrow <InvalidOperationException>().WithMessage("Attribute used on a type that doesn't implement IList.");
        }
Beispiel #3
0
        public void InvalidLengthShouldThrow()
        {
            ListPropertyMaxLengthAttribute attr = new ListPropertyMaxLengthAttribute(-100, "Value", null);

            Action act = () => attr.IsValid("some text");

            act.ShouldThrowExactly <InvalidOperationException>("Invalid length value defined (zero or less than -1 (negative one is magic value in .net framework implementation)).");
        }
Beispiel #4
0
        public void ListIsEmptyInvalidLengthShouldThrow()
        {
            ListPropertyMaxLengthAttribute attr = new ListPropertyMaxLengthAttribute(-100, "Value", null);

            Action act = () => attr.IsValid(new List <string>());

            act.ShouldThrowExactly <InvalidOperationException>().WithMessage("ListPropertyMaxLengthAttribute must have a length value that is greater than zero.", "Because length attribute is negative.");
        }
Beispiel #5
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.");
        }
Beispiel #6
0
        public void ListIsEmptyIsValid()
        {
            ListPropertyMaxLengthAttribute attr = new ListPropertyMaxLengthAttribute(100, "Value", null);

            attr.IsValid(new List <string>()).Should().BeTrue();
        }