public void TestMessageInheritedClass3()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <ClassA>()
            .Setup(m => m.A)
            .MustBeLessThanOrEqualTo(1)
            .WithMessage("ClassA validation for A");

            builder.For <ClassB>()
            .Setup(m => m.A)
            .WithMessage("ClassB validation for A");

            builder.For <ClassC>()
            .Setup(m => m.A)
            .WithMessage("ClassC validation for A.1")
            .MustBeLessThan(-1)
            .WithMessage("ClassC validation for A.2");

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj    = new ClassC(2, 0, 0);

            report.Validate(obj);
            //m=>m.A will hit A.1 because the rules for base types are executed first. m=>m.A will fail because of rule defined on ClassA!.
            Assert.AreEqual("ClassC validation for A.1", report.GetErrorMessage(obj, o => o.A));

            obj = new ClassC(1, 0, 0);
            report.Validate(obj);
            //m=>m.A will hit A.2 because the rules for base types are fine. m=>m.A will fail because of rule defined on ClassC!.
            Assert.AreEqual("ClassC validation for A.2", report.GetErrorMessage(obj, o => o.A));
        }
Beispiel #2
0
        public void ShouldFindErrorMessage2()
        {
            var engine = new Engine();

            engine.For <CompositeClass>()
            .Setup(c => c.Value)
            .MustBeGreaterThan(0)
            .Setup(c => c.C)
            .MustNotBeNull()
            .Setup(c => c.C.C)
            .WithMessage("Composed value is not valid")
            .MustBeBetween(1, 10, Rules.BetweenRuleBoundsOption.BothInclusive)
            ;

            var o = new CompositeClass()
            {
                C = new ClassC()
            };
            var r = new ValidationReport(engine);

            r.Validate(o);
            Assert.AreEqual("Composed value is not valid", r.GetErrorMessage(o, f => f.C.C));
            //NOTE: There are no errors on ClassC! Should return null.
            Assert.IsNull(r.GetErrorMessage(o.C, f => f.C));
        }
Beispiel #3
0
        public void ShouldFindErrorMessage()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <ClassC>()
            .Setup(c => c.C)
            .WithMessage("C is not valid")
            .MustBeBetween(1, 10, Rules.BetweenRuleBoundsOption.BothInclusive);
            builder.For <CompositeClass>()
            .Setup(c => c.Value)
            .MustBeGreaterThan(0)
            .Setup(c => c.C)
            .CallValidate()
            .WithMessage("Composed value is not valid");

            var engine = builder.Build();

            var o = new CompositeClass()
            {
                C = new ClassC()
            };
            var r = new ValidationReport(engine);

            r.Validate(o);
            Assert.AreEqual("Composed value is not valid", r.GetErrorMessage(o, f => f.C));
            Assert.AreEqual("C is not valid", r.GetErrorMessage(o.C, f => f.C));
        }
Beispiel #4
0
        public void ShouldFindErrorMessageWithElse()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <ClassC>()
            .If(c => c.C > 10)
            .Setup(c => c.C)
            .WithMessage("C is not valid. >10")
            .MustEqual(-1)
            .Else()
            .Setup(c => c.C)
            .WithMessage("C is not valid. <10")
            .MustEqual(-1)
            .EndIf();

            var engine = builder.Build();
            var o      = new ClassC()
            {
                C = 5
            };
            var r = new ValidationReport(engine);

            r.Validate(o);
            Assert.AreEqual("C is not valid. <10", r.GetErrorMessage(o, f => f.C));
        }
