diff --git a/ProxyInterfaceSourceGenerator Solution.sln b/ProxyInterfaceSourceGenerator Solution.sln index 5accc75..efb4084 100644 --- a/ProxyInterfaceSourceGenerator Solution.sln +++ b/ProxyInterfaceSourceGenerator Solution.sln @@ -26,6 +26,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceConsumer", "s EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceConsumerViaNuGet", "src-examples\ProxyInterfaceConsumerViaNuGet\ProxyInterfaceConsumerViaNuGet.csproj", "{6BEBFEB9-635F-44A2-949C-15DDDF0B7740}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyInterfaceSourceGeneratorTests", "tests\ProxyInterfaceSourceGeneratorTests\ProxyInterfaceSourceGeneratorTests.csproj", "{1BDB9046-D6D1-4FB4-AAB5-F24E33EEAE0A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,6 +46,10 @@ Global {6BEBFEB9-635F-44A2-949C-15DDDF0B7740}.Debug|Any CPU.Build.0 = Debug|Any CPU {6BEBFEB9-635F-44A2-949C-15DDDF0B7740}.Release|Any CPU.ActiveCfg = Release|Any CPU {6BEBFEB9-635F-44A2-949C-15DDDF0B7740}.Release|Any CPU.Build.0 = Release|Any CPU + {1BDB9046-D6D1-4FB4-AAB5-F24E33EEAE0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1BDB9046-D6D1-4FB4-AAB5-F24E33EEAE0A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1BDB9046-D6D1-4FB4-AAB5-F24E33EEAE0A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1BDB9046-D6D1-4FB4-AAB5-F24E33EEAE0A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -52,6 +58,7 @@ Global {12344228-91F4-4502-9595-39584E5ABB34} = {ED3DA9DD-1E07-444B-A2D7-2DBA280F96D4} {7E0A10EE-CCC3-4281-9541-B0AF037D3DF9} = {38BA087F-EDA1-4F8A-A140-85B84791B815} {6BEBFEB9-635F-44A2-949C-15DDDF0B7740} = {38BA087F-EDA1-4F8A-A140-85B84791B815} + {1BDB9046-D6D1-4FB4-AAB5-F24E33EEAE0A} = {19009F5B-3267-45E2-A8B6-89F2AB47D72C} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {585F071D-051D-441C-9C6B-226D9E15A1F5} diff --git a/src-examples/ProxyInterfaceConsumer/Person.cs b/src-examples/ProxyInterfaceConsumer/Person.cs index cc0dbc2..f189917 100644 --- a/src-examples/ProxyInterfaceConsumer/Person.cs +++ b/src-examples/ProxyInterfaceConsumer/Person.cs @@ -19,7 +19,7 @@ namespace ProxyInterfaceConsumer public Address Address { get; set; } - public List
AddressesLIst { get; set; } + public List
AddressesList { get; set; } public Dictionary AddressesDict { get; set; } = new Dictionary(); public Dictionary AddressesDict2 { get; set; } = new Dictionary(); diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs index 5479a2c..87aa153 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/PartialInterfacesGenerator.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using ProxyInterfaceSourceGenerator.Enums; using ProxyInterfaceSourceGenerator.Extensions; using ProxyInterfaceSourceGenerator.SyntaxReceiver; @@ -20,17 +21,18 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators { foreach (var ci in _context.CandidateInterfaces) { - yield return GenerateFile(ci.Value); + yield return GenerateFile(ci.Key, ci.Value); } } - private FileData GenerateFile(ProxyData pd) + private FileData GenerateFile(InterfaceDeclarationSyntax ci, ProxyData pd) { + var sourceInterfaceSymbol = GetNamedTypeSymbolByFullName(ci.Identifier.ToString(), pd.Usings); var targetClassSymbol = GetNamedTypeSymbolByFullName(pd.TypeName, pd.Usings); var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName); var file = new FileData( - $"{targetClassSymbol.GetFileName()}.g.cs", + $"{sourceInterfaceSymbol.GetFileName()}.g.cs", CreatePartialInterfaceCode(pd.Namespace, targetClassSymbol, interfaceName, pd.ProxyAll) ); diff --git a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyAttributeGenerator.cs b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyAttributeGenerator.cs index 21aea38..ca91969 100644 --- a/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyAttributeGenerator.cs +++ b/src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyAttributeGenerator.cs @@ -6,7 +6,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators public FileData GenerateFile() { - return new FileData($"{ClassName}.g.cs", $@"//---------------------------------------------------------------------------------------- + return new FileData($"ProxyInterfaceGenerator.{ClassName}.g.cs", $@"//---------------------------------------------------------------------------------------- // // This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator. // diff --git a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj index ce2dc38..e827ecf 100644 --- a/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj +++ b/src/ProxyInterfaceSourceGenerator/ProxyInterfaceSourceGenerator.csproj @@ -1,7 +1,7 @@ - + - 0.0.10-preview-01 + 0.0.11 netstandard2.0 {12344228-91F4-4502-9595-39584E5ABB34} 9 @@ -23,6 +23,7 @@ false true $(BaseIntermediateOutputPath)Generated + true @@ -52,4 +53,14 @@ + + + + <_Parameter1>ProxyInterfaceSourceGeneratorTests + + + <_Parameter1>DynamicProxyGenAssembly2 + + + \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.DTO.IPerson.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.DTO.IPerson.g.cs new file mode 100644 index 0000000..69bdafe --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.DTO.IPerson.g.cs @@ -0,0 +1,52 @@ +//---------------------------------------------------------------------------------------- +// +// 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. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; + +namespace ProxyInterfaceSourceGeneratorTests.DTO +{ + public partial interface IPerson + { + string Name { get; set; } + + string? StringNullable { get; set; } + + long? NullableLong { get; } + + object @object { 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(int x, T1 t1, T2 t2) where T1 : struct where T2 : class, new(); + + System.Threading.Tasks.Task Method1Async(); + + System.Threading.Tasks.Task Method2Async(); + + System.Threading.Tasks.Task Method3Async(); + + + + + } +} +#nullable disable \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.DTO.PersonProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.DTO.PersonProxy.g.cs new file mode 100644 index 0000000..1fa560a --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.DTO.PersonProxy.g.cs @@ -0,0 +1,113 @@ +//---------------------------------------------------------------------------------------- +// +// 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. +// +//---------------------------------------------------------------------------------------- + +#nullable enable +using System; +using AutoMapper; + +namespace ProxyInterfaceSourceGeneratorTests.DTO +{ + public class PersonProxy : IPerson + { + public ProxyInterfaceSourceGeneratorTests.DTO.Person _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 void Void() + { + _Instance.Void(); + } + + public string HelloWorld(string name) + { + string name_ = name; + var result_15289640 = _Instance.HelloWorld(name_); + return result_15289640; + } + + 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_15289640 = _Instance.Add(s_, @string_); + return result_15289640; + } + + public int DefaultValue(int x = 100) + { + int x_ = x; + var result_54302544 = _Instance.DefaultValue(x_); + return result_54302544; + } + + 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(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(x_, t1_, t2_); + return result_40004473; + } + + public System.Threading.Tasks.Task Method1Async() + { + var result_50153955 = _Instance.Method1Async(); + return result_50153955; + } + + public System.Threading.Tasks.Task Method2Async() + { + var result_1151242754 = _Instance.Method2Async(); + return result_1151242754; + } + + public System.Threading.Tasks.Task Method3Async() + { + var result_1190255658 = _Instance.Method3Async(); + return result_1190255658; + } + + + + + + public PersonProxy(ProxyInterfaceSourceGeneratorTests.DTO.Person instance) + { + _Instance = instance; + + + } + + + } +} +#nullable disable \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs new file mode 100644 index 0000000..40b74bd --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs @@ -0,0 +1,67 @@ +using System.IO; +using CSharp.SourceGenerators.Extensions; +using CSharp.SourceGenerators.Extensions.Models; +using FluentAssertions; +using ProxyInterfaceSourceGenerator; +using Xunit; + +namespace FluentBuilderGeneratorTests +{ + public class ProxyInterfaceSourceGeneratorTest + { + private readonly ProxyInterfaceCodeGenerator _sut; + + public ProxyInterfaceSourceGeneratorTest() + { + _sut = new ProxyInterfaceCodeGenerator(); + } + + [Fact] + public void GenerateFiles_ForSingleClass_Should_GenerateCorrectFiles() + { + // Arrange + var attributeFilename = "ProxyInterfaceGenerator.ProxyAttribute.g.cs"; + var interfaceFilename = "ProxyInterfaceSourceGeneratorTests.DTO.IPerson.g.cs"; + var proxyClassFilename = "ProxyInterfaceSourceGeneratorTests.DTO.PersonProxy.g.cs"; + + var path = "./Source/IPerson.cs"; + var sourceFile = new SourceFile + { + Path = path, + Text = File.ReadAllText(path), + AttributeToAddToInterface = new ExtraAttribute + { + Name = "ProxyInterfaceGenerator.Proxy", + ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.DTO.Person)" + } + }; + + // Act + var result = _sut.Execute(new[] { sourceFile }); + + // Assert + result.Valid.Should().BeTrue(); + result.Files.Should().HaveCount(3); + + // Assert interface + 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); + 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(); + File.WriteAllText($"../../../Destination/{proxyClassFilename}", proxyCode); + proxyCode.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassFilename}")); + } + } +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj new file mode 100644 index 0000000..975143a --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj @@ -0,0 +1,53 @@ + + + + net6.0 + false + 9 + enable + false + + + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + + + + + + + \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/IPerson.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/IPerson.cs new file mode 100644 index 0000000..45e7781 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/IPerson.cs @@ -0,0 +1,6 @@ +namespace ProxyInterfaceSourceGeneratorTests.DTO +{ + public partial interface IPerson + { + } +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs new file mode 100644 index 0000000..bf38d53 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/Person.cs @@ -0,0 +1,65 @@ +using System.Threading.Tasks; + +namespace ProxyInterfaceSourceGeneratorTests.DTO +{ + public class Person + { + 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(int x, T1 t1, T2 t2) + where T1 : struct + where T2 : class, new() + { + return true; + } + + public Task Method1Async() + { + return Task.CompletedTask; + } + + public Task Method2Async() + { + return Task.FromResult(1); + } + + public Task Method3Async() + { + return Task.FromResult((string?)""); + } + } +} \ No newline at end of file