Esempio n. 1
0
        public void Rule_WithWhenClause_WhenClauseIsCallable()
        {
            var customer = new Customer ();
            var ruleEngineContext = new RuleEngineContext<Customer> ( customer );
            Predicate<IRuleEngineContext> whenClause = e => string.IsNullOrWhiteSpace ( ( ( RuleEngineContext<Customer> )e ).Subject.FirstName );

            var rule = new Rule ( "Name" ) { WhenClause = whenClause };
            var isNullOrWhiteSpace = rule.WhenClause ( ruleEngineContext );

            Assert.IsTrue ( isNullOrWhiteSpace );
        }
Esempio n. 2
0
        public void DoNotRunIfHasRuleViolation_ShouldRunClauseAdded()
        {
            var rule = new Rule ( "Rule1" );
            var ruleBuilder = new RuleBuilder<RuleEngineContext<Customer>, Customer> ( rule );
            ruleBuilder.DoNotRunIfHasRuleViolation ();

            var ruleEngineContext = new RuleEngineContext ( new Customer () );
            ruleEngineContext.RuleViolationReporter.Report ( new RuleViolation ( rule, null, null ) );

            var shouldRunResult = rule.ShouldRunRule ( ruleEngineContext );

            Assert.IsFalse ( shouldRunResult );
        }
Esempio n. 3
0
        public void Then_ProvidingAThenClauseOverrideToTheBuilder_RuleShouldHaveAThenClauseOverride()
        {
            var rule = new Rule ( "Rule1" );
            var ruleBuilder = new RuleBuilder<RuleEngineContext<Customer>, Customer> ( rule );
            ruleBuilder.When ( ( cust, ctxt ) => true ).Then ( ( cust, ctxt ) => cust.Age++ );

            var customer = new Customer { Age = 10 };
            var context = new RuleEngineContext<Customer> ( customer );

            foreach ( var clause in rule.ThenClauses )
            {
                clause ( context );
            }

            Assert.AreEqual ( 11, customer.Age );
        }
Esempio n. 4
0
        public void Rule_WithThenClauses_ThenClausesAreCallable()
        {
            var customer = new Customer ();
            var ruleEngineContext = new RuleEngineContext<Customer> ( customer );

            var rule = new Rule ( "Name" );

            Action<IRuleEngineContext> thenClause1 = e => ( ( Customer )e.Subject ).Age = 20;
            rule.AddThenClause ( thenClause1 );

            Action<IRuleEngineContext> thenClause2 = e => ( ( Customer )e.Subject ).Age++;
            rule.AddThenClause ( thenClause2 );

            foreach ( var clause in rule.ThenClauses )
            {
                clause ( ruleEngineContext );
            }

            Assert.AreEqual ( 21, customer.Age );
        }
Esempio n. 5
0
 public void Rule_WithName_RuleIsNamed()
 {
     var rule = new Rule ( "Name" );
     Assert.AreEqual ( "Name", rule.Name );
 }
Esempio n. 6
0
        public void WithPrioroty_RulePriorityIsSet()
        {
            var rule = new Rule ( "Rule1" );
            var ruleBuilder = new RuleBuilder<RuleEngineContext<Customer>, Customer> ( rule );
            ruleBuilder.WithPriority ( 5 );

            Assert.AreEqual ( 5, rule.Priority );
        }
Esempio n. 7
0
        public void When_WithAWhenFunction_AssignsWhenFunctionToRule()
        {
            var rule = new Rule ( "Rule1" );
            var ruleBuilder = new RuleBuilder<RuleEngineContext<Customer>, Customer> ( rule );
            ruleBuilder.When ( ( cust, ctxt ) => cust.Age < 20 );

            var customer = new Customer { Age = 10 };
            var context = new RuleEngineContext<Customer> ( customer );

            var lessThan20 = rule.WhenClause ( context );

            Assert.IsTrue ( lessThan20 );
        }
Esempio n. 8
0
        public void When_GivenAWhenPredicate_AssignsWhenPredicateToRule()
        {
            var rule = new Rule ( "Rule1" );
            var ruleBuilder = new RuleBuilder<RuleEngineContext<Customer>, Customer> ( rule );
            ruleBuilder.When ( c => c.Age < 20 );

            var customer = new Customer { Age = 10 };
            var context = new RuleEngineContext<Customer> ( customer );

            var lessThan20 = rule.WhenClause ( context );

            Assert.IsTrue ( lessThan20 );
        }
Esempio n. 9
0
 public void When_GivenAThenClauseWithoutAWhenClause_ThrowsInvalidRuleException()
 {
     var rule = new Rule ( "Rule1" );
     var ruleBuilder = new RuleBuilder<RuleEngineContext<Customer>, Customer> ( rule );
     ruleBuilder.Then ( c => c.Age++ );
 }