Beispiel #5
0
        public void ShouldFindErrorMessageWithNestedIF_Else()
        {
            var engine = new Engine();

            engine.For <ClassC>()
            .If(c => c.C > 1)
            .If(c => c.C > 2)
            .If(c => c.C > 5)
            .Setup(c => c.C)
            .WithMessage("C is not valid. >5")
            .MustEqual(18)
            .Else()
            .Setup(c => c.C)
            .WithMessage("C is not valid. <5")
            .MustEqual(1);


            var o = new ClassC()
            {
                C = 4
            };
            var r = new ValidationReport(engine);

            r.Validate(o);
            Assert.AreEqual("C is not valid. <5", r.GetErrorMessage(o, f => f.C));
        }
        public void TestMessageInheritedClass2()
        {
            //NOTE: Same as TestMessageInheritedClass, with order of registrations changed.

            var builder = new Fluent.FluentBuilder();

            builder.For <ClassC>()
            .Setup(m => m.A)
            .WithMessage("ClassC validation for A");

            builder.For <ClassB>()
            .Setup(m => m.A)
            .WithMessage("ClassB validation for A");

            builder.For <ClassA>()
            .Setup(m => m.A)
            .MustBeLessThanOrEqualTo(1)
            .WithMessage("ClassA validation for A");

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj    = new ClassC(2, 0, 0);

            report.Validate(obj);
            Assert.AreEqual("ClassC validation for A", report.GetErrorMessage(obj, o => o.A));
        }
        public void TestMessageInheritedClass4()
        {
            //NOTE: This test is the same as No.3, with definition of rules order changed.
            var builder = new Fluent.FluentBuilder();

            builder.For <ClassA>()
            .Setup(m => m.A)
            .MustBeLessThanOrEqualTo(1)
            .WithMessage("ClassA validation for A");

            builder.For <ClassB>()
            .Setup(m => m.A)
            .WithMessage("ClassB validation for A");

            builder.For <ClassC>()
            .Setup(m => m.A)
            .WithMessage("ClassC validation for A.1")
            .MustBeLessThan(-1)
            .WithMessage("ClassC validation for A.2");


            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj    = new ClassC(2, 0, 0);

            report.Validate(obj);
            //m=>m.A will hit A.1 because the rules for base types are executed first. m=>m.A will fail because of rule defined on ClassA!.
            Assert.AreEqual("ClassC validation for A.1", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj    = new ClassC(1, 0, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            //m=>m.A will hit A.2 because the rules for base types are fine. m=>m.A will fail because of rule defined on ClassC!.
            Assert.AreEqual("ClassC validation for A.2", report.GetErrorMessage(obj, o => o.A));
        }
Beispiel #8
0
        public void TestMessageInheritedClass6()
        {
            Engine engine = new Engine();

            engine.For <ClassA>()
            .Setup(m => m.A)
            .MustBeLessThanOrEqualTo(1)
            .WithMessage("ClassA validation for A");

            engine.For <ClassB>()
            .Setup(m => m.A)
            .WithMessage("ClassB validation for A");

            var report = new ValidationReport(engine);
            var obj    = new ClassB(2, 0);

            report.Validate(obj);
            Assert.AreEqual("ClassB validation for A", report.GetErrorMessage(obj, o => o.A));
            Assert.AreEqual("ClassB validation for A", report.GetErrorMessage((ClassA)obj, o => o.A));
        }
        public void TestMessageInheritedClass5()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <ClassA>()
            .Setup(m => m.A)
            .MustBeLessThanOrEqualTo(1)
            .WithMessage("ClassA validation for A");

            builder.For <ClassB>()
            .Setup(m => m.B)
            .WithMessage("ClassB validation for B");

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj    = new ClassB(2, 0);

            report.Validate(obj);
            Assert.AreEqual("ClassA validation for A", report.GetErrorMessage(obj, o => o.A));
            Assert.AreEqual("ClassA validation for A", report.GetErrorMessage((ClassA)obj, o => o.A));
        }
        public void ShouldFindErrorMessage2()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For<CompositeClass>()
                    .Setup(c => c.Value)
                        .MustBeGreaterThan(0)
                    .Setup(c => c.C)
                        .MustNotBeNull()
                    .Setup(c => c.C.C)
                        .WithMessage("Composed value is not valid")
                        .MustBeBetween(1, 10, Rules.BetweenRuleBoundsOption.BothInclusive)
                        ;
            var engine = builder.Build();
            var o = new CompositeClass() { C = new ClassC() };
            var r = new ValidationReport(engine);
            r.Validate(o);
            Assert.AreEqual("Composed value is not valid", r.GetErrorMessage(o.C, f => f.C));
            //NOTE: There are no errors on ClassC! Should return null.
            Assert.IsNull(r.GetErrorMessage(o, f => f.C.C));

        }
Beispiel #11
0
        public void ShouldFindErrorMessageWithIF()
        {
            var engine = new Engine();

            engine.For <ClassC>()
            .If(c => c.C > 10)
            .Setup(c => c.C)
            .WithMessage("C is not valid. >10")
            .MustBeBetween(10, 15)
            .EndIf();

            var o = new ClassC()
            {
                C = 20
            };
            var r = new ValidationReport(engine);

            r.Validate(o);
            Assert.AreEqual("C is not valid. >10", r.GetErrorMessage(o, f => f.C));
        }
Beispiel #12
0
        public void ShouldFindErrorMessage3()
        {
            var engine = new Engine();

            engine.For <ClassC>().WithMessage("ClassC is not valid")
            .Setup(c => c.C)
            .MustBeBetween(1, 10);

            engine.For <CompositeClass>()
            .Setup(c => c.C)
            .CallValidate();

            var o = new CompositeClass()
            {
                C = new ClassC()
            };
            var r = new ValidationReport(engine);

            r.Validate(o);
            Assert.AreEqual("ClassC is not valid", r.GetErrorMessage(o, f => f.C));
        }
