Skip to content

Commit 511fe30

Browse files
Copilotdex3r
andauthored
Refactor fluent generator API to explicit Method().WithParameter().WithReturnType() chain (#18)
* Initial plan * refactor fluent API to Method().WithParameter().WithReturnType() Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> * test: polish mapper-like fluent test naming Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
1 parent 0679469 commit 511fe30

4 files changed

Lines changed: 92 additions & 9 deletions

File tree

MattSourceGenHelpers.Abstractions/Generate.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,44 @@ public static class Generate
44
{
55
public static IGeneratorsFactory CurrentGenerator { get; set; } = new RecordingGeneratorsFactory();
66

7-
public static IMethodImplementationGenerator MethodImplementation() => CurrentGenerator.CreateImplementation();
8-
public static IMethodImplementationGenerator<TReturnType> MethodImplementation<TReturnType>() => CurrentGenerator.CreateImplementation<TReturnType>();
9-
public static IMethodImplementationGenerator<TArg1, TReturnType> MethodImplementation<TArg1, TReturnType>() => CurrentGenerator.CreateImplementation<TArg1, TReturnType>();
7+
public static IMethodBuilder Method() => new MethodBuilder(CurrentGenerator);
8+
}
9+
10+
public interface IMethodBuilder
11+
{
12+
IMethodBuilder<TArg1> WithParameter<TArg1>();
13+
IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>();
14+
}
15+
16+
public interface IMethodBuilder<TArg1>
17+
{
18+
IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>();
19+
}
20+
21+
public class MethodBuilder : IMethodBuilder
22+
{
23+
private readonly IGeneratorsFactory _generatorsFactory;
24+
25+
public MethodBuilder(IGeneratorsFactory generatorsFactory)
26+
{
27+
_generatorsFactory = generatorsFactory;
28+
}
29+
30+
public IMethodBuilder<TArg1> WithParameter<TArg1>() => new MethodBuilder<TArg1>(_generatorsFactory);
31+
32+
public IMethodImplementationGenerator<TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TReturnType>();
33+
}
34+
35+
public class MethodBuilder<TArg1> : IMethodBuilder<TArg1>
36+
{
37+
private readonly IGeneratorsFactory _generatorsFactory;
38+
39+
public MethodBuilder(IGeneratorsFactory generatorsFactory)
40+
{
41+
_generatorsFactory = generatorsFactory;
42+
}
43+
44+
public IMethodImplementationGenerator<TArg1, TReturnType> WithReturnType<TReturnType>() => _generatorsFactory.CreateImplementation<TArg1, TReturnType>();
1045
}
1146

1247
public class EmptyGeneratorsFactory : IGeneratorsFactory
@@ -40,4 +75,4 @@ public interface IGeneratorsFactory
4075
IMethodImplementationGenerator CreateImplementation();
4176
IMethodImplementationGenerator<TReturnType> CreateImplementation<TReturnType>();
4277
IMethodImplementationGenerator<TArg1, TReturnType> CreateImplementation<TArg1, TReturnType>();
43-
}
78+
}

MattSourceGenHelpers.Examples/ColorsClassFluent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public partial class ColorsClassFluent
99
[GeneratesMethod(nameof(GetAllColorsString))]
1010
static IMethodImplementationGenerator GetAllColorsString_Generator() =>
1111
Generate
12-
.MethodImplementation<string>()
12+
.Method().WithReturnType<string>()
1313
.WithBody(() => string.Join(", ", Enum.GetNames<ColorsEnum>()));
1414
}
1515

@@ -20,4 +20,4 @@ public string GetAllColorsString()
2020
{
2121
return "Red, Green, Blue";
2222
}
23-
*/
23+
*/

MattSourceGenHelpers.Examples/PiExampleFluent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static partial class PiExampleFluent
1010
[GeneratesMethod(nameof(GetPiDecimal))]
1111
static IMethodImplementationGenerator GetPiDecimal_Generator_Specialized() =>
1212
Generate
13-
.MethodImplementation<int, int>()
13+
.Method().WithParameter<int>().WithReturnType<int>()
1414
.WithSwitchBody()
1515
.ForCases(0, 1, 2, Integer.Range(300, 303)).ReturnConstantValue(decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber))
1616
.ForDefaultCase().WithBody(decimalNumber => () => SlowMath.CalculatePiDecimal(decimalNumber));
@@ -32,4 +32,4 @@ public static int GetPiDecimal(int decimalNumber)
3232
default: return CalculatePiDecimal(decimalNumber);
3333
}
3434
}
35-
*/
35+
*/

MattSourceGenHelpers.Tests/PiExampleFluentTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ public static partial int GetPiDecimal(int decimalNumber)
4949

5050
Assert.That(generatedCode, Is.EqualTo(expectedCode));
5151
}
52+
53+
[TestCase(1, "Dog")]
54+
[TestCase(2, "Cat")]
55+
[TestCase(3, "Unknown")]
56+
public void MapperFluentLikeGenerator_ProducesExpectedRuntimeOutput(int source, string expected)
57+
{
58+
string result = TestMapperFluent.MapToMammal(source);
59+
60+
Assert.That(result, Is.EqualTo(expected));
61+
}
62+
63+
[Test]
64+
public void MapperFluentLikeGenerator_ProducesExpectedGeneratedCode()
65+
{
66+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestMapperFluent_MapToMammal.g.cs");
67+
string expectedCode = """
68+
namespace MattSourceGenHelpers.Tests;
69+
70+
static partial class TestMapperFluent
71+
{
72+
public static partial string MapToMammal(int animalCode)
73+
{
74+
switch (animalCode)
75+
{
76+
case 1: return "Dog";
77+
case 2: return "Cat";
78+
default: return "Unknown";
79+
}
80+
}
81+
}
82+
""".ReplaceLineEndings("\n").TrimEnd();
83+
84+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
85+
}
5286
}
5387

5488
public static partial class TestPiFluentClass
@@ -58,8 +92,22 @@ public static partial class TestPiFluentClass
5892
[GeneratesMethod(nameof(GetPiDecimal))]
5993
static IMethodImplementationGenerator GetPiDecimal_Generator() =>
6094
Generate
61-
.MethodImplementation<int, int>()
95+
.Method().WithParameter<int>().WithReturnType<int>()
6296
.WithSwitchBody()
6397
.ForCases(0, 1, 2, Integer.Range(300, 303)).ReturnConstantValue(decimalNumber => TestSlowMath.CalculatePiDecimal(decimalNumber))
6498
.ForDefaultCase().WithBody(decimalNumber => () => TestSlowMath.CalculatePiDecimal(decimalNumber));
6599
}
100+
101+
public static partial class TestMapperFluent
102+
{
103+
public static partial string MapToMammal(int animalCode);
104+
105+
[GeneratesMethod(nameof(MapToMammal))]
106+
static IMethodImplementationGenerator MapToMammal_Generator() =>
107+
Generate
108+
.Method().WithParameter<int>().WithReturnType<string>()
109+
.WithSwitchBody()
110+
.ForCases(1).ReturnConstantValue(_ => "Dog")
111+
.ForCases(2).ReturnConstantValue(_ => "Cat")
112+
.ForDefaultCase().WithBody(_ => () => "Unknown");
113+
}

0 commit comments

Comments
 (0)