Ejemplo n.º 1
0
        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);
        }
        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.GetMostSevereFixableDiagnosticAsync(document, TextSpan.FromBounds(0, 0), cancellationToken : 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);
        }