Exemple #1
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            if (testMethodBody.UseVariableForExpected)
            {
                return(RenderComplexNumberAssert(testMethodBody));
            }

            return(base.RenderTestMethodBodyAssert(testMethodBody));
        }
Exemple #2
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            if (testMethodBody.CanonicalDataCase.Property == "consistency")
            {
                return(RenderConsistencyToAssert(testMethodBody));
            }

            return(base.RenderTestMethodBodyAssert(testMethodBody));
        }
Exemple #3
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            if (testMethodBody.CanonicalDataCase.Property == "IsAllergicTo")
            {
                return(RenderIsAllergicToAssert(testMethodBody));
            }

            return(base.RenderTestMethodBodyAssert(testMethodBody));
        }
Exemple #4
0
        private string RenderEqualBodyAssert(TestMethodBody testMethodBody)
        {
            const string template = @"Assert.Equal(expected, sut.{{ TestedMethodName }});";

            var templateParameters = new
            {
                TestedMethodName = testMethodBody.CanonicalDataCase.Property.ToTestedMethodName()
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #5
0
        private string RenderThrowsBodyAssert(TestMethodBody testMethodBody)
        {
            const string template = @"Assert.Throws<InvalidNucleotideException>(() => new NucleotideCount(""{{ Input }}""));";

            var templateParameters = new
            {
                Input = testMethodBody.CanonicalDataCase.Input["strand"]
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #6
0
        private string RenderDefaultMethodBodyAct(TestMethodBody testMethodBody)
        {
            string template = @"sut.{{MethodInvocation}}();";

            var templateParameters = new
            {
                MethodInvocation = testMethodBody.CanonicalDataCase.Property.ToTestedMethodName()
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #7
0
        protected override string RenderTestMethodBodyAct(TestMethodBody testMethodBody)
        {
            switch (testMethodBody.CanonicalDataCase.Property)
            {
            case "create": return(string.Empty);

            case "instructions": return(RenderInstructionsMethodBodyAct(testMethodBody));

            default: return(RenderDefaultMethodBodyAct(testMethodBody));
            }
        }
Exemple #8
0
        private static string RenderIsAllergicToAssert(TestMethodBody testMethodBody)
        {
            const string template =
                @"{%- for allergy in Allergies -%}
Assert.{% if allergy.result %}True{% else %}False{% endif %}(sut.IsAllergicTo(""{{ allergy.substance }}""));
{%- endfor -%}";

            var templateParameters = new { Allergies = testMethodBody.CanonicalDataCase.Expected };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #9
0
        protected override string RenderTestMethodBodyAct(TestMethodBody testMethodBody)
        {
            const string template = @"var result = {{MethodInvocation}};";

            var templateParameters = new
            {
                MethodInvocation = testMethodBody.Data.TestedMethodInvocation
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            var    input                     = testMethodBody.CanonicalDataCase.Properties["input"] as System.Collections.Generic.Dictionary <string, object>;
            var    operation                 = testMethodBody.CanonicalDataCase.Properties["property"].ToString();
            var    expected                  = testMethodBody.CanonicalDataCase.Properties["expected"];
            var    operationName             = char.ToUpper(operation[0]) + operation.Substring(1);
            string assertCodeLine            = "";
            string operationsWithOverloading = "add|+|sub|-|mul|*|div|/";
            string operationCode             = operationsWithOverloading.Substring(operationsWithOverloading.IndexOf(operation.ToLower()) + 4, 1);

            switch (operation.ToLower())
            {
            case "add":
            case "sub":
            case "mul":
            case "div":
            {
                var r1 = new RationalNumber((int[])input["r1"]);
                var r2 = new RationalNumber((int[])input["r2"]);
                var e  = new RationalNumber((int[])expected);
                assertCodeLine = "Assert.Equal(" + $"new RationalNumber ({e.Numerator}, {e.Denominator}), new RationalNumber({r1.Numerator}, {r1.Denominator}) {operationCode} (new RationalNumber({r2.Numerator}, {r2.Denominator})));";
            }
            break;

            case "abs":
            case "reduce":
            {
                var r = new RationalNumber((int[])input["r"]);
                var e = new RationalNumber((int[])expected);
                assertCodeLine = "Assert.Equal(" + $"new RationalNumber ({e.Numerator}, {e.Denominator}), new RationalNumber({r.Numerator}, {r.Denominator}).{operationName}());";
            }
            break;

            case "exprational":
            {
                var r = new RationalNumber((int[])input["r"]);
                var n = input["n"];
                var e = new RationalNumber((int[])expected);
                assertCodeLine = "Assert.Equal(" + $"new RationalNumber ({e.Numerator}, {e.Denominator}), new RationalNumber({r.Numerator}, {r.Denominator}).{operationName}({n}));";
            }
            break;

            case "expreal":
            {
                var x = input["x"].ToString();
                var r = new RationalNumber((int[])input["r"]);
                var e = expected;
                var p = precision(e);
                assertCodeLine = "Assert.Equal(" + $"{e}, {x}.{operationName}(new RationalNumber({r.Numerator}, {r.Denominator})),{p});";
            }
            break;
            }
            return(TemplateRenderer.RenderInline(assertCodeLine, testMethodBody.AssertTemplateParameters));
        }
Exemple #11
0
        private string RenderInstructionsMethodBodyAct(TestMethodBody testMethodBody)
        {
            string template = @"sut.{{MethodInvocation}}(""{{Instructions}}"");";

            var templateParameters = new
            {
                MethodInvocation = "Simulate",
                Instructions     = testMethodBody.CanonicalDataCase.Input["instructions"]
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #12
0
        private static string RenderConsistencyToAssert(TestMethodBody testMethodBody)
        {
            const string template = @"Assert.Equal(""{{ExpectedOutput}}"", {{ExerciseName}}.Decode({{ExerciseName}}.Encode(""{{ExpectedOutput}}"")));";

            var templateParameters = new
            {
                ExpectedOutput = testMethodBody.CanonicalDataCase.Expected,
                ExerciseName   = testMethodBody.CanonicalData.Exercise.ToTestedClassName()
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #13
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            var lines = new StringBuilder();

            lines.AppendLine(RenderSut(testMethodBody.CanonicalDataCase));

            foreach (var operation in testMethodBody.CanonicalDataCase.Input["operations"])
            {
                lines.AppendLine(RenderOperation(operation));
            }

            return(lines.ToString());
        }
Exemple #14
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            var expectedDictionary = testMethodBody.CanonicalDataCase.Properties["expected"] as IDictionary <string, dynamic>;

            var assert = new List <string>();

            foreach (var kv in expectedDictionary)
            {
                assert.Add(RenderTestMethodBodyAssertForSearchWord(kv.Key, kv.Value));
            }

            return(string.Join("\n", assert));
        }
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            if (testMethodBody.CanonicalDataCase.ExceptionThrown != null)
            {
                return(base.RenderTestMethodBodyAssert(testMethodBody));
            }

            return(string.Join("\n", new[]
            {
                "Assert.Equal(expected.Item1, actual.Item1);",
                "Assert.Equal(expected.Item2, actual.Item2);"
            }));
        }
Exemple #16
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            if (testMethodBody.CanonicalDataCase.Property == "canAttack")
            {
                return(RenderCanAttackAssert(testMethodBody));
            }

            if (testMethodBody.UseVariableForTested)
            {
                return(string.Empty);
            }

            return(base.RenderTestMethodBodyAssert(testMethodBody));
        }
Exemple #17
0
        private static string RenderEqualToAssert(TestMethodBody testMethodBody)
        {
            var ExpectedParameter = testMethodBody.CanonicalDataCase.Input[ParamClock2];
            var TestedValue       = "sut";
            var expectedEqual     = testMethodBody.CanonicalDataCase.Properties[PropertyEqualityExpectance];

            testMethodBody.AssertTemplateParameters = new { ExpectedParameter, TestedValue };

            var template = expectedEqual
                ? $"Assert.Equal({{{{ ExpectedParameter }}}}, {{{{ TestedValue }}}}); "
                : $"Assert.NotEqual({{{{ ExpectedParameter }}}}, {{{{ TestedValue }}}});";

            return(TemplateRenderer.RenderInline(template, testMethodBody.AssertTemplateParameters));
        }
Exemple #18
0
 protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
 {
     if (testMethodBody.CanonicalDataCase.Property == PropertyEquals)
     {
         return(RenderEqualToAssert(testMethodBody));
     }
     else if (testMethodBody.CanonicalDataCase.Property != PropertyToString)
     {
         return(RenderConsistencyToAssert(testMethodBody));
     }
     else
     {
         return(base.RenderTestMethodBodyAssert(testMethodBody));
     }
 }
Exemple #19
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            if (testMethodBody.CanonicalDataCase.Property == "new")
            {
                var key = ValueFormatter.Format(testMethodBody.CanonicalDataCase.Input["key"]);
                return($"Assert.Throws<ArgumentException>(() => new SimpleCipher({key}));");
            }
            else if (testMethodBody.CanonicalDataCase.Property == "key")
            {
                var pattern = ValueFormatter.Format(testMethodBody.CanonicalDataCase.Expected["match"]);
                return($"Assert.Matches({pattern}, sut.Key);");
            }

            return(base.RenderTestMethodBodyAssert(testMethodBody));
        }
Exemple #20
0
        protected override string RenderTestMethodBodyArrange(TestMethodBody testMethodBody)
        {
            var arrange = new StringBuilder();

            var tree = RenderTree(testMethodBody.CanonicalDataCase.Input["initialTree"]);

            arrange.AppendLine($"var tree = {tree};");
            arrange.AppendLine("var sut = Zipper.FromTree(tree);");

            var operations = RenderOperations(testMethodBody.CanonicalDataCase.Input["operations"]);

            arrange.AppendLine($"var actual = sut{operations};");

            return(arrange.ToString());
        }
Exemple #21
0
        protected override string RenderTestMethodBodyArrange(TestMethodBody testMethodBody)
        {
            var builder = new StringBuilder();

            builder.AppendLine("var sut = new BowlingGame();");

            if (testMethodBody.CanonicalDataCase.Properties.ContainsKey(PreviousRolls))
            {
                int[] array = testMethodBody.CanonicalDataCase.Properties[PreviousRolls];
                builder.Append("var previousRolls = new int[] {");
                builder.AppendJoin(", ", array);
                builder.AppendLine("};");
            }

            return(builder.ToString());
        }
Exemple #22
0
        protected override string RenderTestMethodBodyArrange(TestMethodBody testMethodBody)
        {
            const string template = @"var {{ObjectName}} = new RobotSimulator(Bearing.{{Bearing}}, new Coordinate({{X}}, {{Y}}));";

            dynamic input = testMethodBody.CanonicalDataCase.ConstructorInput["robot"];

            testMethodBody.AssertTemplateParameters = new
            {
                ObjectName,
                X       = input["position"]["x"],
                Y       = input["position"]["y"],
                Bearing = input["direction"]
            };

            return(TemplateRenderer.RenderInline(template.ToString(), testMethodBody.AssertTemplateParameters));
        }
Exemple #23
0
        protected override string RenderTestMethodBodyArrange(TestMethodBody testMethodBody)
        {
            var arrange = new StringBuilder();

            arrange.AppendLine("var sut = new Reactor();");

            var cells = RenderCells(testMethodBody.CanonicalDataCase.Input["cells"]);

            arrange.AppendLine(cells);

            var operations = RenderOperations(testMethodBody.CanonicalDataCase.Input["operations"]);

            arrange.AppendLine(operations);

            return(arrange.ToString());
        }
Exemple #24
0
        protected override string RenderTestMethodBodyArrange(TestMethodBody testMethodBody)
        {
            // The ValueFormatter doesn't handle Dictionary<int, IList<string>>. Need to format manually.
            Dictionary<int, string[]> inputDictionary = testMethodBody.CanonicalDataCase.Properties["input"];
            var newInput = FormatMultiLineEnumerable(
                inputDictionary.Keys.Select((key, i) => $"[{ValueFormatter.Format(key)}] = {ValueFormatter.Format(inputDictionary[key])}" + (i < inputDictionary.Keys.Count - 1 ? "," : "")), 
                "input", "new Dictionary<int, IList<string>>");
            newInput = AddTrailingSemicolon(newInput);

            var arrangeVariables = testMethodBody.Data.Variables.ToList();
            arrangeVariables.RemoveAt(0);
            arrangeVariables.InsertRange(0, newInput);
            testMethodBody.ArrangeTemplateParameters = new { Variables = arrangeVariables };

            return base.RenderTestMethodBodyArrange(testMethodBody);
        }
Exemple #25
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            const string template =
                @"Assert.Equal({{MovesExpected}}, result.Moves);
Assert.Equal({{OtherBucketExpected}}, result.OtherBucket);
Assert.Equal({% if GoalBucketExpected == 'two' %}Bucket.Two{% else %}Bucket.One{% endif %}, result.GoalBucket);";

            var templateParameters = new
            {
                MovesExpected       = testMethodBody.CanonicalDataCase.Expected["moves"],
                OtherBucketExpected = testMethodBody.CanonicalDataCase.Expected["other_bucket"],
                GoalBucketExpected  = testMethodBody.CanonicalDataCase.Expected["goal_bucket"],
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #26
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            var assert = new StringBuilder();

            var expected = RenderExpected(testMethodBody.CanonicalDataCase.Expected);

            if (expected == null)
            {
                assert.AppendLine("Assert.Null(actual);");
            }
            else
            {
                assert.AppendLine($"var expected = {expected};");
                assert.AppendLine("Assert.Equal(expected, actual);");
            }

            return(assert.ToString());
        }
Exemple #27
0
        private static string RenderCanAttackAssert(TestMethodBody testMethodBody)
        {
            const string template =
                @"var whiteQueen = QueenAttack.Create({{whiteQueenX}},{{whiteQueenY}});
var blackQueen = QueenAttack.Create({{blackQueenX}},{{blackQueenY}});
Assert.{% if Expected %}True{% else %}False{% endif %}(QueenAttack.CanAttack(whiteQueen, blackQueen));";

            var whiteQueenPositions = GetCoordinatesFromPosition(testMethodBody.CanonicalDataCase.Input["white_queen"]);
            var blackQueenPositions = GetCoordinatesFromPosition(testMethodBody.CanonicalDataCase.Input["black_queen"]);

            var templateParameters = new
            {
                whiteQueenX = whiteQueenPositions.Item1,
                whiteQueenY = whiteQueenPositions.Item2,
                blackQueenX = blackQueenPositions.Item1,
                blackQueenY = blackQueenPositions.Item2,
                Expected    = testMethodBody.CanonicalDataCase.Expected
            };

            return(TemplateRenderer.RenderInline(template, templateParameters));
        }
Exemple #28
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            var template = string.Empty;

            if (testMethodBody.CanonicalDataCase.ExceptionThrown != null && testMethodBody.CanonicalDataCase.Input.ContainsKey("roll"))
            {
                template = "Assert.Throws<ArgumentException>(() => sut.Roll({{RollVal}}));";
                var templateParams = new
                {
                    RollVal = testMethodBody.CanonicalDataCase.Input["roll"]
                };
                return(TemplateRenderer.RenderInline(template, templateParams));
            }
            else if (testMethodBody.CanonicalDataCase.ExceptionThrown != null && testMethodBody.CanonicalDataCase.Property == "score")
            {
                template = "Assert.Throws<ArgumentException>(() => sut.Score());";
                return(template);
            }

            return(base.RenderTestMethodBodyAssert(testMethodBody));
        }
Exemple #29
0
        protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
        {
            var template = new StringBuilder();
            var expected = (IDictionary <string, dynamic>)testMethodBody.CanonicalDataCase.Expected;

            if (expected.ContainsKey("position"))
            {
                template.AppendLine(@"Assert.Equal(new Coordinate({{X}}, {{Y}}), {{ObjectName}}.Coordinate);");
                testMethodBody.AssertTemplateParameters = new
                {
                    ObjectName,
                    X = expected["position"]["x"],
                    Y = expected["position"]["y"]
                };
            }

            if (expected.ContainsKey("direction"))
            {
                template.AppendLine(@"Assert.Equal(Bearing.{{Bearing}}, {{ObjectName}}.Bearing);");
                testMethodBody.AssertTemplateParameters = new
                {
                    ObjectName,
                    Bearing = expected["direction"]
                };
            }

            if (expected.ContainsKey("position") && expected.ContainsKey("direction"))
            {
                testMethodBody.AssertTemplateParameters = new
                {
                    ObjectName,
                    X       = expected["position"]["x"],
                    Y       = expected["position"]["y"],
                    Bearing = expected["direction"]
                };
            }

            return(TemplateRenderer.RenderInline(template.ToString(), testMethodBody.AssertTemplateParameters));
        }
Exemple #30
0
        protected override string RenderTestMethodBodyArrange(TestMethodBody testMethodBody)
        {
            var builder = new StringBuilder();

            builder.AppendLine("var sut = new BowlingGame();");

            if (testMethodBody.CanonicalDataCase.Input.ContainsKey(PreviousRolls))
            {
                var array = testMethodBody.CanonicalDataCase.Input[PreviousRolls] as int[];
                if (array == null)
                {
                    builder.Append("var previousRolls = new int[0];");
                }
                else
                {
                    builder.Append("var previousRolls = new [] { ");
                    builder.AppendJoin(", ", array);
                    builder.AppendLine(" };");
                }
            }

            return(builder.ToString());
        }