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");
        }
 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());
 }
        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;
            InvalidValue[] invalidValues = validator.GetInvalidValues(b);
            Assert.Greater(invalidValues.Length, 0);
            Assert.AreEqual("{notpresent.Key} and #{key.notPresent} and {key.notPresent2} 1", invalidValues[0].Message,
                            "Missing key should be left unchanged");
        }
예제 #4
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");
        }
 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");
 }
        public void MessageNull()
        {
            IClassValidator vtor = new ClassValidator(typeof(Foo2));

            Foo2 f = new Foo2();

            vtor.GetInvalidValues(f);
        }
        public void ExceptionMustBeThrown()
        {
            IClassValidator vtor = new ClassValidator(typeof (Foo));

            Foo f = new Foo();

            vtor.GetInvalidValues(f);
        }
 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);
 }
        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 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));
            InvalidValue[] ivalues;
            Composition c = new Composition();

            c.Any = bBroken;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(1, ivalues.Length);

            c.Any = dFullBroken;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(2, ivalues.Length);

            c.Any = bOk;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(0, ivalues.Length);

            c.Any = dPartialBroken;
            ivalues = vtor.GetInvalidValues(c);
            Assert.AreEqual(1, ivalues.Length);
            Assert.AreEqual("A", ivalues[0].PropertyName);
        }
 public void WhenNoTagIsSpecified_ValidateAnything()
 {
     // all propeties are wrong
     IClassValidator cv = new ClassValidator(typeof(Entity));
     cv.GetInvalidValues(new Entity()).Should().Have.Count.EqualTo(4);
 }
 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);
 }
 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);
 }
        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);
        }
        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);
        }
        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);
        }
        public void DoPolimorphismWithClasses()
        {
            DerivatedClass d = new DerivatedClass();
            d.A = "hola";
            d.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(DerivatedClass));
            InvalidValue[] values = vtor.GetInvalidValues(d);
            Assert.AreEqual(2,values.Length);

            ClassValidator vtor2 = new ClassValidator(typeof(BaseClass));
            InvalidValue[] values2 = vtor2.GetInvalidValues(d);
            Assert.AreEqual(1, values2.Length, "Polimorphic support is no working");

            ValidatorEngine ve = new ValidatorEngine();
            values = ve.Validate(d);
            Assert.AreEqual(2, values.Length);
        }
 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);
 }
 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);
 }
 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);
 }
        public void DoPolimorphismWithInterfaces()
        {
            Impl obj = new Impl();
            obj.A = "hola";
            obj.B = "hola";

            ClassValidator vtor = new ClassValidator(typeof(Impl));
            InvalidValue[] values = vtor.GetInvalidValues(obj);
            Assert.AreEqual(2,values.Length);

            ClassValidator vtor2 = new ClassValidator(typeof(IContract));
            InvalidValue[] values2 = vtor2.GetInvalidValues(obj);
            Assert.AreEqual(1, values2.Length, "Polimorphic support is no working");

            ValidatorEngine ve = new ValidatorEngine();
            values = ve.Validate(obj);
            Assert.AreEqual(2, values.Length);
        }
 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);
 }
예제 #23
0
        /// <summary>
        /// Retorna los valores invalidos de una entidad dada
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public IEnumerable<InvalidValue> GetInvalidValues(object entity)
        {
            ClassValidator validator = new ClassValidator(entity.GetType());

            return validator.GetInvalidValues(entity);
        }