Skip to content

Commit

Permalink
Restructured test method generators
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Mar 19, 2017
1 parent 24637d0 commit 5d446b2
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 105 deletions.
27 changes: 0 additions & 27 deletions generators/BooleanTestMethod.cs

This file was deleted.

16 changes: 16 additions & 0 deletions generators/BooleanTestMethodGenerator.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
41 changes: 0 additions & 41 deletions generators/EqualityTestMethod.cs

This file was deleted.

8 changes: 8 additions & 0 deletions generators/EqualityTestMethodGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Generators
{
public class EqualityTestMethodGenerator : TestMethodGenerator
{
protected override string GetBody()
=> $"Assert.Equal({Expected}, {TestedClassName}.{TestedMethod}({Input}));";
}
}
2 changes: 1 addition & 1 deletion generators/Exercises/AcronymExercise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public AcronymExercise() : base("acronym")
protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
{
testMethodData.InputProperty = "phrase";
return new EqualityTestMethod(testMethodData);
return CreateEqualityTestMethod(testMethodData);
}
}
}
29 changes: 19 additions & 10 deletions generators/Exercises/Exercise.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
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;
}

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<TestMethod> 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);
}
}
2 changes: 1 addition & 1 deletion generators/Exercises/LeapExercise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public LeapExercise() : base("leap")
}

protected override TestMethod CreateTestMethod(TestMethodData testMethodData)
=> new BooleanTestMethod(testMethodData);
=> CreateBooleanTestMethod(testMethodData);
}
}
8 changes: 8 additions & 0 deletions generators/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
25 changes: 0 additions & 25 deletions generators/TestMethodBase.cs

This file was deleted.

52 changes: 52 additions & 0 deletions generators/TestMethodGenerator.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}

0 comments on commit 5d446b2

Please sign in to comment.