public async Task HandleRequestAsync_DocumentNotFound_ClearsDiagnostics()
        {
            // Arrange
            var documentManager            = new TestDocumentManager();
            var requestInvoker             = Mock.Of <LSPRequestInvoker>(MockBehavior.Strict);
            var diagnosticsProvider        = Mock.Of <LSPDiagnosticsTranslator>(MockBehavior.Strict);
            var documentSynchronizer       = Mock.Of <LSPDocumentSynchronizer>(MockBehavior.Strict);
            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new VSInternalDocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            var report = Assert.Single(result);

            Assert.Null(report.Diagnostics);
            Assert.Null(report.ResultId);
        }
        public async Task HandleRequestAsync_RemapsDiagnosticRange()
        {
            // Arrange
            var called          = false;
            var documentManager = CreateDocumentManager();

            var requestInvoker = GetRequestInvoker <DocumentDiagnosticsParams, DiagnosticReport[]>(
                RoslynDiagnosticResponse,
                (method, serverContentType, diagnosticParams, ct) =>
            {
                Assert.Equal(MSLSPMethods.DocumentPullDiagnosticName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            var diagnosticsProvider  = GetDiagnosticsProvider(ValidDiagnostic_UnknownName_MappedRange, ValidDiagnostic_InvalidExpression_MappedRange);
            var documentSynchronizer = CreateDocumentSynchronizer();

            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new DocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            var diagnosticReport = Assert.Single(result);

            Assert.Equal(RoslynDiagnosticResponse.First().ResultId, diagnosticReport.ResultId);
            Assert.Collection(diagnosticReport.Diagnostics,
                              d =>
            {
                Assert.Equal(ValidDiagnostic_UnknownName.Code, d.Code);
                Assert.Equal(ValidDiagnostic_UnknownName_MappedRange, d.Range);
            },
                              d =>
            {
                Assert.Equal(ValidDiagnostic_InvalidExpression.Code, d.Code);
                Assert.Equal(ValidDiagnostic_InvalidExpression_MappedRange, d.Range);
            });
        }
        public async Task HandleRequestAsync_DocumentSynchronizationFails_ReturnsNullDiagnostic()
        {
            // Arrange
            var called          = false;
            var documentManager = CreateDocumentManager();

            var requestInvoker = GetRequestInvoker <DocumentDiagnosticsParams, DiagnosticReport[]>(
                RoslynDiagnosticResponse,
                (method, serverContentType, diagnosticParams, ct) =>
            {
                Assert.Equal(MSLSPMethods.DocumentPullDiagnosticName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            var diagnosticsProvider = GetDiagnosticsProvider(ValidDiagnostic_UnknownName_MappedRange, ValidDiagnostic_InvalidExpression_MappedRange);

            var documentSynchronizer = new Mock <LSPDocumentSynchronizer>(MockBehavior.Strict);

            documentSynchronizer
            .Setup(d => d.TrySynchronizeVirtualDocumentAsync(It.IsAny <int>(), It.IsAny <CSharpVirtualDocumentSnapshot>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(false));

            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer.Object, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new DocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.False(called);
            var diagnosticReport = Assert.Single(result);

            Assert.Equal(diagnosticRequest.PreviousResultId, diagnosticReport.ResultId);
            Assert.Null(diagnosticReport.Diagnostics);
        }
        public async Task HandleRequestAsync_VersionMismatch_DiscardsLocation()
        {
            // Arrange
            var called          = false;
            var documentManager = CreateDocumentManager(hostDocumentVersion: 1);

            var requestInvoker = GetRequestInvoker <DocumentDiagnosticsParams, DiagnosticReport[]>(
                RoslynDiagnosticResponse,
                (method, serverContentType, diagnosticParams, ct) =>
            {
                Assert.Equal(MSLSPMethods.DocumentPullDiagnosticName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            // Note the HostDocumentVersion provided by the DiagnosticsProvider = 0,
            // which is different from document version (1) from the DocumentManager
            var diagnosticsProvider  = GetDiagnosticsProvider(ValidDiagnostic_UnknownName_MappedRange, ValidDiagnostic_InvalidExpression_MappedRange);
            var documentSynchronizer = CreateDocumentSynchronizer();

            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new DocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            var returnedReport = Assert.Single(result);

            Assert.Equal(RoslynDiagnosticResponse.First().ResultId, returnedReport.ResultId);
            Assert.Null(returnedReport.Diagnostics);
        }
Example #5
0
        public async Task HandleRequestAsync_RemapFailure_DiscardsLocation()
        {
            // Arrange
            var called          = false;
            var documentManager = CreateDocumentManager();

            var requestInvoker = GetRequestInvoker <DocumentDiagnosticsParams, DiagnosticReport[]>(
                RoslynDiagnosticResponse,
                (method, serverContentType, diagnosticParams, ct) =>
            {
                Assert.Equal(MSLSPMethods.DocumentPullDiagnosticName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            var diagnosticsProvider  = GetDiagnosticsProvider();
            var documentSynchronizer = CreateDocumentSynchronizer();

            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer, diagnosticsProvider);
            var diagnosticRequest          = new DocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            var returnedReport = Assert.Single(result);

            Assert.Equal(RoslynDiagnosticResponse.First().ResultId, returnedReport.ResultId);
            Assert.Null(returnedReport.Diagnostics);
        }
Example #6
0
        public async Task HandleRequestAsync_DocumentNotFound_ReturnsNull()
        {
            // Arrange
            var documentManager            = new TestDocumentManager();
            var requestInvoker             = Mock.Of <LSPRequestInvoker>();
            var diagnosticsProvider        = Mock.Of <LSPDiagnosticsTranslator>();
            var documentSynchronizer       = Mock.Of <LSPDocumentSynchronizer>();
            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer, diagnosticsProvider);
            var diagnosticRequest          = new DocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.Null(result);
        }
        public async Task HandleRequestAsync_NoDiagnosticsAfterFiltering_ReturnsNullDiagnostic()
        {
            // Arrange
            var called          = false;
            var documentManager = CreateDocumentManager();

            var filteredDiagnostic = new Diagnostic()
            {
                Range = new Range()
                {
                    Start = new Position(159, 19),
                    End   = new Position(159, 23)
                },
                Code     = "RemoveUnnecessaryImportsFixable",
                Severity = DiagnosticSeverity.Warning
            };

            var filteredDiagnostic_mappedRange = new Range()
            {
                Start = new Position(49, 19),
                End   = new Position(49, 23)
            };

            var diagnosticReport = new DiagnosticReport()
            {
                ResultId    = "6",
                Diagnostics = new Diagnostic[]
                {
                    filteredDiagnostic
                }
            };

            var requestInvoker = GetRequestInvoker <DocumentDiagnosticsParams, DiagnosticReport[]>(
                new[] { diagnosticReport },
                (method, serverContentType, diagnosticParams, ct) =>
            {
                Assert.Equal(MSLSPMethods.DocumentPullDiagnosticName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            var diagnosticsProvider  = GetDiagnosticsProvider(filteredDiagnostic_mappedRange);
            var documentSynchronizer = CreateDocumentSynchronizer();

            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new DocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            var returnedReport = Assert.Single(result);

            Assert.Equal(diagnosticReport.ResultId, returnedReport.ResultId);
            Assert.Empty(returnedReport.Diagnostics);
        }
        public async Task HandleRequestAsync_RemapFailsButErrorDiagnosticIsShown()
        {
            // Arrange
            var called          = false;
            var documentManager = CreateDocumentManager();

            var unmappableDiagnostic_errorSeverity = new Diagnostic()
            {
                Range = new Range()
                {
                    Start = new Position(149, 19),
                    End   = new Position(149, 23)
                },
                Code     = "CS0103",
                Severity = DiagnosticSeverity.Error
            };

            var unmappableDiagnostic_warningSeverity = new Diagnostic()
            {
                Range = new Range()
                {
                    Start = new Position(159, 19),
                    End   = new Position(159, 23)
                },
                Code     = "IDE003",
                Severity = DiagnosticSeverity.Warning
            };

            var diagnosticReport = new DiagnosticReport()
            {
                ResultId    = "6",
                Diagnostics = new Diagnostic[]
                {
                    unmappableDiagnostic_errorSeverity,
                    unmappableDiagnostic_warningSeverity
                }
            };

            var requestInvoker = GetRequestInvoker <DocumentDiagnosticsParams, DiagnosticReport[]>(
                new[] { diagnosticReport },
                (method, serverContentType, diagnosticParams, ct) =>
            {
                Assert.Equal(MSLSPMethods.DocumentPullDiagnosticName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            var undefinedRange = new Range()
            {
                Start = new Position(-1, -1), End = new Position(-1, -1)
            };
            var diagnosticsProvider = GetDiagnosticsProvider(undefinedRange, undefinedRange);

            var documentSynchronizer = CreateDocumentSynchronizer();

            var documentDiagnosticsHandler = new DocumentPullDiagnosticsHandler(requestInvoker, documentManager, documentSynchronizer, diagnosticsProvider, LoggerProvider);
            var diagnosticRequest          = new DocumentDiagnosticsParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                PreviousResultId = "4"
            };

            // Act
            var result = await documentDiagnosticsHandler.HandleRequestAsync(diagnosticRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);

            var diagnosticReportResult = Assert.Single(result);

            Assert.Equal(diagnosticReport.ResultId, diagnosticReportResult.ResultId);

            var returnedDiagnostic = Assert.Single(diagnosticReportResult.Diagnostics);

            Assert.Equal(unmappableDiagnostic_errorSeverity.Code, returnedDiagnostic.Code);
            Assert.True(returnedDiagnostic.Range.IsUndefined());
        }