Skip to content

Commit

Permalink
Enable internal classes
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherthomas committed Mar 20, 2024
1 parent 7d87360 commit b12d15b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 33 deletions.
5 changes: 4 additions & 1 deletion AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand All @@ -25,7 +26,9 @@ is not ClassDeclarationSyntax classSyntax

var interfaceName = $"I{classSyntax.GetClassName()}";

var interfaceGenerator = new InterfaceBuilder(namespaceName, interfaceName);
var accessSpecifier = typeSymbol.DeclaredAccessibility.ToString().ToLower(new CultureInfo("C"));

var interfaceGenerator = new InterfaceBuilder(namespaceName, interfaceName, accessSpecifier);

interfaceGenerator.AddUsings(GetUsings(typeSymbol));
interfaceGenerator.AddClassDocumentation(GetDocumentationForClass(classSyntax));
Expand Down
4 changes: 2 additions & 2 deletions AutomaticInterface/AutomaticInterface/InterfaceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal sealed record MethodInfo(

public record EventInfo(string Name, string Type, string Documentation);

public class InterfaceBuilder(string nameSpaceName, string interfaceName)
public class InterfaceBuilder(string nameSpaceName, string interfaceName, string accessSpecifier="public")
{
private const string Autogenerated = """
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -117,7 +117,7 @@ public string Build()
cb.AppendAndNormalizeMultipleLines(classDocumentation);

cb.AppendLine($"[GeneratedCode(\"AutomaticInterface\", \"\")]");
cb.AppendLine($"public partial interface {interfaceName}{genericType}");
cb.AppendLine($"{accessSpecifier} partial interface {interfaceName}{genericType}");
cb.AppendLine("{");

cb.Indent();
Expand Down
106 changes: 76 additions & 30 deletions AutomaticInterface/Tests/GeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void GeneratesEmptyInterface()
namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
}
}
Expand Down Expand Up @@ -244,7 +244,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string Hello { get; set; }
}
Expand Down Expand Up @@ -290,7 +290,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
private string x;
public string Hello { set => x = value; }
Expand Down Expand Up @@ -337,7 +337,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
private string x;
public string Hello { get; }
Expand Down Expand Up @@ -385,7 +385,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public DirectoryInfo Hello { get; set; }
}
Expand Down Expand Up @@ -433,7 +433,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string Hello(){return "";}
Expand Down Expand Up @@ -483,7 +483,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public async Task<string> Hello(){return "";}
}
Expand Down Expand Up @@ -532,7 +532,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string Hello(string x){return x;}
Expand Down Expand Up @@ -582,7 +582,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string Hello(Task<string> x){return "";}
}
Expand Down Expand Up @@ -631,7 +631,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string Hello(string x, int y, double z){return x;}
}
Expand Down Expand Up @@ -679,7 +679,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
private string Hello(string x, int y, double z){return x;}
internal string Hello2(string x, int y, double z){return x;}
Expand Down Expand Up @@ -725,7 +725,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
Expand Down Expand Up @@ -778,7 +778,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/**
Expand Down Expand Up @@ -829,7 +829,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <inheritdoc />
public string Hello { get; private set; }
Expand Down Expand Up @@ -876,7 +876,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
/// Bla bla
Expand Down Expand Up @@ -927,7 +927,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string Hello { get; private set; }
}
Expand Down Expand Up @@ -978,7 +978,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
DemoClass(string x)
{
Expand Down Expand Up @@ -1034,7 +1034,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public static string Hello => "abc"; // property
Expand Down Expand Up @@ -1087,7 +1087,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass<T,U> where T:class
public class DemoClass<T,U> where T:class
{
}
}
Expand Down Expand Up @@ -1135,7 +1135,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
Expand Down Expand Up @@ -1194,7 +1194,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
private int[] arr = new int[100];
Expand Down Expand Up @@ -1254,7 +1254,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
/// Property Documentation will be copied
Expand Down Expand Up @@ -1353,7 +1353,7 @@ public void WorksWithFileScopedNamespace()
namespace AutomaticInterfaceExample;
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string Hello { get; set; }
}
Expand Down Expand Up @@ -1452,7 +1452,7 @@ namespace AutomaticInterfaceExample
{
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public string AMethod(string x, string y)
{
Expand Down Expand Up @@ -1606,7 +1606,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
Expand Down Expand Up @@ -1665,7 +1665,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
Expand Down Expand Up @@ -1724,7 +1724,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
Expand Down Expand Up @@ -1784,7 +1784,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
/// <summary>
Expand Down Expand Up @@ -1844,7 +1844,7 @@ namespace AutomaticInterfaceExample
/// Bla bla
/// </summary>
[GenerateAutomaticInterface]
class DemoClass
public class DemoClass
{
public async Task<Stream?> GetFinalDocumentsByIDFails(
Expand Down Expand Up @@ -1916,7 +1916,7 @@ public GenerateAutomaticInterfaceAttribute(string namespaceName = "") { }
/// Bla bla
/// </summary>
[GenerateAutomaticInterface("CustomNameSpace")]
class DemoClass
public class DemoClass
{
public async Task<Stream?> GetFinalDocumentsByIDFails(
Expand Down Expand Up @@ -2211,4 +2211,50 @@ public partial interface ISecondClass
""";
GenerateCode(code).Should().Be(expected);
}

[Fact]
public void WorksWithInternal()
{
const string code = """
using AutomaticInterfaceAttribute;
using System.Threading.Tasks;
namespace AutomaticInterfaceExample;
[GenerateAutomaticInterface]
internal class InternalClass
{
public int AProperty { get; set; }
}
""";

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;
using System.Threading.Tasks;
namespace AutomaticInterfaceExample
{
[GeneratedCode("AutomaticInterface", "")]
internal partial interface IInternalClass
{
/// <inheritdoc />
int AProperty { get; set; }
}
}
""";
GenerateCode(code).Should().Be(expected);
}
}

0 comments on commit b12d15b

Please sign in to comment.