public async Task TestGetFirstDiagnosticWithFixAsync() { var diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); var fixers = CreateFixers(); var code = @" a "; using (var workspace = await TestWorkspace.CreateCSharpAsync(code)) { var logger = SpecializedCollections.SingletonEnumerable(new Lazy<IErrorLoggerService>(() => workspace.Services.GetService<IErrorLoggerService>())); var fixService = new CodeFixService( diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable<Lazy<ISuppressionFixProvider, CodeChangeProviderMetadata>>()); var incrementalAnalyzer = (IIncrementalAnalyzerProvider)diagnosticService; // register diagnostic engine to solution crawler var analyzer = incrementalAnalyzer.CreateIncrementalAnalyzer(workspace); var reference = new MockAnalyzerReference(); var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference); var document = project.Documents.Single(); var unused = await fixService.GetFirstDiagnosticWithFixAsync(document, TextSpan.FromBounds(0, 0), considerSuppressionFixes: false, cancellationToken: CancellationToken.None); var fixer1 = fixers.Single().Value as MockFixer; var fixer2 = reference.Fixer as MockFixer; // check to make sure both of them are called. Assert.True(fixer1.Called); Assert.True(fixer2.Called); } }
public void TestGetFirstDiagnosticWithFixAsync() { var diagnosticService = new DiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); var fixers = CreateFixers(); var fixService = new CodeFixService( diagnosticService, fixers, SpecializedCollections.EmptyEnumerable<Lazy<ISuppressionFixProvider, CodeChangeProviderMetadata>>()); var code = @" a "; using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(code)) { var incrementalAnalzyer = (IIncrementalAnalyzerProvider)diagnosticService; // register diagnostic engine to solution crawler var analyzer = incrementalAnalzyer.CreateIncrementalAnalyzer(workspace); var reference = new MockAnalyzerReference(); var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference); var document = project.Documents.Single(); var unused = fixService.GetFirstDiagnosticWithFixAsync(document, TextSpan.FromBounds(0, 0), CancellationToken.None).Result; var fixer1 = fixers.Single().Value as MockFixer; var fixer2 = reference.Fixer as MockFixer; // check to make sure both of them are called. Assert.True(fixer1.Called); Assert.True(fixer2.Called); } }
public FixAllDiagnosticProvider(CodeFixService codeFixService, ImmutableHashSet <string>?diagnosticIds, bool includeSuppressedDiagnostics) { Debug.Assert(diagnosticIds == null || !diagnosticIds.Contains(IDEDiagnosticIds.RemoveUnnecessarySuppressionDiagnosticId)); Debug.Assert(!includeSuppressedDiagnostics || diagnosticIds == null); _codeFixService = codeFixService; _diagnosticIds = diagnosticIds; _includeSuppressedDiagnostics = includeSuppressedDiagnostics; }
public void TestNoDuplicateSuppressionCodeFixes() { var source = @" class Class { void Method() { [|int x = 0, y = 0;|] } }"; using (var workspace = CreateWorkspaceFromFile(source, parseOptions: null, compilationOptions: null)) { var diagnosticService = new TestDiagnosticAnalyzerService(LanguageNames.CSharp, new CSharpCompilerDiagnosticAnalyzer()); var incrementalAnalyzer = diagnosticService.CreateIncrementalAnalyzer(workspace); var suppressionProvider = CreateDiagnosticProviderAndFixer(workspace).Item2; var suppressionProviderFactory = new Lazy<ISuppressionFixProvider, CodeChangeProviderMetadata>(() => suppressionProvider, new CodeChangeProviderMetadata("SuppressionProvider", languages: new[] { LanguageNames.CSharp })); var fixService = new CodeFixService(diagnosticService, SpecializedCollections.EmptyEnumerable<Lazy<IErrorLoggerService>>(), SpecializedCollections.EmptyEnumerable<Lazy<CodeFixProvider, CodeChangeProviderMetadata>>(), SpecializedCollections.SingletonEnumerable(suppressionProviderFactory)); TextSpan span; var document = GetDocumentAndSelectSpan(workspace, out span); var diagnostics = diagnosticService.GetDiagnosticsForSpanAsync(document, span, CancellationToken.None) .WaitAndGetResult(CancellationToken.None) .Where(d => d.Id == "CS0219"); Assert.Equal(2, diagnostics.Count()); var fixes = fixService.GetFixesAsync(document, span, includeSuppressionFixes: true, cancellationToken: CancellationToken.None) .WaitAndGetResult(CancellationToken.None) .SelectMany(fixCollection => fixCollection.Fixes) .Where(fix => fix.PrimaryDiagnostic.Id == "CS0219"); // Ensure that both the fixes have identical equivalence key, and hence get de-duplicated in LB menu. Assert.Equal(2, fixes.Count()); Assert.NotNull(fixes.First().Action.EquivalenceKey); Assert.Equal(fixes.First().Action.EquivalenceKey, fixes.Last().Action.EquivalenceKey); } }
private static async Task<Tuple<TestWorkspace, TestDiagnosticAnalyzerService, CodeFixService, IErrorLoggerService>> ServiceSetupAsync(CodeFixProvider codefix) { var diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); var fixers = SpecializedCollections.SingletonEnumerable( new Lazy<CodeFixProvider, CodeChangeProviderMetadata>( () => codefix, new CodeChangeProviderMetadata("Test", languages: LanguageNames.CSharp))); var code = @"class Program { }"; var workspace = await CSharpWorkspaceFactory.CreateWorkspaceFromFileAsync(code); var logger = SpecializedCollections.SingletonEnumerable(new Lazy<IErrorLoggerService>(() => new TestErrorLogger())); var errorLogger = logger.First().Value; var fixService = new CodeFixService( diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable<Lazy<ISuppressionFixProvider, CodeChangeProviderMetadata>>()); return Tuple.Create(workspace, diagnosticService, fixService, errorLogger); }
public FixAllDiagnosticProvider(CodeFixService codeFixService, ImmutableHashSet <string> diagnosticIds) { _codeFixService = codeFixService; _diagnosticIds = diagnosticIds; }
private static TestWorkspace ServiceSetup(CodeFixProvider codefix, out TestDiagnosticAnalyzerService diagnosticService, out CodeFixService fixService, out IErrorLoggerService errorLogger) { diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); var fixers = SpecializedCollections.SingletonEnumerable( new Lazy<CodeFixProvider, CodeChangeProviderMetadata>( () => codefix, new CodeChangeProviderMetadata("Test", languages: LanguageNames.CSharp))); var code = @"class Program { }"; var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(code); var logger = SpecializedCollections.SingletonEnumerable(new Lazy<IErrorLoggerService>(() => new TestErrorLogger())); errorLogger = logger.First().Value; fixService = new CodeFixService( diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable<Lazy<ISuppressionFixProvider, CodeChangeProviderMetadata>>()); return workspace; }
public async Task TestNoDuplicateSuppressionCodeFixes() { var source = @" class Class { void Method() { [|int x = 0, y = 0; string s;|] } }"; using (var workspace = await CreateWorkspaceFromFileAsync(source, parseOptions: null, compilationOptions: null)) { var diagnosticService = new TestDiagnosticAnalyzerService(LanguageNames.CSharp, new CSharpCompilerDiagnosticAnalyzer()); var incrementalAnalyzer = diagnosticService.CreateIncrementalAnalyzer(workspace); var suppressionProvider = CreateDiagnosticProviderAndFixer(workspace).Item2; var suppressionProviderFactory = new Lazy<ISuppressionFixProvider, CodeChangeProviderMetadata>(() => suppressionProvider, new CodeChangeProviderMetadata("SuppressionProvider", languages: new[] { LanguageNames.CSharp })); var fixService = new CodeFixService(diagnosticService, SpecializedCollections.EmptyEnumerable<Lazy<IErrorLoggerService>>(), SpecializedCollections.EmptyEnumerable<Lazy<CodeFixProvider, CodeChangeProviderMetadata>>(), SpecializedCollections.SingletonEnumerable(suppressionProviderFactory)); var document = GetDocumentAndSelectSpan(workspace, out var span); var diagnostics = await diagnosticService.GetDiagnosticsForSpanAsync(document, span); Assert.Equal(2, diagnostics.Where(d => d.Id == "CS0219").Count()); var allFixes = (await fixService.GetFixesAsync(document, span, includeSuppressionFixes: true, cancellationToken: CancellationToken.None)) .SelectMany(fixCollection => fixCollection.Fixes); var cs0219Fixes = allFixes.Where(fix => fix.PrimaryDiagnostic.Id == "CS0219"); // Ensure that both the fixes have identical equivalence key, and hence get de-duplicated in LB menu. Assert.Equal(2, cs0219Fixes.Count()); var cs0219EquivalenceKey = cs0219Fixes.First().Action.EquivalenceKey; Assert.NotNull(cs0219EquivalenceKey); Assert.Equal(cs0219EquivalenceKey, cs0219Fixes.Last().Action.EquivalenceKey); // Ensure that there *is* a fix for the other warning and that it has a *different* // equivalence key so that it *doesn't* get de-duplicated Assert.Equal(1, diagnostics.Where(d => d.Id == "CS0168").Count()); var cs0168Fixes = allFixes.Where(fix => fix.PrimaryDiagnostic.Id == "CS0168"); var cs0168EquivalenceKey = cs0168Fixes.Single().Action.EquivalenceKey; Assert.NotNull(cs0168EquivalenceKey); Assert.NotEqual(cs0219EquivalenceKey, cs0168EquivalenceKey); } }