Exemple #1
0
        private static Task <(RazorCodeDocument, VersionStamp, VersionStamp, VersionStamp)> GetOutputAsync(ProjectState project, DocumentState document)
        {
            var projectSnapshot  = new DefaultProjectSnapshot(project);
            var documentSnapshot = new DefaultDocumentSnapshot(projectSnapshot, document);

            return(document.GetGeneratedOutputAndVersionAsync(projectSnapshot, documentSnapshot));
        }
Exemple #2
0
        public void SetOutput(RazorCSharpDocument csharpDocument, DefaultDocumentSnapshot document)
        {
            lock (_setOutputLock)
            {
                if (!document.TryGetTextVersion(out var version))
                {
                    Debug.Fail("The text version should have already been evaluated.");
                    return;
                }

                if (_sourceVersion.HasValue && _sourceVersion == SourceVersion.GetNewerVersion(version))
                {
                    // Latest document is newer than the provided document.
                    return;
                }

                if (!document.TryGetText(out var source))
                {
                    Debug.Fail("The text should have already been evaluated.");
                    return;
                }

                _source         = source;
                _sourceVersion  = version;
                _output         = csharpDocument;
                _latestDocument = document;
                _textContainer.SetText(SourceText.From(Output.GeneratedCode));
            }
        }
        public void SetOutput_InvokesChangedEvent()
        {
            // Arrange
            using var workspace = TestWorkspace.Create();

            var services     = workspace.Services;
            var hostProject  = new HostProject("C:/project.csproj", RazorConfiguration.Default, "project");
            var projectState = ProjectState.Create(services, hostProject);
            var project      = new DefaultProjectSnapshot(projectState);

            var text           = SourceText.From("...");
            var textAndVersion = TextAndVersion.Create(text, VersionStamp.Default);
            var hostDocument   = new HostDocument("C:/file.cshtml", "C:/file.cshtml");
            var documentState  = new DocumentState(services, hostDocument, text, VersionStamp.Default, () => Task.FromResult(textAndVersion));
            var document       = new DefaultDocumentSnapshot(project, documentState);
            var csharpDocument = RazorCSharpDocument.Create("...", RazorCodeGenerationOptions.CreateDefault(), Enumerable.Empty <RazorDiagnostic>());
            var htmlDocument   = RazorHtmlDocument.Create("...", RazorCodeGenerationOptions.CreateDefault());

            var version       = VersionStamp.Create();
            var container     = new GeneratedDocumentContainer();
            var csharpChanged = false;
            var htmlChanged   = false;

            container.GeneratedCSharpChanged += (o, a) => { csharpChanged = true; };
            container.GeneratedHtmlChanged   += (o, a) => { htmlChanged = true; };

            // Act
            container.SetOutput(document, csharpDocument, htmlDocument, version, version, version);

            // Assert
            Assert.NotNull(container.LatestDocument);
            Assert.True(csharpChanged);
            Assert.True(htmlChanged);
        }
        public DefaultDocumentSnapshotTest()
        {
            SourceText = SourceText.From("<p>Hello World</p>");
            Version    = VersionStamp.Create();

            // Create a new HostDocument to avoid mutating the code container
            ComponentCshtmlHostDocument = new HostDocument(TestProjectData.SomeProjectCshtmlComponentFile5);
            ComponentHostDocument       = new HostDocument(TestProjectData.SomeProjectComponentFile1);
            LegacyHostDocument          = new HostDocument(TestProjectData.SomeProjectFile1);
            NestedComponentHostDocument = new HostDocument(TestProjectData.SomeProjectNestedComponentFile3);

            var projectState = ProjectState.Create(Workspace.Services, TestProjectData.SomeProject);
            var project      = new DefaultProjectSnapshot(projectState);

            var textAndVersion = TextAndVersion.Create(SourceText, Version);

            var documentState = DocumentState.Create(Workspace.Services, LegacyHostDocument, () => Task.FromResult(textAndVersion));

            LegacyDocument = new DefaultDocumentSnapshot(project, documentState);

            documentState     = DocumentState.Create(Workspace.Services, ComponentHostDocument, () => Task.FromResult(textAndVersion));
            ComponentDocument = new DefaultDocumentSnapshot(project, documentState);

            documentState           = DocumentState.Create(Workspace.Services, ComponentCshtmlHostDocument, () => Task.FromResult(textAndVersion));
            ComponentCshtmlDocument = new DefaultDocumentSnapshot(project, documentState);

            documentState           = DocumentState.Create(Workspace.Services, NestedComponentHostDocument, () => Task.FromResult(textAndVersion));
            NestedComponentDocument = new DefaultDocumentSnapshot(project, documentState);
        }
        public void SetOutput_AcceptsSameVersionedDocuments()
        {
            // Arrange
            using var workspace = TestWorkspace.Create();

            var services     = workspace.Services;
            var hostProject  = new HostProject("C:/project.csproj", RazorConfiguration.Default, "project");
            var projectState = ProjectState.Create(services, hostProject);
            var project      = new DefaultProjectSnapshot(projectState);

            var text           = SourceText.From("...");
            var textAndVersion = TextAndVersion.Create(text, VersionStamp.Default);
            var hostDocument   = new HostDocument("C:/file.cshtml", "C:/file.cshtml");
            var documentState  = new DocumentState(services, hostDocument, text, VersionStamp.Default, () => Task.FromResult(textAndVersion));
            var document       = new DefaultDocumentSnapshot(project, documentState);
            var newDocument    = new DefaultDocumentSnapshot(project, documentState);

            var csharpDocument = RazorCSharpDocument.Create("...", RazorCodeGenerationOptions.CreateDefault(), Enumerable.Empty <RazorDiagnostic>());
            var htmlDocument   = RazorHtmlDocument.Create("...", RazorCodeGenerationOptions.CreateDefault());

            var version   = VersionStamp.Create();
            var container = new GeneratedDocumentContainer();

            container.SetOutput(document, csharpDocument, htmlDocument, version, version, version);

            // Act
            container.SetOutput(newDocument, csharpDocument, htmlDocument, version, version, version);

            // Assert
            Assert.Same(newDocument, container.LatestDocument);
        }
        public void SetOutput(
            DefaultDocumentSnapshot document,
            RazorCSharpDocument outputCSharp,
            RazorHtmlDocument outputHtml,
            VersionStamp inputVersion,
            VersionStamp outputCSharpVersion,
            VersionStamp outputHtmlVersion)
        {
            lock (_setOutputLock)
            {
                if (_inputVersion.HasValue &&
                    _inputVersion != inputVersion &&
                    _inputVersion == _inputVersion.Value.GetNewerVersion(inputVersion))
                {
                    // Latest document is newer than the provided document.
                    return;
                }

                if (!document.TryGetText(out var source))
                {
                    Debug.Fail("The text should have already been evaluated.");
                    return;
                }

                _source              = source;
                _inputVersion        = inputVersion;
                _outputCSharpVersion = outputCSharpVersion;
                _outputHtmlVersion   = outputHtmlVersion;
                _outputCSharp        = outputCSharp;
                _outputHtml          = outputHtml;
                _latestDocument      = document;
                _csharpTextContainer.SetText(SourceText.From(_outputCSharp.GeneratedCode));
                _htmlTextContainer.SetText(SourceText.From(_outputHtml.GeneratedHtml));
            }
        }
        public DefaultDocumentSnapshotTest()
        {
            var projectState = ProjectState.Create(Workspace.Services, TestProjectData.SomeProject);
            var project      = new DefaultProjectSnapshot(projectState);

            HostDocument = new HostDocument(TestProjectData.SomeProjectFile1.FilePath, TestProjectData.SomeProjectFile1.TargetPath);
            SourceText   = SourceText.From("<p>Hello World</p>");
            Version      = VersionStamp.Create();
            var textAndVersion = TextAndVersion.Create(SourceText, Version);
            var documentState  = DocumentState.Create(Workspace.Services, HostDocument, () => Task.FromResult(textAndVersion));

            Document = new DefaultDocumentSnapshot(project, documentState);
        }
