예제 #1
0
        public void GetLatestCodeDocumentAsync_MultipleCallsWithPendingChangesMemoizesReturnedTasks()
        {
            // Arrange
            var documentTracker = CreateDocumentTracker();

            using (var parser = new DefaultVisualStudioRazorParser(
                       Dispatcher,
                       documentTracker,
                       ProjectEngineFactory,
                       new DefaultErrorReporter(),
                       Mock.Of <VisualStudioCompletionBroker>()))
            {
                var latestChange   = new SourceChange(0, 0, string.Empty);
                var latestSnapshot = documentTracker.TextBuffer.CurrentSnapshot;
                parser._latestChangeReference = new BackgroundParser.ChangeReference(latestChange, latestSnapshot);
                var codeDocument = TestRazorCodeDocument.CreateEmpty();
                codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(TestRazorSourceDocument.Create()));
                var args = new BackgroundParserResultsReadyEventArgs(
                    parser._latestChangeReference,
                    codeDocument);

                // Act
                var getLatestCodeDocumentTask1 = parser.GetLatestCodeDocumentAsync();
                var getLatestCodeDocumentTask2 = parser.GetLatestCodeDocumentAsync();

                // Assert
                Assert.Same(getLatestCodeDocumentTask1, getLatestCodeDocumentTask2);
            }
        }
        public void GetLatestCodeDocumentAsync_MultipleCallsDifferentSnapshotsReturnDifferentTasks()
        {
            // Arrange
            var documentTracker = CreateDocumentTracker();

            using (var parser = new DefaultVisualStudioRazorParser(
                       Dispatcher,
                       documentTracker,
                       ProjectEngineFactory,
                       new DefaultErrorReporter(),
                       Mock.Of <VisualStudioCompletionBroker>()))
            {
                var latestChange   = new SourceChange(0, 0, string.Empty);
                var latestSnapshot = documentTracker.TextBuffer.CurrentSnapshot;
                parser._latestChangeReference = new BackgroundParser.ChangeReference(latestChange, latestSnapshot);
                var snapshot1 = new StringTextSnapshot("Snapshot 1");
                var snapshot2 = new StringTextSnapshot("Snapshot 2");

                // Act
                var getLatestCodeDocumentTask1 = parser.GetLatestCodeDocumentAsync(snapshot1);
                var getLatestCodeDocumentTask2 = parser.GetLatestCodeDocumentAsync(snapshot2);

                // Assert
                Assert.NotSame(getLatestCodeDocumentTask1, getLatestCodeDocumentTask2);
            }
        }
        public void GetLatestCodeDocumentAsync_MultipleCallsSameSnapshotMemoizesReturnedTasks()
        {
            // Arrange
            var documentTracker = CreateDocumentTracker();

            using (var parser = new DefaultVisualStudioRazorParser(
                       JoinableTaskContext,
                       documentTracker,
                       ProjectEngineFactory,
                       new DefaultErrorReporter(),
                       Mock.Of <VisualStudioCompletionBroker>(MockBehavior.Strict)))
            {
                var latestChange   = new SourceChange(0, 0, string.Empty);
                var latestSnapshot = documentTracker.TextBuffer.CurrentSnapshot;
                parser._latestChangeReference = new BackgroundParser.ChangeReference(latestChange, latestSnapshot);
                var sameSnapshot = StringTextSnapshot.Empty;

                // Act
                var getLatestCodeDocumentTask1 = parser.GetLatestCodeDocumentAsync(sameSnapshot);
                var getLatestCodeDocumentTask2 = parser.GetLatestCodeDocumentAsync(sameSnapshot);

                // Assert
                Assert.Same(getLatestCodeDocumentTask1, getLatestCodeDocumentTask2);
            }
        }
예제 #4
0
        public async Task GetLatestCodeDocumentAsync_NoPendingChangesReturnsImmediately()
        {
            // Arrange
            var documentTracker = CreateDocumentTracker();

            using (var parser = new DefaultVisualStudioRazorParser(
                       Dispatcher,
                       documentTracker,
                       ProjectEngineFactory,
                       new DefaultErrorReporter(),
                       Mock.Of <VisualStudioCompletionBroker>()))
            {
                var latestChange   = new SourceChange(0, 0, string.Empty);
                var latestSnapshot = documentTracker.TextBuffer.CurrentSnapshot;
                parser._latestChangeReference = new BackgroundParser.ChangeReference(latestChange, latestSnapshot);
                var codeDocument = TestRazorCodeDocument.CreateEmpty();
                codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(TestRazorSourceDocument.Create()));
                var args = new BackgroundParserResultsReadyEventArgs(
                    parser._latestChangeReference,
                    codeDocument);
                parser.OnDocumentStructureChanged(args);

                // Act - 1
                var getLatestCodeDocumentTask = parser.GetLatestCodeDocumentAsync();

                // Assert - 1
                Assert.True(getLatestCodeDocumentTask.IsCompleted);

                // Act - 2
                var latestCodeDocument = await getLatestCodeDocumentTask;

                // Assert - 2
                Assert.Same(latestCodeDocument, codeDocument);
            }
        }
예제 #5
0
        public async Task GetLatestCodeDocumentAsync_WaitsForParse()
        {
            // Arrange
            var documentTracker = CreateDocumentTracker();

            using (var parser = new DefaultVisualStudioRazorParser(
                       Dispatcher,
                       documentTracker,
                       ProjectEngineFactory,
                       new DefaultErrorReporter(),
                       Mock.Of <VisualStudioCompletionBroker>(MockBehavior.Strict)))
            {
                var latestChange   = new SourceChange(0, 0, string.Empty);
                var latestSnapshot = documentTracker.TextBuffer.CurrentSnapshot;
                parser._latestChangeReference = new BackgroundParser.ChangeReference(latestChange, latestSnapshot);
                var codeDocument = TestRazorCodeDocument.CreateEmpty();
                var syntaxTree   = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create());
                codeDocument.SetSyntaxTree(syntaxTree);
                var args = new BackgroundParserResultsReadyEventArgs(
                    parser._latestChangeReference,
                    codeDocument);

                // Act - 1
                var getLatestCodeDocumentTask = parser.GetLatestCodeDocumentAsync(StringTextSnapshot.Empty);

                // Assert - 1
                Assert.False(getLatestCodeDocumentTask.IsCompleted);

                // Act - 2
                await Task.Run(() => parser.OnResultsReady(sender: null, args));

                // Assert - 2
                Assert.True(getLatestCodeDocumentTask.IsCompleted);

                // Act - 3
                var latestCodeDocument = await getLatestCodeDocumentTask;

                // Assert - 3
                Assert.Same(latestCodeDocument, codeDocument);
            }
        }
        public async Task GetLatestCodeDocumentAsync_ParserDisposed_ReturnsImmediately()
        {
            // Arrange
            var documentTracker = CreateDocumentTracker();
            var codeDocument    = TestRazorCodeDocument.CreateEmpty();
            var syntaxTree      = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create());
            DefaultVisualStudioRazorParser parser;

            codeDocument.SetSyntaxTree(syntaxTree);
            using (parser = new DefaultVisualStudioRazorParser(
                       JoinableTaskContext,
                       documentTracker,
                       ProjectEngineFactory,
                       new DefaultErrorReporter(),
                       Mock.Of <VisualStudioCompletionBroker>(MockBehavior.Strict)))
            {
                var latestChange   = new SourceChange(0, 0, string.Empty);
                var latestSnapshot = documentTracker.TextBuffer.CurrentSnapshot;
                parser._latestChangeReference = new BackgroundParser.ChangeReference(latestChange, latestSnapshot);
                var args = new BackgroundParserResultsReadyEventArgs(
                    parser._latestChangeReference,
                    codeDocument);

                // Initialize the document with some content so we have a syntax tree to return.
                await Task.Run(() => parser.OnResultsReady(sender: null, args));
            }

            var newerSnapshot = new StringTextSnapshot("Newer", versionNumber: 1337);

            // Act - 1
            var getLatestCodeDocumentTask = parser.GetLatestCodeDocumentAsync(newerSnapshot);

            // Assert - 1
            Assert.True(getLatestCodeDocumentTask.IsCompleted);

            // Act - 2
            var latestCodeDocument = await getLatestCodeDocumentTask;

            // Assert - 2
            Assert.Same(latestCodeDocument, codeDocument);
        }