From 313ab02e8b6dacced51b36d3ee900c2a5fe91b61 Mon Sep 17 00:00:00 2001 From: Tom Biddulph Date: Fri, 6 Feb 2026 11:41:06 +0000 Subject: [PATCH] Refactor PropertyResolverGenerator to remove commented code and simplify logic; update tests for generated methods --- .../PropertyResolverGenerator.cs | 11 ----------- .../PropertyResolverGeneratorTests.cs | 8 ++++++-- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/PropertyResolvers.Generators/PropertyResolverGenerator.cs b/src/PropertyResolvers.Generators/PropertyResolverGenerator.cs index 3ce9490..becae4a 100644 --- a/src/PropertyResolvers.Generators/PropertyResolverGenerator.cs +++ b/src/PropertyResolvers.Generators/PropertyResolverGenerator.cs @@ -19,11 +19,6 @@ public class PropertyResolverGenerator : IIncrementalGenerator /// public void Initialize(IncrementalGeneratorInitializationContext context) { - // // Register the attribute source - // context.RegisterPostInitializationOutput(ctx => ctx.AddSource( - // "GeneratePropertyResolverAttribute.g.cs", - // SourceText.From(AttributeSourceCode, Encoding.UTF8))); - // Get configs and root namespace from compilation var compilationData = context.CompilationProvider .Select((compilation, _) => ( @@ -239,18 +234,12 @@ private static void GenerateResolvers( .Select(p => (TypeFullName: t.FullName, PropertyName: p.Name, p.IsNullable))) .ToList(); - if (matches.Count == 0) - { - continue; - } - const string returnType = "string?"; var methodName = $"Get{config.PropertyName}"; methods.AppendLine($" public static {returnType} {methodName}(object? obj) => obj switch"); methods.AppendLine(" {"); - foreach (var (typeName, propertyName, isNullable) in matches) { var toStringCall = isNullable ? "?.ToString()" : ".ToString()"; diff --git a/tests/PropertyResolvers.Tests/PropertyResolverGeneratorTests.cs b/tests/PropertyResolvers.Tests/PropertyResolverGeneratorTests.cs index d073d1e..6103bf1 100644 --- a/tests/PropertyResolvers.Tests/PropertyResolverGeneratorTests.cs +++ b/tests/PropertyResolvers.Tests/PropertyResolverGeneratorTests.cs @@ -217,7 +217,8 @@ public class Order var generatedCode = generatedFile.GetText().ToString(); Assert.Contains("public static class NonExistentPropertyResolver", generatedCode); - Assert.DoesNotContain("public static string?", generatedCode); + Assert.Contains("public static string? GetNonExistentProperty(object? obj)", generatedCode); + Assert.Contains("_ => null", generatedCode); } [Fact] @@ -449,7 +450,10 @@ public class AnotherGeneric var generatedCode = generatedFile.GetText().ToString(); Assert.Contains("public static class AccountIdResolver", generatedCode); - Assert.DoesNotContain("public static string?", generatedCode); + Assert.Contains("public static string? GetAccountId(object? obj)", generatedCode); + Assert.Contains("_ => null", generatedCode); + // No type-specific match arms since all types are generic + Assert.DoesNotContain("global::TestNamespace", generatedCode); } [Fact]