Repository navigation
Expand file tree
/
Copy pathFileContextTests.cs
More file actions
99 lines (82 loc) · 3.88 KB
/
Copy pathFileContextTests.cs
File metadata and controls
99 lines (82 loc) · 3.88 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
99
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Xunit;
namespace PowerShellEditorServices.Test.Extensions
{
[Trait("Category", "Extensions")]
public class FileContextTests
{
private static EditorContext CreateEditorContext(string content, BufferRange selectedRange)
{
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
ScriptFile scriptFile = ScriptFile.Create(new Uri(filePath), content, new Version("7.0"));
return new EditorContext(
editorOperations: null,
scriptFile,
new BufferPosition(line: 1, column: 1),
selectedRange);
}
// Regression test for https://github.com/PowerShell/PowerShellEditorServices/issues/1496
// where $Context.CurrentFile.GetText($Context.SelectedRange) failed because GetText only
// accepted the concrete FileRange type rather than the IFileRange that SelectedRange returns.
[Fact]
public void CanGetTextFromSelectedRange()
{
EditorContext editorContext = CreateEditorContext(
"Line One\nLine Two\nLine Three",
new BufferRange(2, 1, 2, 9));
IFileRange selectedRange = editorContext.SelectedRange;
string text = editorContext.CurrentFile.GetText(selectedRange);
Assert.Equal("Line Two", text);
}
[Fact]
public void CanGetTextLinesFromSelectedRange()
{
EditorContext editorContext = CreateEditorContext(
"Line One\nLine Two\nLine Three",
new BufferRange(1, 1, 2, 9));
string[] lines = editorContext.CurrentFile.GetTextLines(editorContext.SelectedRange);
Assert.Equal(new[] { "Line One", "Line Two" }, lines);
}
[Fact]
public void CanGetTextFromConstructedFileRange()
{
EditorContext editorContext = CreateEditorContext(
"Line One\nLine Two\nLine Three",
BufferRange.None);
IFileRange range = new FileRange(
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 1),
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 11));
Assert.Equal("Line Three", editorContext.CurrentFile.GetText(range));
}
// The concrete FileRange overloads exist to preserve binary compatibility
// with callers compiled before GetText/GetTextLines were widened to IFileRange.
// Declaring the variable as FileRange (not IFileRange) selects those overloads.
[Fact]
public void CanGetTextFromConcreteFileRange()
{
EditorContext editorContext = CreateEditorContext(
"Line One\nLine Two\nLine Three",
BufferRange.None);
FileRange range = new(
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 1),
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(3, 11));
Assert.Equal("Line Three", editorContext.CurrentFile.GetText(range));
}
[Fact]
public void CanGetTextLinesFromConcreteFileRange()
{
EditorContext editorContext = CreateEditorContext(
"Line One\nLine Two\nLine Three",
BufferRange.None);
FileRange range = new(
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(1, 1),
new Microsoft.PowerShell.EditorServices.Extensions.FilePosition(2, 9));
Assert.Equal(new[] { "Line One", "Line Two" }, editorContext.CurrentFile.GetTextLines(range));
}
}
}