Ejemplo n.º 1
0
        public void TestMessageForSpecificRule()
        {
            Engine engine = new Engine();

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

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

            report.Validate(obj);
            report.AssertError(obj, o => o.A, RuleKinds.LessThanOrEqualToRule, 1);
        }
Ejemplo n.º 2
0
        public void TestGreaterThanRuleCrossField()
        {
            Engine engine = new Engine();

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

            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.º 3
0
        public void TestBetweenRuleCrossFieldLessAndGreater()
        {
            Engine engine = new Engine();

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

            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);
        }
Ejemplo n.º 4
0
        public void TestBetweenRuleCrossFieldGreater()
        {
            Engine engine = new Engine();

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

            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);
        }
Ejemplo n.º 5
0
        public void TestBetweenRuleCrossFieldLess()
        {
            Engine engine = new Engine();

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

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

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

            v.Validate(o);

            v.AssertError(o, p1 => p1.Value1, RuleKinds.BetweenRule, 6, 3, 4, BetweenRuleBoundsOption.BothInclusive);
        }
Ejemplo n.º 6
0
        public void SimpleInterfaceTest()
        {
            var engine = new Engine();

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

            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);
        }