Add support for base (proxy) class (#29)

This commit is contained in:
Stef Heyenrath
2022-02-04 11:33:26 +01:00
committed by GitHub
parent 94d322cfb3
commit d7483d6b7e
38 changed files with 400 additions and 309 deletions
@@ -9,16 +9,19 @@
#nullable enable
using System;
using AutoMapper;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class HumanProxy : IHuman
public partial class HumanProxy : IHuman
{
public ProxyInterfaceSourceGeneratorTests.Source.Human _Instance { get; }
public bool IsAlive { get => _Instance.IsAlive; set => _Instance.IsAlive = value; }
public string GetterOnly { get => _Instance.GetterOnly; }
@@ -28,6 +31,7 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public HumanProxy(ProxyInterfaceSourceGeneratorTests.Source.Human instance)
{
_Instance = instance;
}
@@ -16,6 +16,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
{
bool IsAlive { get; set; }
string GetterOnly { get; }
@@ -24,6 +24,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
System.Collections.Generic.IList<ProxyInterfaceSourceGeneratorTests.Source.IHuman> AddHuman(ProxyInterfaceSourceGeneratorTests.Source.IHuman h);
void Void();
string HelloWorld(string name);
@@ -26,6 +26,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
bool IsAlive { get; set; }
string GetterOnly { get; }
string StaticMethod(int x, string y);
@@ -9,13 +9,14 @@
#nullable enable
using System;
using AutoMapper;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial class PersonExtendsProxy : IPersonExtends
{
public ProxyInterfaceSourceGeneratorTests.Source.PersonExtends _Instance { get; }
public string StaticString { get => ProxyInterfaceSourceGeneratorTests.Source.PersonExtends.StaticString; set => ProxyInterfaceSourceGeneratorTests.Source.PersonExtends.StaticString = value; }
@@ -29,14 +30,16 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public bool IsAlive { get => _Instance.IsAlive; set => _Instance.IsAlive = value; }
public string GetterOnly { get => _Instance.GetterOnly; }
public string StaticMethod(int x, string y)
{
int x_ = x;
string y_ = y;
var result_6851397 = ProxyInterfaceSourceGeneratorTests.Source.PersonExtends.StaticMethod(x_, y_);
return result_6851397;
var result__1647028461 = ProxyInterfaceSourceGeneratorTests.Source.PersonExtends.StaticMethod(x_, y_);
return result__1647028461;
}
public void Void()
@@ -47,8 +50,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public string HelloWorld(string name)
{
string name_ = name;
var result_6851397 = _Instance.HelloWorld(name_);
return result_6851397;
var result_282270798 = _Instance.HelloWorld(name_);
return result_282270798;
}
public void WithParams(params string[] values)
@@ -61,15 +64,15 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
{
string s_ = s;
string @string_ = @string;
var result_6851397 = _Instance.Add(s_, @string_);
return result_6851397;
var result__1127157211 = _Instance.Add(s_, @string_);
return result__1127157211;
}
public int DefaultValue(int x = 100)
{
int x_ = x;
var result_3873514 = _Instance.DefaultValue(x_);
return result_3873514;
var result__378509684 = _Instance.DefaultValue(x_);
return result__378509684;
}
public void In_Out_Ref1(in int a, out int b, ref int c)
@@ -86,26 +89,26 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
int x_ = x;
T1 t1_ = t1;
T2 t2_ = t2;
var result_14331071 = _Instance.Generic2<T1, T2>(x_, t1_, t2_);
return result_14331071;
var result_542538942 = _Instance.Generic2<T1, T2>(x_, t1_, t2_);
return result_542538942;
}
public System.Threading.Tasks.Task Method1Async()
{
var result_39535275 = _Instance.Method1Async();
return result_39535275;
var result__57678382 = _Instance.Method1Async();
return result__57678382;
}
public System.Threading.Tasks.Task<int> Method2Async()
{
var result_772784336 = _Instance.Method2Async();
return result_772784336;
var result__57677169 = _Instance.Method2Async();
return result__57677169;
}
public System.Threading.Tasks.Task<string?> Method3Async()
{
var result_769806453 = _Instance.Method3Async();
return result_769806453;
var result__57684656 = _Instance.Method3Async();
return result__57684656;
}
@@ -115,6 +118,7 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public PersonExtendsProxy(ProxyInterfaceSourceGeneratorTests.Source.PersonExtends instance)
{
_Instance = instance;
}
@@ -13,9 +13,11 @@ using AutoMapper;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class PersonProxy : IPerson
public partial class PersonProxy : ProxyInterfaceSourceGeneratorTests.Source.HumanProxy, IPerson
{
public ProxyInterfaceSourceGeneratorTests.Source.Person _Instance { get; }
public new ProxyInterfaceSourceGeneratorTests.Source.Person _Instance { get; }
public ProxyInterfaceSourceGeneratorTests.Source.Human _InstanceBase { get; }
public string Name { get => _Instance.Name; set => _Instance.Name = value; }
@@ -27,6 +29,13 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public System.Collections.Generic.IList<ProxyInterfaceSourceGeneratorTests.Source.IHuman> AddHuman(ProxyInterfaceSourceGeneratorTests.Source.IHuman h)
{
ProxyInterfaceSourceGeneratorTests.Source.Human h_ = _mapper.Map<ProxyInterfaceSourceGeneratorTests.Source.Human>(h);
var result_907493286 = _Instance.AddHuman(h_);
return _mapper.Map<System.Collections.Generic.IList<ProxyInterfaceSourceGeneratorTests.Source.IHuman>>(result_907493286);
}
public void Void()
{
_Instance.Void();
@@ -35,8 +44,8 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
public string HelloWorld(string name)
{
string name_ = name;
var result_37309470 = _Instance.HelloWorld(name_);
return result_37309470;
var result_282270798 = _Instance.HelloWorld(name_);
return result_282270798;
}
public void WithParams(params string[] values)
@@ -49,15 +58,15 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
{
string s_ = s;
string @string_ = @string;
var result_37309470 = _Instance.Add(s_, @string_);
return result_37309470;
var result__1127157211 = _Instance.Add(s_, @string_);
return result__1127157211;
}
public int DefaultValue(int x = 100)
{
int x_ = x;
var result_24216618 = _Instance.DefaultValue(x_);
return result_24216618;
var result__378509684 = _Instance.DefaultValue(x_);
return result__378509684;
}
public void In_Out_Ref1(in int a, out int b, ref int c)
@@ -74,41 +83,46 @@ namespace ProxyInterfaceSourceGeneratorTests.Source
int x_ = x;
T1 t1_ = t1;
T2 t2_ = t2;
var result_60333940 = _Instance.Generic2<T1, T2>(x_, t1_, t2_);
return result_60333940;
var result_542538942 = _Instance.Generic2<T1, T2>(x_, t1_, t2_);
return result_542538942;
}
public System.Threading.Tasks.Task Method1Async()
{
var result_2292327 = _Instance.Method1Async();
return result_2292327;
var result__57678382 = _Instance.Method1Async();
return result__57678382;
}
public System.Threading.Tasks.Task<int> Method2Async()
{
var result_1229624901 = _Instance.Method2Async();
return result_1229624901;
var result__57677169 = _Instance.Method2Async();
return result__57677169;
}
public System.Threading.Tasks.Task<string?> Method3Async()
{
var result_1242717753 = _Instance.Method3Async();
return result_1242717753;
var result__57684656 = _Instance.Method3Async();
return result__57684656;
}
public PersonProxy(ProxyInterfaceSourceGeneratorTests.Source.Person instance)
public PersonProxy(ProxyInterfaceSourceGeneratorTests.Source.Person instance) : base(instance)
{
_Instance = instance;
_InstanceBase = instance;
_mapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ProxyInterfaceSourceGeneratorTests.Source.Human, ProxyInterfaceSourceGeneratorTests.Source.IHuman>().ConstructUsing(instance => new ProxyInterfaceSourceGeneratorTests.Source.HumanProxy(instance));
cfg.CreateMap<ProxyInterfaceSourceGeneratorTests.Source.IHuman, ProxyInterfaceSourceGeneratorTests.Source.Human>().ConstructUsing(proxy => ((ProxyInterfaceSourceGeneratorTests.Source.HumanProxy) proxy)._Instance);
}).CreateMapper();
}
public bool IsAlive { get; set; }
private readonly IMapper _mapper;
}
}
#nullable disable
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using AnyOfTypes;
using CSharp.SourceGenerators.Extensions;
using CSharp.SourceGenerators.Extensions.Models;
using FluentAssertions;
using ProxyInterfaceSourceGenerator;
using ProxyInterfaceSourceGeneratorTests.Source;
using Xunit;
using Xunit.Sdk;
namespace ProxyInterfaceSourceGeneratorTests
{
@@ -20,6 +17,36 @@ namespace ProxyInterfaceSourceGeneratorTests
public ProxyInterfaceSourceGeneratorTest()
{
_sut = new ProxyInterfaceCodeGenerator();
var pp = new PersonProxy(new Person());
var h = pp.AddHuman(new HumanProxy(new Human()));
int x = 0;
}
[Fact]
public void GenerateFiles_ForStruct_Should_Not_GenerateProxyCode()
{
// Arrange
var path = "./Source/IMyStruct.cs";
var sourceFile = new SourceFile
{
Path = path,
Text = File.ReadAllText(path),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.MyStruct)"
}
};
// Act
var result = _sut.Execute(new[] { sourceFile });
// Assert
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(1);
}
[Fact]
@@ -111,7 +138,7 @@ namespace ProxyInterfaceSourceGeneratorTests
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(5);
throw new Exception();
// throw new Exception();
// Assert attribute
var attribute = result.Files[0].SyntaxTree;
@@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
@@ -28,26 +29,10 @@
<ItemGroup>
<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\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">
<Compile Update="Source\*.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
@@ -1,7 +1,11 @@
using Xunit.Sdk;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class Human
{
public bool IsAlive { get; set; }
public string GetterOnly => "x";
}
}
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IMyStruct
{
}
}
@@ -0,0 +1,7 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public struct MyStruct
{
public int Id { get; set; }
}
}
@@ -1,9 +1,15 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class Person
public class Person : Human
{
public IList<Human> AddHuman(Human h)
{
return new List<Human> { h, new Human { IsAlive = true } };
}
public string Name { get; set; }
public string? StringNullable { get; set; }
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ProxyInterfaceSourceGeneratorTests.Source