Ejemplo n.º 1
0
        public void TestMessageForSpecificRule()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <MyMessageTestClass>()
            .Setup(m => m.A)
            .MustBeLessThanOrEqualTo(1)
            .WithMessage("Must be Less Than or equal to {0}");

            var engine = builder.Build();
            var report = new TestingValidationReport(engine);
            var obj    = new MyMessageTestClass(2, 2);

            report.Validate(obj);
            report.AssertError(obj, o => o.A, RuleKinds.LessThanOrEqualToRule, 1);
        }
        public void ShouldBlameExplicitlySelectedCulprit_BlameIsAfterTheRule()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <Person>()
            .Setup(p => p.FirstName)
            .MustBeOneOf("John", "Paul", "James")
            .Blame(p => p.HomeAddress, a => a.Line1);

            var engine  = builder.Build();
            var report  = new TestingValidationReport(engine);
            var address = MakeAddress("18 Perkins St", null, "1234", "Brisbane");

            report.Validate(MakePerson("Jane", "Holland", address));
            report.AssertError(address, m => m.Line1, RuleKinds.OneOfRule, null);
        }
Ejemplo n.º 3
0
        public void TestGreaterThanRuleCrossField()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <MyDomainObject <int> >()
            .Setup(m => m.Value1)
            .MustBeGreaterThan(a => a.Value2);

            var engine = builder.Build();

            Assert.IsFalse(engine.Validate(new MyDomainObject <int>(3, 4)));
            Assert.IsFalse(engine.Validate(new MyDomainObject <int>(3, 3)));
            Assert.IsTrue(engine.Validate(new MyDomainObject <int>(3, 2)));

            TestingValidationReport v = new TestingValidationReport(engine);
            MyDomainObject <int>    o = new MyDomainObject <int>(3, 4);

            v.Validate(o);
            v.AssertError(o, p1 => p1.Value1, RuleKinds.GreaterThanRule, 4);
        }
Ejemplo n.º 4
0
        public void TestBetweenRuleCrossFieldLessAndGreater()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <MyDomainObject <int> >()
            .Setup(m => m.Value1)
            .MustBeBetween(a => a.Value2, a => a.Value3);

            var engine = builder.Build();

            Assert.IsTrue(engine.Validate(new MyDomainObject <int>(3, 2, 5)));
            Assert.IsTrue(engine.Validate(new MyDomainObject <int>(3, 3, 3)));
            Assert.IsFalse(engine.Validate(new MyDomainObject <int>(3, 4, 5)));
            Assert.IsFalse(engine.Validate(new MyDomainObject <int>(3, 1, 2)));

            TestingValidationReport v = new TestingValidationReport(engine);
            MyDomainObject <int>    o = new MyDomainObject <int>(4, 1, 3);

            v.Validate(o);
            v.AssertError(o, p1 => p1.Value1, RuleKinds.BetweenRule, 1, 3, 4, BetweenRuleBoundsOption.BothInclusive);
        }
        public void ShouldBlameExplicitlySelectedCulprit_DifferentCulpritForDifferentRules()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <Person>()
            .Setup(p => p.FirstName)
            .MustNotEqual("Paul")
            .Blame(p => p.HomeAddress, a => a.Line1)
            .MustNotEqual("Peter")
            .Blame(p => p.HomeAddress, a => a.PostCode);

            var engine  = builder.Build();
            var report  = new TestingValidationReport(engine);
            var address = MakeAddress("18 Perkins St", null, "1234", "Brisbane");

            report.Validate(MakePerson("Paul", "Holland", address));
            report.AssertError(address, m => m.Line1, RuleKinds.NotEqualRule, null);

            report.Clear();
            report.Validate(MakePerson("Peter", "Holland", address));
            report.AssertError(address, m => m.PostCode, RuleKinds.NotEqualRule, null);
        }
Ejemplo n.º 6
0
        public void TestBetweenRuleCrossFieldGreater()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <MyDomainObject <int> >()
            .Setup(m => m.Value1)
            .MustBeBetween(a => a.Value2, 10);

            var engine = builder.Build();

            Assert.IsTrue(engine.Validate(new MyDomainObject <int>(7, 5)));
            Assert.IsTrue(engine.Validate(new MyDomainObject <int>(6, 6)));
            Assert.IsFalse(engine.Validate(new MyDomainObject <int>(5, 6)));
            Assert.IsFalse(engine.Validate(new MyDomainObject <int>(11, 15)));

            TestingValidationReport v = new TestingValidationReport(engine);
            MyDomainObject <int>    o = new MyDomainObject <int>(3, 4);

            engine.Validate(o, v, ValidationReportDepth.FieldShortCircuit);

            v.AssertError(o, p1 => p1.Value1, RuleKinds.BetweenRule, 4, 10, 3, BetweenRuleBoundsOption.BothInclusive);
        }
        //TODO: Blame Culprit when using composition!
        //[Test]
        public void ShouldBlameExplicitlySelectedCulprit_Composition()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <Person>()
            .Setup(p => p.HomeAddress)
            .CallValidate()
            .Blame(p => p.FirstName);

            builder.For <Address>()
            .Setup(a => a.Line1)
            .MustEqual("Line1");



            var engine  = builder.Build();
            var report  = new TestingValidationReport(engine);
            var address = MakeAddress("Not Line 1", null, "1234", "Brisbane");
            var person  = MakePerson("Blinky", "Bill", address);

            report.Validate(person);
            report.AssertError(person, m => m.FirstName, RuleKinds.EqualRule, "Line1");
        }
Ejemplo n.º 8
0
        public void SimpleInterfaceTest()
        {
            var builder = new Fluent.FluentBuilder();

            builder.For <IMyClass>()
            .Setup(m => m.A)
            .MustBeGreaterThan(0);

            var engine = builder.Build();
            var report = new TestingValidationReport(engine);

            var o1 = new MyClass(0);

            Assert.IsFalse(report.Validate(o1));
            Assert.AreEqual(1, report.Errors.Length);
            report.AssertError <IMyClass, int>(o1, p1 => p1.A, RuleKinds.GreaterThanRule, 0);

            var o2 = new MyClassExplicit(0);

            Assert.IsFalse(report.Validate(o2));
            Assert.AreEqual(1, report.Errors.Length);
            report.AssertError <IMyClass, int>(o2, p1 => p1.A, RuleKinds.GreaterThanRule, 0);
        }