コード例 #1
0
        public void FactoryOrBuilderMethodsDontCount(string methodName, int expectedProblemCount)
        {
            var rule = new MethodCallRule();

            rule.Check(AssemblyReader.GetMethodByName(typeof(ClassF), methodName));
            Assert.That(rule.Problems.Count, Is.EqualTo(expectedProblemCount));
        }
コード例 #2
0
        public void CannotInvokeConcreteMethods(string methodName, int expectedProblemCount)
        {
            var rule = new MethodCallRule();

            rule.Check(AssemblyReader.GetMethodByName(typeof(ClassF), methodName));
            Assert.That(rule.Problems.Count, Is.EqualTo(expectedProblemCount));
        }
コード例 #3
0
        public void CallEqualsMethodOnNameUsingConstantRuleToAndFromJson(string param1, bool expectedResult)
        {
            // call Equals method on Name string object
            // compiles to: Param_0.Name.Equals("Game 1", CurrentCultureIgnoreCase)
            var rule = new MethodCallRule <Game, bool>
            {
                ObjectToCallMethodOn = "Name",
                MethodToCall         = "Equals",
                MethodParameters     = { new ConstantRule <string>       {
                                             Value = param1
                                         },
                                         new ConstantRule <StringComparison> {
                                             Value = "CurrentCultureIgnoreCase"
                                         } }
            };

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

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson  = (MethodCallRule <Game, bool>)JsonConvert.DeserializeObject <Rule>(ruleJson, new JsonConverterForRule());
            var compileResult = ruleFromJson.Compile();

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

            var executeResult2 = ruleFromJson.Execute(_game1);

            executeResult2.Should().Be(expectedResult);
            executeResult2 = ruleFromJson.Execute(_game2);
            executeResult2.Should().Be(!expectedResult);
        }
コード例 #4
0
        public void CallAStringMethodOnDescriptionObjectToAndFromJson()
        {
            // Description is a string - Call Contains method on Description
            // compiles to: Param_0.Description.Contains("cool")
            var rule = new MethodCallRule <Game, bool>
            {
                MethodToCall         = "Contains",
                ObjectToCallMethodOn = "Description",
                MethodParameters     = { new ConstantRule <string> {
                                             Value = "cool"
                                         } }
            };

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

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson  = (MethodCallRule <Game, bool>)JsonConvert.DeserializeObject <Rule>(ruleJson, new JsonConverterForRule());
            var compileResult = ruleFromJson.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");
            // check to see if _game1 description contains keyword "cool"
            var executeResult2 = ruleFromJson.Execute(_game1);

            executeResult2.Should().BeFalse();

            // check to see if _game2 description contains keyword "cool"
            executeResult2 = ruleFromJson.Execute(_game2);
            executeResult2.Should().BeTrue();
        }
コード例 #5
0
        public void CheckToSeeIfPlayerExistsInAGameToAndFromJson(int id, bool expectedResult)
        {
            // call HasPlayer method on the game object
            // compiles to: Param_0.HasPlayer(1000)
            var rule = new MethodCallRule <Game, bool>
            {
                MethodToCall     = "HasPlayer",
                MethodParameters = { new ConstantRule <int> {
                                         Value = id.ToString()
                                     } }
            };

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

            _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson  = (MethodCallRule <Game, bool>)JsonConvert.DeserializeObject <Rule>(ruleJson, new JsonConverterForRule());
            var compileResult = ruleFromJson.Compile();

            compileResult.Should().BeTrue();
            _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" +
                                        $"{ruleFromJson.ExpressionDebugView()}");
            var executeResult2 = ruleFromJson.Execute(_game1);

            executeResult2.Should().Be(expectedResult);
        }
コード例 #6
0
        public static MethodCallRule <T1, T2> CreateMethodCallRule <T1, T2>(string methodToCall, string methodClassName,
                                                                            Expression <Func <T1, object> > objectToCallMethodOn, IList <Rule> methodParams)
        {
            var rule = new MethodCallRule <T1, T2>
            {
                MethodToCall         = methodToCall,
                MethodClassName      = methodClassName,
                ObjectToCallMethodOn = objectToCallMethodOn?.GetObjectToWorkOnFromExpression()
            };

            rule.MethodParameters.AddRange(methodParams);
            return(rule);
        }
コード例 #7
0
        public void CallToUpperToAndFromJson()
        {
            var rule = new MethodCallRule <string, string> {
                MethodToCall = "ToUpper"
            };

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

            _testOutputHelper.WriteLine($"ruleJson:{Environment.NewLine}{ruleJson}");
            // re-hydrate from json
            var ruleFromJson = JsonConvert.DeserializeObject <MethodCallRule <string, string> >(ruleJson, converter);

            var compileResult = ruleFromJson.Compile();

            compileResult.Should().BeTrue();

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

            FOO.Should().Be("FOO");
        }