Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion EasySourceGenerators.Examples/PiExample.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using EasySourceGenerators.Abstractions;
// SwitchCase/SwitchDefault attribute-based generation is commented out pending replacement with a data-driven approach.
// See DataMethodBodyBuilders.cs for details on the planned replacement.

/*
using EasySourceGenerators.Abstractions;
// ReSharper disable ConvertClosureToMethodGroup

namespace EasySourceGenerators.Examples;
Expand All @@ -18,6 +22,7 @@ static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
[SwitchDefault]
static Func<int, int> GetPiDecimal_Generator_Fallback() => decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber);
}
*/

/*
This will generate the following method:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ public static class GenHost
Assert.That(result.error, Does.Contain("FormatException"));
}

// ExecuteFluentGeneratorMethod with SwitchBodyData is commented out pending replacement
// with the data abstraction layer. See DataMethodBodyBuilders.cs for details.
/*
[Test]
public void ExecuteFluentGeneratorMethod_CollectsSwitchBodyData()
{
Expand Down Expand Up @@ -246,9 +249,49 @@ private static IMethodBodyGenerator GenerateMethod()
Assert.That(result.record.CasePairs.Select(pair => pair.key), Is.EqualTo(new object[] { 1, 2, 3, 4 }));
Assert.That(result.record.CasePairs.Select(pair => pair.value), Is.EqualTo(new[] { "2", "4", "6", "8" }));
}
*/

[Test]
public void ExecuteFluentGeneratorMethod_ReturnsErrorWhenAbstractionsReferenceIsMissing()
public void ExecuteFluentBodyGeneratorMethod_WithBodyReturningConstant_ExtractsReturnValue()
{
CSharpCompilation compilation = CreateCompilation("""
using EasySourceGenerators.Abstractions;

namespace TestNamespace;

public static partial class Target
{
public static partial string GetValue();
}

public static class GenHost
{
public static IMethodBodyGenerator Generate()
{
return global::EasySourceGenerators.Abstractions.Generate
.MethodBody()
.ForMethod()
.WithReturnType<string>()
.WithNoParameters()
.BodyReturningConstant(() => "hello fluent");
}
}
""");

IMethodSymbol generatorMethod = GetMethodSymbol(compilation, "TestNamespace.GenHost", "Generate");
IMethodSymbol partialMethod = GetMethodSymbol(compilation, "TestNamespace.Target", "GetValue");

(FluentBodyResult? result, string? error) outcome =
GeneratesMethodExecutionRuntime.ExecuteFluentBodyGeneratorMethod(generatorMethod, partialMethod, compilation);

Assert.That(outcome.error, Is.Null);
Assert.That(outcome.result, Is.Not.Null);
Assert.That(outcome.result!.ReturnValue, Is.EqualTo("hello fluent"));
Assert.That(outcome.result.IsVoid, Is.False);
}

[Test]
public void ExecuteFluentBodyGeneratorMethod_ReturnsErrorWhenAbstractionsReferenceIsMissing()
{
CSharpCompilation originalCompilation = CreateCompilation("""
namespace TestNamespace;
Expand Down Expand Up @@ -279,11 +322,11 @@ public static string Generate()
IMethodSymbol generatorMethod = GetMethodSymbol(originalCompilation, "TestNamespace.GenHost", "Generate");
IMethodSymbol partialMethod = GetMethodSymbol(originalCompilation, "TestNamespace.Target", "GetValue");

(SwitchBodyData? record, string? error) result =
GeneratesMethodExecutionRuntime.ExecuteFluentGeneratorMethod(generatorMethod, partialMethod, compilation);
(FluentBodyResult? result, string? error) outcome =
GeneratesMethodExecutionRuntime.ExecuteFluentBodyGeneratorMethod(generatorMethod, partialMethod, compilation);

Assert.That(result.record, Is.Null);
Assert.That(result.error, Does.StartWith("Compilation failed:"));
Assert.That(outcome.result, Is.Null);
Assert.That(outcome.error, Does.StartWith("Compilation failed:"));
}

private static CSharpCompilation CreateCompilation(
Expand Down
15 changes: 15 additions & 0 deletions EasySourceGenerators.GeneratorTests/GeneratorDiagnosticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ public partial class MyClass

// -----------------------------------------------------------------------
// MSGH005 – Generator method has too many parameters (switch pattern)
// SwitchCase-based tests are commented out pending replacement with data-driven approach
// -----------------------------------------------------------------------

/*
[Test]
public void GeneratesMethod_SwitchCaseWithMoreThanOneParameter_EmitsMSGH005()
{
Expand Down Expand Up @@ -166,6 +168,7 @@ public static partial class MyClass
Assert.That(diagnostics.Any(d => d.Id == "MSGH005"), Is.False,
"Should not emit MSGH005 for a generator method with zero parameters");
}
*/

// -----------------------------------------------------------------------
// MSGH004 – Generator method execution failed (unfinished fluent API)
Expand Down Expand Up @@ -201,8 +204,10 @@ private static IMethodBodyGenerator GetValue_Generator() =>

// -----------------------------------------------------------------------
// Partial method called from fluent API / generator execution
// SwitchCase-based tests are commented out pending replacement
// -----------------------------------------------------------------------

/*
[Test]
public void GeneratesMethod_PartialMethodCalledInsideGenerator_EmitsMSGH004WithHelpfulMessage()
{
Expand Down Expand Up @@ -243,11 +248,14 @@ private static int GetValue_Generator(int key)
.Or.Contain("partial method"),
"Error message should hint that a partial method was called during generation");
}
*/

// -----------------------------------------------------------------------
// MSGH006 – SwitchCase argument type mismatch
// SwitchCase-based tests are commented out pending replacement
// -----------------------------------------------------------------------

/*
[Test]
public void GeneratesMethod_SwitchCaseWithWrongArgumentType_EmitsMSGH006()
{
Expand Down Expand Up @@ -299,6 +307,7 @@ public static partial class MyClass
Assert.That(diagnostics.Any(d => d.Id == "MSGH006"), Is.False,
"Should not emit MSGH006 when SwitchCase argument type matches the parameter type");
}
*/

// -----------------------------------------------------------------------
// Type mismatch between generator return type and partial method return type
Expand Down Expand Up @@ -358,6 +367,8 @@ public partial class MyClass
"Valid generator configuration should produce no error diagnostics");
}

// SwitchCase-based tests are commented out pending replacement with data-driven approach
/*
[Test]
public void GeneratesMethod_SwitchCasePattern_ValidConfiguration_ProducesNoDiagnosticErrors()
{
Expand All @@ -382,6 +393,7 @@ public static partial class MyClass
Assert.That(diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error), Is.Empty,
"Valid switch-case generator configuration should produce no error diagnostics");
}
*/

// -----------------------------------------------------------------------
// CompilationReference scenario – simulates Rider's code inspector
Expand Down Expand Up @@ -452,8 +464,10 @@ public partial class MyClass

// -----------------------------------------------------------------------
// Bool switch parameter – valid configuration
// SwitchCase-based tests are commented out pending replacement
// -----------------------------------------------------------------------

/*
[Test]
public void GeneratesMethod_SwitchCaseWithBoolParameter_ValidConfiguration_ProducesNoDiagnosticErrors()
{
Expand Down Expand Up @@ -483,4 +497,5 @@ public static partial class MyClass
Assert.That(diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error), Is.Empty,
"Valid bool switch case generator should produce no error diagnostics");
}
*/
}
Loading
Loading