Skip to content

Commit

Permalink
Merge pull request #51 from simonmckenzie/fix/prevent-duplicates-when…
Browse files Browse the repository at this point in the history
…-overriding-or-shadowing

Prevent duplicates when overriding or shadowing methods
  • Loading branch information
ChristianSauer authored Aug 4, 2024
2 parents 218ba17 + 668ccfe commit c3421b7
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
9 changes: 9 additions & 0 deletions AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public static class Builder
{
private const string InheritDoc = "/// <inheritdoc />"; // we use inherit doc because that should be able to fetch documentation from base classes.

private static readonly SymbolDisplayFormat MethodSignatureDisplayFormat =
new(
memberOptions: SymbolDisplayMemberOptions.IncludeParameters,
parameterOptions: SymbolDisplayParameterOptions.IncludeType
| SymbolDisplayParameterOptions.IncludeParamsRefOut
);

public static string BuildInterfaceFor(ITypeSymbol typeSymbol)
{
if (
Expand Down Expand Up @@ -72,6 +79,8 @@ InterfaceBuilder codeGenerator
.Where(x => x.MethodKind == MethodKind.Ordinary)
.Where(x => !x.IsStatic)
.Where(x => x.ContainingType.Name != nameof(Object))
.GroupBy(x => x.ToDisplayString(MethodSignatureDisplayFormat))
.Select(g => g.First())
.ToList()
.ForEach(method =>
{
Expand Down
147 changes: 147 additions & 0 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,4 +2211,151 @@ public partial interface ISecondClass
""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithMethodOverrides()
{
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);
}

[Fact]
public void WorksWithMethodShadowing()
{
const string code = """
using AutomaticInterfaceAttribute;
namespace AutomaticInterfaceExample;
public class BaseClass
{
public bool AMethod();
}
[GenerateAutomaticInterface]
public class DemoClass : BaseClass
{
public new 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);
}

[Fact]
public void WorksWithParameterDirectionOverloads()
{
const string code = """
using AutomaticInterfaceAttribute;
namespace AutomaticInterfaceExample;
[GenerateAutomaticInterface]
public class DemoClass
{
public void AMethod(int val) => return true;
public void AMethod(ref int val) => 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 />
void AMethod(int val);
/// <inheritdoc />
void AMethod(ref int val);
}
}
""";
GenerateCode(code).Should().Be(expected);
}
}

0 comments on commit c3421b7

Please sign in to comment.