forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenOperations.tests.ps1
More file actions
198 lines (188 loc) · 7.86 KB
/
TokenOperations.tests.ps1
File metadata and controls
198 lines (188 loc) · 7.86 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
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "TokenOperations" {
It "Should return correct AST position for assignment operator in hash table" {
$scriptText = @'
$h = @{
a = 72
b = @{ z = "hi" }
}
'@
$tokens = $null
$parseErrors = $null
$scriptAst = [System.Management.Automation.Language.Parser]::ParseInput($scriptText, [ref] $tokens, [ref] $parseErrors)
$tokenOperations = New-Object Microsoft.Windows.PowerShell.ScriptAnalyzer.TokenOperations -ArgumentList @($tokens, $scriptAst)
$operatorToken = $tokens | Where-Object { $_.Extent.StartLineNumber -eq 3 -and $_.Extent.StartColumnNumber -eq 14}
$hashTableAst = $tokenOperations.GetAstPosition($operatorToken)
$hashTableAst | Should -BeOfType [System.Management.Automation.Language.HashTableAst]
$hashTableAst.Extent.Text | Should -Be '@{ z = "hi" }'
}
Context 'Braced Member Access Ranges' {
BeforeDiscovery {
$RangeTests = @(
@{
Name = 'No braced member access'
ScriptDef = '$object.Prop'
ExpectedRanges = @()
}
@{
Name = 'No braced member access on braced variable name'
ScriptDef = '${object}.Prop'
ExpectedRanges = @()
}
@{
Name = 'Braced member access'
ScriptDef = '$object.{Prop}'
ExpectedRanges = @(
,@(8, 14)
)
}
@{
Name = 'Braced member access with spaces'
ScriptDef = '$object. { Prop }'
ExpectedRanges = @(
,@(9, 17)
)
}
@{
Name = 'Braced member access with newline'
ScriptDef = "`$object.`n{ Prop }"
ExpectedRanges = @(
,@(9, 17)
)
}
@{
Name = 'Braced member access with comment'
ScriptDef = "`$object. <#comment#>{Prop}"
ExpectedRanges = @(
,@(20, 26)
)
}
@{
Name = 'Braced member access with multi-line comment'
ScriptDef = "`$object. <#`ncomment`n#>{Prop}"
ExpectedRanges = @(
,@(22, 28)
)
}
@{
Name = 'Braced member access with inline comment'
ScriptDef = "`$object. #comment`n{Prop}"
ExpectedRanges = @(
,@(18, 24)
)
}
@{
Name = 'Braced member access with inner curly braces'
ScriptDef = "`$object.{{Prop}}"
ExpectedRanges = @(
,@(8, 16)
)
}
@{
Name = 'Indexed Braced member access'
ScriptDef = "`$object[0].{Prop}"
ExpectedRanges = @(
,@(11, 17)
)
}
@{
Name = 'Parenthesized Braced member access'
ScriptDef = "(`$object).{Prop}"
ExpectedRanges = @(
,@(10, 16)
)
}
@{
Name = 'Chained Braced member access'
ScriptDef = "`$object.{Prop}.{InnerProp}"
ExpectedRanges = @(
,@(8, 14)
,@(15, 26)
)
}
@{
Name = 'Multiple Braced member access in larger script'
ScriptDef = @'
$var = 1
$a.prop.{{inner}}
$a.{
$a.{Prop}
}
'@
ExpectedRanges = @(
,@(17, 26)
,@(30, 47)
)
}
)
}
It 'Should correctly identify range for <Name>' -ForEach $RangeTests {
$tokens = $null
$parseErrors = $null
$scriptAst = [System.Management.Automation.Language.Parser]::ParseInput($ScriptDef, [ref] $tokens, [ref] $parseErrors)
$tokenOperations = [Microsoft.Windows.PowerShell.ScriptAnalyzer.TokenOperations]::new($tokens, $scriptAst)
$ranges = $tokenOperations.GetBracedMemberAccessRanges()
$ranges.Count | Should -Be $ExpectedRanges.Count
for ($i = 0; $i -lt $ranges.Count; $i++) {
$ranges[$i].Item1 | Should -Be $ExpectedRanges[$i][0]
$ranges[$i].Item2 | Should -Be $ExpectedRanges[$i][1]
}
}
It 'Should not identify dot-sourcing as braced member access' {
$scriptText = @'
. {5+5}
$a=4;. {10+15}
'@
$tokens = $null
$parseErrors = $null
$scriptAst = [System.Management.Automation.Language.Parser]::ParseInput($scriptText, [ref] $tokens, [ref] $parseErrors)
$tokenOperations = [Microsoft.Windows.PowerShell.ScriptAnalyzer.TokenOperations]::new($tokens, $scriptAst)
$ranges = $tokenOperations.GetBracedMemberAccessRanges()
$ranges.Count | Should -Be 0
}
It 'Should not return a range for an incomplete bracket pair (parse error)' {
$scriptText = @'
$object.{MemberName
'@
$tokens = $null
$parseErrors = $null
$scriptAst = [System.Management.Automation.Language.Parser]::ParseInput($scriptText, [ref] $tokens, [ref] $parseErrors)
$tokenOperations = [Microsoft.Windows.PowerShell.ScriptAnalyzer.TokenOperations]::new($tokens, $scriptAst)
$ranges = $tokenOperations.GetBracedMemberAccessRanges()
$ranges.Count | Should -Be 0
}
It 'Should find the correct range for null-conditional braced member access' {
$scriptText = '$object?.{Prop}'
$tokens = $null
$parseErrors = $null
$scriptAst = [System.Management.Automation.Language.Parser]::ParseInput($scriptText, [ref] $tokens, [ref] $parseErrors)
$tokenOperations = [Microsoft.Windows.PowerShell.ScriptAnalyzer.TokenOperations]::new($tokens, $scriptAst)
$ranges = $tokenOperations.GetBracedMemberAccessRanges()
$ranges.Count | Should -Be 1
$ExpectedRanges = @(
,@(9, 15)
)
for ($i = 0; $i -lt $ranges.Count; $i++) {
$ranges[$i].Item1 | Should -Be $ExpectedRanges[$i][0]
$ranges[$i].Item2 | Should -Be $ExpectedRanges[$i][1]
}
} -Skip:$($PSVersionTable.PSVersion.Major -lt 7)
It 'Should find the correct range for nested null-conditional braced member access' {
$scriptText = '$object?.{Prop?.{InnerProp}}'
$tokens = $null
$parseErrors = $null
$scriptAst = [System.Management.Automation.Language.Parser]::ParseInput($scriptText, [ref] $tokens, [ref] $parseErrors)
$tokenOperations = [Microsoft.Windows.PowerShell.ScriptAnalyzer.TokenOperations]::new($tokens, $scriptAst)
$ranges = $tokenOperations.GetBracedMemberAccessRanges()
$ranges.Count | Should -Be 1
$ExpectedRanges = @(
,@(9, 28)
)
for ($i = 0; $i -lt $ranges.Count; $i++) {
$ranges[$i].Item1 | Should -Be $ExpectedRanges[$i][0]
$ranges[$i].Item2 | Should -Be $ExpectedRanges[$i][1]
}
} -Skip:$($PSVersionTable.PSVersion.Major -lt 7)
}
}