Skip to content

Commit

Permalink
Fix ProxyAttribute<T> (#86)
Browse files Browse the repository at this point in the history
* Fix ProxyAttribute<T>

* .
  • Loading branch information
StefH authored Jan 14, 2025
1 parent 4c8b52d commit bdfd521
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,23 @@ public ProxyAttribute() : this(false, ProxyClassAccessibility.Public)
{{
}}
public ProxyAttribute(bool proxyBaseClasses) : this(type, proxyBaseClasses, ProxyClassAccessibility.Public)
public ProxyAttribute(bool proxyBaseClasses) : this(proxyBaseClasses, ProxyClassAccessibility.Public)
{{
}}
public ProxyAttribute(ProxyClassAccessibility accessibility) : this(type, false, accessibility)
public ProxyAttribute(ProxyClassAccessibility accessibility) : this(false, accessibility)
{{
}}
public ProxyAttribute(ProxyClassAccessibility accessibility, {stringArray} membersToIgnore) : this(type, false, accessibility, membersToIgnore)
public ProxyAttribute(ProxyClassAccessibility accessibility, {stringArray} membersToIgnore) : this(false, accessibility, membersToIgnore)
{{
}}
public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, null)
public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(proxyBaseClasses, accessibility, null)
{{
}}
public ProxyAttribute({stringArray} membersToIgnore) : this(type, false, ProxyClassAccessibility.Public, null)
public ProxyAttribute({stringArray} membersToIgnore) : this(false, ProxyClassAccessibility.Public, null)
{{
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//----------------------------------------------------------------------------------------
// <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 ProxyInterfaceGenerator
{

[AttributeUsage(AttributeTargets.Interface)]
internal sealed class ProxyAttribute : Attribute
{
public Type Type { get; }
public bool ProxyBaseClasses { get; }
public ProxyClassAccessibility Accessibility { get; }
public string[]? MembersToIgnore { get; }

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, ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(type, false, accessibility, membersToIgnore)
{
}

public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, null)
{
}

public ProxyAttribute(Type type, string[]? membersToIgnore) : this(type, false, ProxyClassAccessibility.Public, null)
{
}

public ProxyAttribute(Type type, bool proxyBaseClasses, ProxyClassAccessibility accessibility, string[]? membersToIgnore)
{
Type = type;
ProxyBaseClasses = proxyBaseClasses;
Accessibility = accessibility;
MembersToIgnore = membersToIgnore;
}
}


[AttributeUsage(AttributeTargets.Interface)]
internal sealed class ProxyAttribute<T> : Attribute where T : class
{
public Type Type { get; }
public bool ProxyBaseClasses { get; }
public ProxyClassAccessibility Accessibility { get; }
public string[]? MembersToIgnore { get; }

public ProxyAttribute() : this(false, ProxyClassAccessibility.Public)
{
}

public ProxyAttribute(bool proxyBaseClasses) : this(proxyBaseClasses, ProxyClassAccessibility.Public)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility) : this(false, accessibility)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(false, accessibility, membersToIgnore)
{
}

public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(proxyBaseClasses, accessibility, null)
{
}

public ProxyAttribute(string[]? membersToIgnore) : this(false, ProxyClassAccessibility.Public, null)
{
}

public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility, string[]? membersToIgnore)
{
Type = typeof(T);
ProxyBaseClasses = proxyBaseClasses;
Accessibility = accessibility;
MembersToIgnore = membersToIgnore;
}
}

[Flags]
internal enum ProxyClassAccessibility
{
Public = 0,

Internal = 1
}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ namespace ProxyInterfaceGenerator
{
}

public ProxyAttribute(bool proxyBaseClasses) : this(type, proxyBaseClasses, ProxyClassAccessibility.Public)
public ProxyAttribute(bool proxyBaseClasses) : this(proxyBaseClasses, ProxyClassAccessibility.Public)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility) : this(type, false, accessibility)
public ProxyAttribute(ProxyClassAccessibility accessibility) : this(false, accessibility)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(type, false, accessibility, membersToIgnore)
public ProxyAttribute(ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(false, accessibility, membersToIgnore)
{
}

public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, null)
public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(proxyBaseClasses, accessibility, null)
{
}

