Skip to content

Commit 3a47f4a

Browse files
committed
Add tests for more types of operators
1 parent d945a41 commit 3a47f4a

File tree

1 file changed

+146
-1
lines changed

1 file changed

+146
-1
lines changed

Tests/Rules/UseConsistentWhitespace.tests.ps1

+146-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function foo($param) {
119119
}
120120
}
121121

122-
Context "When there is whitespace around assignment and binary operators" {
122+
Context "When there is whitespace around operators" {
123123
BeforeAll {
124124
$ruleConfiguration.CheckInnerBrace = $false
125125
$ruleConfiguration.CheckOpenParen = $false
@@ -180,6 +180,151 @@ $x = $true -and
180180
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
181181
}
182182

183+
It "Should find a violation if there is no space around an arithmetic operator" {
184+
$def = '$z = 3+4'
185+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
186+
Test-CorrectionExtentFromContent $def $violations 1 '+' ' + '
187+
}
188+
189+
It "Should find a violation if there is no space around a bitwise operator" {
190+
$def = '$value = 7-band3'
191+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
192+
Test-CorrectionExtentFromContent $def $violations 1 '-band' ' -band '
193+
}
194+
195+
It "Should find a violation if there is no space around an equality comparison operator" {
196+
$def = '$obviously = 3-lt4'
197+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
198+
Test-CorrectionExtentFromContent $def $violations 1 '-lt' ' -lt '
199+
}
200+
201+
It "Should find a violation if there is no space around a matching operator" {
202+
$def = '$shouldSend = $fromAddress-like"*@*.com"'
203+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
204+
Test-CorrectionExtentFromContent $def $violations 1 '-like' ' -like '
205+
}
206+
207+
It "Should find a violation if there is no space around replace operator" {
208+
$def = '$a = "string"-replace"ing", "aight"'
209+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
210+
Test-CorrectionExtentFromContent $def $violations 1 '-replace' ' -replace '
211+
}
212+
213+
It "Should find a violation if there is no space around a containment operator" {
214+
$def = 'if ("filename.txt"-in$FileList) { }'
215+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
216+
Test-CorrectionExtentFromContent $def $violations 1 '-in' ' -in '
217+
}
218+
219+
It "Should find a violation if there is no space around a type operator" {
220+
$def = '$HoustonWeHaveAProblem = $a-isnot[System.Object]'
221+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
222+
Test-CorrectionExtentFromContent $def $violations 1 '-isnot' ' -isnot '
223+
}
224+
225+
It "Should find a violation if there is no space around a logical operator" {
226+
$def = '$a = $b-xor$c'
227+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
228+
Test-CorrectionExtentFromContent $def $violations 1 '-xor' ' -xor '
229+
}
230+
231+
It "Should find a violation if there is no space around logical not operator but only on one side" {
232+
$def = '$lie = (-not$true)'
233+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
234+
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
235+
}
236+
237+
It "Should find a violation if there is no space around redirection operator" {
238+
$def = '"hi">>secretmessage.txt'
239+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
240+
Test-CorrectionExtentFromContent $def $violations 1 '>>' ' >> '
241+
}
242+
243+
It "Should find a violation if there is no space around binary split operator" {
244+
$def = '$numbers = "one:two:three"-split":"'
245+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
246+
Test-CorrectionExtentFromContent $def $violations 1 '-split' ' -split '
247+
}
248+
249+
It "Should find a violation if there is no space around unary join operator but only on one side" {
250+
$def = 'ConvertFrom-Json (-join(dotnet gitversion))'
251+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
252+
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
253+
}
254+
255+
It "Should find a violation if there is no space around format operator" {
256+
$def = '"{0:X}"-f88'
257+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
258+
Test-CorrectionExtentFromContent $def $violations 1 '-f' ' -f '
259+
}
260+
261+
It "Should find violations if there is no space around ternary operator" -Skip:($PSVersionTable.PSVersion -lt '7.0') {
262+
$def = '($a -is [System.Object])?3:4'
263+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
264+
$violations | Should -HaveCount 2
265+
Test-CorrectionExtentFromContent $def $violations[0] 1 '?' ' ? '
266+
Test-CorrectionExtentFromContent $def $violations[1] 1 ':' ' : '
267+
}
268+
269+
It "Should not find a violation if there is no space around pipeline operator" {
270+
$def = 'Get-It|Forget-It'
271+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
272+
}
273+
274+
It "Should find a violation if there is no space around pipeline chain operator" -Skip:($PSVersionTable.PSVersion -lt '7.0') {
275+
$def = 'Start-Service $ServiceName||Write-Error "Could not start $ServiceName"'
276+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
277+
Test-CorrectionExtentFromContent $def $violations 1 '||' ' || '
278+
}
279+
280+
It "Should find a violation if there is no space around null coalescing operator" -Skip:($PSVersionTable.PSVersion -lt '7.0') {
281+
$def = '${a}??3'
282+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
283+
Test-CorrectionExtentFromContent $def $violations 1 '??' ' ?? '
284+
}
285+
286+
It "Should find a violation if there is no space around call operator" {
287+
$def = '(&$ScriptFile)'
288+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
289+
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
290+
}
291+
292+
It "Should find a violation if there is no space around background operator" -Skip:($PSVersionTable.PSVersion -lt '6.0') {
293+
$def = '(Get-LongThing&)'
294+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
295+
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
296+
}
297+
298+
It "Should find a violation if there is no space around dot sourcing operator" {
299+
$def = '(.$ScriptFile)'
300+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
301+
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
302+
}
303+
304+
It "Should not find a violation if there is no space around member access operator" {
305+
$def = '$PSVersionTable.PSVersion'
306+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
307+
}
308+
309+
It "Should not find a violation if there is no space around comma operator" {
310+
$def = '$somenumbers = 3,4,5'
311+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
312+
}
313+
314+
It "Should not find a violation if there is no space around prefix operator" {
315+
$def = '--$counter'
316+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
317+
}
318+
319+
It "Should not find a violation if there is no space around postfix operator" {
320+
$def = '$counter++'
321+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
322+
}
323+
324+
It "Should not find a violation if there is no space around exclaim operator" {
325+
$def = 'if(!$true){ "FALSE!@!!!!" }'
326+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
327+
}
183328
}
184329

185330
Context "When a comma is not followed by a space" {

0 commit comments

Comments
 (0)