Ejemplo n.º 1
0
        public void DocumentProcessed_NewWorkQueued_RestartsTimer()
        {
            // Arrange
            var processedOpenDocument = TestDocumentSnapshot.Create(OpenedDocument.FilePath);
            var codeDocument          = CreateCodeDocument(SingleDiagnosticCollection);

            processedOpenDocument.With(codeDocument);
            var languageServerDocument = Mock.Of <ILanguageServerDocument>();
            var languageServer         = Mock.Of <ILanguageServer>(server => server.Document == languageServerDocument);

            using (var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer, LoggerFactory)
            {
                BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: true),
                NotifyBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false),
            })
            {
                publisher.Initialize(ProjectManager);
                publisher.DocumentProcessed(processedOpenDocument);
                Assert.True(publisher.NotifyBackgroundWorkCompleting.Wait(TimeSpan.FromSeconds(2)));
                publisher.NotifyBackgroundWorkCompleting.Reset();

                // Act
                publisher.DocumentProcessed(processedOpenDocument);
                publisher.BlockBackgroundWorkCompleting.Set();

                // Assert
                // Verify that background work starts completing "again"
                Assert.True(publisher.NotifyBackgroundWorkCompleting.Wait(TimeSpan.FromSeconds(2)));
            }
        }
        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 void ClearClosedDocuments_ClearsDiagnosticsForClosedDocument()
        {
            // Arrange
            var languageServerDocument = new Mock <ILanguageServerDocument>();

            languageServerDocument.Setup(lsd => lsd.SendNotification(It.IsAny <string>(), It.IsAny <PublishDiagnosticsParams>()))
            .Callback <string, PublishDiagnosticsParams>((method, diagnosticParams) =>
            {
                Assert.Equal(ClosedDocument.FilePath.TrimStart('/'), diagnosticParams.Uri.AbsolutePath);
                Assert.Empty(diagnosticParams.Diagnostics);
            }).Verifiable();
            var languageServer = new Mock <ILanguageServer>();

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

                // Act
                publisher.ClearClosedDocuments();

                // 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();
            }
        }
        public void DocumentProcessed_NewWorkQueued_RestartsTimer()
        {
            // Arrange
            var processedOpenDocument = TestDocumentSnapshot.Create(OpenedDocument.FilePath);
            var codeDocument          = CreateCodeDocument(SingleDiagnosticCollection);

            processedOpenDocument.With(codeDocument);
            // ILanguageServerDocument
            var languageServerDocument = new Mock <ITextDocumentLanguageServer>(MockBehavior.Strict).Object;

            Mock.Get(languageServerDocument).Setup(d => d.SendNotification(It.IsAny <IRequest>())).Verifiable();
            using (var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServerDocument, LoggerFactory)
            {
                BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: true),
                NotifyBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false),
            })
            {
                publisher.Initialize(ProjectManager);
                publisher.DocumentProcessed(processedOpenDocument);
                Assert.True(publisher.NotifyBackgroundWorkCompleting.Wait(TimeSpan.FromSeconds(2)));
                publisher.NotifyBackgroundWorkCompleting.Reset();

                // Act
                publisher.DocumentProcessed(processedOpenDocument);
                publisher.BlockBackgroundWorkCompleting.Set();

                // Assert
                // Verify that background work starts completing "again"
                Assert.True(publisher.NotifyBackgroundWorkCompleting.Wait(TimeSpan.FromSeconds(2)));
            }
        }
        public void ClearClosedDocuments_NoopsIfDocumentIsClosedButNoDiagnostics()
        {
            // Arrange
            var languageServer = new Mock <ITextDocumentLanguageServer>();

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

                // Act & Assert
                publisher.ClearClosedDocuments();
            }
        }
        public void ClearClosedDocuments_NoopsIfDocumentIsStillOpen()
        {
            // Arrange
            var languageServer = new Mock <ITextDocumentLanguageServer>(MockBehavior.Strict);

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

                // Act & Assert
                publisher.ClearClosedDocuments();
            }
        }
        public void ClearClosedDocuments_NoopsIfDocumentIsStillOpen()
        {
            // Arrange
            var languageServer = new Mock <ILanguageServer>();

            languageServer.Setup(server => server.Document).Throws <XunitException>();
            using (var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, LoggerFactory))
            {
                publisher._publishedDiagnostics[OpenedDocument.FilePath] = SingleDiagnosticCollection;
                publisher.Initialize(ProjectManager);

                // Act & Assert
                publisher.ClearClosedDocuments();
            }
        }
        public async Task PublishDiagnosticsAsync_NoopsIfDiagnosticsAreSameAsPreviousPublish()
        {
            // Arrange
            var languageServer        = new Mock <ITextDocumentLanguageServer>();
            var processedOpenDocument = TestDocumentSnapshot.Create(OpenedDocument.FilePath);
            var codeDocument          = CreateCodeDocument(SingleDiagnosticCollection);

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

                // Act & Assert
                await publisher.PublishDiagnosticsAsync(processedOpenDocument);
            }
        }
        public void ClearClosedDocuments_RestartsTimerIfDocumentsStillOpen()
        {
            // Arrange
            var languageServer = new Mock <ITextDocumentLanguageServer>();

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

                // Act
                publisher.ClearClosedDocuments();

                // Assert
                Assert.NotNull(publisher._documentClosedTimer);
            }
        }
        public void ClearClosedDocuments_ClearsDiagnosticsForClosedDocument()
        {
            // Arrange
            var languageServer = new Mock <ITextDocumentLanguageServer>(MockBehavior.Strict);

            languageServer.Setup(server => server.SendNotification(It.IsAny <IRequest>())).Callback <IRequest>((@params) =>
            {
                var diagnosticParams = (PublishDiagnosticsParams)@params;
                Assert.Equal(ClosedDocument.FilePath.TrimStart('/'), diagnosticParams.Uri.ToUri().AbsolutePath);
                Assert.Empty(diagnosticParams.Diagnostics);
            });
            using (var publisher = new TestRazorDiagnosticsPublisher(Dispatcher, languageServer.Object, LoggerFactory))
            {
                publisher._publishedDiagnostics[ClosedDocument.FilePath] = SingleDiagnosticCollection;
                publisher.Initialize(ProjectManager);

                // Act
                publisher.ClearClosedDocuments();

                // Assert
                languageServer.VerifyAll();
            }
        }