Beispiel #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 );
        }
Beispiel #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 );
        }
Beispiel #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 );
        }
Beispiel #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 );
        }
Beispiel #5
0
 public void Rule_WithName_RuleIsNamed()
 {
     var rule = new Rule ( "Name" );
     Assert.AreEqual ( "Name", rule.Name );
 }
Beispiel #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 );
        }
Beispiel #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 );
        }
Beispiel #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 );
        }
Beispiel #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++ );
 }