Skip to content

Commit

Permalink
Prevent duplicates when overriding
Browse files Browse the repository at this point in the history
The current code will insert multiple definitions of a method when that method has been overridden.

This change fixes this problem by excluding override methods when generating class definitions, as the base method will always be included in the set of class members
  • Loading branch information
Simon McKenzie committed Jun 14, 2024
1 parent 218ba17 commit 1e57b19
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ InterfaceBuilder codeGenerator
.Where(x => x.Kind == SymbolKind.Method)
.OfType<IMethodSymbol>()
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
.Where(x => !x.IsOverride)
.Where(x => x.MethodKind == MethodKind.Ordinary)
.Where(x => !x.IsStatic)
.Where(x => x.ContainingType.Name != nameof(Object))
Expand Down
49 changes: 49 additions & 0 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,4 +2211,53 @@ public partial interface ISecondClass
""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithOverrides()
{
const string code = """
using AutomaticInterfaceAttribute;
namespace AutomaticInterfaceExample;
public class BaseClass
{
public virtual bool AMethod();
}
[GenerateAutomaticInterface]
public class DemoClass : BaseClass
{
public override bool AMethod() => return true;
}
""";

const string expected = """
//--------------------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//--------------------------------------------------------------------------------------------------
using System.CodeDom.Compiler;
using AutomaticInterfaceAttribute;
namespace AutomaticInterfaceExample
{
[GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
bool AMethod();
}
}
""";
GenerateCode(code).Should().Be(expected);
}
}

0 comments on commit 1e57b19

Please sign in to comment.