From b063a4b6aff5f2bdf4b6721bf259ed39a24a1a27 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 28 Apr 2024 12:41:55 +0200 Subject: [PATCH] Add tests for interfaces with same name but different namespace (#70) --- ...ests.Namespace1.ClassInNamespaceProxy.g.cs | 29 ++++++++++ ...torTests.Namespace1.IClassInNamespace.g.cs | 22 ++++++++ ...ests.Namespace2.ClassInNamespaceProxy.g.cs | 29 ++++++++++ ...torTests.Namespace2.IClassInNamespace.g.cs | 22 ++++++++ .../ProxyInterfaceSourceGeneratorTest.cs | 54 +++++++++++++++++++ .../ProxyInterfaceSourceGeneratorTests.csproj | 2 +- .../Source/ClassInNamespace1.cs | 7 +++ .../Source/ClassInNamespace2.cs | 7 +++ .../Source/IClassInNamespace1.cs | 7 +++ .../Source/IClassInNamespace2.cs | 7 +++ 10 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.ClassInNamespaceProxy.g.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.IClassInNamespace.g.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.ClassInNamespaceProxy.g.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.IClassInNamespace.g.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace1.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace2.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace1.cs create mode 100644 tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace2.cs diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.ClassInNamespaceProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.ClassInNamespaceProxy.g.cs new file mode 100644 index 0000000..8d015f6 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.ClassInNamespaceProxy.g.cs @@ -0,0 +1,29 @@ +//---------------------------------------------------------------------------------------- +// +// 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.Namespace1 +{ + public partial class ClassInNamespaceProxy : global::ProxyInterfaceSourceGeneratorTests.Namespace1.IClassInNamespace + { + public global::ProxyInterfaceSourceGeneratorTests.Namespace1.ClassInNamespace _Instance { get; } + + + public ClassInNamespaceProxy(global::ProxyInterfaceSourceGeneratorTests.Namespace1.ClassInNamespace instance) + { + _Instance = instance; + + + + } + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.IClassInNamespace.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.IClassInNamespace.g.cs new file mode 100644 index 0000000..09a85ca --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace1.IClassInNamespace.g.cs @@ -0,0 +1,22 @@ +//---------------------------------------------------------------------------------------- +// +// 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.Namespace1 +{ + public partial interface IClassInNamespace + { + global::ProxyInterfaceSourceGeneratorTests.Namespace1.ClassInNamespace _Instance { get; } + + + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.ClassInNamespaceProxy.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.ClassInNamespaceProxy.g.cs new file mode 100644 index 0000000..0480f4b --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.ClassInNamespaceProxy.g.cs @@ -0,0 +1,29 @@ +//---------------------------------------------------------------------------------------- +// +// 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.Namespace2 +{ + public partial class ClassInNamespaceProxy : global::ProxyInterfaceSourceGeneratorTests.Namespace2.IClassInNamespace + { + public global::ProxyInterfaceSourceGeneratorTests.Namespace2.ClassInNamespace _Instance { get; } + + + public ClassInNamespaceProxy(global::ProxyInterfaceSourceGeneratorTests.Namespace2.ClassInNamespace instance) + { + _Instance = instance; + + + + } + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.IClassInNamespace.g.cs b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.IClassInNamespace.g.cs new file mode 100644 index 0000000..ddad4d3 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Destination/ProxyInterfaceSourceGeneratorTests.Namespace2.IClassInNamespace.g.cs @@ -0,0 +1,22 @@ +//---------------------------------------------------------------------------------------- +// +// 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.Namespace2 +{ + public partial interface IClassInNamespace + { + global::ProxyInterfaceSourceGeneratorTests.Namespace2.ClassInNamespace _Instance { get; } + + + } +} +#nullable restore \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs index 27ea67e..1a1f749 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTest.cs @@ -572,4 +572,58 @@ public class ProxyInterfaceSourceGeneratorTest if (Write) File.WriteAllText($"../../../Destination/{proxyClassIHttpMessageInvokerFilename}", proxyIMessageInvoker); proxyIMessageInvoker.Should().NotBeNullOrEmpty().And.Be(File.ReadAllText($"../../../Destination/{proxyClassIHttpMessageInvokerFilename}")); } + + [Fact] + public void GenerateFiles_ForClassWithSameName_But_DifferentNamespace_Should_GenerateCorrectFiles() + { + // Arrange + const string @class = "ClassInNamespace"; + foreach (var x in new[] { 1, 2 }) + { + var attributeFilename = "ProxyInterfaceGenerator.Extra.g.cs"; + var interfaceFilename = $"ProxyInterfaceSourceGeneratorTests.Namespace{x}.I{@class}.g.cs"; + var proxyClassFilename = $"ProxyInterfaceSourceGeneratorTests.Namespace{x}.{@class}Proxy.g.cs"; + + var path = $"./Source/I{@class}{x}.cs"; + var sourceFile = new SourceFile + { + Path = path, + Text = File.ReadAllText(path), + AttributeToAddToInterface = new ExtraAttribute + { + Name = "ProxyInterfaceGenerator.Proxy", + ArgumentList = new[] { $"typeof(ProxyInterfaceSourceGeneratorTests.Namespace{x}.{@class})", "true" } + } + }; + + // Act + var result = _sut.Execute(new[] { sourceFile }); + + // Assert + result.Valid.Should().BeTrue(); + result.Files.Should().HaveCount(3); + + // 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(); + if (Write) 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(); + if (Write) 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 index c9156d3..5acaa96 100644 --- a/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj +++ b/tests/ProxyInterfaceSourceGeneratorTests/ProxyInterfaceSourceGeneratorTests.csproj @@ -3,7 +3,7 @@ net8.0 false - 12.0 + latest enable Debug;Release;DebugAttach enable diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace1.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace1.cs new file mode 100644 index 0000000..c5a1490 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace1.cs @@ -0,0 +1,7 @@ +// ReSharper disable once CheckNamespace +namespace ProxyInterfaceSourceGeneratorTests.Namespace1 +{ + public class ClassInNamespace + { + } +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace2.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace2.cs new file mode 100644 index 0000000..a485120 --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/ClassInNamespace2.cs @@ -0,0 +1,7 @@ +// ReSharper disable once CheckNamespace +namespace ProxyInterfaceSourceGeneratorTests.Namespace2 +{ + public class ClassInNamespace + { + } +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace1.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace1.cs new file mode 100644 index 0000000..ea5f7bd --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace1.cs @@ -0,0 +1,7 @@ +// ReSharper disable once CheckNamespace +namespace ProxyInterfaceSourceGeneratorTests.Namespace1 +{ + public partial interface IClassInNamespace + { + } +} \ No newline at end of file diff --git a/tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace2.cs b/tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace2.cs new file mode 100644 index 0000000..25dcc6b --- /dev/null +++ b/tests/ProxyInterfaceSourceGeneratorTests/Source/IClassInNamespace2.cs @@ -0,0 +1,7 @@ +// ReSharper disable once CheckNamespace +namespace ProxyInterfaceSourceGeneratorTests.Namespace2 +{ + public partial interface IClassInNamespace + { + } +} \ No newline at end of file