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 = new Dictionary <string, DocumentState>(FilePathComparer.Instance); foreach (var kvp in Documents) { documents.Add(kvp.Key, kvp.Value); } documents.Add(hostDocument.FilePath, DocumentState.Create(Services, hostDocument, loader)); var difference = ProjectDifference.DocumentAdded; var state = new ProjectState(this, difference, HostProject, WorkspaceProject, documents); return(state); }
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 async Task DocumentState_CreatedNew_HasEmptyText() { // Arrange & Act var state = DocumentState.Create(Workspace.Services, HostDocument, DocumentState.EmptyLoader); // Assert var text = await state.GetTextAsync(); Assert.Equal(0, text.Length); }
public async Task DocumentState_WithText_CreatesNewState() { // Arrange var original = DocumentState.Create(Workspace.Services, HostDocument, DocumentState.EmptyLoader); // Act var state = original.WithText(Text, VersionStamp.Create()); // Assert var text = await state.GetTextAsync(); Assert.Same(Text, text); }
public void DocumentState_WithProjectWorkspaceStateChange_CachesSnapshotText() { // Arrange var original = DocumentState.Create(Workspace.Services, HostDocument, DocumentState.EmptyLoader) .WithText(Text, VersionStamp.Create()); // Act var state = original.WithProjectWorkspaceStateChange(); // Assert Assert.True(state.TryGetText(out _)); Assert.True(state.TryGetTextVersion(out _)); }
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); }
public void DocumentState_WithWorkspaceProjectChange_TriesToCacheGeneratedOutput() { // Arrange var original = DocumentState.Create(Workspace.Services, Document, DocumentState.EmptyLoader); GC.KeepAlive(original.GeneratedOutput); // Act var state = original.WithWorkspaceProjectChange(); // Assert Assert.Same(state.GeneratedOutput.Older, original.GeneratedOutput); }
public async Task DocumentState_WithProjectWorkspaceStateChange_CachesLoadedText() { // Arrange var original = DocumentState.Create(Workspace.Services, HostDocument, DocumentState.EmptyLoader) .WithTextLoader(TextLoader); await original.GetTextAsync(); // Act var state = original.WithProjectWorkspaceStateChange(); // Assert Assert.True(state.TryGetText(out _)); Assert.True(state.TryGetTextVersion(out _)); }
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); }
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); }