Exemplo n.º 1
0
        public void EmptyListIsValidProtectedIsValid()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "DemoStrings";

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

            Validator.TryValidateProperty(obj.DemoStrings, ctx, validationResults).Should().BeTrue();
        }
Exemplo n.º 2
0
        public void EmptyPatternShouldThrow()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            obj.EmptyRegExPattern.Add("Something");

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "EmptyRegExPattern";

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

            act.ShouldThrow <InvalidOperationException>("RegEx pattern cannot be empty string.");
        }
Exemplo n.º 3
0
        public void InvalidPatternShouldThrow()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            obj.InvalidRegExPattern.Add("Something");

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "InvalidRegExPattern";

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

            act.ShouldThrowExactly <ArgumentException>("Because regex pattern is invalid.");
        }
Exemplo n.º 4
0
        public void ShouldThrowInvalidPropertyTypeUsage()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            // this needs to be different from null so that the TryValidateProperty doesn't throw ArgumentNullException
            obj.InvalidPropertyUsage = "some string";

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "InvalidPropertyUsage";

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

            act.ShouldThrow <InvalidOperationException>("Attribute used on a property that doesn't implement IList.");
        }
Exemplo n.º 5
0
        public void PublicIsValidAndProtectedIsValidShouldUseSameImplementationEmptyList()
        {
            ListRegularExpressionAttribute attr = new ListRegularExpressionAttribute(@"^\d+$");
            // actually currently what happens is that list is converted to string and it doesn't match the numeric string pattern
            bool publicIsValid = attr.IsValid(new List <string>());

            RegExpDemoObject obj = new RegExpDemoObject();

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "DemoStrings";

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

            Validator.TryValidateProperty(obj.DemoStrings, ctx, validationResults).Should().Be(publicIsValid);
        }
Exemplo n.º 6
0
        public void ListContainsValidNumericStrings()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            obj.DemoStrings.Add("555");
            obj.DemoStrings.Add("1");
            obj.DemoStrings.Add("2017");

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "DemoStrings";

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

            Validator.TryValidateProperty(obj.DemoStrings, ctx, validationResults).Should().BeTrue();
        }
Exemplo n.º 7
0
        public void ListContainsValidThreeLetterStrings()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            obj.ThreeLetterItems = new List <string>()
            {
                "ana",
                "ptv",
                "vrk"
            };

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "ThreeLetterItems";

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

            Validator.TryValidateProperty(obj.ThreeLetterItems, ctx, validationResults).Should().BeTrue();
        }
Exemplo n.º 8
0
        public void ListContainsThreeLetterStringsAndNullShouldntCauseInternalCrash()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            obj.ThreeLetterItems = new List <string>()
            {
                "ana",
                "ptv",
                "vrk",
                null
            };

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "ThreeLetterItems";

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

            act.ShouldNotThrow <NullReferenceException>("Code shouldn't crash to the null value but check for nulls.");
        }
Exemplo n.º 9
0
        public void ListContainsInvalidThreeLetterStrings()
        {
            RegExpDemoObject obj = new RegExpDemoObject();

            obj.ThreeLetterItems = new List <string>()
            {
                "ana",
                "ptv",
                "vrk",
                "IS"
            };

            ValidationContext ctx = new ValidationContext(obj);

            ctx.MemberName = "ThreeLetterItems";

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

            Validator.TryValidateProperty(obj.ThreeLetterItems, ctx, validationResults).Should().BeFalse("Because 'IS' is not a three letter string.");
        }