Skip to content

Commit

Permalink
Fully qualify type names to prevent types with the same name from bei…
Browse files Browse the repository at this point in the history
…ng seen as identical

- Updated the test to show that nested generics work correctly
- Added test to ensure same-named generic parameters are distinguishable from one another
  • Loading branch information
simonmckenzie committed Sep 1, 2024
1 parent c660b14 commit 85c5aa7
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
3 changes: 2 additions & 1 deletion AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public static class Builder
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
memberOptions: SymbolDisplayMemberOptions.IncludeParameters,
parameterOptions: SymbolDisplayParameterOptions.IncludeType
| SymbolDisplayParameterOptions.IncludeParamsRefOut
| SymbolDisplayParameterOptions.IncludeParamsRefOut,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces
);

private static readonly SymbolDisplayFormat TypeDisplayFormat =
Expand Down
80 changes: 66 additions & 14 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1788,20 +1788,29 @@ public partial interface IDemoClass
}

[Fact]
public void WorksWithGenericParameterOverloads()
public void WorksWithGenericParameterOverloadsWithIdenticalTypeNames()
{
const string code = """
using AutomaticInterface;
namespace AutomaticInterfaceExample;
[GenerateAutomaticInterface]
public class DemoClass
namespace AutomaticInterfaceExample
{
public void AMethod(Func<int> getValue) {}
public void AMethod(Func<float> getValue) {}
namespace Types1 {
public class Model;
}
namespace Types2 {
public class Model;
}
[GenerateAutomaticInterface]
public class DemoClass
{
public void AMethod(Func<Task<Types1.Model>> getValue) {}
public void AMethod(Func<Task<Types2.Model>> getValue) {}
}
}
""";
Expand All @@ -1815,19 +1824,16 @@ public void AMethod(Func<float> getValue) {}
// </auto-generated>
//--------------------------------------------------------------------------------------------------
using System.CodeDom.Compiler;
using AutomaticInterface;
namespace AutomaticInterfaceExample
{
[GeneratedCode("AutomaticInterface", "")]
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
void AMethod(Func<int> getValue);
void AMethod(Func<Task<global::AutomaticInterfaceExample.Types1.Model>> getValue);
/// <inheritdoc />
void AMethod(Func<float> getValue);
void AMethod(Func<Task<global::AutomaticInterfaceExample.Types2.Model>> getValue);
}
}
Expand All @@ -1836,6 +1842,52 @@ public partial interface IDemoClass
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithGenericParameterOverloads()
{
const string code = """
using AutomaticInterface;
namespace AutomaticInterfaceExample;
[GenerateAutomaticInterface]
public class DemoClass
{
public void AMethod(Func<Task<int>> getValue) {}
public void AMethod(Func<Task<float>> getValue) {}
}
""";

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>
//--------------------------------------------------------------------------------------------------
namespace AutomaticInterfaceExample
{
[global::System.CodeDom.Compiler.GeneratedCode("AutomaticInterface", "")]
public partial interface IDemoClass
{
/// <inheritdoc />
void AMethod(Func<Task<int>> getValue);
/// <inheritdoc />
void AMethod(Func<Task<float>> getValue);
}
}
""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithEventOverrides()
{
Expand Down

0 comments on commit 85c5aa7

Please sign in to comment.