提交 449b9652 编写于 作者: M Manish Vasani

Add unit test to demonstrate the breaking change introduced in #41768, and...

Add unit test to demonstrate the breaking change introduced in #41768, and described in https://github.com/dotnet/roslyn/issues/44553#issuecomment-636305256

Verified the test failed without any product change.
上级 19c0d73e
......@@ -7,7 +7,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -97,33 +96,33 @@ public async Task TestGetFixesAsyncWithDuplicateDiagnostics()
public async Task TestGetCodeFixWithExceptionInRegisterMethod()
{
await GetFirstDiagnosticWithFixAsync(new ErrorCases.ExceptionInRegisterMethod());
await GetAddedFixesAsync(new ErrorCases.ExceptionInRegisterMethod());
await GetAddedFixesWithExceptionValidationAsync(new ErrorCases.ExceptionInRegisterMethod());
}
[Fact]
public async Task TestGetCodeFixWithExceptionInRegisterMethodAsync()
{
await GetFirstDiagnosticWithFixAsync(new ErrorCases.ExceptionInRegisterMethodAsync());
await GetAddedFixesAsync(new ErrorCases.ExceptionInRegisterMethodAsync());
await GetAddedFixesWithExceptionValidationAsync(new ErrorCases.ExceptionInRegisterMethodAsync());
}
[Fact]
public async Task TestGetCodeFixWithExceptionInFixableDiagnosticIds()
{
await GetDefaultFixesAsync(new ErrorCases.ExceptionInFixableDiagnosticIds());
await GetAddedFixesAsync(new ErrorCases.ExceptionInFixableDiagnosticIds());
await GetAddedFixesWithExceptionValidationAsync(new ErrorCases.ExceptionInFixableDiagnosticIds());
}
[Fact(Skip = "https://github.com/dotnet/roslyn/issues/21533")]
public async Task TestGetCodeFixWithExceptionInFixableDiagnosticIds2()
{
await GetDefaultFixesAsync(new ErrorCases.ExceptionInFixableDiagnosticIds2());
await GetAddedFixesAsync(new ErrorCases.ExceptionInFixableDiagnosticIds2());
await GetAddedFixesWithExceptionValidationAsync(new ErrorCases.ExceptionInFixableDiagnosticIds2());
}
[Fact]
public async Task TestGetCodeFixWithExceptionInGetFixAllProvider()
=> await GetAddedFixesAsync(new ErrorCases.ExceptionInGetFixAllProvider());
=> await GetAddedFixesWithExceptionValidationAsync(new ErrorCases.ExceptionInGetFixAllProvider());
private async Task GetDefaultFixesAsync(CodeFixProvider codefix)
{
......@@ -136,7 +135,10 @@ private async Task GetDefaultFixesAsync(CodeFixProvider codefix)
Assert.True(((TestErrorLogger)tuple.errorLogger).Messages.TryGetValue(codefix.GetType().Name, out var message));
}
private async Task GetAddedFixesAsync(CodeFixProvider codefix)
private Task<ImmutableArray<CodeFixCollection>> GetAddedFixesWithExceptionValidationAsync(CodeFixProvider codefix)
=> GetAddedFixesAsync(codefix, diagnosticAnalyzer: new MockAnalyzerReference.MockDiagnosticAnalyzer(), exception: true);
private async Task<ImmutableArray<CodeFixCollection>> GetAddedFixesAsync(CodeFixProvider codefix, DiagnosticAnalyzer diagnosticAnalyzer, bool exception = false)
{
var tuple = ServiceSetup(codefix);
......@@ -145,13 +147,18 @@ private async Task GetAddedFixesAsync(CodeFixProvider codefix)
GetDocumentAndExtensionManager(tuple.analyzerService, workspace, out var document, out var extensionManager);
var incrementalAnalyzer = (IIncrementalAnalyzerProvider)tuple.analyzerService;
var analyzer = incrementalAnalyzer.CreateIncrementalAnalyzer(workspace);
var reference = new MockAnalyzerReference(codefix);
var reference = new MockAnalyzerReference(codefix, ImmutableArray.Create(diagnosticAnalyzer));
var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference);
document = project.Documents.Single();
var fixes = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), includeConfigurationFixes: true, cancellationToken: CancellationToken.None);
Assert.True(extensionManager.IsDisabled(codefix));
Assert.False(extensionManager.IsIgnored(codefix));
if (exception)
{
Assert.True(extensionManager.IsDisabled(codefix));
Assert.False(extensionManager.IsIgnored(codefix));
}
return fixes;
}
private async Task GetFirstDiagnosticWithFixAsync(CodeFixProvider codefix)
......@@ -527,5 +534,52 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return Task.CompletedTask;
}
}
[Theory, WorkItem(44553, "https://github.com/dotnet/roslyn/issues/44553")]
[InlineData(null)]
[InlineData("CodeFixProviderWithDuplicateEquivalenceKeyActions")]
public async Task TestRegisteredCodeActionsWithSameEquivalenceKey(string? equivalenceKey)
{
var diagnosticId = "ID1";
var analyzer = new MockAnalyzerReference.MockDiagnosticAnalyzer(ImmutableArray.Create(diagnosticId));
var fixer = new CodeFixProviderWithDuplicateEquivalenceKeyActions(diagnosticId, equivalenceKey);
// Verify multiple code actions registered with same equivalence key are not de-duped.
var fixes = (await GetAddedFixesAsync(fixer, analyzer)).SelectMany(fixCollection => fixCollection.Fixes).ToList();
Assert.Equal(2, fixes.Count);
}
private sealed class CodeFixProviderWithDuplicateEquivalenceKeyActions : CodeFixProvider
{
private readonly string _diagnosticId;
private readonly string? _equivalenceKey;
public CodeFixProviderWithDuplicateEquivalenceKeyActions(string diagnosticId, string? equivalenceKey)
{
_diagnosticId = diagnosticId;
_equivalenceKey = equivalenceKey;
}
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(_diagnosticId);
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
// Register duplicate code actions with same equivalence key, but different title.
RegisterCodeFix(context, titleSuffix: "1");
RegisterCodeFix(context, titleSuffix: "2");
return Task.CompletedTask;
}
private void RegisterCodeFix(CodeFixContext context, string titleSuffix)
{
context.RegisterCodeFix(
CodeAction.Create(
nameof(CodeFixProviderWithDuplicateEquivalenceKeyActions) + titleSuffix,
ct => Task.FromResult(context.Document),
_equivalenceKey),
context.Diagnostics);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册