public void UpdateIntRefUsingFactory()
        {
            var sourceDataRule = ConstantRulesFactory.CreateConstantRule <int>("99");
            var rule           = UpdateValueRulesFactory.CreateUpdateRefValueRule <int>(sourceDataRule);

            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{rule.ExpressionDebugView()}");

            var myInt = 0;

            rule.RefUpdate(ref myInt);
            myInt.Should().Be(99);

            var rule2 = UpdateValueRulesFactory.CreateUpdateRefValueRule <int>();

            compileResult = rule2.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"UpdateRefValueRule<int, int>:{Environment.NewLine}" +
                                        $"{rule2.ExpressionDebugView()}");

            rule2.RefUpdate(ref myInt, -99);
            myInt.Should().Be(-99);
        }
        public void UpdateMultiplePropertiesOfaGameObjectUsingFactory()
        {
            var nameConstRule         = ConstantRulesFactory.CreateConstantRule <string>("some fancy name");
            var nameChangeRule        = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Name, nameConstRule);
            var rankConstRule         = ConstantRulesFactory.CreateConstantRule <int>("1000");
            var rankingChangeRule     = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Ranking, rankConstRule);
            var descConstRule         = ConstantRulesFactory.CreateConstantRule <string>("some cool description");
            var descriptionChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Description, descConstRule);

            IList <Rule> rules = new List <Rule>
            {
                nameChangeRule,
                rankingChangeRule,
                descriptionChangeRule
            };
            var blockRule = BlockRulesFactory.CreateActionBlockRule <Game>(rules);

            var compileResult = blockRule.Compile();

            compileResult.Should().BeTrue();

            _testOutputHelper.WriteLine(blockRule.ExpressionDebugView());

            var game = new Game();

            blockRule.Execute(game);
            _testOutputHelper.WriteLine($"game object updated:{Environment.NewLine}{game}");
            game.Name.Should().Be("some fancy name");
            game.Ranking.Should().Be(1000);
            game.Description.Should().Be("some cool description");
        }
        public void ReturnsUpdatedGameUsingFactory()
        {
            var sourceNameRule = ConstantRulesFactory.CreateConstantRule <string>("some fancy name");
            var nameChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Name, sourceNameRule);

            var sourceRankRule    = ConstantRulesFactory.CreateConstantRule <int>("1000");
            var rankingChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Ranking, sourceRankRule);

            var sourceDescRule        = ConstantRulesFactory.CreateConstantRule <string>("some cool description");
            var descriptionChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Description, sourceDescRule);

            var selfReturnRule = new SelfReturnRule <Game>();

            var subRules = new List <Rule>
            {
                nameChangeRule,
                rankingChangeRule,
                descriptionChangeRule,
                selfReturnRule
            };

            var blockRule = BlockRulesFactory.CreateFuncBlockRule <Game, Game>(subRules);

            var compileResult = blockRule.Compile();

            compileResult.Should().BeTrue();

            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}");
        }
        public void UpdateStringRefUsingFactory()
        {
            var sourceDataRule = ConstantRulesFactory.CreateConstantRule <string>("something");
            var rule           = UpdateValueRulesFactory.CreateUpdateRefValueRule <string>(sourceDataRule);

            var compileResult = rule.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"UpdateRefValueRule<string>:{Environment.NewLine}" +
                                        $"{rule.ExpressionDebugView()}");

            var string1 = "one";

            rule.RefUpdate(ref string1);
            string1.Should().Be("something");

            // source value shall come as argument
            var rule2 = UpdateValueRulesFactory.CreateUpdateRefValueRule <string>();

            compileResult = rule2.Compile();
            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"UpdateRefValueRule<string, string>:{Environment.NewLine}" +
                                        $"{rule2.ExpressionDebugView()}");

            string1 = null;
            rule2.RefUpdate(ref string1, "some other value");
            string1.Should().Be("some other value");
        }
