コード例 #1
0
        public void EmptyBlockRuleThrowsException()
        {
            var emptyBlockRule = new FuncBlockRule <object, object>();
            var exception      = Assert.Throws <RuleEngineException>(() => emptyBlockRule.Compile());

            exception.Message.Should().Be("last rule must return a value of System.Object");
        }
コード例 #2
0
        public void ExceptionWhenLastRuleReturnsNoValue()
        {
            var someRule      = new ConditionalIfThActionRule <object>();
            var someBlockRule = new FuncBlockRule <object, object>();

            someBlockRule.Rules.Add(someRule);
            var exception = Assert.Throws <RuleEngineException>(() => someBlockRule.Compile());

            exception.Message.Should().Be("last rule must return a value of System.Object");
        }
コード例 #3
0
        public void ReturnsUpdatedGame()
        {
            var nameChangeRule = new UpdateValueRule <Game>
            {
                ObjectToUpdate = "Name",
                SourceDataRule = new ConstantRule <string> {
                    Value = "some fancy name"
                }
            };
            var rankingChangeRule = new UpdateValueRule <Game>
            {
                ObjectToUpdate = "Ranking",
                SourceDataRule = new ConstantRule <int> {
                    Value = "1000"
                }
            };
            var descriptionChangeRule = new UpdateValueRule <Game>
            {
                ObjectToUpdate = "Description",
                SourceDataRule = new ConstantRule <string> {
                    Value = "some cool description"
                }
            };
            var selfReturnRule = new SelfReturnRule <Game>();

            var blockRule = new FuncBlockRule <Game, Game>();

            blockRule.Rules.Add(nameChangeRule);
            blockRule.Rules.Add(rankingChangeRule);
            blockRule.Rules.Add(descriptionChangeRule);
            blockRule.Rules.Add(selfReturnRule);

            var compileResult = blockRule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(blockRule)}:{Environment.NewLine}" +
                                        $"{blockRule.ExpressionDebugView()}");

            var game = blockRule.Execute(new Game());

            game.Name.Should().Be("some fancy name");
            game.Ranking.Should().Be(1000);
            game.Description.Should().Be("some cool description");
            _testOutputHelper.WriteLine($"{game}");
        }
コード例 #4
0
        public void FuncBlockRuleReturnsLastRuleResult()
        {
            var ruleReturning5 = new ConstantRule <int, int> {
                Value = "5"
            };
            var blockRule = new FuncBlockRule <int, int>();

            blockRule.Rules.Add(ruleReturning5);
            var compileResult = blockRule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleReturning5)}:{Environment.NewLine}" +
                                        $"{ruleReturning5.ExpressionDebugView()}");

            var five = blockRule.Execute(99);

            five.Should().Be(5);
        }
コード例 #5
0
        public void ReturnsNewOrUpdatedGame()
        {
            var nullGame = new ConstantRule <Game> {
                Value = "null"
            };
            var nullGameCheckRule = new ValidationRule <Game>
            {
                ValueToValidateAgainst = nullGame,
                OperatorToUse          = "Equal"
            };

            var newGameRule = new StaticMethodCallRule <Game>
            {
                MethodClassName = "ModelForUnitTests.Game",
                MethodToCall    = "CreateGame"
            };

            var selfReturnRule = new SelfReturnRule <Game>();
            var gameObjectRule = new ConditionalFuncRule <Game, Game>
            {
                ConditionRule = nullGameCheckRule,
                TrueRule      = newGameRule,
                FalseRule     = selfReturnRule
            };
            var assignRule = new UpdateValueRule <Game> {
                SourceDataRule = gameObjectRule
            };

            var nameChangeRule = new UpdateValueRule <Game>
            {
                ObjectToUpdate = "Name",
                SourceDataRule = new ConstantRule <string> {
                    Value = "some fancy name"
                }
            };
            var rankingChangeRule = new UpdateValueRule <Game>
            {
                ObjectToUpdate = "Ranking",
                SourceDataRule = new ConstantRule <int> {
                    Value = "1000"
                }
            };
            var descriptionChangeRule = new UpdateValueRule <Game>
            {
                ObjectToUpdate = "Description",
                SourceDataRule = new ConstantRule <string> {
                    Value = "some cool description"
                }
            };

            var blockRule = new FuncBlockRule <Game, Game>();

            blockRule.Rules.Add(assignRule);
            blockRule.Rules.Add(nameChangeRule);
            blockRule.Rules.Add(rankingChangeRule);
            blockRule.Rules.Add(descriptionChangeRule);
            blockRule.Rules.Add(selfReturnRule);

            var compileResult = blockRule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(blockRule)}:{Environment.NewLine}" +
                                        $"{blockRule.ExpressionDebugView()}");

            var game = blockRule.Execute(null);

            game.Name.Should().Be("some fancy name");
            game.Ranking.Should().Be(1000);
            game.Description.Should().Be("some cool description");
            game.Rating.Should().BeNullOrEmpty();
            _testOutputHelper.WriteLine($"{game}");

            var newGame = new Game {
                Rating = "high"
            };

            // newGame is not same as game object
            ReferenceEquals(game, newGame).Should().BeFalse();
            game = blockRule.Execute(newGame);
            // this call shall return the same newGame object with updated values
            ReferenceEquals(game, newGame).Should().BeTrue();
            game.Rating.Should().Be("high");
            _testOutputHelper.WriteLine($"newGame: {game}");
        }