-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathTokenOperationsTests.cs
351 lines (307 loc) · 10.9 KB
/
TokenOperationsTests.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using Xunit;
namespace PowerShellEditorServices.Test.Language
{
public class TokenOperationsTests
{
/// <summary>
/// Helper method to create a stub script file and then call FoldableRegions
/// </summary>
private static FoldingReference[] GetRegions(string text)
{
ScriptFile scriptFile = new(
// Use any absolute path. Even if it doesn't exist.
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
text,
Version.Parse("5.0"));
FoldingReference[] result = TokenOperations.FoldableReferences(scriptFile.ScriptTokens).ToArray();
// The foldable regions need to be deterministic for testing so sort the array.
Array.Sort(result);
return result;
}
/// <summary>
/// Helper method to create FoldingReference objects with less typing
/// </summary>
private static FoldingReference CreateFoldingReference(int startLine, int startCharacter, int endLine, int endCharacter, FoldingRangeKind? matchKind)
{
return new FoldingReference
{
StartLine = startLine,
StartCharacter = startCharacter,
EndLine = endLine,
EndCharacter = endCharacter,
Kind = matchKind
};
}
// This PowerShell script will exercise all of the
// folding regions and regions which should not be
// detected. Due to file encoding this could be CLRF or LF line endings
private const string allInOneScript =
@"#Region This should fold
<#
Nested different comment types. This should fold
#>
#EndRegion
# region This should not fold due to whitespace
$shouldFold = $false
# endRegion
function short-func-not-fold {};
<#
.SYNOPSIS
This whole comment block should fold, not just the SYNOPSIS
.EXAMPLE
This whole comment block should fold, not just the EXAMPLE
#>
function New-VSCodeShouldFold {
<#
.SYNOPSIS
This whole comment block should fold, not just the SYNOPSIS
.EXAMPLE
This whole comment block should fold, not just the EXAMPLE
#>
$I = @'
herestrings should fold
'@
# This won't confuse things
Get-Command -Param @I
$I = @""
double quoted herestrings should also fold
""@
# this won't be folded
# This block of comments should be foldable as a single block
# This block of comments should be foldable as a single block
# This block of comments should be foldable as a single block
#region This fools the indentation folding.
Write-Host ""Hello""
#region Nested regions should be foldable
Write-Host ""Hello""
# comment1
Write-Host ""Hello""
#endregion
Write-Host ""Hello""
# comment2
Write-Host ""Hello""
#endregion
$c = {
Write-Host ""Script blocks should be foldable""
}
# Array fools indentation folding
$d = @(
'should fold1',
'should fold2'
)
}
# Make sure contiguous comment blocks can be folded properly
# Comment Block 1
# Comment Block 1
# Comment Block 1
#region Comment Block 3
# Comment Block 2
# Comment Block 2
# Comment Block 2
$something = $true
#endregion Comment Block 3
# What about anonymous variable assignment
${this
is
valid} = 5
#RegIon This should fold due to casing
$foo = 'bar'
#EnDReGion
";
private readonly FoldingReference[] expectedAllInOneScriptFolds = {
CreateFoldingReference(0, 0, 4, 10, FoldingRangeKind.Region),
CreateFoldingReference(1, 0, 3, 2, FoldingRangeKind.Comment),
CreateFoldingReference(10, 0, 15, 2, FoldingRangeKind.Comment),
CreateFoldingReference(16, 30, 63, 1, null),
CreateFoldingReference(17, 0, 22, 2, FoldingRangeKind.Comment),
CreateFoldingReference(23, 7, 26, 2, null),
CreateFoldingReference(31, 5, 34, 2, null),
CreateFoldingReference(38, 2, 40, 0, FoldingRangeKind.Comment),
CreateFoldingReference(42, 2, 52, 14, FoldingRangeKind.Region),
CreateFoldingReference(44, 4, 48, 14, FoldingRangeKind.Region),
CreateFoldingReference(54, 7, 56, 3, null),
CreateFoldingReference(59, 7, 62, 3, null),
CreateFoldingReference(67, 0, 69, 0, FoldingRangeKind.Comment),
CreateFoldingReference(70, 0, 75, 26, FoldingRangeKind.Region),
CreateFoldingReference(71, 0, 73, 0, FoldingRangeKind.Comment),
CreateFoldingReference(78, 0, 80, 6, null),
};
/// <summary>
/// Assertion helper to compare two FoldingReference arrays.
/// </summary>
private static void AssertFoldingReferenceArrays(
FoldingReference[] expected,
FoldingReference[] actual)
{
for (int index = 0; index < expected.Length; index++)
{
Assert.Equal(expected[index], actual[index]);
}
Assert.Equal(expected.Length, actual.Length);
}
[Trait("Category", "Folding")]
[Fact]
public void LanguageServiceFindsFoldablRegionsWithLF()
{
// Remove and CR characters
string testString = allInOneScript.Replace("\r", "");
// Ensure that there are no CR characters in the string
Assert.True(testString.IndexOf("\r\n") == -1, "CRLF should not be present in the test string");
FoldingReference[] result = GetRegions(testString);
AssertFoldingReferenceArrays(expectedAllInOneScriptFolds, result);
}
[Trait("Category", "Folding")]
[Fact]
public void LanguageServiceFindsFoldablRegionsWithCRLF()
{
// The Foldable regions should be the same regardless of line ending type
// Enforce CRLF line endings, if none exist
string testString = allInOneScript;
if (!testString.Contains("\r\n"))
{
testString = testString.Replace("\n", "\r\n");
}
// Ensure that there are CRLF characters in the string
Assert.True(testString.IndexOf("\r\n") != -1, "CRLF should be present in the teststring");
FoldingReference[] result = GetRegions(testString);
AssertFoldingReferenceArrays(expectedAllInOneScriptFolds, result);
}
[Trait("Category", "Folding")]
[Fact]
public void LanguageServiceFindsFoldablRegionsWithMismatchedRegions()
{
const string testString =
@"#endregion should not fold - mismatched
#region This should fold
$something = 'foldable'
#endregion
#region should not fold - mismatched
";
FoldingReference[] expectedFolds = {
CreateFoldingReference(2, 0, 4, 10, FoldingRangeKind.Region)
};
FoldingReference[] result = GetRegions(testString);
AssertFoldingReferenceArrays(expectedFolds, result);
}
[Trait("Category", "Folding")]
[Fact]
public void LanguageServiceFindsFoldablRegionsWithDuplicateRegions()
{
const string testString =
@"# This script causes duplicate/overlapping ranges due to the `(` and `{` characters
$AnArray = @(Get-ChildItem -Path C:\ -Include *.ps1 -File).Where({
$_.FullName -ne 'foo'}).ForEach({
# Do Something
})
";
FoldingReference[] expectedFolds = {
CreateFoldingReference(1, 64, 2, 27, null),
CreateFoldingReference(2, 35, 4, 2, null)
};
FoldingReference[] result = GetRegions(testString);
AssertFoldingReferenceArrays(expectedFolds, result);
}
// This tests that token matching { -> }, @{ -> } and
// ( -> ), @( -> ) and $( -> ) does not confuse the folder
[Trait("Category", "Folding")]
[Fact]
public void LanguageServiceFindsFoldablRegionsWithSameEndToken()
{
const string testString =
@"foreach ($1 in $2) {
$x = @{
'abc' = 'def'
}
}
$y = $(
$arr = @('1', '2'); Write-Host ($arr)
)
";
FoldingReference[] expectedFolds = {
CreateFoldingReference(0, 19, 5, 1, null),
CreateFoldingReference(2, 9, 4, 5, null),
CreateFoldingReference(7, 5, 9, 1, null)
};
FoldingReference[] result = GetRegions(testString);
AssertFoldingReferenceArrays(expectedFolds, result);
}
// A simple PowerShell Classes test
[Trait("Category", "Folding")]
[Fact]
public void LanguageServiceFindsFoldablRegionsWithClasses()
{
const string testString =
@"class TestClass {
[string[]] $TestProperty = @(
'first',
'second',
'third')
[string] TestMethod() {
return $this.TestProperty[0]
}
}
";
FoldingReference[] expectedFolds = {
CreateFoldingReference(0, 16, 9, 1, null),
CreateFoldingReference(1, 31, 4, 16, null),
CreateFoldingReference(6, 26, 8, 5, null)
};
FoldingReference[] result = GetRegions(testString);
AssertFoldingReferenceArrays(expectedFolds, result);
}
// This tests DSC style keywords and param blocks
[Trait("Category", "Folding")]
[Fact]
public void LanguageServiceFindsFoldablRegionsWithDSC()
{
const string testString =
@"Configuration Example
{
param
(
[Parameter()]
[System.String[]]
$NodeName = 'localhost',
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$Credential
)
Import-DscResource -Module ActiveDirectoryCSDsc
Node $AllNodes.NodeName
{
WindowsFeature ADCS-Cert-Authority
{
Ensure = 'Present'
Name = 'ADCS-Cert-Authority'
}
AdcsCertificationAuthority CertificateAuthority
{
IsSingleInstance = 'Yes'
Ensure = 'Present'
Credential = $Credential
CAType = 'EnterpriseRootCA'
DependsOn = '[WindowsFeature]ADCS-Cert-Authority'
}
}
}
";
FoldingReference[] expectedFolds = {
CreateFoldingReference(1, 0, 33, 1, null),
CreateFoldingReference(3, 4, 12, 5, null),
CreateFoldingReference(17, 4, 32, 5, null),
CreateFoldingReference(19, 8, 22, 9, null),
CreateFoldingReference(25, 8, 31, 9, null)
};
FoldingReference[] result = GetRegions(testString);
AssertFoldingReferenceArrays(expectedFolds, result);
}
}
}