diff --git a/generators/BooleanTestMethod.cs b/generators/BooleanTestMethod.cs deleted file mode 100644 index 00e7321faa..0000000000 --- a/generators/BooleanTestMethod.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace Generators -{ - public class BooleanTestMethod : TestMethodBase - { - public BooleanTestMethod(TestMethodData testMethodData) : base(testMethodData) - { - } - - protected override string GetBody(TestMethodData testMethodData) - { - var isTrue = Convert.ToBoolean(testMethodData.CanonicalDataCase.Expected); - var testedClassName = GetTestedClassName(testMethodData); - var testedMethod = GetTestedMethod(testMethodData); - var input = testMethodData.CanonicalDataCase.Input; - - return $"Assert.{isTrue}({testedClassName}.{testedMethod}({input}));"; - } - - protected override string GetTestedMethod(TestMethodData testMethodData) - { - var testedMethod = base.GetTestedMethod(testMethodData); - return testedMethod.StartsWith("Is") ? testedMethod : "Is" + testedMethod; - } - } -} \ No newline at end of file diff --git a/generators/BooleanTestMethodGenerator.cs b/generators/BooleanTestMethodGenerator.cs new file mode 100644 index 0000000000..5ade13017c --- /dev/null +++ b/generators/BooleanTestMethodGenerator.cs @@ -0,0 +1,16 @@ +using System; + +namespace Generators +{ + public class BooleanTestMethodGenerator : TestMethodGenerator + { + protected override string GetBody() + => $"{Assertion}({TestedClassName}.{TestedMethod}({Input}));"; + + private string Assertion + => $"Assert.{Convert.ToBoolean(TestMethodData.CanonicalDataCase.Expected)}"; + + protected override string TestedMethod + => base.TestedMethod.EnsureStartsWith("Is"); + } +} \ No newline at end of file diff --git a/generators/EqualityTestMethod.cs b/generators/EqualityTestMethod.cs deleted file mode 100644 index ea3d0c5858..0000000000 --- a/generators/EqualityTestMethod.cs +++ /dev/null @@ -1,41 +0,0 @@ -namespace Generators -{ - public class EqualityTestMethod : TestMethodBase - { - public EqualityTestMethod(TestMethodData testMethodData) : base(testMethodData) - { - } - - protected override string GetBody(TestMethodData testMethodData) - { - var input = GetInput(testMethodData); - var expected = GetExpected(testMethodData); - var testedClassName = GetTestedClassName(testMethodData); - var testedMethod = GetTestedMethod(testMethodData); - - return $"Assert.Equal({expected}, {testedClassName}.{testedMethod}({input}));"; - } - - protected virtual object GetInput(TestMethodData testMethodData) - { - if (testMethodData.InputProperty != null) - return FormatValue(testMethodData.CanonicalDataCase.Data[testMethodData.InputProperty]); - - return FormatValue(testMethodData.CanonicalDataCase.Input); - } - - protected virtual object GetExpected(TestMethodData testMethodData) - => FormatValue(testMethodData.CanonicalDataCase.Expected); - - private static object FormatValue(object val) - { - switch (val) - { - case string s: - return $"\"{s}\""; - default: - return val; - } - } - } -} \ No newline at end of file diff --git a/generators/EqualityTestMethodGenerator.cs b/generators/EqualityTestMethodGenerator.cs new file mode 100644 index 0000000000..7508b2e254 --- /dev/null +++ b/generators/EqualityTestMethodGenerator.cs @@ -0,0 +1,8 @@ +namespace Generators +{ + public class EqualityTestMethodGenerator : TestMethodGenerator + { + protected override string GetBody() + => $"Assert.Equal({Expected}, {TestedClassName}.{TestedMethod}({Input}));"; + } +} \ No newline at end of file diff --git a/generators/Exercises/AcronymExercise.cs b/generators/Exercises/AcronymExercise.cs index aa0a3bdcb0..13c6878a21 100644 --- a/generators/Exercises/AcronymExercise.cs +++ b/generators/Exercises/AcronymExercise.cs @@ -9,7 +9,7 @@ public AcronymExercise() : base("acronym") protected override TestMethod CreateTestMethod(TestMethodData testMethodData) { testMethodData.InputProperty = "phrase"; - return new EqualityTestMethod(testMethodData); + return CreateEqualityTestMethod(testMethodData); } } } \ No newline at end of file diff --git a/generators/Exercises/Exercise.cs b/generators/Exercises/Exercise.cs index c4bc58b316..b9318f2cdc 100644 --- a/generators/Exercises/Exercise.cs +++ b/generators/Exercises/Exercise.cs @@ -1,10 +1,14 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using Humanizer; namespace Generators.Exercises { public abstract class Exercise { + private static readonly BooleanTestMethodGenerator BooleanTestMethodGenerator = new BooleanTestMethodGenerator(); + private static readonly EqualityTestMethodGenerator EqualityTestMethodGenerator = new EqualityTestMethodGenerator(); + protected Exercise(string name) { Name = name; @@ -12,20 +16,25 @@ protected Exercise(string name) public string Name { get; } - public TestClass CreateTestClass(CanonicalData canonicalData) + public TestClass CreateTestClass(CanonicalData canonicalData) => new TestClass { - return new TestClass - { - ClassName = Name.Transform(To.TestClassName), - TestMethods = canonicalData.Cases.Select((canonicalDataCase, index) => CreateTestMethod(new TestMethodData + ClassName = Name.Transform(To.TestClassName), + TestMethods = CreateTestMethods(canonicalData).ToArray() + }; + + private IEnumerable CreateTestMethods(CanonicalData canonicalData) + => canonicalData.Cases.Select((canonicalDataCase, index) => CreateTestMethod(new TestMethodData { CanonicalData = canonicalData, CanonicalDataCase = canonicalDataCase, Index = index - })).ToArray() - }; - } - + })); protected abstract TestMethod CreateTestMethod(TestMethodData testMethodData); + + protected TestMethod CreateBooleanTestMethod(TestMethodData testMethodData) + => BooleanTestMethodGenerator.Create(testMethodData); + + protected TestMethod CreateEqualityTestMethod(TestMethodData testMethodData) + => EqualityTestMethodGenerator.Create(testMethodData); } } \ No newline at end of file diff --git a/generators/Exercises/LeapExercise.cs b/generators/Exercises/LeapExercise.cs index 1d07fa1c1e..14f0e7f04a 100644 --- a/generators/Exercises/LeapExercise.cs +++ b/generators/Exercises/LeapExercise.cs @@ -7,6 +7,6 @@ public LeapExercise() : base("leap") } protected override TestMethod CreateTestMethod(TestMethodData testMethodData) - => new BooleanTestMethod(testMethodData); + => CreateBooleanTestMethod(testMethodData); } } \ No newline at end of file diff --git a/generators/StringExtensions.cs b/generators/StringExtensions.cs new file mode 100644 index 0000000000..b4d20a82b6 --- /dev/null +++ b/generators/StringExtensions.cs @@ -0,0 +1,8 @@ +namespace Generators +{ + public static class StringExtensions + { + public static string EnsureStartsWith(this string str, string value) + => str.StartsWith(value) ? str : value + str; + } +} \ No newline at end of file diff --git a/generators/TestMethodBase.cs b/generators/TestMethodBase.cs deleted file mode 100644 index 2d8c47e16e..0000000000 --- a/generators/TestMethodBase.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Humanizer; - -namespace Generators -{ - public abstract class TestMethodBase : TestMethod - { - protected TestMethodBase(TestMethodData testMethodData) - { - MethodName = GetMethodName(testMethodData); - Body = GetBody(testMethodData); - Index = testMethodData.Index; - } - - protected abstract string GetBody(TestMethodData testMethodData); - - protected virtual string GetMethodName(TestMethodData testMethodData) - => testMethodData.CanonicalDataCase.Description.Replace(":", " is").Transform(To.TestMethodName); - - protected virtual string GetTestedClassName(TestMethodData testMethodData) - => testMethodData.CanonicalData.Exercise.Transform(To.TestedClassName); - - protected virtual string GetTestedMethod(TestMethodData testMethodData) - => testMethodData.CanonicalDataCase.Property.Transform(To.TestedMethodName); - } -} \ No newline at end of file diff --git a/generators/TestMethodGenerator.cs b/generators/TestMethodGenerator.cs new file mode 100644 index 0000000000..b714785a9b --- /dev/null +++ b/generators/TestMethodGenerator.cs @@ -0,0 +1,52 @@ +using Humanizer; + +namespace Generators +{ + public abstract class TestMethodGenerator + { + public TestMethod Create(TestMethodData testMethodData) + { + TestMethodData = testMethodData; + + return new TestMethod + { + MethodName = MethodName, + Body = GetBody(), + Index = TestMethodData.Index + }; + } + + protected TestMethodData TestMethodData { get; private set; } + + protected abstract string GetBody(); + + protected virtual string MethodName + => TestMethodData.CanonicalDataCase.Description.Replace(":", " is").Transform(To.TestMethodName); + + protected virtual string TestedClassName + => TestMethodData.CanonicalData.Exercise.Transform(To.TestedClassName); + + protected virtual string TestedMethod + => TestMethodData.CanonicalDataCase.Property.Transform(To.TestedMethodName); + + protected virtual object Input => FormatValue(InputValue); + + protected virtual object Expected => FormatValue(TestMethodData.CanonicalDataCase.Expected); + + protected virtual object InputValue + => TestMethodData.InputProperty == null + ? TestMethodData.CanonicalDataCase.Input + : TestMethodData.CanonicalDataCase.Data[TestMethodData.InputProperty]; + + protected virtual object FormatValue(object val) + { + switch (val) + { + case string s: + return $"\"{s}\""; + default: + return val; + } + } + } +} \ No newline at end of file