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);
        }
Example #2
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);
        }
Example #3
0
        public DefaultDocumentSnapshotTest()
        {
            SourceText = SourceText.From("<p>Hello World</p>");
            Version    = VersionStamp.Create();

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

            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);
        }
Example #4
0
        public ProjectState WithAddedHostDocument(HostDocument hostDocument, Func <Task <TextAndVersion> > loader)
        {
            if (hostDocument == null)
            {
                throw new ArgumentNullException(nameof(hostDocument));
            }

            if (loader == null)
            {
                throw new ArgumentNullException(nameof(loader));
            }

            // Ignore attempts to 'add' a document with different data, we only
            // care about one, so it might as well be the one we have.
            if (Documents.ContainsKey(hostDocument.FilePath))
            {
                return(this);
            }

            var documents = Documents.Add(hostDocument.FilePath, DocumentState.Create(Services, hostDocument, loader));

            // Compute the effect on the import map
            var importTargetPaths         = GetImportDocumentTargetPaths(hostDocument.TargetPath);
            var importsToRelatedDocuments = AddToImportsToRelatedDocuments(ImportsToRelatedDocuments, hostDocument, importTargetPaths);

            // Now check if the updated document is an import - it's important this this happens after
            // updating the imports map.
            if (importsToRelatedDocuments.TryGetValue(hostDocument.TargetPath, out var relatedDocuments))
            {
                foreach (var relatedDocument in relatedDocuments)
                {
                    documents = documents.SetItem(relatedDocument, documents[relatedDocument].WithImportsChange());
                }
            }

            var state = new ProjectState(this, ProjectDifference.DocumentAdded, HostProject, ProjectWorkspaceState, documents, importsToRelatedDocuments);

            return(state);
        }
Example #5
0
        public void TrySetOutput_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 codeDocument   = CreateCodeDocument(csharpDocument, htmlDocument);

            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
            var result = container.TrySetOutput(document, codeDocument, version, version, version);

            // Assert
            Assert.NotNull(container.LatestDocument);
            Assert.True(csharpChanged);
            Assert.True(htmlChanged);
            Assert.True(result);
        }
Example #6
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));
        }
 public ComputedStateTracker(DocumentState state, ComputedStateTracker older = null)
 {
     _lock  = state._lock;
     _older = older;
 }