diff --git a/src/IDisposableGenerator/DisposableCodeWriter.cs b/src/IDisposableGenerator/DisposableCodeWriter.cs index b16ede9..342c976 100644 --- a/src/IDisposableGenerator/DisposableCodeWriter.cs +++ b/src/IDisposableGenerator/DisposableCodeWriter.cs @@ -227,7 +227,7 @@ namespace {workItem.Namespace}; // inject the created sources into the users compilation. sourceBuilder.ToString().ToSourceFile($@"Disposables{( workItemCollection.GetWorkItems().Count > 1 - ? $"{workItemCollection.GetWorkItems().IndexOf(workItem)}" : + ? $".{workItemCollection.GetWorkItems().IndexOf(workItem)}" : string.Empty)}.g.cs", ref context); } } diff --git a/tests/IDisposableGeneratorTests.CSharp10.cs b/tests/IDisposableGeneratorTests.CSharp10.cs index 7a29a1e..9862862 100644 --- a/tests/IDisposableGeneratorTests.CSharp10.cs +++ b/tests/IDisposableGeneratorTests.CSharp10.cs @@ -325,4 +325,99 @@ internal partial class TestDisposable public string? test { get; set; } = ""stuff here.""; } ", LanguageVersion.CSharp10).ConfigureAwait(false); + + [Fact] + public async Task TestGeneratingDisposableWithMultipleNamespacesCSharp10() + { + var testSources = new List + { + @"namespace MyApp.AnotherNamespace; + +[GenerateDispose(false)] +internal partial class AnotherDisposable +{ +} +", + }; + var generatedSources = new Dictionary + { + {"Disposables.0.g.cs", @"// +namespace MyApp; + +internal partial class TestDisposable : IDisposable +{ + private bool isDisposed; + + /// + /// Cleans up the resources used by . + /// + public void Dispose() => this.Dispose(true); + + private void Dispose(bool disposing) + { + if (!this.isDisposed && disposing) + { + this.test = null; + this.isDisposed = true; + } + } +} +"}, + {"Disposables.1.g.cs", @"// +namespace MyApp.AnotherNamespace; + +internal partial class AnotherDisposable : IDisposable +{ + private bool isDisposed; + + /// + /// Cleans up the resources used by . + /// + public void Dispose() => this.Dispose(true); + + private void Dispose(bool disposing) + { + if (!this.isDisposed && disposing) + { + this.isDisposed = true; + } + } +} +"} + }; + await RunTest(@"// +namespace MyApp; + +internal partial class TestDisposable : IDisposable +{ + private bool isDisposed; + + /// + /// Cleans up the resources used by . + /// + public void Dispose() => this.Dispose(true); + + private void Dispose(bool disposing) + { + if (!this.isDisposed && disposing) + { + this.test = null; + this.isDisposed = true; + } + } +} +", @"global using System; +global using System.ComponentModel.DataAnnotations; +global using IDisposableGenerator; + +namespace MyApp; + +[GenerateDispose(false)] +internal partial class TestDisposable +{ + [NullOnDispose] + public string? test { get; set; } = ""stuff here.""; +} +", LanguageVersion.CSharp10, testSources, generatedSources).ConfigureAwait(false); + } } diff --git a/tests/IDisposableGeneratorTests.cs b/tests/IDisposableGeneratorTests.cs index 200fe39..b7f7f25 100644 --- a/tests/IDisposableGeneratorTests.cs +++ b/tests/IDisposableGeneratorTests.cs @@ -12,7 +12,9 @@ public async Task TestGeneratingNoInput() private static async Task RunTest( string generatedSource, string testSource, - LanguageVersion? languageVersion = LanguageVersion.CSharp9) + LanguageVersion? languageVersion = LanguageVersion.CSharp9, + List? testSources = null, + Dictionary? generatedSources = null) where TestType : SourceGeneratorTest, IGeneratorTestBase, new() { var test = new TestType @@ -35,8 +37,26 @@ private static async Task RunTest( var generatedAttributeSource = Properties.Resources.AttributeCodeCSharp!; test.TestState.GeneratedSources.Add( (typeof(IDisposableGenerator), "GeneratedAttributes.g.cs", generatedAttributeSource)); - test.TestState.GeneratedSources.Add( - (typeof(IDisposableGenerator), "Disposables.g.cs", generatedSource)); + if (generatedSources is not null + && languageVersion == LanguageVersion.CSharp10) + { + foreach (var source in testSources!) + { + test.TestState.Sources.Add(source); + } + + foreach (var (key, value) in generatedSources) + { + test.TestState.GeneratedSources.Add( + (typeof(IDisposableGenerator), key, value)); + } + } + else + { + test.TestState.GeneratedSources.Add( + (typeof(IDisposableGenerator), "Disposables.g.cs", generatedSource)); + } + break; } case false when test is VBGeneratorTest: