예제 #1
0
        public void CallCreateGameStaticMethod2ToAndFromJson()
        {
            //var game = Game.CreateGame("cool game");
            var ruleBefore = new StaticMethodCallRule <Game>
            {
                MethodClassName  = "ModelForUnitTests.Game",
                MethodToCall     = "CreateGame",
                MethodParameters = { new ConstantRule <string> {
                                         Value = "cool game"
                                     } }
            };

            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()}");

            var game = ((StaticMethodCallRule <Game>)ruleAfter).Execute();

            game.Should().NotBeNull();
            game.Name.Should().Be("cool game");
        }
        public static StaticMethodCallRule <T> CreateStaticMethodCallRule <T>(string methodToCall, string methodClassName, IList <Rule> methodParams)
        {
            var rule = new StaticMethodCallRule <T>
            {
                MethodToCall    = methodToCall,
                MethodClassName = methodClassName
            };

            rule.MethodParameters.AddRange(methodParams);

            return(rule);
        }
예제 #3
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}");
        }