forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSemanticTokenTest.cs
185 lines (171 loc) · 7.16 KB
/
SemanticTokenTest.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Management.Automation.Language;
using Microsoft.PowerShell.EditorServices.Handlers;
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 SemanticTokenTest
{
[Fact]
public void TokenizesFunctionElements()
{
const string text = @"
function Get-Sum {
param( [int]$a, [int]$b )
return $a + $b
}
";
ScriptFile scriptFile = ScriptFile.Create(
// Use any absolute path. Even if it doesn't exist.
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
text,
Version.Parse("5.0"));
foreach (Token t in scriptFile.ScriptTokens)
{
List<SemanticToken> mappedTokens = new(PsesSemanticTokensHandler.ConvertToSemanticTokens(t));
switch (t.Text)
{
case "function":
case "param":
case "return":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Keyword == sToken.Type);
break;
case "Get-Sum":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Function == sToken.Type);
break;
case "$a":
case "$b":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Variable == sToken.Type);
break;
case "[int]":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Type == sToken.Type);
break;
case "+":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Operator == sToken.Type);
break;
}
}
}
[Fact]
public void TokenizesStringExpansion()
{
const string text = "Write-Host \"$(Test-Property Get-Whatever) $(Get-Whatever)\"";
ScriptFile scriptFile = ScriptFile.Create(
// Use any absolute path. Even if it doesn't exist.
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
text,
Version.Parse("5.0"));
Token commandToken = scriptFile.ScriptTokens[0];
List<SemanticToken> mappedTokens = new(PsesSemanticTokensHandler.ConvertToSemanticTokens(commandToken));
Assert.Single(mappedTokens, sToken => SemanticTokenType.Function == sToken.Type);
Token stringExpandableToken = scriptFile.ScriptTokens[1];
mappedTokens = new List<SemanticToken>(PsesSemanticTokensHandler.ConvertToSemanticTokens(stringExpandableToken));
Assert.Collection(mappedTokens,
sToken => Assert.Equal(SemanticTokenType.Function, sToken.Type),
sToken => Assert.Equal(SemanticTokenType.Function, sToken.Type),
sToken => Assert.Equal(SemanticTokenType.Function, sToken.Type)
);
}
[Fact]
public void RecognizesTokensWithAsterisk()
{
const string text = @"
function Get-A*A {
}
Get-A*A
";
ScriptFile scriptFile = ScriptFile.Create(
// Use any absolute path. Even if it doesn't exist.
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
text,
Version.Parse("5.0"));
foreach (Token t in scriptFile.ScriptTokens)
{
List<SemanticToken> mappedTokens = new(PsesSemanticTokensHandler.ConvertToSemanticTokens(t));
switch (t.Text)
{
case "function":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Keyword == sToken.Type);
break;
case "Get-A*A":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Function == sToken.Type);
break;
}
}
}
[Fact]
public void RecognizesArrayPropertyInExpandableString()
{
const string text = "\"$(@($Array).Count) OtherText\"";
ScriptFile scriptFile = ScriptFile.Create(
// Use any absolute path. Even if it doesn't exist.
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
text,
Version.Parse("5.0"));
foreach (Token t in scriptFile.ScriptTokens)
{
List<SemanticToken> mappedTokens = new(PsesSemanticTokensHandler.ConvertToSemanticTokens(t));
switch (t.Text)
{
case "$Array":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Variable == sToken.Type);
break;
case "Count":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Property == sToken.Type);
break;
}
}
}
[Fact]
public void RecognizesCurlyQuotedString()
{
const string text = "“^[-'a-z]*”";
ScriptFile scriptFile = ScriptFile.Create(
// Use any absolute path. Even if it doesn't exist.
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
text,
Version.Parse("5.0"));
List<SemanticToken> mappedTokens = new(PsesSemanticTokensHandler.ConvertToSemanticTokens(scriptFile.ScriptTokens[0]));
Assert.Single(mappedTokens, sToken => SemanticTokenType.String == sToken.Type);
}
[Fact]
public void RecognizeEnum()
{
const string text = @"
enum MyEnum{
one
two
three
}
";
ScriptFile scriptFile = ScriptFile.Create(
// Use any absolute path. Even if it doesn't exist.
DocumentUri.FromFileSystemPath(Path.Combine(Path.GetTempPath(), "TestFile.ps1")),
text,
Version.Parse("5.0"));
foreach (Token t in scriptFile.ScriptTokens)
{
List<SemanticToken> mappedTokens = new(PsesSemanticTokensHandler.ConvertToSemanticTokens(t));
switch (t.Text)
{
case "enum":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Keyword == sToken.Type);
break;
case "MyEnum":
case "one":
case "two":
case "three":
Assert.Single(mappedTokens, sToken => SemanticTokenType.Property == sToken.Type);
break;
}
}
}
}
}