Add support for public and internal ProxyClass (#58)

* x

* fx

* .
This commit is contained in:
Stef Heyenrath
2023-02-24 16:22:26 +01:00
committed by GitHub
parent 4c7f7cde4d
commit eadcf8585f
15 changed files with 296 additions and 36 deletions
@@ -0,0 +1,28 @@
//----------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
using System;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface ITestClassInternal
{
ProxyInterfaceSourceGeneratorTests.Source.TestClassInternal _Instance { get; }
bool Test { get; set; }
}
}
#nullable disable
@@ -0,0 +1,39 @@
//----------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
using System;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
internal partial class TestClassInternalProxy : ITestClassInternal
{
public ProxyInterfaceSourceGeneratorTests.Source.TestClassInternal _Instance { get; }
public bool Test { get => _Instance.Test; set => _Instance.Test = value; }
public TestClassInternalProxy(ProxyInterfaceSourceGeneratorTests.Source.TestClassInternal instance)
{
_Instance = instance;
}
}
}
#nullable disable
@@ -20,13 +20,35 @@ namespace ProxyInterfaceGenerator
{
public Type Type { get; }
public bool ProxyBaseClasses { get; }
public ProxyClassAccessibility Accessibility { get; }
public ProxyAttribute(Type type, bool proxyBaseClasses = false)
public ProxyAttribute(Type type) : this(type, false, ProxyClassAccessibility.Public)
{
}
public ProxyAttribute(Type type, bool proxyBaseClasses) : this(type, proxyBaseClasses, ProxyClassAccessibility.Public)
{
}
public ProxyAttribute(Type type, ProxyClassAccessibility accessibility) : this(type, false, accessibility)
{
}
public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility)
{
Type = type;
ProxyBaseClasses = proxyBaseClasses;
Accessibility = accessibility;
}
}
[Flags]
internal enum ProxyClassAccessibility
{
Public = 0,
Internal = 1
}
}
},
{
@@ -317,6 +317,49 @@ public class ProxyInterfaceSourceGeneratorTest
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassFilename}"));
}
[Fact]
public void GenerateFiles_ForSingleClass_AsInternal_Should_GenerateCorrectFiles()
{
// Arrange
var interfaceFilename = "ProxyInterfaceSourceGeneratorTests.Source.ITestClassInternal.g.cs";
var proxyClassFilename = "ProxyInterfaceSourceGeneratorTests.Source.TestClassInternalProxy.g.cs";
var path = "./Source/ITestClassInternal.cs";
var sourceFile = new SourceFile
{
Path = path,
Text = File.ReadAllText(path),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = new[] { "typeof(ProxyInterfaceSourceGeneratorTests.Source.TestClassInternal)", "ProxyClassAccessibility.Internal" }
}
};
// Act
var result = _sut.Execute(new[] { sourceFile });
// Assert
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(3);
// Assert interface
var @interface = result.Files[1].SyntaxTree;
@interface.FilePath.Should().EndWith(interfaceFilename);
var interfaceCode = @interface.ToString();
if (Write) File.WriteAllText($"../../../Destination/{interfaceFilename}", interfaceCode);
interfaceCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfaceFilename}"));
// Assert Proxy
var proxyClass = result.Files[2].SyntaxTree;
proxyClass.FilePath.Should().EndWith(proxyClassFilename);
var proxyCode = proxyClass.ToString();
if (Write) File.WriteAllText($"../../../Destination/{proxyClassFilename}", proxyCode);
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassFilename}"));
}
[Fact]
public void GenerateFiles_ForTwoClasses_Should_GenerateCorrectFiles()
{
@@ -398,6 +441,4 @@ public class ProxyInterfaceSourceGeneratorTest
if (Write) File.WriteAllText($"../../../Destination/{proxyClassPersonFilename}", proxyCode);
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassPersonFilename}"));
}
}
@@ -10,6 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Verify.SourceGenerators" Version="2.0.1" />
<PackageReference Include="Verify.Xunit" Version="19.6.0" />
<PackageReference Include="Akka.Remote" Version="1.4.47" />
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="CultureAwareTesting.xUnit" Version="0.0.1" />
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface ITestClassInternal
{
}
}
@@ -0,0 +1,7 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class TestClassInternal
{
public bool Test { get; set; }
}
}