Repository navigation
Expand file tree
/
Copy pathEditorOperationsServiceTests.cs
More file actions
98 lines (79 loc) · 4.45 KB
/
Copy pathEditorOperationsServiceTests.cs
File metadata and controls
98 lines (79 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using OmniSharp.Extensions.LanguageServer.Protocol;
using Xunit;
namespace PowerShellEditorServices.Test.Extensions
{
[Trait("Category", "Extensions")]
public class EditorOperationsServiceTests
{
[Fact]
public void GetWorkspaceOpenDocumentsReturnsOnlyOpenDocumentsAndCurrentInMemoryState()
{
WorkspaceService workspaceService = new(NullLoggerFactory.Instance);
ScriptFile openSaved = CreateFileBuffer(workspaceService, "open-saved.ps1");
openSaved.IsOpen = true;
openSaved.IsInMemory = false;
ScriptFile openUnsaved = CreateFileBuffer(workspaceService, "open-unsaved.ps1");
openUnsaved.IsOpen = true;
openUnsaved.IsInMemory = true;
ScriptFile closed = CreateFileBuffer(workspaceService, "closed.ps1");
closed.IsOpen = false;
closed.IsInMemory = false;
EditorOperationsService editorOperationsService = new(
psesHost: null,
workspaceService,
languageServer: null);
WorkspaceOpenDocument[] documents = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Equal(2, documents.Length);
Assert.Contains(documents, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved);
Assert.Contains(documents, static document => document.Path.EndsWith("open-unsaved.ps1") && !document.Saved);
Assert.DoesNotContain(documents, static document => document.Path.EndsWith("closed.ps1"));
}
[Fact]
public void GetWorkspaceOpenDocumentsTracksEditedAndUntitledSaveStates()
{
WorkspaceService workspaceService = new(NullLoggerFactory.Instance);
ScriptFile openSaved = CreateFileBuffer(workspaceService, "open-saved.ps1");
openSaved.IsOpen = true;
ScriptFile openUntitled = workspaceService.GetFileBuffer("untitled:Untitled-1", initialBuffer: string.Empty);
openUntitled.IsOpen = true;
EditorOperationsService editorOperationsService = new(
psesHost: null,
workspaceService,
languageServer: null);
WorkspaceOpenDocument[] initialDocuments = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Contains(initialDocuments, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved);
Assert.Contains(initialDocuments, static document => document.Path.StartsWith("untitled:", StringComparison.Ordinal) && !document.Saved);
openSaved.ApplyChange(new FileChange
{
Line = 1,
Offset = 1,
EndLine = 1,
EndOffset = 1,
InsertString = "Set-StrictMode -Version Latest"
});
Assert.Contains("Set-StrictMode -Version Latest", openSaved.Contents, StringComparison.Ordinal);
WorkspaceOpenDocument[] editedDocuments = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Contains(editedDocuments, static document => document.Path.EndsWith("open-saved.ps1") && !document.Saved);
MarkAsSaved(openSaved);
MarkAsSaved(openUntitled);
WorkspaceOpenDocument[] savedDocuments = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Contains(savedDocuments, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved);
Assert.Contains(savedDocuments, static document => document.Path.StartsWith("untitled:", StringComparison.Ordinal) && !document.Saved);
}
private static ScriptFile CreateFileBuffer(WorkspaceService workspaceService, string fileName)
{
string filePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), fileName);
return workspaceService.GetFileBuffer(DocumentUri.FromFileSystemPath(filePath), initialBuffer: string.Empty);
}
private static void MarkAsSaved(ScriptFile scriptFile) => scriptFile.IsInMemory = scriptFile.IsUntitled;
}
}