Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicates when overriding or shadowing methods #51

Merged
merged 2 commits into from
Aug 4, 2024
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
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);
}
}
Loading