Ejemplo n.º 1
0
        public void ProofFor_NHV48()
        {
            IClassValidator cv = new ClassValidator(typeof(Book));

            cv.GetInvalidValues(new Book(), BTags.Draft).Should().Have.Count.EqualTo(1);
            cv.GetInvalidValues(new Book(), BTags.Publish).Should().Have.Count.EqualTo(2);
        }
Ejemplo n.º 2
0
        public void WhenTagIsSpecified_MatchTagsContainOnlyMatchs()
        {
            // only property with 'typeof(Error)' as tag
            IClassValidator cv            = new ClassValidator(typeof(aEntity));
            var             invalidValues = cv.GetInvalidValues(new aEntity(), new[] { "information" }).ToArray();

            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("information");

            invalidValues = cv.GetInvalidValues(new aEntity {
                ValueMinMax = 101
            }, new[] { "information", "warning" }).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("warning");
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning",
                                                                                                          "information");

            invalidValues = cv.GetInvalidValues(new aEntity(), new[] { "error", "warning" }).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error");
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning");

            invalidValues = cv.GetInvalidValues(new aEntity {
                ValueMinMax = 120
            }, new[] { "error", "warning" }).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error", "warning");
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning");

            invalidValues = cv.GetInvalidValues(new aEntity(), new[] { "error", null }).ToArray();
            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error");
            invalidValues.First(iv => iv.PropertyName == "ValueWithoutTags").MatchTags.Should().Have.Count.EqualTo(0);
        }
        public void AllowMoreThanOne()
        {
            var e = new WithRegEx {
                Value = "aaa"
            };
            var ca = new ClassValidator(typeof(WithRegEx));
            var iv = ca.GetInvalidValues(e).ToArray();

            ActionAssert.NotThrow(() => iv = ca.GetInvalidValues(e).ToArray());
        }
Ejemplo n.º 4
0
        public void WhenTagIsSpecified_ValidateRelationForGivenTags()
        {
            // specifying [Valid] the relation is always analized
            IClassValidator cv = new ClassValidator(typeof(EntityWithReletion));

            cv.GetInvalidValues(new EntityWithReletion {
                Reference = new Entity()
            }, typeof(Error)).Should().Have.Count.EqualTo(1);
            cv.GetInvalidValues(new EntityWithReletion {
                Reference = new Entity()
            }, typeof(Error), null).Should().Have.Count.EqualTo(2);
        }
Ejemplo n.º 5
0
        public void WhenTagsIncludeNull_ValidateOnlyWithTagAndWithNoTags()
        {
            // only properties with 'typeof(Error)' as tag and with no tag (prop 'ValueWithoutTags')
            IClassValidator cv = new ClassValidator(typeof(Entity));

            cv.GetInvalidValues(new Entity(), typeof(Error), null).Should().Have.Count.EqualTo(2);
        }
Ejemplo n.º 6
0
        public void WhenTagIsSpecified_ValidateOnlyWithTag()
        {
            // only property with 'typeof(Error)' as tag
            IClassValidator cv = new ClassValidator(typeof(Entity));

            cv.GetInvalidValues(new Entity(), typeof(Error)).Should().Have.Count.EqualTo(1);
        }
Ejemplo n.º 7
0
        public void WhenNoTagIsSpecified_ValidateAnything()
        {
            // all propeties are wrong
            IClassValidator cv = new ClassValidator(typeof(Entity));

            cv.GetInvalidValues(new Entity()).Should().Have.Count.EqualTo(4);
        }
Ejemplo n.º 8
0
        public void WhenTagIsSpecified_ValidateCollectionForGivenTags()
        {
            // specifying [Valid] the collection is always analized
            IClassValidator cv = new ClassValidator(typeof(EntityWithCollection));

            cv.GetInvalidValues(new EntityWithCollection {
                Entities = new List <Entity> {
                    new Entity(), new Entity()
                }
            }, typeof(Error)).Should().Have.Count.EqualTo(2);
            cv.GetInvalidValues(new EntityWithCollection {
                Entities = new List <Entity> {
                    new Entity(), new Entity()
                }
            }, typeof(Error), null).Should().Have.Count.EqualTo(4);
        }
