public void TestGetFirstDiagnosticWithFixAsync() { var diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); var fixers = CreateFixers(); var code = @" a "; using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(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 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), considerSuppressionFixes: false, cancellationToken: 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); } }
private static (TestWorkspace workspace, DiagnosticAnalyzerService analyzerService, CodeFixService codeFixService, IErrorLoggerService errorLogger) ServiceSetup( CodeFixProvider codefix, bool includeConfigurationFixProviders = false, bool throwExceptionInFixerCreation = false) { var fixers = SpecializedCollections.SingletonEnumerable( new Lazy <CodeFixProvider, CodeChangeProviderMetadata>( () => throwExceptionInFixerCreation ? throw new Exception() : codefix, new CodeChangeProviderMetadata("Test", languages: LanguageNames.CSharp))); var code = @"class Program { }"; var workspace = TestWorkspace.CreateCSharp(code, composition: s_compositionWithMockDiagnosticUpdateSourceRegistrationService, openDocuments: true); var analyzerReference = new TestAnalyzerReferenceByLanguage(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference })); Assert.IsType <MockDiagnosticUpdateSourceRegistrationService>(workspace.GetService <IDiagnosticUpdateSourceRegistrationService>()); var diagnosticService = Assert.IsType <DiagnosticAnalyzerService>(workspace.GetService <IDiagnosticAnalyzerService>()); var logger = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => new TestErrorLogger())); var errorLogger = logger.First().Value; var configurationFixProviders = includeConfigurationFixProviders ? workspace.ExportProvider.GetExports <IConfigurationFixProvider, CodeChangeProviderMetadata>() : SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, CodeChangeProviderMetadata> >(); var fixService = new CodeFixService( diagnosticService, logger, fixers, configurationFixProviders); return(workspace, diagnosticService, fixService, errorLogger); }
public async Task TestGetFirstDiagnosticWithFixAsync() { var diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); var fixers = CreateFixers(); var code = @" a "; using var workspace = TestWorkspace.CreateCSharp(code); var logger = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => workspace.Services.GetService <IErrorLoggerService>())); var fixService = new CodeFixService( workspace.ExportProvider.GetExportedValue <IThreadingContext>(), diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, 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.GetMostSevereFixableDiagnosticAsync(document, TextSpan.FromBounds(0, 0), 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); }
private async Task <ImmutableArray <CodeFixCollection> > GetNuGetAndVsixCodeFixersCoreAsync( NuGetCodeFixProvider?nugetFixer, VsixCodeFixProvider?vsixFixer, MockAnalyzerReference.MockDiagnosticAnalyzer?diagnosticAnalyzer = null) { var code = @"class C { }"; var diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); var vsixFixers = vsixFixer != null ? SpecializedCollections.SingletonEnumerable(new Lazy <CodeFixProvider, CodeChangeProviderMetadata>(() => vsixFixer, new CodeChangeProviderMetadata(name: nameof(VsixCodeFixProvider), languages: LanguageNames.CSharp))) : SpecializedCollections.EmptyEnumerable <Lazy <CodeFixProvider, CodeChangeProviderMetadata> >(); using var workspace = TestWorkspace.CreateCSharp(code, openDocuments: true); var logger = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => workspace.Services.GetRequiredService <IErrorLoggerService>())); var fixService = new CodeFixService( workspace.ExportProvider.GetExportedValue <IThreadingContext>(), diagnosticService, logger, vsixFixers, SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, CodeChangeProviderMetadata> >()); var incrementalAnalyzer = (IIncrementalAnalyzerProvider)diagnosticService; // register diagnostic engine to solution crawler var analyzer = incrementalAnalyzer.CreateIncrementalAnalyzer(workspace); diagnosticAnalyzer ??= new MockAnalyzerReference.MockDiagnosticAnalyzer(); var analyzers = ImmutableArray.Create <DiagnosticAnalyzer>(diagnosticAnalyzer); var reference = new MockAnalyzerReference(nugetFixer, analyzers); var project = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference); var document = project.Documents.Single(); return(await fixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0), includeConfigurationFixes : false, cancellationToken : CancellationToken.None)); }
private static (TestWorkspace workspace, TestDiagnosticAnalyzerService analyzerService, CodeFixService codeFixService, IErrorLoggerService errorLogger) ServiceSetup( CodeFixProvider codefix, bool includeConfigurationFixProviders = false) { var fixers = SpecializedCollections.SingletonEnumerable( new Lazy <CodeFixProvider, CodeChangeProviderMetadata>( () => codefix, new CodeChangeProviderMetadata("Test", languages: LanguageNames.CSharp))); var code = @"class Program { }"; var workspace = TestWorkspace.CreateCSharp(code, openDocuments: true); var analyzerReference = new TestAnalyzerReferenceByLanguage(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference })); var diagnosticService = new TestDiagnosticAnalyzerService(); var logger = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => new TestErrorLogger())); var errorLogger = logger.First().Value; var configurationFixProviders = includeConfigurationFixProviders ? TestExportProvider.ExportProviderWithCSharpAndVisualBasic.GetExports <IConfigurationFixProvider, CodeChangeProviderMetadata>() : SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, CodeChangeProviderMetadata> >(); var fixService = new CodeFixService( workspace.ExportProvider.GetExportedValue <IThreadingContext>(), diagnosticService, logger, fixers, configurationFixProviders); return(workspace, diagnosticService, fixService, errorLogger); }
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); }
private static Tuple <TestWorkspace, TestDiagnosticAnalyzerService, CodeFixService, IErrorLoggerService> ServiceSetup(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 = TestWorkspace.CreateCSharp(code); var logger = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => new TestErrorLogger())); var errorLogger = logger.First().Value; var fixService = new CodeFixService( workspace.ExportProvider.GetExportedValue <IThreadingContext>(), diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, CodeChangeProviderMetadata> >()); return(Tuple.Create(workspace, diagnosticService, fixService, errorLogger)); }
public async Task TestGetFirstDiagnosticWithFixAsync() { var fixers = CreateFixers(); var code = @" a "; using var workspace = TestWorkspace.CreateCSharp(code, composition: s_compositionWithMockDiagnosticUpdateSourceRegistrationService, openDocuments: true); Assert.IsType <MockDiagnosticUpdateSourceRegistrationService>(workspace.GetService <IDiagnosticUpdateSourceRegistrationService>()); var diagnosticService = Assert.IsType <DiagnosticAnalyzerService>(workspace.GetService <IDiagnosticAnalyzerService>()); var analyzerReference = new TestAnalyzerReferenceByLanguage(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap()); workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference })); var logger = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => workspace.Services.GetRequiredService <IErrorLoggerService>())); var fixService = new CodeFixService( diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, 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.GetMostSevereFixAsync( document, TextSpan.FromBounds(0, 0), CodeActionRequestPriority.None, CodeActionOptions.DefaultProvider, isBlocking : false, CancellationToken.None); var fixer1 = (MockFixer)fixers.Single().Value; var fixer2 = (MockFixer)reference.Fixer !; // check to make sure both of them are called. Assert.True(fixer1.Called); Assert.True(fixer2.Called); }