Exemplo n.º 1
0
        public void RuleContext_ShouldReturnTheHighestPriorityBasedOnIConsequencePrioritizer_WhenMultipleRulesAreTriggered()
        {
            //Setup
            Func <SampleFact, bool>         cond = x => x.SampleInt == 0;
            Func <SampleFact, SampleOutput> cons = y => new SampleOutput(3);
            var sampleRule1 = GetSampleRule(cond, cons);
            var sampleRule2 = GetSampleRule(cond, y => new SampleOutput(4));
            var sampleFact  = new SampleFact()
            {
                SampleInt = 0 //Making sure that the ruleCondition is matched
            };

            _moqConsequencePriorizter
            .Setup(x => x.GetHighestPriorityOutput(
                       It.IsAny <IEnumerable <Rule <SampleFact, SampleOutput> > >(), It.IsAny <SampleFact>())).Returns(cons.Invoke(sampleFact));

            var ruleContext = GetSampleExecuter();

            ruleContext.AddRule(sampleRule2);
            ruleContext.AddRule(sampleRule1);

            //Execution
            var result = ruleContext.ExecuteFact(sampleFact);

            //Assertion
            Assert.Equal(cons(sampleFact), result);
        }
Exemplo n.º 2
0
        public void RuleContext_ShouldReturnNullOrDefault_WhenNoRuleIsTriggered()
        {
            //Setup
            var ruleContext = GetSampleExecuter();
            Func <SampleFact, bool>         cond = x => x.SampleInt == 0;
            Func <SampleFact, SampleOutput> cons = y => new SampleOutput(3);
            var sampleRule = GetSampleRule(cond, cons);
            var sampleFact = new SampleFact()
            {
                SampleInt = 3 //Making sure that the ruleCondition is not matched
            };

            ruleContext.AddRule(sampleRule);

            //Execution
            var output = ruleContext.ExecuteFact(sampleFact);

            //Assert
            Assert.NotEqual(cons.Invoke(sampleFact), output);
        }
Exemplo n.º 3
0
        public void RuleContext_ShouldReturnExecutedRuleOutput_WhenSuccessfulRuleIsTriggered()
        {
            //Setup
            var sampleFact = new SampleFact()
            {
                SampleInt = 0 //Making sure that the ruleCondition is matched
            };

            SetUpDefaultMoqConsequence(sampleFact);

            var ruleContext = GetSampleExecuter();
            Func <SampleFact, bool>         cond = x => x.SampleInt == 0;
            Func <SampleFact, SampleOutput> cons = y => new SampleOutput(3);
            var sampleRule = GetSampleRule(cond, cons);

            ruleContext.AddRule(sampleRule);

            //Execution
            var output = ruleContext.ExecuteFact(sampleFact);

            //Assert
            Assert.Equal(cons.Invoke(sampleFact), output);
        }
Exemplo n.º 4
0
 private void SetUpDefaultMoqConsequence(SampleFact sampleFact)
 {
     _moqConsequencePriorizter.Setup(x => x.GetHighestPriorityOutput(It.IsAny <IEnumerable <Rule <SampleFact, SampleOutput> > >(), It.IsAny <SampleFact>()))
     .Returns(new SampleOutput(3));
 }