Ejemplo n.º 9
0
        public void WhenTagsIncludeOnlyNull_ValidateOnlyWithNoTags()
        {
            // only property 'ValueWithoutTags'
            IClassValidator cv = new ClassValidator(typeof(Entity));

            cv.GetInvalidValues(new Entity(), new object[] { null }).Should().Have.Count.EqualTo(1);
        }
Ejemplo n.º 10
0
        public void MissingKey()
        {
            Building b = new Building();

            b.Address = "2323 Younge St";
            ClassValidator validator = new ClassValidator(typeof(Building));

            validator.GetInvalidValues(b);                     // message should be interpolated lazily in DefaultMessageInterpolator

            b         = new Building();
            b.Address = string.Empty;
            var invalidValues = validator.GetInvalidValues(b);

            invalidValues.Should().Not.Be.Empty();
            invalidValues.Select(iv => iv.Message).Should("Missing key should be left unchanged").Contain(
                "{notpresent.Key} and {key.notPresent} and {key.notPresent2} 1");
        }
Ejemplo n.º 11
0
        public void ShouldUseTheValidatorForTheProperty()
        {
            var cv = new ClassValidator(typeof(ObjWithPropertyValidator));
            var iv = cv.GetInvalidValues(new ObjWithPropertyValidator()).ToArray();

            iv.Should().Not.Be.Empty();
            iv.Single().Message.Should().Be.EqualTo("something for prop");
        }
Ejemplo n.º 12
0
        public void WhenNoTagIsSpecified_AllTagsMatch()
        {
            // all propeties are wrong
            IClassValidator cv            = new ClassValidator(typeof(aEntity));
            var             invalidValues = cv.GetInvalidValues(new aEntity {
                ValueMinMax = 101
            }).ToArray();

            invalidValues.First(iv => iv.PropertyName == "ValueMinMax").MatchTags.Should().Have.SameValuesAs("error", "warning");
            invalidValues.First(iv => iv.PropertyName == "ValueMin").MatchTags.Should().Have.SameValuesAs("warning", "information");
            invalidValues.First(iv => iv.PropertyName == "ValueWithoutTags").MatchTags.Should().Have.Count.EqualTo(0);
        }
Ejemplo n.º 13
0
        public void Coposition()
        {
            DerivatedClass dFullBroken = new DerivatedClass();

            dFullBroken.A = "1234";
            dFullBroken.B = "1234";

            DerivatedClass dPartialBroken = new DerivatedClass();

            dPartialBroken.A = "1234";

            BaseClass bOk = new BaseClass();

            bOk.A = "123";

            BaseClass bBroken = new BaseClass();

            bBroken.A = "1234";

            ClassValidator vtor = new ClassValidator(typeof(Composition));
            Composition    c    = new Composition();

            c.Any = bBroken;
            vtor.GetInvalidValues(c).Should().Have.Count.EqualTo(1);

            c.Any = dFullBroken;
            vtor.GetInvalidValues(c).Should().Have.Count.EqualTo(2);

            c.Any = bOk;
            vtor.GetInvalidValues(c).Should().Be.Empty();

            c.Any = dPartialBroken;
            var ivalue = vtor.GetInvalidValues(c).ToArray();

            ivalue.Should().Not.Be.Empty();
            ivalue.Single().PropertyName.Should().Be.EqualTo("A");
        }
