Skip to content

Commit

Permalink
Fix code generation for interfaces with overloaded methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoX104447 committed Jan 23, 2025
1 parent 8022968 commit 7926059
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/MockMe.Generator/Extensions/MethodSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,26 @@ public static string GetPropertyName(this IMethodSymbol methodSymbol)
}

public static string GetUniqueMethodName(this IMethodSymbol methodSymbol)
=> GenerateUniqueName(methodSymbol, false);
public static string GetUniqueStoreName(this IMethodSymbol methodSymbol)
=> GenerateUniqueName(methodSymbol, true);

private static string GenerateUniqueName(IMethodSymbol methodSymbol, bool includeContainingType)
{
var methodName = methodSymbol.Name;
var parameterTypes = methodSymbol.Parameters.Select(p =>
var parameterTypes = string.Join("_", methodSymbol.Parameters.Select(p =>
(p.RefKind == RefKind.None ? "" : p.RefKind.ToString()) + p.Type.Name
);
var uniqueMethodName = $"{methodName}_{string.Join("_", parameterTypes)}";
return uniqueMethodName;
));
if (!includeContainingType)
{
return $"{methodName}_{parameterTypes}";
}

var containingType = methodSymbol.ContainingType?.Name ?? "global";
var accessModifier = methodSymbol.DeclaredAccessibility;
var returnType = methodSymbol.ReturnType.Name;
var typeParameters = string.Join("_", methodSymbol.TypeParameters.Select(tp => tp.Name));

return $"{containingType}_{accessModifier}_{returnType}_{methodName}_{typeParameters}_{parameterTypes}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ protected string GetArgCollectionName(bool includeGenericType = true)
return this.methodSymbol.GetUniqueMethodName() + "Collection";
}

protected string GetBagStoreName() => this.methodSymbol.GetUniqueMethodName() + "BagStore";
protected string GetBagStoreName() => this.methodSymbol.GetUniqueStoreName() + "BagStore";

protected string GetCallStoreName() => this.methodSymbol.GetUniqueMethodName() + "CallStore";
protected string GetCallStoreName() => this.methodSymbol.GetUniqueStoreName() + "CallStore";

protected string GetSetupMethod()
{
Expand Down
19 changes: 19 additions & 0 deletions tests/MockMe.Tests.ExampleClasses/Interfaces/ICalculator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
namespace MockMe.Tests.ExampleClasses.Interfaces
{
public interface ISymbolVisitor
{
void VisitAddition(IAddition addition);
void VisitSubtraction(ISubtraction subtraction);
void VisitMultiplication(IMultiplication multiplication);
void VisitDivision(IDivision division);
}

public interface ISymbolVisitor<out T>
{
T VisitAddition(IAddition addition);
T VisitSubtraction(ISubtraction subtraction);
T VisitMultiplication(IMultiplication multiplication);
T VisitDivision(IDivision division);
}

public interface IAddition
{
int Add(int x, int y);
Expand All @@ -26,5 +42,8 @@ public interface ICalculator : IAddition, ISubtraction, IMultiplication, IDivisi
void DivideByZero(double numToDivide);
bool IsOn();
void TurnOff();

void Accept(ISymbolVisitor visitor);
T Accept<T>(ISymbolVisitor<T> visitor);
}
}

0 comments on commit 7926059

Please sign in to comment.