From 7e493935d7d7f04c2e1bd6c5ee3b3689c79343a2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 28 Feb 2026 20:35:18 +0000 Subject: [PATCH 1/3] Initial plan From c58c636103d9bf873fff2ba7732311da1b46d098 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 28 Feb 2026 20:39:54 +0000 Subject: [PATCH 2/3] refactor fluent API to Method().WithParameter().WithReturnType() Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> --- MattSourceGenHelpers.Abstractions/Generate.cs | 43 ++++++++++++++-- .../ColorsClassFluent.cs | 4 +- .../PiExampleFluent.cs | 4 +- .../PiExampleFluentTests.cs | 50 ++++++++++++++++++- 4 files changed, 92 insertions(+), 9 deletions(-) diff --git a/MattSourceGenHelpers.Abstractions/Generate.cs b/MattSourceGenHelpers.Abstractions/Generate.cs index 3f13977..a5a66a6 100644 --- a/MattSourceGenHelpers.Abstractions/Generate.cs +++ b/MattSourceGenHelpers.Abstractions/Generate.cs @@ -4,9 +4,44 @@ public static class Generate { public static IGeneratorsFactory CurrentGenerator { get; set; } = new RecordingGeneratorsFactory(); - public static IMethodImplementationGenerator MethodImplementation() => CurrentGenerator.CreateImplementation(); - public static IMethodImplementationGenerator MethodImplementation() => CurrentGenerator.CreateImplementation(); - public static IMethodImplementationGenerator MethodImplementation() => CurrentGenerator.CreateImplementation(); + public static IMethodBuilder Method() => new MethodBuilder(CurrentGenerator); +} + +public interface IMethodBuilder +{ + IMethodBuilder WithParameter(); + IMethodImplementationGenerator WithReturnType(); +} + +public interface IMethodBuilder +{ + IMethodImplementationGenerator WithReturnType(); +} + +public class MethodBuilder : IMethodBuilder +{ + private readonly IGeneratorsFactory _generatorsFactory; + + public MethodBuilder(IGeneratorsFactory generatorsFactory) + { + _generatorsFactory = generatorsFactory; + } + + public IMethodBuilder WithParameter() => new MethodBuilder(_generatorsFactory); + + public IMethodImplementationGenerator WithReturnType() => _generatorsFactory.CreateImplementation(); +} + +public class MethodBuilder : IMethodBuilder +{ + private readonly IGeneratorsFactory _generatorsFactory; + + public MethodBuilder(IGeneratorsFactory generatorsFactory) + { + _generatorsFactory = generatorsFactory; + } + + public IMethodImplementationGenerator WithReturnType() => _generatorsFactory.CreateImplementation(); } public class EmptyGeneratorsFactory : IGeneratorsFactory @@ -40,4 +75,4 @@ public interface IGeneratorsFactory IMethodImplementationGenerator CreateImplementation(); IMethodImplementationGenerator CreateImplementation(); IMethodImplementationGenerator CreateImplementation(); -} \ No newline at end of file +} diff --git a/MattSourceGenHelpers.Examples/ColorsClassFluent.cs b/MattSourceGenHelpers.Examples/ColorsClassFluent.cs index 88816a9..1032b74 100644 --- a/MattSourceGenHelpers.Examples/ColorsClassFluent.cs +++ b/MattSourceGenHelpers.Examples/ColorsClassFluent.cs @@ -9,7 +9,7 @@ public partial class ColorsClassFluent [GeneratesMethod(nameof(GetAllColorsString))] static IMethodImplementationGenerator GetAllColorsString_Generator() => Generate - .MethodImplementation() + .Method().WithReturnType() .WithBody(() => string.Join(", ", Enum.GetNames())); } @@ -20,4 +20,4 @@ public string GetAllColorsString() { return "Red, Green, Blue"; } -*/ \ No newline at end of file +*/ diff --git a/MattSourceGenHelpers.Examples/PiExampleFluent.cs b/MattSourceGenHelpers.Examples/PiExampleFluent.cs index 43047e4..b147ef3 100644 --- a/MattSourceGenHelpers.Examples/PiExampleFluent.cs +++ b/MattSourceGenHelpers.Examples/PiExampleFluent.cs @@ -10,7 +10,7 @@ public static partial class PiExampleFluent [GeneratesMethod(nameof(GetPiDecimal))] static IMethodImplementationGenerator GetPiDecimal_Generator_Specialized() => Generate - .MethodImplementation() + .Method().WithParameter().WithReturnType() .WithSwitchBody() .ForCases(0, 1, 2, Integer.Range(300, 303)).ReturnConstantValue(decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber)) .ForDefaultCase().WithBody(decimalNumber => () => SlowMath.CalculatePiDecimal(decimalNumber)); @@ -32,4 +32,4 @@ public static int GetPiDecimal(int decimalNumber) default: return CalculatePiDecimal(decimalNumber); } } -*/ \ No newline at end of file +*/ diff --git a/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs b/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs index 6d769c1..6bd2bc3 100644 --- a/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs +++ b/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs @@ -49,6 +49,40 @@ public static partial int GetPiDecimal(int decimalNumber) Assert.That(generatedCode, Is.EqualTo(expectedCode)); } + + [TestCase(1, "Dog")] + [TestCase(2, "Cat")] + [TestCase(3, "Unknown")] + public void MapperFluentLikeGenerator_ProducesExpectedRuntimeOutput(int source, string expected) + { + string result = TestMapperFluent.MapToMammal(source); + + Assert.That(result, Is.EqualTo(expected)); + } + + [Test] + public void MapperFluentLikeGenerator_ProducesExpectedGeneratedCode() + { + string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("TestMapperFluent_MapToMammal.g.cs"); + string expectedCode = """ + namespace MattSourceGenHelpers.Tests; + + static partial class TestMapperFluent + { + public static partial string MapToMammal(int fourLeggedAnimal) + { + switch (fourLeggedAnimal) + { + case 1: return "Dog"; + case 2: return "Cat"; + default: return "Unknown"; + } + } + } + """.ReplaceLineEndings("\n").TrimEnd(); + + Assert.That(generatedCode, Is.EqualTo(expectedCode)); + } } public static partial class TestPiFluentClass @@ -58,8 +92,22 @@ public static partial class TestPiFluentClass [GeneratesMethod(nameof(GetPiDecimal))] static IMethodImplementationGenerator GetPiDecimal_Generator() => Generate - .MethodImplementation() + .Method().WithParameter().WithReturnType() .WithSwitchBody() .ForCases(0, 1, 2, Integer.Range(300, 303)).ReturnConstantValue(decimalNumber => TestSlowMath.CalculatePiDecimal(decimalNumber)) .ForDefaultCase().WithBody(decimalNumber => () => TestSlowMath.CalculatePiDecimal(decimalNumber)); } + +public static partial class TestMapperFluent +{ + public static partial string MapToMammal(int fourLeggedAnimal); + + [GeneratesMethod(nameof(MapToMammal))] + static IMethodImplementationGenerator MapToMammal_Generator() => + Generate + .Method().WithParameter().WithReturnType() + .WithSwitchBody() + .ForCases(1).ReturnConstantValue(_ => "Dog") + .ForCases(2).ReturnConstantValue(_ => "Cat") + .ForDefaultCase().WithBody(_ => () => "Unknown"); +} From b88c40b460ce04ee6727a23f983d57c424a02d24 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 28 Feb 2026 20:42:26 +0000 Subject: [PATCH 3/3] test: polish mapper-like fluent test naming Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com> --- MattSourceGenHelpers.Tests/PiExampleFluentTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs b/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs index 6bd2bc3..588b37f 100644 --- a/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs +++ b/MattSourceGenHelpers.Tests/PiExampleFluentTests.cs @@ -69,9 +69,9 @@ namespace MattSourceGenHelpers.Tests; static partial class TestMapperFluent { - public static partial string MapToMammal(int fourLeggedAnimal) + public static partial string MapToMammal(int animalCode) { - switch (fourLeggedAnimal) + switch (animalCode) { case 1: return "Dog"; case 2: return "Cat"; @@ -100,7 +100,7 @@ static IMethodImplementationGenerator GetPiDecimal_Generator() => public static partial class TestMapperFluent { - public static partial string MapToMammal(int fourLeggedAnimal); + public static partial string MapToMammal(int animalCode); [GeneratesMethod(nameof(MapToMammal))] static IMethodImplementationGenerator MapToMammal_Generator() =>