Exemple #8
0
        public override DocumentSnapshot GetDocument(string filePath)
        {
            lock (_lock)
            {
                if (!_documents.TryGetValue(filePath, out var result) &&
                    State.Documents.TryGetValue(filePath, out var state))
                {
                    result = new DefaultDocumentSnapshot(this, state);
                    _documents.Add(filePath, result);
                }

                return(result);
            }
        }
        public async Task GetGeneratedOutputAsync_OnlySetsOutputIfDocumentNewer()
        {
            // Arrange
            var newSourceText    = SourceText.From("NEW!");
            var newDocumentState = Document.State.WithText(newSourceText, Version.GetNewerVersion());
            var newDocument      = new DefaultDocumentSnapshot(Document.ProjectInternal, newDocumentState);

            // Force the output to be the new output
            await newDocument.GetGeneratedOutputAsync();

            // Act
            await Document.GetGeneratedOutputAsync();

            // Assert
            Assert.NotNull(HostDocument.GeneratedCodeContainer.Output);
            Assert.Same(newSourceText, HostDocument.GeneratedCodeContainer.Source);
        }
        public async Task GetGeneratedOutputAsync_OnlySetsOutputIfDocumentNewer()
        {
            // Arrange
            var newSourceText    = SourceText.From("NEW!");
            var newDocumentState = LegacyDocument.State.WithText(newSourceText, Version.GetNewerVersion());
            var newDocument      = new DefaultDocumentSnapshot(LegacyDocument.ProjectInternal, newDocumentState);

            // Force the output to be the new output
            await newDocument.GetGeneratedOutputAsync();

            // Act
            await LegacyDocument.GetGeneratedOutputAsync();

            // Assert
            Assert.NotNull(LegacyHostDocument.GeneratedDocumentContainer.OutputCSharp);
            Assert.Same(newSourceText, LegacyHostDocument.GeneratedDocumentContainer.Source);
            Assert.Equal("NEW!", LegacyHostDocument.GeneratedDocumentContainer.OutputHtml.GeneratedHtml);
        }
        public DefaultDocumentSnapshotTest()
        {
            var services = TestServices.Create(
                new[] { new TestProjectSnapshotProjectEngineFactory() },
                new[] { new TestTagHelperResolver() });

            Workspace = TestWorkspace.Create(services);
            var hostProject  = new HostProject("C:/some/path/project.csproj", RazorConfiguration.Default);
            var projectState = ProjectState.Create(Workspace.Services, hostProject);
            var project      = new DefaultProjectSnapshot(projectState);

            HostDocument = new HostDocument("C:/some/path/file.cshtml", "C:/some/path/file.cshtml");
            SourceText   = Text.SourceText.From("<p>Hello World</p>");
            Version      = VersionStamp.Default.GetNewerVersion();
            var textAndVersion = TextAndVersion.Create(SourceText, Version);
            var documentState  = DocumentState.Create(Workspace.Services, HostDocument, () => Task.FromResult(textAndVersion));

            Document = new DefaultDocumentSnapshot(project, documentState);
        }
