ProxyBaseClasses (#27)

This commit is contained in:
Stef Heyenrath
2022-02-01 18:49:01 +01:00
committed by GitHub
parent 649ed89bb6
commit f9664e0564
45 changed files with 1305 additions and 836 deletions
@@ -0,0 +1,38 @@
//----------------------------------------------------------------------------------------
// <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;
using AutoMapper;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class HumanProxy : IHuman
{
public ProxyInterfaceSourceGeneratorTests.Source.Human _Instance { get; }
public bool IsAlive { get => _Instance.IsAlive; set => _Instance.IsAlive = value; }
public HumanProxy(ProxyInterfaceSourceGeneratorTests.Source.Human instance)
{
_Instance = instance;
}
}
}
#nullable disable
@@ -0,0 +1,26 @@
//----------------------------------------------------------------------------------------
// <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 IHuman
{
bool IsAlive { get; set; }
}
}
#nullable disable
@@ -10,7 +10,7 @@
#nullable enable
using System;
namespace ProxyInterfaceSourceGeneratorTests.DTO
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IPerson
{
@@ -0,0 +1,56 @@
//----------------------------------------------------------------------------------------
// <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 IPersonExtends
{
string Name { get; set; }
string? StringNullable { get; set; }
long? NullableLong { get; }
object @object { get; set; }
bool IsAlive { get; set; }
bool X { get; set; }
void Void();
string HelloWorld(string name);
void WithParams(params string[] values);
string Add(string s, string @string);
int DefaultValue(int x = 100);
void In_Out_Ref1(in int a, out int b, ref int c);
bool Generic2<T1, T2>(int x, T1 t1, T2 t2) where T1 : struct where T2 : class, new();
System.Threading.Tasks.Task Method1Async();
System.Threading.Tasks.Task<int> Method2Async();
System.Threading.Tasks.Task<string?> Method3Async();
}
}
#nullable disable
@@ -0,0 +1,117 @@
//----------------------------------------------------------------------------------------
// <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;
using AutoMapper;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class PersonExtendsProxy : IPersonExtends
{
public ProxyInterfaceSourceGeneratorTests.Source.PersonExtends _Instance { get; }
public string Name { get => _Instance.Name; set => _Instance.Name = value; }
public string? StringNullable { get => _Instance.StringNullable; set => _Instance.StringNullable = value; }
public long? NullableLong { get => _Instance.NullableLong; }
public object @object { get => _Instance.@object; set => _Instance.@object = value; }
public bool IsAlive { get => _Instance.IsAlive; set => _Instance.IsAlive = value; }
public bool X { get => _Instance.X; set => _Instance.X = value; }
public void Void()
{
_Instance.Void();
}
public string HelloWorld(string name)
{
string name_ = name;
var result_58477331 = _Instance.HelloWorld(name_);
return result_58477331;
}
public void WithParams(params string[] values)
{
string[] values_ = values;
_Instance.WithParams(values_);
}
public string Add(string s, string @string)
{
string s_ = s;
string @string_ = @string;
var result_58477331 = _Instance.Add(s_, @string_);
return result_58477331;
}
public int DefaultValue(int x = 100)
{
int x_ = x;
var result_42930144 = _Instance.DefaultValue(x_);
return result_42930144;
}
public void In_Out_Ref1(in int a, out int b, ref int c)
{
int a_ = a;
int b_;
int c_ = c;
_Instance.In_Out_Ref1(in a_, out b_, ref c_);
b = b_;
}
public bool Generic2<T1, T2>(int x, T1 t1, T2 t2) where T1 : struct where T2 : class, new()
{
int x_ = x;
T1 t1_ = t1;
T2 t2_ = t2;
var result_38995950 = _Instance.Generic2<T1, T2>(x_, t1_, t2_);
return result_38995950;
}
public System.Threading.Tasks.Task Method1Async()
{
var result_51708797 = _Instance.Method1Async();
return result_51708797;
}
public System.Threading.Tasks.Task<int> Method2Async()
{
var result_1620952573 = _Instance.Method2Async();
return result_1620952573;
}
public System.Threading.Tasks.Task<string?> Method3Async()
{
var result_1636499760 = _Instance.Method3Async();
return result_1636499760;
}
public PersonExtendsProxy(ProxyInterfaceSourceGeneratorTests.Source.PersonExtends instance)
{
_Instance = instance;
}
}
}
#nullable disable
@@ -11,11 +11,11 @@
using System;
using AutoMapper;
namespace ProxyInterfaceSourceGeneratorTests.DTO
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class PersonProxy : IPerson
{
public ProxyInterfaceSourceGeneratorTests.DTO.Person _Instance { get; }
public ProxyInterfaceSourceGeneratorTests.Source.Person _Instance { get; }
public string Name { get => _Instance.Name; set => _Instance.Name = value; }
@@ -29,78 +29,78 @@ namespace ProxyInterfaceSourceGeneratorTests.DTO
public void Void()
{
_Instance.Void();
_Instance.Void();
}
public string HelloWorld(string name)
{
string name_ = name;
var result_15289640 = _Instance.HelloWorld(name_);
return result_15289640;
string name_ = name;
var result_56365455 = _Instance.HelloWorld(name_);
return result_56365455;
}
public void WithParams(params string[] values)
{
string[] values_ = values;
_Instance.WithParams(values_);
string[] values_ = values;
_Instance.WithParams(values_);
}
public string Add(string s, string @string)
{
string s_ = s;
string @string_ = @string;
var result_15289640 = _Instance.Add(s_, @string_);
return result_15289640;
string s_ = s;
string @string_ = @string;
var result_56365455 = _Instance.Add(s_, @string_);
return result_56365455;
}
public int DefaultValue(int x = 100)
{
int x_ = x;
var result_54302544 = _Instance.DefaultValue(x_);
return result_54302544;
int x_ = x;
var result_39875940 = _Instance.DefaultValue(x_);
return result_39875940;
}
public void In_Out_Ref1(in int a, out int b, ref int c)
{
int a_ = a;
int b_;
int c_ = c;
_Instance.In_Out_Ref1(in a_, out b_, ref c_);
b = b_;
int a_ = a;
int b_;
int c_ = c;
_Instance.In_Out_Ref1(in a_, out b_, ref c_);
b = b_;
}
public bool Generic2<T1, T2>(int x, T1 t1, T2 t2) where T1 : struct where T2 : class, new()
{
int x_ = x;
T1 t1_ = t1;
T2 t2_ = t2;
var result_40004473 = _Instance.Generic2<T1, T2>(x_, t1_, t2_);
return result_40004473;
int x_ = x;
T1 t1_ = t1;
T2 t2_ = t2;
var result_41799290 = _Instance.Generic2<T1, T2>(x_, t1_, t2_);
return result_41799290;
}
public System.Threading.Tasks.Task Method1Async()
{
var result_50153955 = _Instance.Method1Async();
return result_50153955;
var result_32599313 = _Instance.Method1Async();
return result_32599313;
}
public System.Threading.Tasks.Task<int> Method2Async()
{
var result_1151242754 = _Instance.Method2Async();
return result_1151242754;
var result_1620495907 = _Instance.Method2Async();
return result_1620495907;
}
public System.Threading.Tasks.Task<string?> Method3Async()
{
var result_1190255658 = _Instance.Method3Async();
return result_1190255658;
var result_1604006392 = _Instance.Method3Async();
return result_1604006392;
}
public PersonProxy(ProxyInterfaceSourceGeneratorTests.DTO.Person instance)
public PersonProxy(ProxyInterfaceSourceGeneratorTests.Source.Person instance)
{
_Instance = instance;
@@ -1,14 +1,18 @@
using System.Collections.Generic;
using System.IO;
using AnyOfTypes;
using CSharp.SourceGenerators.Extensions;
using CSharp.SourceGenerators.Extensions.Models;
using FluentAssertions;
using ProxyInterfaceSourceGenerator;
using Xunit;
namespace FluentBuilderGeneratorTests
namespace ProxyInterfaceSourceGeneratorTests
{
public class ProxyInterfaceSourceGeneratorTest
{
private bool Write = true;
private readonly ProxyInterfaceCodeGenerator _sut;
public ProxyInterfaceSourceGeneratorTest()
@@ -21,10 +25,10 @@ namespace FluentBuilderGeneratorTests
{
// Arrange
var attributeFilename = "ProxyInterfaceGenerator.ProxyAttribute.g.cs";
var interfaceFilename = "ProxyInterfaceSourceGeneratorTests.DTO.IPerson.g.cs";
var proxyClassFilename = "ProxyInterfaceSourceGeneratorTests.DTO.PersonProxy.g.cs";
var interfaceFilename = "ProxyInterfaceSourceGeneratorTests.Source.IPersonExtends.g.cs";
var proxyClassFilename = "ProxyInterfaceSourceGeneratorTests.Source.PersonExtendsProxy.g.cs";
var path = "./Source/IPerson.cs";
var path = "./Source/IPersonExtends.cs";
var sourceFile = new SourceFile
{
Path = path,
@@ -32,7 +36,7 @@ namespace FluentBuilderGeneratorTests
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.DTO.Person)"
ArgumentList = new [] { "typeof(ProxyInterfaceSourceGeneratorTests.Source.PersonExtends)", "true" }
}
};
@@ -43,16 +47,16 @@ namespace FluentBuilderGeneratorTests
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(3);
// Assert interface
var @attribute = result.Files[0].SyntaxTree;
@attribute.FilePath.Should().EndWith(attributeFilename);
// 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();
File.WriteAllText($"../../../Destination/{interfaceFilename}", interfaceCode);
if (Write) File.WriteAllText($"../../../Destination/{interfaceFilename}", interfaceCode);
interfaceCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfaceFilename}"));
// Assert Proxy
@@ -60,8 +64,90 @@ namespace FluentBuilderGeneratorTests
proxyClass.FilePath.Should().EndWith(proxyClassFilename);
var proxyCode = proxyClass.ToString();
File.WriteAllText($"../../../Destination/{proxyClassFilename}", proxyCode);
if (Write) File.WriteAllText($"../../../Destination/{proxyClassFilename}", proxyCode);
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassFilename}"));
}
[Fact]
public void GenerateFiles_ForTwoClasses_Should_GenerateCorrectFiles()
{
// Arrange
var attributeFilename = "ProxyInterfaceGenerator.ProxyAttribute.g.cs";
var interfaceHumanFilename = "ProxyInterfaceSourceGeneratorTests.Source.IHuman.g.cs";
var proxyClassHumanFilename = "ProxyInterfaceSourceGeneratorTests.Source.HumanProxy.g.cs";
var interfacePersonFilename = "ProxyInterfaceSourceGeneratorTests.Source.IPerson.g.cs";
var proxyClassPersonFilename = "ProxyInterfaceSourceGeneratorTests.Source.PersonProxy.g.cs";
var pathPerson = "./Source/IPerson.cs";
var sourceFilePerson = new SourceFile
{
Path = pathPerson,
Text = File.ReadAllText(pathPerson),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.Person)"
}
};
var pathHuman = "./Source/IHuman.cs";
var sourceFileHuman = new SourceFile
{
Path = pathHuman,
Text = File.ReadAllText(pathHuman),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.Human)"
}
};
// Act
var result = _sut.Execute(new[] { sourceFileHuman, sourceFilePerson });
// Assert
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(5);
// Assert attribute
var attribute = result.Files[0].SyntaxTree;
attribute.FilePath.Should().EndWith(attributeFilename);
// Assert interface Human
var interfaceHuman = result.Files[1].SyntaxTree;
interfaceHuman.FilePath.Should().EndWith(interfaceHumanFilename);
var interfaceCodeHuman = interfaceHuman.ToString();
if (Write) File.WriteAllText($"../../../Destination/{interfaceHumanFilename}", interfaceCodeHuman);
interfaceCodeHuman.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfaceHumanFilename}"));
// Assert interface Person
var interfacePerson = result.Files[2].SyntaxTree;
interfacePerson.FilePath.Should().EndWith(interfacePersonFilename);
var interfaceCodePerson = interfacePerson.ToString();
if (Write) File.WriteAllText($"../../../Destination/{interfacePersonFilename}", interfaceCodePerson);
interfaceCodePerson.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{interfacePersonFilename}"));
// Assert Proxy Human
var proxyClassHuman = result.Files[3].SyntaxTree;
proxyClassHuman.FilePath.Should().EndWith(proxyClassHumanFilename);
var proxyCodeHuman = proxyClassHuman.ToString();
if (Write) File.WriteAllText($"../../../Destination/{proxyClassHumanFilename}", proxyCodeHuman);
proxyCodeHuman.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassHumanFilename}"));
// Assert Proxy Person
var proxyClassPerson = result.Files[4].SyntaxTree;
proxyClassPerson.FilePath.Should().EndWith(proxyClassPersonFilename);
var proxyCode = proxyClassPerson.ToString();
if (Write) File.WriteAllText($"../../../Destination/{proxyClassPersonFilename}", proxyCode);
proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassPersonFilename}"));
}
}
}
@@ -8,11 +8,6 @@
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Destination\ProxyInterfaceSourceGeneratorTests.DTO.IPerson.g.cs" />
<Compile Remove="Destination\ProxyInterfaceSourceGeneratorTests.DTO.PersonProxy.g.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
@@ -32,22 +27,29 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="CSharp.SourceGenerators.Extensions" Version="0.0.5" />
<PackageReference Include="CSharp.SourceGenerators.Extensions" Version="0.0.6" />
<!--<ProjectReference Include="..\..\..\FluentBuilder\src-extensions\CSharp.SourceGenerators.Extensions\CSharp.SourceGenerators.Extensions.csproj" />-->
</ItemGroup>
<ItemGroup>
<Compile Update="Source\IPerson.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Source\Person.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="Destination\ProxyInterfaceSourceGeneratorTests.DTO.IPerson.g.cs" />
<None Include="Destination\ProxyInterfaceSourceGeneratorTests.DTO.PersonProxy.g.cs" />
<Compile Update="Source\Human.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Source\IHuman.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Source\IPersonExtends.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Source\IPerson.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Source\PersonExtends.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Source\Person.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
</Project>
@@ -0,0 +1,12 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class Human : Animal
{
public bool IsAlive { get; set; }
}
public class Animal
{
public bool X { get; set; }
}
}
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IHuman
{
}
}
@@ -1,4 +1,4 @@
namespace ProxyInterfaceSourceGeneratorTests.DTO
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IPerson
{
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IPersonExtends
{
}
}
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
namespace ProxyInterfaceSourceGeneratorTests.DTO
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class Person
{
@@ -0,0 +1,65 @@
using System.Threading.Tasks;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class PersonExtends : Human
{
public string Name { get; set; }
public string? StringNullable { get; set; }
public long? NullableLong { get; }
public object @object { get; set; }
public void Void()
{
}
public string HelloWorld(string name)
{
return $"Hello {name} !";
}
public void WithParams(params string[] values)
{
}
public string Add(string s, string @string)
{
return s + @string;
}
public int DefaultValue(int x = 100)
{
return x + 1;
}
public void In_Out_Ref1(in int a, out int b, ref int c)
{
b = 1;
}
public bool Generic2<T1, T2>(int x, T1 t1, T2 t2)
where T1 : struct
where T2 : class, new()
{
return true;
}
public Task Method1Async()
{
return Task.CompletedTask;
}
public Task<int> Method2Async()
{
return Task.FromResult(1);
}
public Task<string?> Method3Async()
{
return Task.FromResult((string?)"");
}
}
}