diff --git a/src/SubtleEngineering.Analyzers.Tests/Obsolescence/CorrectObsolescenceCodeFixTests.cs b/src/SubtleEngineering.Analyzers.Tests/Obsolescence/CorrectObsolescenceCodeFixTests.cs index 481c740..9c837f7 100644 --- a/src/SubtleEngineering.Analyzers.Tests/Obsolescence/CorrectObsolescenceCodeFixTests.cs +++ b/src/SubtleEngineering.Analyzers.Tests/Obsolescence/CorrectObsolescenceCodeFixTests.cs @@ -10,9 +10,12 @@ public class CorrectObsolescenceCodeFixTests { - [Fact] - public async Task ObsoleteMethodWithoutEditorBrowsable_ShouldAddAttribute() + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task ObsoleteMethodWithoutEditorBrowsable_ShouldAddAttribute(bool hide) { + string expectedBrowsable = hide ? "Never" : "Always"; const string code = """ using System; @@ -25,14 +28,14 @@ class TestClass } """; - const string fixedCode = """ + string fixedCode = $$""" using System; using System.ComponentModel; class TestClass { [Obsolete] - [EditorBrowsable(EditorBrowsableState.Never)] + [EditorBrowsable(EditorBrowsableState.{{expectedBrowsable}})] public void ObsoleteMethod() { } @@ -46,10 +49,10 @@ public void ObsoleteMethod() .WithArguments("ObsoleteMethod") }; - var sut = CreateSut(code, fixedCode, expectedDiagnostics); + var sut = CreateSut(code, fixedCode, expectedDiagnostics, hide ? DiagnosticsDetails.Obsolescence.HideEquivalenceKey: DiagnosticsDetails.Obsolescence.KeepEquivalenceKey); - sut.CodeFixTestBehaviors = CodeFixTestBehaviors.SkipFixAllInDocumentCheck; // Ensures only the specific fix is applied - sut.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; // If suppression is involved + //sut.CodeFixTestBehaviors = CodeFixTestBehaviors.SkipFixAllInDocumentCheck; // Ensures only the specific fix is applied + //sut.TestBehaviors |= TestBehaviors.SkipSuppressionCheck; // If suppression is involved await sut.RunAsync(); } @@ -377,7 +380,7 @@ public void ObsoleteMethod() await sut.RunAsync(); } - private VerifyCf.Test CreateSut(string code, string fixedCode, List expected) + private VerifyCf.Test CreateSut(string code, string fixedCode, List expected, string equivalencyKey = DiagnosticsDetails.Obsolescence.HideEquivalenceKey) { var test = new VerifyCf.Test { @@ -392,6 +395,7 @@ private VerifyCf.Test CreateSut(string code, string fixedCode, List AddEditorBrowsableAttributeAsync(context.Document, declarationNode, c), - equivalenceKey: "AddEditorBrowsableAttribute"), + createChangedDocument: c => AddEditorBrowsableAttributeAsync(context.Document, declarationNode, c, true), + equivalenceKey: DiagnosticsDetails.Obsolescence.HideEquivalenceKey), + diagnostic); + context.RegisterCodeFix( + CodeAction.Create( + title: "Keep obsolete element visible for IntelliSense", + createChangedDocument: c => AddEditorBrowsableAttributeAsync(context.Document, declarationNode, c, false), + equivalenceKey: DiagnosticsDetails.Obsolescence.KeepEquivalenceKey), diagnostic); } - private async Task AddEditorBrowsableAttributeAsync(Document document, SyntaxNode declarationNode, CancellationToken cancellationToken) + private async Task AddEditorBrowsableAttributeAsync(Document document, SyntaxNode declarationNode, CancellationToken cancellationToken, bool hide) { var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); @@ -82,7 +88,7 @@ private async Task AddEditorBrowsableAttributeAsync(Document document, SyntaxFactory.MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("EditorBrowsableState"), - SyntaxFactory.IdentifierName("Never")))))); + SyntaxFactory.IdentifierName(hide ? "Never" : "Always")))))); var newAttributeList = SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(editorBrowsableAttribute));