Beispiel #13
0
        public void ShouldFindErrorMessageWithNestedIF()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <ClassC>()
            .If(c => c.C > 10)
            .If(c => c.C > 11)
            .If(c => c.C > 12)
            .Setup(c => c.C)
            .WithMessage("C is not valid. >12")
            .MustBeBetween(10, 15);

            var engine = builder.Build();
            var o      = new ClassC()
            {
                C = 20
            };
            var r = new ValidationReport(engine);

            r.Validate(o);
            Assert.AreEqual("C is not valid. >12", r.GetErrorMessage(o, f => f.C));
        }
Beispiel #14
0
        public void TestMessageForProperty()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <MyMessageTestClass>()
            .WithMessage("Object is not valid")
            .Setup(m => m.A)
            .WithMessage("Property A is not valid.")
            .MustBeLessThanOrEqualTo(10)
            .MustNotEqual(5)
            .MustNotEqual(0)
            .WithMessage("Must not equal zero")
            .Setup(m => m.B)
            .MustEqual(0);


            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj    = new MyMessageTestClass(11, 0);

            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Property A is not valid.", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj    = new MyMessageTestClass(5, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Property A is not valid.", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj    = new MyMessageTestClass(0, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Must not equal zero", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj    = new MyMessageTestClass(9, 1);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Object is not valid", report.GetErrorMessage(obj, o => o.B));
        }
        public void TestMessageForProperty()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<MyMessageTestClass>()
                .WithMessage("Object is not valid")
                .Setup(m => m.A)
                    .WithMessage("Property A is not valid.")
                    .MustBeLessThanOrEqualTo(10)
                    .MustNotEqual(5)
                    .MustNotEqual(0)
                    .WithMessage("Must not equal zero")
                .Setup(m => m.B)
                    .MustEqual(0);


            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj = new MyMessageTestClass(11, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Property A is not valid.", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj = new MyMessageTestClass(5, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Property A is not valid.", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj = new MyMessageTestClass(0, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Must not equal zero", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj = new MyMessageTestClass(9, 1);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            Assert.AreEqual("Object is not valid", report.GetErrorMessage(obj, o => o.B));
        }
Beispiel #16
0
 public string GetErrorMessage <T, R>(T value, System.Linq.Expressions.Expression <Func <T, R> > expression, IErrorResolver resolver)
 {
     return(_innerReport.GetErrorMessage <T, R>(value, expression, resolver));
 }
        public void ShouldFindErrorMessage4()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<ClassC>()
                    .Setup(c => c.C)
                        .WithMessage("C is not valid")
                        .MustBeBetween(1, 10, Rules.BetweenRuleBoundsOption.BothInclusive);
            builder.For<CompositeClass>()
                    .Setup(c => c.Value)
                        .MustBeGreaterThan(0)
                    .Setup(c => c.C)
                        .MustNotBeNull()
                        .WithMessage("C Must not be null")
                        .CallValidate()
                        .WithMessage("Composed value is not valid");

            var engine = builder.Build();

            var o = new CompositeClass() { C = new ClassC() };
            var r = new ValidationReport(engine);
            r.Validate(o);
            Assert.AreEqual("Composed value is not valid", r.GetErrorMessage(o, f => f.C));
            Assert.AreEqual("C is not valid", r.GetErrorMessage(o.C, f => f.C));

            var o1 = new CompositeClass() { C = null };
            var r1 = new ValidationReport(engine);
            r1.Validate(o1);
            Assert.AreEqual("C Must not be null", r1.GetErrorMessage(o1, f => f.C));
        }
        public void TestMessageInheritedClass2()
        {
            //NOTE: Same as TestMessageInheritedClass, with order of registrations changed.

            var builder = new Fluent.FluentBuilder();

            builder.For<ClassC>()
                .Setup(m => m.A)
                    .WithMessage("ClassC validation for A");

            builder.For<ClassB>()
                .Setup(m => m.A)
                    .WithMessage("ClassB validation for A");

            builder.For<ClassA>()
                .Setup(m => m.A)
                    .MustBeLessThanOrEqualTo(1)
                    .WithMessage("ClassA validation for A");

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj = new ClassC(2, 0, 0);
            report.Validate(obj);
            Assert.AreEqual("ClassC validation for A", report.GetErrorMessage(obj, o => o.A));
        }
        public void ShouldFindErrorMessageWithNestedIF()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<ClassC>()
                .If(c => c.C > 10)
                .If(c => c.C > 11)
                .If(c => c.C > 12)
                    .Setup(c => c.C)
                    .WithMessage("C is not valid. >12")
                    .MustBeBetween(10, 15);

            var engine = builder.Build();
            var o = new ClassC() { C = 20 };
            var r = new ValidationReport(engine);
            r.Validate(o);
            Assert.AreEqual("C is not valid. >12", r.GetErrorMessage(o, f => f.C));

        }
        public void TestMessageInheritedClass6()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For<ClassA>()
                .Setup(m => m.A)
                    .MustBeLessThanOrEqualTo(1)
                    .WithMessage("ClassA validation for A");

            builder.For<ClassB>()
                .Setup(m => m.A)
                    .WithMessage("ClassB validation for A");

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj = new ClassB(2, 0);
            report.Validate(obj);
            Assert.AreEqual("ClassB validation for A", report.GetErrorMessage(obj, o => o.A));
            Assert.AreEqual("ClassB validation for A", report.GetErrorMessage((ClassA)obj, o => o.A));
        }
        public void TestMessageInheritedClass4()
        {
            //NOTE: This test is the same as No.3, with definition of rules order changed.
            var builder = new Fluent.FluentBuilder();

            builder.For<ClassA>()
                .Setup(m => m.A)
                    .MustBeLessThanOrEqualTo(1)
                    .WithMessage("ClassA validation for A");

            builder.For<ClassB>()
                .Setup(m => m.A)
                    .WithMessage("ClassB validation for A");

            builder.For<ClassC>()
                .Setup(m => m.A)
                    .WithMessage("ClassC validation for A.1")
                    .MustBeLessThan(-1)
                    .WithMessage("ClassC validation for A.2");


            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj = new ClassC(2, 0, 0);
            report.Validate(obj);
            //m=>m.A will hit A.1 because the rules for base types are executed first. m=>m.A will fail because of rule defined on ClassA!.
            Assert.AreEqual("ClassC validation for A.1", report.GetErrorMessage(obj, o => o.A));

            report = new ValidationReport(engine);
            obj = new ClassC(1, 0, 0);
            engine.Validate(obj, report, ValidationReportDepth.FieldShortCircuit);
            //m=>m.A will hit A.2 because the rules for base types are fine. m=>m.A will fail because of rule defined on ClassC!.
            Assert.AreEqual("ClassC validation for A.2", report.GetErrorMessage(obj, o => o.A));

        }
        public void TestMessageInheritedClass3()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<ClassA>()
                .Setup(m => m.A)
                    .MustBeLessThanOrEqualTo(1)
                    .WithMessage("ClassA validation for A");

            builder.For<ClassB>()
                .Setup(m => m.A)
                    .WithMessage("ClassB validation for A");

            builder.For<ClassC>()
                .Setup(m => m.A)
                    .WithMessage("ClassC validation for A.1")
                    .MustBeLessThan(-1)
                    .WithMessage("ClassC validation for A.2");

            var engine = builder.Build();
            var report = new ValidationReport(engine);
            var obj = new ClassC(2, 0, 0);
            report.Validate(obj);
            //m=>m.A will hit A.1 because the rules for base types are executed first. m=>m.A will fail because of rule defined on ClassA!.
            Assert.AreEqual("ClassC validation for A.1", report.GetErrorMessage(obj, o => o.A));

            obj = new ClassC(1, 0, 0);
            report.Validate(obj);
            //m=>m.A will hit A.2 because the rules for base types are fine. m=>m.A will fail because of rule defined on ClassC!.
            Assert.AreEqual("ClassC validation for A.2", report.GetErrorMessage(obj, o => o.A));
        }
        public void ShouldFindErrorMessage3()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For<ClassC>().WithMessage("ClassC is not valid")
                .Setup(c => c.C)
                    .MustBeBetween(1, 10);

            builder.For<CompositeClass>()
                    .Setup(c => c.C)
                        .CallValidate();
            
            var engine = builder.Build();
            var o = new CompositeClass() { C = new ClassC() };
            var r = new ValidationReport(engine);
            r.Validate(o);
            Assert.AreEqual("ClassC is not valid", r.GetErrorMessage(o, f => f.C));
        }
        public void ShouldFindErrorMessageWithNestedIF_Else()
        {
            var builder = new Fluent.FluentBuilder();
            builder.For<ClassC>()
                .If(c => c.C > 1)
                .If(c => c.C > 2)
                .If(c => c.C > 5)
                    .Setup(c => c.C)
                    .WithMessage("C is not valid. >5")
                    .MustEqual(18)
                .Else()
                    .Setup(c => c.C)
                    .WithMessage("C is not valid. <5")
                    .MustEqual(1);

            var engine = builder.Build();
            var o = new ClassC() { C = 4 };
            var r = new ValidationReport(engine);
            r.Validate(o);
            Assert.AreEqual("C is not valid. <5", r.GetErrorMessage(o, f => f.C));

        }