diff --git a/EasySourceGenerators.Generators/GeneratesMethodPatternSourceBuilder.cs b/EasySourceGenerators.Generators/GeneratesMethodPatternSourceBuilder.cs index 3d40772..5aab0ba 100644 --- a/EasySourceGenerators.Generators/GeneratesMethodPatternSourceBuilder.cs +++ b/EasySourceGenerators.Generators/GeneratesMethodPatternSourceBuilder.cs @@ -261,6 +261,11 @@ private static string GenerateSwitchMethodSource( private static void AppendNamespaceAndTypeHeader(StringBuilder builder, INamedTypeSymbol containingType, IMethodSymbol partialMethod) { + builder.AppendLine("// "); + builder.AppendLine($"// Generated by {typeof(GeneratesMethodGenerator).FullName} for method '{partialMethod.Name}'."); + builder.AppendLine("#pragma warning disable"); + builder.AppendLine(); + string? namespaceName = containingType.ContainingNamespace?.IsGlobalNamespace == false ? containingType.ContainingNamespace.ToDisplayString() : null; diff --git a/EasySourceGenerators.Tests/BoolSwitchKeyTests.cs b/EasySourceGenerators.Tests/BoolSwitchKeyTests.cs index f9b2ab3..70e947b 100644 --- a/EasySourceGenerators.Tests/BoolSwitchKeyTests.cs +++ b/EasySourceGenerators.Tests/BoolSwitchKeyTests.cs @@ -37,6 +37,16 @@ public static partial string GetBoolLabel(bool flag) Assert.That(generatedCode, Is.EqualTo(expectedCode)); } + + [Test] + public void BoolSwitchKey_GeneratedCode_HasAutoGeneratedHeaderAndWarningSuppression() + { + string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCodeRaw("TestBoolSwitchClass_GetBoolLabel.g.cs"); + + Assert.That(generatedCode, Does.StartWith("// ")); + Assert.That(generatedCode, Does.Contain("Generated by EasySourceGenerators.Generators.GeneratesMethodGenerator for method 'GetBoolLabel'")); + Assert.That(generatedCode, Does.Contain("#pragma warning disable")); + } } public static partial class TestBoolSwitchClass diff --git a/EasySourceGenerators.Tests/GeneratedCodeTestHelper.cs b/EasySourceGenerators.Tests/GeneratedCodeTestHelper.cs index ba4f5ef..091b472 100644 --- a/EasySourceGenerators.Tests/GeneratedCodeTestHelper.cs +++ b/EasySourceGenerators.Tests/GeneratedCodeTestHelper.cs @@ -5,11 +5,40 @@ internal static class GeneratedCodeTestHelper private const char UnicodeBom = '\uFEFF'; internal static string ReadGeneratedCode(string generatedFileName) + { + string rawCode = ReadGeneratedCodeRaw(generatedFileName); + return StripGeneratedPreamble(rawCode); + } + + internal static string ReadGeneratedCodeRaw(string generatedFileName) { string generatedCodePath = GetGeneratedCodePath(generatedFileName); return File.ReadAllText(generatedCodePath).TrimStart(UnicodeBom).ReplaceLineEndings("\n").TrimEnd(); } + private static string StripGeneratedPreamble(string generatedCode) + { + string[] lines = generatedCode.Split('\n'); + int index = 0; + + while (index < lines.Length) + { + string currentLine = lines[index]; + bool isGeneratedPreambleLine = currentLine.StartsWith("//", StringComparison.Ordinal) + || currentLine.StartsWith("#pragma warning disable", StringComparison.Ordinal) + || string.IsNullOrWhiteSpace(currentLine); + + if (!isGeneratedPreambleLine) + { + break; + } + + index++; + } + + return string.Join('\n', lines[index..]).TrimEnd(); + } + private static string GetGeneratedCodePath(string generatedFileName) { string projectDirectory = FindProjectDirectory();