Exemple #12
0
        public void SetOutput_AcceptsInitialOutput()
        {
            // Arrange
            var csharpDocument = RazorCSharpDocument.Create("...", RazorCodeGenerationOptions.CreateDefault(), Enumerable.Empty <RazorDiagnostic>());
            var hostProject    = new HostProject("C:/project.csproj", RazorConfiguration.Default);
            var services       = TestWorkspace.Create().Services;
            var projectState   = ProjectState.Create(services, hostProject);
            var project        = new DefaultProjectSnapshot(projectState);
            var hostDocument   = new HostDocument("C:/file.cshtml", "C:/file.cshtml");
            var text           = SourceText.From("...");
            var textAndVersion = TextAndVersion.Create(text, VersionStamp.Default);
            var documentState  = new DocumentState(services, hostDocument, text, VersionStamp.Default, () => Task.FromResult(textAndVersion));
            var document       = new DefaultDocumentSnapshot(project, documentState);
            var container      = new GeneratedCodeContainer();

            // Act
            container.SetOutput(csharpDocument, document);

            // Assert
            Assert.NotNull(container.LatestDocument);
        }
        public bool TrySetOutput(
            DefaultDocumentSnapshot document,
            RazorCodeDocument codeDocument,
            VersionStamp inputVersion,
            VersionStamp outputCSharpVersion,
            VersionStamp outputHtmlVersion)
        {
            lock (_setOutputLock)
            {
                if (_inputVersion.HasValue &&
                    _inputVersion.Value != inputVersion &&
                    _inputVersion == _inputVersion.Value.GetNewerVersion(inputVersion))
                {
                    // Latest document is newer than the provided document.
                    return(false);
                }

                if (!document.TryGetText(out var source))
                {
                    Debug.Fail("The text should have already been evaluated.");
                    return(false);
                }

                _source              = source;
                _inputVersion        = inputVersion;
                _outputCSharpVersion = outputCSharpVersion;
                _outputHtmlVersion   = outputHtmlVersion;
                _outputCSharp        = codeDocument.GetCSharpDocument();
                _outputHtml          = codeDocument.GetHtmlDocument();
                _latestDocument      = document;
                var csharpSourceText = codeDocument.GetCSharpSourceText();
                _csharpTextContainer.SetText(csharpSourceText);
                var htmlSourceText = codeDocument.GetHtmlSourceText();
                _htmlTextContainer.SetText(htmlSourceText);

                return(true);
            }
        }
 public Task <(RazorCodeDocument output, VersionStamp inputVersion, VersionStamp outputCSharpVersion, VersionStamp outputHtmlVersion)> GetGeneratedOutputAndVersionAsync(DefaultProjectSnapshot project, DefaultDocumentSnapshot document)
 {
     return(ComputedState.GetGeneratedOutputAndVersionAsync(project, document));
 }