public ProxyAttribute(string[]? membersToIgnore) : this(type, false, ProxyClassAccessibility.Public, null)
public ProxyAttribute(string[]? membersToIgnore) : this(false, ProxyClassAccessibility.Public, null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ namespace ProxyInterfaceGenerator
{
}

public ProxyAttribute(bool proxyBaseClasses) : this(type, proxyBaseClasses, ProxyClassAccessibility.Public)
public ProxyAttribute(bool proxyBaseClasses) : this(proxyBaseClasses, ProxyClassAccessibility.Public)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility) : this(type, false, accessibility)
public ProxyAttribute(ProxyClassAccessibility accessibility) : this(false, accessibility)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(type, false, accessibility, membersToIgnore)
public ProxyAttribute(ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(false, accessibility, membersToIgnore)
{
}

public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, null)
public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(proxyBaseClasses, accessibility, null)
{
}

public ProxyAttribute(string[]? membersToIgnore) : this(type, false, ProxyClassAccessibility.Public, null)
public ProxyAttribute(string[]? membersToIgnore) : this(false, ProxyClassAccessibility.Public, null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ namespace ProxyInterfaceGenerator
{
}

public ProxyAttribute(bool proxyBaseClasses) : this(type, proxyBaseClasses, ProxyClassAccessibility.Public)
public ProxyAttribute(bool proxyBaseClasses) : this(proxyBaseClasses, ProxyClassAccessibility.Public)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility) : this(type, false, accessibility)
public ProxyAttribute(ProxyClassAccessibility accessibility) : this(false, accessibility)
{
}

public ProxyAttribute(ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(type, false, accessibility, membersToIgnore)
public ProxyAttribute(ProxyClassAccessibility accessibility, string[]? membersToIgnore) : this(false, accessibility, membersToIgnore)
{
}

public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(type, proxyBaseClasses, accessibility, null)
public ProxyAttribute(bool proxyBaseClasses, ProxyClassAccessibility accessibility) : this(proxyBaseClasses, accessibility, null)
{
}

public ProxyAttribute(string[]? membersToIgnore) : this(type, false, ProxyClassAccessibility.Public, null)
public ProxyAttribute(string[]? membersToIgnore) : this(false, ProxyClassAccessibility.Public, null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,12 @@ public void GenerateFiles_When_MixedVisibility_Should_GenerateCorrectFiles()
public void GenerateFiles_ForSingleClass_Should_GenerateCorrectFiles()
{
// Arrange
var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs";
var interfaceFilename = "ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs";
var proxyClassFilename = "ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs";
var fileNames = new[]
{
"ProxyInterfaceGenerator.Extra.g.cs",
"ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs",
"ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs",
};

var path = Path.Combine(_basePath, "Source", "IPersonExtends.cs");
var sourceFile = new SourceFile
Expand All @@ -334,30 +337,7 @@ public void GenerateFiles_ForSingleClass_Should_GenerateCorrectFiles()
var result = _sut.Execute([sourceFile]);

// Assert
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(3);

// Assert attribute
var attribute = result.Files[0].SyntaxTree;
attribute.FilePath.Should().EndWith(attributeFilename);

// Assert interface
var @interface = result.Files[1].SyntaxTree;
@interface.FilePath.Should().EndWith(interfaceFilename);

var interfaceCode = @interface.ToString();
var interfaceCodeFilename = Path.Combine(_basePath, "Destination", interfaceFilename);
if (Write) File.WriteAllText(interfaceCodeFilename, interfaceCode);
interfaceCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText(interfaceCodeFilename));

// Assert Proxy
var proxyClass = result.Files[2].SyntaxTree;
proxyClass.FilePath.Should().EndWith(proxyClassFilename);

var proxyCode = proxyClass.ToString();
var proxyCodeFilename = Path.Combine(_basePath, "Destination", proxyClassFilename);
if (Write) File.WriteAllText(proxyCodeFilename, proxyCode);
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText(proxyCodeFilename));
Assert(result, fileNames, false);
}

[Fact]
Expand Down Expand Up @@ -816,14 +796,16 @@ public void GenerateFiles_ForTimeProvider_Should_GenerateCorrectFiles()
Assert(result, fileNames);
}

private void Assert(ExecuteResult result, string[] fileNames)
private void Assert(ExecuteResult result, string[] fileNames, bool skipExtra = true)
{
var skip = skipExtra ? 1 : 0;

result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(fileNames.Length + 1);
result.Files.Should().HaveCount(fileNames.Length + skip);

foreach (var fileName in fileNames.Select((fileName, index) => new { fileName, index }))
{
var builder = result.Files[fileName.index + 1]; // +1 means skip the attribute
var builder = result.Files[fileName.index + skip]; // +1 means skip the attribute
builder.Path.Should().EndWith(fileName.fileName);

var destinationFilename = Path.Combine(_basePath, $"Destination/{fileName.fileName}");
Expand Down

0 comments on commit bdfd521

Please sign in to comment.