Пример #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 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();
        }
        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 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);
        }
Пример #6
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}");
        }