Add unit test for generic class + change generated filename (#50)

This commit is contained in:
Stef Heyenrath
2023-01-08 09:10:17 +01:00
committed by GitHub
parent 0df5944d06
commit d25601cf22
7 changed files with 139 additions and 6 deletions
@@ -28,7 +28,7 @@ internal static class NamedTypeSymbolExtensions
var typeName = namedTypeSymbol.GetFullType();
return !(typeName.Contains('<') && typeName.Contains('>')) ?
typeName :
$"{typeName.Replace('.', '_').Replace('<', '_').Replace('>', '_').Replace(", ", "-")}_{typeName.Count(c => c == ',') + 1}";
$"{typeName.Replace('<', '_').Replace('>', '_').Replace(", ", "-")}_{typeName.Count(c => c == ',') + 1}";
}
public static string GetFullType(this INamedTypeSymbol namedTypeSymbol)
@@ -0,0 +1,42 @@
//----------------------------------------------------------------------------------------
// <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 class GenericProxy<T> : IGeneric<T>
{
public ProxyInterfaceSourceGeneratorTests.Source.Generic<T> _Instance { get; }
public T Test(T value)
{
T value_ = value;
var result__1701808026 = _Instance.Test(value_);
return result__1701808026;
}
public GenericProxy(ProxyInterfaceSourceGeneratorTests.Source.Generic<T> instance)
{
_Instance = instance;
}
}
}
#nullable disable
@@ -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 IGeneric<T>
{
ProxyInterfaceSourceGeneratorTests.Source.Generic<T> _Instance { get; }
T Test(T value);
}
}
#nullable disable
@@ -1,5 +1,3 @@
using System.IO;
using System.Linq;
using CSharp.SourceGenerators.Extensions;
using CSharp.SourceGenerators.Extensions.Models;
using FluentAssertions;
@@ -21,9 +19,7 @@ namespace ProxyInterfaceSourceGeneratorTests
var pp = new PersonProxy(new Person());
var h = pp.AddHuman(new HumanProxy(new Human()));
int x = 0;
_ = pp.AddHuman(new HumanProxy(new Human()));
}
[Fact]
@@ -50,6 +46,48 @@ namespace ProxyInterfaceSourceGeneratorTests
result.Files.Should().HaveCount(1);
}
[Fact]
public void GenerateFiles_ForGenericType_Should_GenerateCorrectFiles()
{
// Arrange
var fileNames = new[]
{
"ProxyInterfaceSourceGeneratorTests.Source.IGeneric.g.cs",
"ProxyInterfaceSourceGeneratorTests.Source.Generic_T__1Proxy.g.cs"
};
var path = "./Source/IGeneric.cs";
var sourceFile = new SourceFile
{
Path = path,
Text = File.ReadAllText(path),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.Generic<>)"
}
};
// Act
var result = _sut.Execute(new[]
{
sourceFile
});
// Assert
result.Valid.Should().BeTrue();
result.Files.Should().HaveCount(fileNames.Length + 1);
foreach (var fileName in fileNames.Select((fileName, index) => new { fileName, index }))
{
var builder = result.Files[fileName.index + 1]; // +1 means skip the attribute
builder.Path.Should().EndWith(fileName.fileName);
if (Write) File.WriteAllText($"../../../Destination/{fileName.fileName}", builder.Text);
builder.Text.Should().Be(File.ReadAllText($"../../../Destination/{fileName.fileName}"));
}
}
[Fact]
public void GenerateFiles_When_NoNamespace_Should_GenerateCorrectFiles()
{
@@ -53,4 +53,13 @@
<Folder Include="Destination\AkkaGenerated\" />
</ItemGroup>
<ItemGroup>
<Compile Update="Source\Generic.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
<Compile Update="Source\IGeneric.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
</Project>
@@ -0,0 +1,10 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class Generic<T>
{
public T Test(T value)
{
return value;
}
}
}
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public partial interface IGeneric
{
}
}