Ejemplo n.º 14
0
        public void UseOnlySpecificTaggedValidators()
        {
            IClassValidator cv = new ClassValidator(typeof(Entity));

            cv.GetInvalidValues(new Entity {
                ValueUsingTypes = 5
            }, "ValueUsingTypes", typeof(Warning)).Should().Have.Count.
            EqualTo(0);
            cv.GetInvalidValues(new Entity {
                ValueUsingTypes = 101
            }, "ValueUsingTypes", typeof(Warning)).Should().Have.Count.
            EqualTo(1);
            cv.GetInvalidValues(new Entity {
                ValueUsingTypes = 5
            }, "ValueUsingTypes", typeof(Error)).Should().Have.Count.
            EqualTo(1);
            cv.GetInvalidValues(new Entity {
                ValueUsingTypes = 101
            }, "ValueUsingTypes", typeof(Error)).Should().Have.Count.
            EqualTo(1);

            // Mixing
            cv.GetInvalidValues(new Entity(), typeof(Error), "error").Should().Have.Count.EqualTo(2);
        }
Ejemplo n.º 15
0
        public void DoPolimorphismWithClasses()
        {
            DerivatedClass d = new DerivatedClass();

            d.A = "hola";
            d.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(DerivatedClass));

            vtor.GetInvalidValues(d).Should().Have.Count.EqualTo(2);

            ClassValidator vtor2 = new ClassValidator(typeof(BaseClass));

            vtor2.GetInvalidValues(d).Should("Polimorphic support is no working").Have.Count.EqualTo(1);

            ValidatorEngine ve = new ValidatorEngine();

            ve.Validate(d).Should().Have.Count.EqualTo(2);
        }
Ejemplo n.º 16
0
        public void DoPolimorphismWithInterfaces()
        {
            Impl obj = new Impl();

            obj.A = "hola";
            obj.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(Impl));

            vtor.GetInvalidValues(obj).Should().Have.Count.EqualTo(2);

            ClassValidator vtor2 = new ClassValidator(typeof(IContract));

            vtor2.GetInvalidValues(obj).Should("Polimorphic support is no working").Have.Count.EqualTo(1);

            ValidatorEngine ve = new ValidatorEngine();

            ve.Validate(obj).Should().Have.Count.EqualTo(2);
        }
Ejemplo n.º 17
0
        private static void RunSerializationTest(IClassValidator cv)
        {
            Address a = new Address();

            Address.blacklistedZipCode = null;
            a.Country = "Australia";
            a.Zip     = "1221341234123";
            a.State   = "Vic";
            a.Line1   = "Karbarook Ave";
            a.Id      = 3;
            var validationMessages = cv.GetInvalidValues(a);

            // All work before serialization
            validationMessages.Should().Have.Count.EqualTo(2);             //static field is tested also
            validationMessages.Select(iv => iv.Message).Satisfy(vm => vm.All(m => m.StartsWith("prefix_")));

            // Serialize and deserialize
            ClassValidator cvAfter = (ClassValidator)SerializationHelper.Deserialize(SerializationHelper.Serialize(cv));

            validationMessages = cvAfter.GetInvalidValues(a);
            // Now test after
            validationMessages.Should().Have.Count.EqualTo(2);             //static field is tested also
            validationMessages.Select(iv => iv.Message).Satisfy(vm => vm.All(m => m.StartsWith("prefix_")));
        }
Ejemplo n.º 18
0
        private static void RunSerializationTest(IClassValidator cv)
        {
            Address a = new Address();

            Address.blacklistedZipCode = null;
            a.Country = "Australia";
            a.Zip     = "1221341234123";
            a.State   = "Vic";
            a.Line1   = "Karbarook Ave";
            a.Id      = 3;
            var validationMessages = cv.GetInvalidValues(a).ToArray();

            // All work before serialization
            Assert.That(validationMessages, Has.Length.EqualTo(2));             //static field is tested also
            Assert.That(validationMessages, Has.All.Message.StartsWith("prefix_"));

            // Serialize and deserialize
            ClassValidator cvAfter = (ClassValidator)SerializationHelper.Deserialize(SerializationHelper.Serialize(cv));

            validationMessages = cvAfter.GetInvalidValues(a).ToArray();
            // Now test after
            Assert.That(validationMessages, Has.Length.EqualTo(2));             //static field is tested also
            Assert.That(validationMessages, Has.All.Message.StartsWith("prefix_"));
        }