示例#5
0
        public void ConditionalRuleLookAtOneValueUpdateAnotherUsingFactory()
        {
            var const1   = ConstantRulesFactory.CreateConstantRule <int>("999");
            var trueRule = UpdateValueRulesFactory.CreateUpdateValueRule <Player>(p => p.CurrentCoOrdinates.X, const1);

            var const2         = ConstantRulesFactory.CreateConstantRule <string>("ab");
            var validationRule = ValidationRulesFactory.CreateValidationRule <Player>(p => p.Country.CountryCode,
                                                                                      LogicalOperatorAtTheRootLevel.Equal, const2);

            var conditionalUpdate =
                ConditionalRulesFactory.CreateConditionalIfThActionRule <Player>(validationRule, trueRule);

            var compileResult = conditionalUpdate.Compile();

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

            var player = new Player
            {
                Country = new Country {
                    CountryCode = "ab"
                },
                CurrentCoOrdinates = new CoOrdinate {
                    X = 1, Y = 1
                }
            };

            conditionalUpdate.Execute(player);
            player.CurrentCoOrdinates.X.Should().Be(999);
            _testOutputHelper.WriteLine($"expected: 999 - actual: {player.CurrentCoOrdinates.X}");
        }
示例#6
0
        public void ConditionalRuleToUpdateNameUsingFactory()
        {
            var trueRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Name,
                                                                                ConstantRulesFactory.CreateConstantRule <string>("updated name"));

            var methodParams = new List <Rule>
            {
                ConstantRulesFactory.CreateConstantRule <string>("some name"),
                ConstantRulesFactory.CreateConstantRule <StringComparison>("CurrentCultureIgnoreCase")
            };
            var methodCallRule = MethodCallRulesFactory.CreateMethodCallRule <Game, bool>("Equals", null,
                                                                                          g => g.Name, methodParams);
            var conditionalUpdateValue =
                ConditionalRulesFactory.CreateConditionalIfThActionRule <Game>(methodCallRule, trueRule);

            var compileResult = conditionalUpdateValue.Compile();

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

            var game = new Game {
                Name = "some name"
            };

            _testOutputHelper.WriteLine($"before game.Name: {game.Name}");
            conditionalUpdateValue.Execute(game);
            _testOutputHelper.WriteLine($"after game.Name: {game.Name}");
            game.Name.Should().Be("updated name");
        }
        public void ReturnsNewOrUpdatedGameUsingFactory()
        {
            var nullGame          = ConstantRulesFactory.CreateConstantRule <Game>("null");
            var nullGameCheckRule =
                ValidationRulesFactory.CreateValidationRule <Game>(LogicalOperatorAtTheRootLevel.Equal, nullGame);

            var newGameRule = MethodCallRulesFactory.CreateStaticMethodCallRule <Game>("CreateGame", "ModelForUnitTests.Game", null);

            var selfReturnRule = SelfReturnRuleFactory.CreateSelfReturnRule <Game>();
            var gameObjectRule = ConditionalRulesFactory.CreateConditionalFuncRule <Game, Game>(nullGameCheckRule, newGameRule,
                                                                                                selfReturnRule);

            var assignRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(gameObjectRule);

            var nameConstRule         = ConstantRulesFactory.CreateConstantRule <string>("some fancy name");
            var nameChangeRule        = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Name, nameConstRule);
            var rankConstRule         = ConstantRulesFactory.CreateConstantRule <int>("1000");
            var rankingChangeRule     = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Ranking, rankConstRule);
            var descConstRule         = ConstantRulesFactory.CreateConstantRule <string>("some cool description");
            var descriptionChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Description, descConstRule);

            IList <Rule> rules = new List <Rule>
            {
                assignRule,
                nameChangeRule,
                rankingChangeRule,
                descriptionChangeRule,
                selfReturnRule
            };
            var blockRule = BlockRulesFactory.CreateFuncBlockRule <Game, Game>(rules);

            var compileResult = blockRule.Compile();

            compileResult.Should().BeTrue();

            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}");
        }
        public void UpdatePropertyStingWithDifferentValueUsingFactory()
        {
            var game = new Game {
                Name = "game name"
            };
            var nameChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game, string>((g => g.Name));

            var compileResult = nameChangeRule.Compile();

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

            _testOutputHelper.WriteLine($"before game.Name: {game.Name}");
            nameChangeRule.UpdateFieldOrPropertyValue(game, "new name");
            game.Name.Should().Be("new name");
            _testOutputHelper.WriteLine($"after game.Name: {game.Name}");
        }
        public void UpdatePropertyFromAnotherRuleUsingFactory()
        {
            var game = new Game {
                Name = "game name"
            };
            var const1         = ConstantRulesFactory.CreateConstantRule <string>("name from constant rule");
            var nameChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>((g => g.Name), const1);

            var compileResult = nameChangeRule.Compile();

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

            _testOutputHelper.WriteLine($"before game.Name: {game.Name}");
            nameChangeRule.UpdateFieldOrPropertyValue(game);
            game.Name.Should().Be("name from constant rule");
            _testOutputHelper.WriteLine($"after game.Name: {game.Name}");
        }
        public void ConditionalRuleWithBlockUsingFactory()
        {
            var sourceNameRule = ConstantRulesFactory.CreateConstantRule <string>("some fancy name");
            var nameChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Name, sourceNameRule);

            var sourceRankRule    = ConstantRulesFactory.CreateConstantRule <int>("1000");
            var rankingChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Ranking, sourceRankRule);

            var sourceDescRule        = ConstantRulesFactory.CreateConstantRule <string>("some cool description");
            var descriptionChangeRule = UpdateValueRulesFactory.CreateUpdateValueRule <Game>(g => g.Description, sourceDescRule);

            var subRules = new List <Rule>
            {
                nameChangeRule,
                rankingChangeRule,
                descriptionChangeRule
            };
            var blockRule = BlockRulesFactory.CreateActionBlockRule <Game>(subRules);

            var param1Const    = ConstantRulesFactory.CreateConstantRule <string>("some name");
            var param2Const    = ConstantRulesFactory.CreateConstantRule <StringComparison>("CurrentCultureIgnoreCase");
            var nameEqualsRule = MethodCallRulesFactory.CreateMethodCallRule <Game, bool>("Equals", null, (g => g.Name),
                                                                                          new List <Rule> {
                param1Const, param2Const
            });

            var conditionalUpdateValue =
                ConditionalRulesFactory.CreateConditionalIfThActionRule <Game>(nameEqualsRule, blockRule);

            var compileResult = conditionalUpdateValue.Compile();

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

            var game = new Game {
                Name = "some name"
            };

            _testOutputHelper.WriteLine($"before game.Name: {game.Name}");
            conditionalUpdateValue.Execute(game);
            _testOutputHelper.WriteLine($"after game.Name: {game.Name}");
            game.Name.Should().Be("some fancy name");
            _testOutputHelper.WriteLine($"{game}");

            var jsonConverterForRule = new JsonConverterForRule();
            var json = JsonConvert.SerializeObject(conditionalUpdateValue, jsonConverterForRule);

            _testOutputHelper.WriteLine(json);

            var conditionalUpdateValue2 = JsonConvert.DeserializeObject <Rule>(json, jsonConverterForRule);

            compileResult = conditionalUpdateValue2.Compile();
            compileResult.Should().BeTrue();

            var game2 = new Game {
                Name = "some name"
            };

            _testOutputHelper.WriteLine($"before game2.Name: {game2.Name}");
            (conditionalUpdateValue2 as ConditionalIfThActionRule <Game>)?.Execute(game2);
            _testOutputHelper.WriteLine($"after game2.Name: {game2.Name}");
            game.Name.Should().Be("some fancy name");
            _testOutputHelper.WriteLine($"{game2}");
        }