Skip to content

Commit 5cbe9a1

Browse files
author
Kapil Borle
committed
Add tests for operator violations
1 parent b158d50 commit 5cbe9a1

1 file changed

Lines changed: 76 additions & 35 deletions

File tree

Tests/Rules/UseWhitespace.tests.ps1

Lines changed: 76 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
$ruleName = "PSUseWhitespace"
33
$ruleConfiguration = @{
44
Enable = $true
5-
CheckOpenBrace = $true
6-
CheckOpenParen = $true
5+
CheckOpenBrace = $false
6+
CheckOpenParen = $false
7+
CheckOperator = $false
78
}
89

910
$settings = @{
@@ -14,84 +15,124 @@ $settings = @{
1415
}
1516

1617
Describe "UseWhitespace" {
17-
Context "When no whitespace is present before if block open brace" {
18+
Context "When an open brace follows a keyword" {
1819
BeforeAll {
20+
21+
$ruleConfiguration.CheckOpenBrace = $true
22+
$ruleConfiguration.CheckOpenParen = $false
23+
$ruleConfiguration.CheckOperator = $false
24+
}
25+
26+
It "Should find a violation if an open brace does not follow whitespace" {
1927
$def = @'
2028
if ($true){}
2129
'@
22-
$ruleConfiguration.CheckOpenParen = $false
23-
$ruleConfiguration.CheckOpenBrace = $true
2430
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
31+
$violations.Count | Should Be 1
2532
}
2633

27-
It "Should find a violation" {
28-
$violations.Count | Should Be 1
34+
It "Should find no violation if an open brace follows a whitespace" {
35+
$def = @'
36+
if($true) {}
37+
'@
38+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
39+
$violations.Count | Should Be 0
40+
2941
}
42+
3043
}
3144

32-
Context "When no whitespace is present before open paren of an if block" {
45+
Context "When a parenthesis follows a keyword" {
3346
BeforeAll {
47+
$ruleConfiguration.CheckOpenBrace = $false
48+
$ruleConfiguration.CheckOpenParen = $true
49+
$ruleConfiguration.CheckOperator = $false
50+
}
51+
52+
It "Should find no violation if an open brace follows a whitespace" {
3453
$def = @'
3554
if($true) {}
3655
'@
37-
$ruleConfiguration.CheckOpenParen = $true
38-
$ruleConfiguration.CheckOpenBrace = $false
3956
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
40-
}
41-
42-
It "Should find a violation" {
4357
$violations.Count | Should Be 1
4458
}
45-
}
4659

47-
Context "When no whitespace is present before open paren of a function definition" {
48-
BeforeAll {
60+
It "Should not find a violation if no whitespace is present before open paren of a function definition" {
4961
$def = @'
5062
function foo($param1) {
5163
5264
}
5365
'@
54-
$ruleConfiguration.CheckOpenParen = $true
55-
$ruleConfiguration.CheckOpenBrace = $false
5666
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
57-
}
58-
59-
It "Should not find a violation" {
6067
$violations.Count | Should Be 0
6168
}
62-
}
6369

64-
Context "When no whitespace is present before open of a param block" {
65-
BeforeAll {
70+
It "Should not find a violation if no whitespace is present before open paren of a param block" {
6671
$def = @'
6772
function foo() {
6873
param( )
6974
}
7075
'@
71-
$ruleConfiguration.CheckOpenParen = $true
72-
$ruleConfiguration.CheckOpenBrace = $false
7376
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
74-
}
75-
76-
It "Should not find a violation" {
7777
$violations.Count | Should Be 0
7878
}
79-
}
8079

81-
Context "When no whitespace is present in a nested open paren" {
82-
BeforeAll {
80+
It "Should not find a violation if no whitespace is present in a nested open paren" {
8381
$def = @'
8482
function foo($param) {
8583
((Get-Process))
8684
}
8785
'@
88-
$ruleConfiguration.CheckOpenParen = $true
86+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
87+
$violations.Count | Should Be 0
88+
}
89+
}
90+
91+
Context "When there is whitespace around assignment and binary operators" {
92+
BeforeAll {
93+
$ruleConfiguration.CheckOpenParen = $false
8994
$ruleConfiguration.CheckOpenBrace = $false
95+
$ruleConfiguration.CheckOperator = $true
96+
}
97+
98+
It "Should find a violation if no whitespace around an assignment operator" {
99+
$def = @'
100+
$x=1
101+
'@
102+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
103+
$violations.Count | Should Be 1
104+
}
105+
106+
It "Should find a violation if no whitespace before an assignment operator" {
107+
$def = @'
108+
$x= 1
109+
'@
110+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
111+
$violations.Count | Should Be 1
112+
}
113+
114+
It "Should find a violation if no whitespace after an assignment operator" {
115+
$def = @'
116+
$x =1
117+
'@
118+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
119+
$violations.Count | Should Be 1
120+
}
121+
122+
It "Should find a violation if there is a whitespaces not of size 1 around an assignment operator" {
123+
$def = @'
124+
$x = 1
125+
'@
90126
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
127+
$violations.Count | Should Be 1
91128
}
92129

93-
It "Should not find a violation" {
130+
It "Should find no violation if there are whitespaces of size 1 around an assignment operator" {
131+
$def = @'
132+
$x = 1
133+
'@
134+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
94135
$violations.Count | Should Be 0
95136
}
96137
}
97-
}
138+
}

0 commit comments

Comments
 (0)