public async Task GetLatestCodeDocumentAsync_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 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);
        }