Skip to content

feat: ability to inherit from GenerateAutoInterfaceAttribute. #28

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 InterfaceGenerator.Contract/AutoInterfaceIgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace InterfaceGenerator
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = true)]
public class AutoInterfaceIgnoreAttribute : Attribute
{
}
}
23 changes: 23 additions & 0 deletions InterfaceGenerator.Contract/AutoInterfaceNameTemplateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace InterfaceGenerator
{
/// <summary>
/// Mark the attribute derived from GenerateAutoInterfaceAttribute or from GenerateGenericAutoInterfaceAttribute
/// with the attribute to override interface name template.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public sealed class AutoInterfaceNameTemplateAttribute : Attribute
{
/// <param name="NameTemplate">Default is "I{Name}"</param>
public AutoInterfaceNameTemplateAttribute(string NameTemplate)
{
this.NameTemplate = NameTemplate;
}

/// <summary>
/// Default is "I{Name}"
/// </summary>
public string NameTemplate { get; set; } = "I{Name}";
}
}
23 changes: 23 additions & 0 deletions InterfaceGenerator.Contract/GenerateAutoInterfaceAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace InterfaceGenerator
{
/// <summary>
/// Mark the class/struct with the attribute to generate auto interface.
/// If an implementation is public or Visibility modifier is public then auto interface will derive from IAutoInterface.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true)]
public class GenerateAutoInterfaceAttribute : Attribute
{
public string? VisibilityModifier { get; set; }
public string? Name { get; set; }
/// <summary>
/// Default is "I{Name}"
/// </summary>
public string NameTemplate { get; set; } = "I{Name}";

public GenerateAutoInterfaceAttribute()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace InterfaceGenerator
{
/// <summary>
/// Mark the class/struct with the attribute to generate auto interface.
/// If an implementation is public or Visibility modifier is public then auto interface will derive from IAutoInterface<TImplementer>.
/// TImplementer must be public.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = true)]
public class GenerateGenericAutoInterfaceAttribute : Attribute
{
public string? VisibilityModifier { get; set; }
public string? Name { get; set; }
/// <summary>
/// Default is "I{Name}"
/// </summary>
public string NameTemplate { get; set; } = "I{Name}";

public GenerateGenericAutoInterfaceAttribute()
{
}
}
}
16 changes: 16 additions & 0 deletions InterfaceGenerator.Contract/IAutoInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace InterfaceGenerator
{
/// <summary>
/// The base interface for generated public auto interfaces
/// </summary>
public interface IAutoInterface
{
}

/// <summary>
/// The base generic interface for generated public auto interfaces, where T is an implementation type
/// </summary>
public interface IAutoInterface<T> : IAutoInterface
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
49 changes: 49 additions & 0 deletions InterfaceGenerator.Tests/GenericAutoInterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Reflection;
using FluentAssertions;
using Xunit;

namespace InterfaceGenerator.Tests;

public class GenericAutoInterfaceTests
{
[Fact]
public void GenericParametersGeneratedCorrectly()
{
var t = typeof(IGenericAutoInterfaceTestsService<,>);
var genericArgs = t.GetGenericArguments();

genericArgs.Should().HaveCount(2);
genericArgs[0].Name.Should().Be("TX");
genericArgs[1].Name.Should().Be("TY");

genericArgs[0].IsClass.Should().BeTrue();
genericArgs[0]
.GenericParameterAttributes
.Should()
.HaveFlag(GenericParameterAttributes.DefaultConstructorConstraint);

var iEquatableOfTx = typeof(IEquatable<>).MakeGenericType(genericArgs[0]);
genericArgs[0].GetGenericParameterConstraints().Should().HaveCount(1).And.Contain(iEquatableOfTx);

genericArgs[1].IsValueType.Should().BeTrue();

// base
var interfaces = t.GetInterfaces();
interfaces.Should().HaveCount(2);
interfaces[0].Name.Should().Be(typeof(IAutoInterface<>).Name);
interfaces[1].Name.Should().Be(typeof(IAutoInterface).Name);
var interfaceGenericArgs = interfaces[0].GetGenericArguments();
interfaceGenericArgs.Should().HaveCount(1);
interfaceGenericArgs[0].Name.Should().Be(typeof(GenericAutoInterfaceTestsService<,>).Name);
}
}

[GenerateGenericAutoInterface]
// ReSharper disable once UnusedType.Global
public class GenericAutoInterfaceTestsService<TX, TY> : IGenericAutoInterfaceTestsService<TX, TY>
where TX : class, IEquatable<TX>, new ()
where TY : struct
{
public string A { get; set; }
}
24 changes: 23 additions & 1 deletion InterfaceGenerator.Tests/GenericInterfaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class GenericInterfaceTests
[Fact]
public void GenericParametersGeneratedCorrectly()
{
var genericArgs = typeof(IGenericInterfaceTestsService<,>).GetGenericArguments();
var t = typeof(IGenericInterfaceTestsService<,>);
var genericArgs = t.GetGenericArguments();

genericArgs.Should().HaveCount(2);
genericArgs[0].Name.Should().Be("TX");
Expand All @@ -26,6 +27,19 @@ public void GenericParametersGeneratedCorrectly()
genericArgs[0].GetGenericParameterConstraints().Should().HaveCount(1).And.Contain(iEquatableOfTx);

genericArgs[1].IsValueType.Should().BeTrue();

// does not implement IAutoInterface because not public
t.GetInterfaces().Should().HaveCount(0);
}

[Fact]
public void ImplementsIAutoInterface()
{
var t = typeof(IGenericInterfaceTestsService2<,>);

var interfaces = t.GetInterfaces();
interfaces.Should().HaveCount(1);
interfaces[0].Should().Be(typeof(IAutoInterface));
}
}

Expand All @@ -35,4 +49,12 @@ internal class GenericInterfaceTestsService<TX, TY> : IGenericInterfaceTestsServ
where TX : class, IEquatable<TX>, new()
where TY : struct
{
}

[GenerateAutoInterface]
// ReSharper disable once UnusedType.Global
public class GenericInterfaceTestsService2<TX, TY> : IGenericInterfaceTestsService2<TX, TY>
where TX : class, IEquatable<TX>, new()
where TY : struct
{
}
36 changes: 23 additions & 13 deletions InterfaceGenerator.Tests/InterfaceGenerator.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\InterfaceGenerator\InterfaceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<!-- Rererence the source generator project -->
<ProjectReference Include="..\InterfaceGenerator\InterfaceGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
<!-- Don't reference the generator dll -->

<!-- Rererence the contract project "treat as an analyzer"-->
<ProjectReference Include="..\InterfaceGenerator.Contract\InterfaceGenerator.Contract.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="true" />
<!-- We DO reference the contract dll -->
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions InterfaceGenerator.Tests/InterfaceNameTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace InterfaceGenerator.Tests;

[GenerateAutoInterface(Name = "ICustomNameInterface")]
internal class CustomName1 : ICustomNameInterface
{

}

[GenerateAutoInterface(NameTemplate = "IPrefix{Name}Suffix")]
internal class CustomName2 : IPrefixCustomName2Suffix
{

}

[NestedCustomName3(NameTemplate = "INested{Name}")]
internal class CustomName3 : INestedCustomName3
{

}

public class NestedCustomName3Attribute : GenerateAutoInterfaceAttribute
{
}

[NestedCustomName4]
public class CustomName4 : INestedCustomName4
{

}

[AutoInterfaceNameTemplate("INested{Name}")]
public class NestedCustomName4Attribute : GenerateGenericAutoInterfaceAttribute
{
}
Loading