コード例 #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 ReturnsUpdatedGameToAndFromJson()
        {
            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 jsonConverterForRule = new JsonConverterForRule();
            var json = JsonConvert.SerializeObject(blockRule, Formatting.Indented, jsonConverterForRule);

            _testOutputHelper.WriteLine(json);

            var blockRule2    = JsonConvert.DeserializeObject <FuncBlockRule <Game, Game> >(json, jsonConverterForRule);
            var compileResult = blockRule2.Compile();

            compileResult.Should().BeTrue();

            var game = blockRule2.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 EmptyBlockRuleThrowsExceptionToAndFromJson()
        {
            var emptyBlockRule = new FuncBlockRule <object, object>();

            var converter = new JsonConverterForRule();
            // convert to json
            var ruleJson = JsonConvert.SerializeObject(emptyBlockRule, converter);

            _testOutputHelper.WriteLine(ruleJson);
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <FuncBlockRule <object, object> >(ruleJson, converter);

            var exception = Assert.Throws <RuleEngineException>(() => ruleFromJson.Compile());

            exception.Message.Should().Be("last rule must return a value of System.Object");
        }
コード例 #5
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}");
        }
コード例 #6
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);
        }
コード例 #7
0
        public void ExceptionWhenLastRuleReturnsNoValueToAndFromJson()
        {
            var someRule      = new ConditionalIfThActionRule <object>();
            var someBlockRule = new FuncBlockRule <object, object>();

            someBlockRule.Rules.Add(someRule);

            var converter = new JsonConverterForRule();
            // convert to json
            var ruleJson = JsonConvert.SerializeObject(someBlockRule, Formatting.Indented, converter);

            _testOutputHelper.WriteLine(ruleJson);
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <FuncBlockRule <object, object> >(ruleJson, converter);

            var exception = Assert.Throws <RuleEngineException>(() => ruleFromJson.Compile());

            exception.Message.Should().Be("last rule must return a value of System.Object");
        }
コード例 #8
0
        public void FuncBlockRuleReturnsLastRuleResultToAndFromJson()
        {
            var ruleReturning5 = new ConstantRule <int, int> {
                Value = "5"
            };
            var blockRule = new FuncBlockRule <int, int>();

            blockRule.Rules.Add(ruleReturning5);

            var converter = new JsonConverterForRule();
            // convert to json
            var ruleJson = JsonConvert.SerializeObject(blockRule, Formatting.Indented, converter);

            _testOutputHelper.WriteLine(ruleJson);
            // re-hydrate from json
            var ruleFromJson   = JsonConvert.DeserializeObject <FuncBlockRule <int, int> >(ruleJson, converter);
            var compileResult2 = ruleFromJson.Compile();

            compileResult2.Should().BeTrue();

            var five = ruleFromJson.Execute(99);

            five.Should().Be(5);
        }
コード例 #9
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}");
        }