Add support to generate code for interface without a namespace (#46)

This commit is contained in:
Stef Heyenrath
2022-12-13 18:51:06 +01:00
committed by GitHub
parent 3eb9d8ce5f
commit 72a40e6f6a
13 changed files with 163 additions and 14 deletions
@@ -0,0 +1,27 @@
//----------------------------------------------------------------------------------------
// <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;
public partial interface INoNamespace
{
ProxyInterfaceSourceGeneratorTests.Source.NoNamespace _Instance { get; }
bool Test { get; set; }
}
#nullable disable
@@ -0,0 +1,36 @@
//----------------------------------------------------------------------------------------
// <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;
public partial class NoNamespaceProxy : INoNamespace
{
public ProxyInterfaceSourceGeneratorTests.Source.NoNamespace _Instance { get; }
public bool Test { get => _Instance.Test; set => _Instance.Test = value; }
public NoNamespaceProxy(ProxyInterfaceSourceGeneratorTests.Source.NoNamespace instance)
{
_Instance = instance;
}
}
#nullable disable
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using CSharp.SourceGenerators.Extensions;
using CSharp.SourceGenerators.Extensions.Models;
using FluentAssertions;
@@ -49,6 +50,48 @@ namespace ProxyInterfaceSourceGeneratorTests
result.Files.Should().HaveCount(1);
}
[Fact]
public void GenerateFiles_When_NoNamespace_Should_GenerateCorrectFiles()
{
// Arrange
var fileNames = new[]
{
"INoNamespace.g.cs",
"NoNamespaceProxy.g.cs"
};
var path = "./Source/INoNamespace.cs";
var sourceFile = new SourceFile
{
Path = path,
Text = File.ReadAllText(path),
AttributeToAddToInterface = new ExtraAttribute
{
Name = "ProxyInterfaceGenerator.Proxy",
ArgumentList = "typeof(ProxyInterfaceSourceGeneratorTests.Source.NoNamespace)"
}
};
// 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_ForSingleClass_Should_GenerateCorrectFiles()
{
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGeneratorTests.Source.AkkaActor
{
public partial interface ILocalActorRefProvider
{
}
}
@@ -1,5 +1,3 @@
using Xunit.Sdk;
namespace ProxyInterfaceSourceGeneratorTests.Source
{
public class Human
@@ -0,0 +1,4 @@
// ReSharper disable once CheckNamespace
public partial interface INoNamespace
{
}
@@ -0,0 +1,6 @@
namespace ProxyInterfaceSourceGeneratorTests.Source;
public class NoNamespace
{
public bool Test { get; set; }
}