-
-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24637d0
commit 5d446b2
Showing
10 changed files
with
105 additions
and
105 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}));"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |