Ejemplo n.º 1
0
        public void GetLatestSyntaxTreeAsync_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 getLatestSyntaxTreeTask1 = parser.GetLatestSyntaxTreeAsync(snapshot1);
                var getLatestSyntaxTreeTask2 = parser.GetLatestSyntaxTreeAsync(snapshot2);

                // Assert
                Assert.NotSame(getLatestSyntaxTreeTask1, getLatestSyntaxTreeTask2);
            }
        }
Ejemplo n.º 2
0
        public async Task GetLatestSyntaxTreeAsync_WaitsForParse()
        {
            // 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();
                var syntaxTree   = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create());
                codeDocument.SetSyntaxTree(syntaxTree);
                var args = new BackgroundParserResultsReadyEventArgs(
                    parser._latestChangeReference,
                    codeDocument);

                // Act - 1
                var getLatestSyntaxTreeTask = parser.GetLatestSyntaxTreeAsync(StringTextSnapshot.Empty);

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

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

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

                // Act - 3
                var latestSyntaxTree = await getLatestSyntaxTreeTask;

                // Assert - 3
                Assert.Same(latestSyntaxTree, syntaxTree);
            }
        }
Ejemplo n.º 3
0
        public async Task GetLatestSyntaxTreeAsync_ParserDisposed_ReturnsImmediately()
        {
            // Arrange
            var documentTracker = CreateDocumentTracker();
            var syntaxTree      = RazorSyntaxTree.Parse(TestRazorSourceDocument.Create());
            DefaultVisualStudioRazorParser parser;

            using (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(syntaxTree);
                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 getLatestSyntaxTreeTask = parser.GetLatestSyntaxTreeAsync(newerSnapshot);

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

            // Act - 2
            var latestSyntaxTree = await getLatestSyntaxTreeTask;

            // Assert - 2
            Assert.Same(latestSyntaxTree, syntaxTree);
        }