public async Task PublishDiagnosticsAsync_NewDocumentDiagnosticsGetPublished()
        {
            // Arrange
            var processedOpenDocument = TestDocumentSnapshot.Create(OpenedDocument.FilePath);
            var codeDocument          = CreateCodeDocument(SingleDiagnosticCollection);

            processedOpenDocument.With(codeDocument);
            var languageServerDocument = new Mock <ILanguageServerDocument>();

            languageServerDocument.Setup(lsd => lsd.SendNotification(It.IsAny <string>(), It.IsAny <PublishDiagnosticsParams>()))
            .Callback <string, PublishDiagnosticsParams>((method, diagnosticParams) =>
            {
                Assert.Equal(processedOpenDocument.FilePath.TrimStart('/'), diagnosticParams.Uri.AbsolutePath);
                var diagnostic      = Assert.Single(diagnosticParams.Diagnostics);
                var razorDiagnostic = SingleDiagnosticCollection[0];
                processedOpenDocument.TryGetText(out var sourceText);
                var expectedDiagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText);
                Assert.Equal(expectedDiagnostic.Message, diagnostic.Message);
                Assert.Equal(expectedDiagnostic.Severity, diagnostic.Severity);
                Assert.Equal(expectedDiagnostic.Range, diagnostic.Range);
            }).Verifiable();
            var languageServer = new Mock <ILanguageServer>();

            languageServer.Setup(server => server.Document).Returns(languageServerDocument.Object);
            using (var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, LoggerFactory))
            {
                publisher.Initialize(ProjectManager);

                // Act
                await publisher.PublishDiagnosticsAsync(processedOpenDocument);

                // Assert
                languageServerDocument.VerifyAll();
            }
        }
        public async Task PublishDiagnosticsAsync_NewDiagnosticsGetPublished()
        {
            // Arrange
            var processedOpenDocument = TestDocumentSnapshot.Create(OpenedDocument.FilePath);
            var codeDocument          = CreateCodeDocument(SingleDiagnosticCollection);

            processedOpenDocument.With(codeDocument);
            var languageServer = new Mock <ITextDocumentLanguageServer>(MockBehavior.Strict);

            languageServer.Setup(server => server.SendNotification((It.IsAny <IRequest>()))).Callback <IRequest>((@params) =>
            {
                var diagnosticParams = (PublishDiagnosticsParams)@params;
                Assert.Equal(processedOpenDocument.FilePath.TrimStart('/'), diagnosticParams.Uri.ToUri().AbsolutePath);
                var diagnostic      = Assert.Single(diagnosticParams.Diagnostics);
                var razorDiagnostic = SingleDiagnosticCollection[0];
                processedOpenDocument.TryGetText(out var sourceText);
                var expectedDiagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText);
                Assert.Equal(expectedDiagnostic.Message, diagnostic.Message);
                Assert.Equal(expectedDiagnostic.Severity, diagnostic.Severity);
                Assert.Equal(expectedDiagnostic.Range, diagnostic.Range);
            });

            using (var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, LoggerFactory))
            {
                publisher._publishedDiagnostics[processedOpenDocument.FilePath] = EmptyDiagnostics;
                publisher.Initialize(ProjectManager);

                // Act
                await publisher.PublishDiagnosticsAsync(processedOpenDocument);

                // Assert
                languageServer.VerifyAll();
            }
        }
Пример #3
0
        // Internal for testing
        internal async Task PublishDiagnosticsAsync(DocumentSnapshot document)
        {
            var result = await document.GetGeneratedOutputAsync();

            var diagnostics = result.GetCSharpDocument().Diagnostics;

            lock (_publishedDiagnostics)
            {
                if (_publishedDiagnostics.TryGetValue(document.FilePath, out var previousDiagnostics) &&
                    diagnostics.SequenceEqual(previousDiagnostics))
                {
                    // Diagnostics are the same as last publish
                    return;
                }

                _publishedDiagnostics[document.FilePath] = diagnostics;
            }

            if (!document.TryGetText(out var sourceText))
            {
                Debug.Fail("Document source text should already be available.");
            }
            var convertedDiagnostics = diagnostics.Select(razorDiagnostic => RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText));

            PublishDiagnosticsForFilePath(document.FilePath, convertedDiagnostics);

            if (_logger.IsEnabled(LogLevel.Trace))
            {
                var diagnosticString = string.Join(", ", diagnostics.Select(diagnostic => diagnostic.Id));
                _logger.LogTrace($"Publishing diagnostics for document '{document.FilePath}': {diagnosticString}");
            }
        }
Пример #4
0
        public void Convert_Converts()
        {
            // Arrange
            var razorDiagnostic = RazorDiagnosticFactory.CreateDirective_BlockDirectiveCannotBeImported("test");
            var sourceText      = SourceText.From(string.Empty);

            // Act
            var diagnostic = RazorDiagnosticConverter.Convert(razorDiagnostic, sourceText);

            // Assert
            Assert.Equal(razorDiagnostic.Id, diagnostic.Code);
            Assert.Equal(razorDiagnostic.GetMessage(CultureInfo.InvariantCulture), diagnostic.Message);
            Assert.Null(diagnostic.Range);
            Assert.Equal(DiagnosticSeverity.Error, diagnostic.Severity);
        }