diff --git a/src/IDisposableGenerator/DisposableCodeWriter.cs b/src/IDisposableGenerator/DisposableCodeWriter.cs index 9ba90e7..0eba3b3 100644 --- a/src/IDisposableGenerator/DisposableCodeWriter.cs +++ b/src/IDisposableGenerator/DisposableCodeWriter.cs @@ -60,7 +60,7 @@ If Not Me.isDisposed AndAlso disposing Then if (classItem.Owns.Any()) { - _ = sourceBuilder.Append($@" If Me.{(classItem.Stream ? "KeepOpen" : "IsOwned")} Then + _ = sourceBuilder.Append($@" If {(classItem.Stream ? "Not Me.KeepOpen" : "Me.IsOwned")} Then "); foreach (var ownedItem in classItem.Owns) { @@ -171,7 +171,7 @@ namespace {workItem.Namespace}; if (classItem.Owns.Any()) { - _ = sourceBuilder.Append($@" if (this.{(classItem.Stream ? "KeepOpen" : "IsOwned")}) + _ = sourceBuilder.Append($@" if ({(classItem.Stream ? "!this.KeepOpen" : "this.IsOwned")}) {{ "); foreach (var ownedItem in classItem.Owns) @@ -285,7 +285,7 @@ namespace {workItem.Namespace} if (classItem.Owns.Any()) { - _ = sourceBuilder.Append($@" if (this.{(classItem.Stream ? "KeepOpen" : "IsOwned")}) + _ = sourceBuilder.Append($@" if ({(classItem.Stream ? "!this.KeepOpen" : "this.IsOwned")}) {{ "); foreach (var ownedItem in classItem.Owns) diff --git a/tests/IDisposableGeneratorTests.CSharp10.cs b/tests/IDisposableGeneratorTests.CSharp10.cs index 9862862..feee154 100644 --- a/tests/IDisposableGeneratorTests.CSharp10.cs +++ b/tests/IDisposableGeneratorTests.CSharp10.cs @@ -198,7 +198,7 @@ protected override void Dispose(bool disposing) { if (!this.isDisposed && disposing) { - if (this.KeepOpen) + if (!this.KeepOpen) { this.testDispose?.Dispose(); this.testDispose = null; diff --git a/tests/IDisposableGeneratorTests.CSharp9.cs b/tests/IDisposableGeneratorTests.CSharp9.cs index 67350ba..2cf649f 100644 --- a/tests/IDisposableGeneratorTests.CSharp9.cs +++ b/tests/IDisposableGeneratorTests.CSharp9.cs @@ -216,7 +216,7 @@ protected override void Dispose(bool disposing) { if (!this.isDisposed && disposing) { - if (this.KeepOpen) + if (!this.KeepOpen) { this.testDispose?.Dispose(); this.testDispose = null; diff --git a/tests/IDisposableGeneratorTests.VisualBasic.cs b/tests/IDisposableGeneratorTests.VisualBasic.cs index 289c188..88a9741 100644 --- a/tests/IDisposableGeneratorTests.VisualBasic.cs +++ b/tests/IDisposableGeneratorTests.VisualBasic.cs @@ -243,7 +243,7 @@ Friend ReadOnly Property KeepOpen As Boolean ''' Protected Overrides Sub Dispose(ByVal disposing As Boolean) If Not Me.isDisposed AndAlso disposing Then - If Me.KeepOpen Then + If Not Me.KeepOpen Then Me.testDispose?.Dispose() Me.testDispose = Nothing End If diff --git a/tests/IDisposableGeneratorTests.cs b/tests/IDisposableGeneratorTests.cs index b7f7f25..cce8ab3 100644 --- a/tests/IDisposableGeneratorTests.cs +++ b/tests/IDisposableGeneratorTests.cs @@ -4,10 +4,7 @@ public partial class IDisposableGeneratorTests { [Fact] public async Task TestGeneratingNoInput() - => _ = await Assert.ThrowsAsync([ExcludeFromCodeCoverage] async () => - { - await RunTest(string.Empty, string.Empty).ConfigureAwait(false); - }).ConfigureAwait(false); + => await RunTest(string.Empty, string.Empty).ConfigureAwait(false); private static async Task RunTest( string generatedSource, @@ -34,9 +31,8 @@ private static async Task RunTest( case false when test is CSGeneratorTest tst: { tst.LanguageVersion = languageVersion!.Value; - var generatedAttributeSource = Properties.Resources.AttributeCodeCSharp!; test.TestState.GeneratedSources.Add( - (typeof(IDisposableGenerator), "GeneratedAttributes.g.cs", generatedAttributeSource)); + (typeof(IDisposableGenerator), "GeneratedAttributes.g.cs", Properties.Resources.AttributeCodeCSharp!)); if (generatedSources is not null && languageVersion == LanguageVersion.CSharp10) { @@ -61,14 +57,15 @@ private static async Task RunTest( } case false when test is VBGeneratorTest: { - var generatedAttributeSource = Properties.Resources.AttributeCodeVisualBasic!; test.TestState.GeneratedSources.Add( - (typeof(IDisposableGeneratorVB), "GeneratedAttributes.g.vb", generatedAttributeSource)); + (typeof(IDisposableGeneratorVB), "GeneratedAttributes.g.vb", Properties.Resources.AttributeCodeVisualBasic!)); test.TestState.GeneratedSources.Add( (typeof(IDisposableGeneratorVB), "Disposables.g.vb", generatedSource)); break; } default: + test.TestState.GeneratedSources.Add( + (typeof(IDisposableGenerator), "GeneratedAttributes.g.cs", Properties.Resources.AttributeCodeCSharp!)); test.TestState.Sources.Clear(); break; }