Пример #1
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 CallCreateGameStaticMethod3UsingFactory()
        {
            //var game = Game.CreateGame("game", "description", 1, true);
            var methodParams = new List <Rule>
            {
                ConstantRulesFactory.CreateConstantRule <string>("game"),
                ConstantRulesFactory.CreateConstantRule <string>("description"),
                ConstantRulesFactory.CreateConstantRule <int>("1"),
                ConstantRulesFactory.CreateConstantRule <bool>("true")
            };
            var rule = MethodCallRulesFactory.CreateStaticMethodCallRule <Game>("CreateGame",
                                                                                "ModelForUnitTests.Game", methodParams);

            var compileResult = rule.Compile();

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

            var game = rule.Execute();

            game.Should().NotBeNull();
            game.Name.Should().Be("game");
            _testOutputHelper.WriteLine($"{game}");
        }
        public void CallAStringMethodOnDescriptionObjectUsingFactory()
        {
            // Description is a string - Call Contains method on Description
            // compiles to: Param_0.Description.Contains("cool")
            var const1 = ConstantRulesFactory.CreateConstantRule <string>("cool");
            var gameNameContainsKeyWordCool = MethodCallRulesFactory.CreateMethodCallRule <Game, bool>("Contains",
                                                                                                       null, (g => g.Description), new List <Rule> {
                const1
            });

            var compileResult = gameNameContainsKeyWordCool.Compile();

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

            // check to see if _game1 description contains keyword "cool"
            var executeResult = gameNameContainsKeyWordCool.Execute(_game1);

            executeResult.Should().BeFalse();

            // check to see if _game2 description contains keyword "cool"
            executeResult = gameNameContainsKeyWordCool.Execute(_game2);
            executeResult.Should().BeTrue();
        }
Пример #4
0
        public void CallStaticVoidMethod2ToAndFromJson()
        {
            var ruleBefore = MethodCallRulesFactory.CreateStaticVoidMethodCallRule("SomeVoidStaticMethod", "ModelForUnitTests.Game",
                                                                                   new List <Rule> {
                new ConstantRule <int> {
                    Value = "99"
                }
            });

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

            _testOutputHelper.WriteLine($"ruleJson:{Environment.NewLine}{ruleJson}");
            // read from json
            var ruleAfter = JsonConvert.DeserializeObject <Rule>(ruleJson, jsonConverterForRule);

            var compileResult = ruleAfter.Compile();

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

            Game.SomeStaticIntValue = 0;
            (ruleAfter as StaticVoidMethodCallRule)?.Execute();
            Game.SomeStaticIntValue.Should().Be(99);
        }
Пример #5
0
        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 CallToUpperUsingFactory()
        {
            var rule          = MethodCallRulesFactory.CreateMethodCallRule <string, string>("ToUpper", "System.String", null, null);
            var compileResult = rule.Compile();

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

            var foo = "foo";
            var FOO = rule.Execute(foo);

            FOO.Should().Be("FOO");
        }
        public void CallStaticVoidMethodUsingFactory()
        {
            var rule = MethodCallRulesFactory.CreateStaticVoidMethodCallRule("SomeVoidStaticMethod", "ModelForUnitTests.Game", null);

            var compileResult = rule.Compile();

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

            Game.SomeStaticIntValue = 0;
            rule.Execute();
            Game.SomeStaticIntValue.Should().Be(1);
        }
        public void CallCreateGameStaticMethodUsingFactory()
        {
            //var game = Game.CreateGame();
            var rule = MethodCallRulesFactory.CreateStaticMethodCallRule <Game>("CreateGame", "ModelForUnitTests.Game", null);

            var compileResult = rule.Compile();

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

            var game = rule.Execute();

            game.Should().NotBeNull();
        }
        public void CallAVoidMethodUsingFactory()
        {
            // call FlipActive method on the game object
            // compiles to: Param_0.FlipActive()
            var playerCountRule = MethodCallRulesFactory.CreateMethodVoidCallRule <Game>("FlipActive", null, null, null);

            var compileResult = playerCountRule.Compile();

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

            var currentActiveState = _game1.Active;

            playerCountRule.Execute(_game1);
            _game1.Active.Should().Be(!currentActiveState);
        }
        public void CheckToSeeIfPlayerExistsInAGameUsingFactory(int id, bool expectedResult)
        {
            // call HasPlayer method on the game object
            // compiles to: Param_0.HasPlayer(1000)
            var const1 = ConstantRulesFactory.CreateConstantRule <int>(id.ToString());
            var gameHasPlayerWithCertainId =
                MethodCallRulesFactory.CreateMethodCallRule <Game, bool>("HasPlayer", null, null,
                                                                         new List <Rule> {
                const1
            });

            var compileResult = gameHasPlayerWithCertainId.Compile();

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

            var executeResult = gameHasPlayerWithCertainId.Execute(_game1);

            executeResult.Should().Be(expectedResult);
        }
        public void CallEqualsMethodOnNameUsingConstantRuleUsingFactory(string param1, bool expectedResult)
        {
            // call Equals method on Name string object
            // compiles to: Param_0.Name.Equals("Game 1", CurrentCultureIgnoreCase)
            var param1Const    = ConstantRulesFactory.CreateConstantRule <string>(param1);
            var param2Const    = ConstantRulesFactory.CreateConstantRule <StringComparison>("CurrentCultureIgnoreCase");
            var nameEqualsRule = MethodCallRulesFactory.CreateMethodCallRule <Game, bool>("Equals", null, (g => g.Name),
                                                                                          new List <Rule> {
                param1Const, param2Const
            });
            var compileResult = nameEqualsRule.Compile();

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

            var executeResult = nameEqualsRule.Execute(_game1);

            executeResult.Should().Be(expectedResult);

            executeResult = nameEqualsRule.Execute(_game2);
            executeResult.Should().Be(!expectedResult);
        }
Пример #12
0
        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}");
        }