1
- Import-Module PSScriptAnalyzer
1
+ $directory = Split-Path - Parent $MyInvocation.MyCommand.Path
2
+ $testRootDirectory = Split-Path - Parent $directory
3
+
4
+ Import-Module PSScriptAnalyzer
5
+ Import-Module (Join-Path $testRootDirectory " PSScriptAnalyzerTestHelper.psm1" )
6
+
2
7
$ruleName = " PSUseWhitespace"
3
8
$ruleConfiguration = @ {
4
9
Enable = $true
@@ -18,28 +23,26 @@ $settings = @{
18
23
Describe " UseWhitespace" {
19
24
Context " When an open brace follows a keyword" {
20
25
BeforeAll {
21
-
22
26
$ruleConfiguration.CheckOpenBrace = $true
23
27
$ruleConfiguration.CheckOpenParen = $false
24
28
$ruleConfiguration.CheckOperator = $false
25
29
$ruleConfiguration.CheckSeparator = $false
26
30
}
27
31
28
32
It " Should find a violation if an open brace does not follow whitespace" {
29
- $def = @'
33
+ $def = @'
30
34
if ($true){}
31
35
'@
32
36
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
33
- $ violations.Count | Should Be 1
37
+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
34
38
}
35
39
36
40
It " Should not find violation if an open brace follows a whitespace" {
37
- $def = @'
41
+ $def = @'
38
42
if($true) {}
39
43
'@
40
44
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
41
45
$violations.Count | Should Be 0
42
-
43
46
}
44
47
45
48
}
@@ -53,15 +56,15 @@ if($true) {}
53
56
}
54
57
55
58
It " Should find violation in an if statement" {
56
- $def = @'
59
+ $def = @'
57
60
if($true) {}
58
61
'@
59
62
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
60
- $ violations.Count | Should Be 1
63
+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
61
64
}
62
65
63
66
It " Should not find a violation in a function definition" {
64
- $def = @'
67
+ $def = @'
65
68
function foo($param1) {
66
69
67
70
}
@@ -71,7 +74,7 @@ function foo($param1) {
71
74
}
72
75
73
76
It " Should not find a violation in a param block" {
74
- $def = @'
77
+ $def = @'
75
78
function foo() {
76
79
param( )
77
80
}
@@ -81,7 +84,7 @@ function foo() {
81
84
}
82
85
83
86
It " Should not find a violation in a nested open paren" {
84
- $def = @'
87
+ $def = @'
85
88
function foo($param) {
86
89
((Get-Process))
87
90
}
@@ -91,7 +94,7 @@ function foo($param) {
91
94
}
92
95
93
96
It " Should not find a violation on a method call" {
94
- $def = @'
97
+ $def = @'
95
98
$x.foo("bar")
96
99
'@
97
100
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
@@ -108,39 +111,39 @@ $x.foo("bar")
108
111
}
109
112
110
113
It " Should find a violation if no whitespace around an assignment operator" {
111
- $def = @'
114
+ $def = @'
112
115
$x=1
113
116
'@
114
117
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
115
- $ violations.Count | Should Be 1
118
+ Test-CorrectionExtentFromContent $def $ violations 1 ' = ' ' = '
116
119
}
117
120
118
121
It " Should find a violation if no whitespace before an assignment operator" {
119
- $def = @'
122
+ $def = @'
120
123
$x= 1
121
124
'@
122
125
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
123
- $ violations.Count | Should Be 1
126
+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
124
127
}
125
128
126
129
It " Should find a violation if no whitespace after an assignment operator" {
127
- $def = @'
130
+ $def = @'
128
131
$x =1
129
132
'@
130
133
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
131
- $ violations.Count | Should Be 1
134
+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
132
135
}
133
136
134
137
It " Should find a violation if there is a whitespaces not of size 1 around an assignment operator" {
135
- $def = @'
138
+ $def = @'
136
139
$x = 1
137
140
'@
138
141
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
139
- $ violations.Count | Should Be 1
142
+ Test-CorrectionExtentFromContent $def $ violations 1 ' = ' ' = '
140
143
}
141
144
142
145
It " Should not find violation if there are whitespaces of size 1 around an assignment operator" {
143
- $def = @'
146
+ $def = @'
144
147
$x = @"
145
148
"abc"
146
149
"@
@@ -150,7 +153,7 @@ $x = @"
150
153
}
151
154
152
155
It " Should not find violation if there are whitespaces of size 1 around an assignment operator for here string" {
153
- $def = @'
156
+ $def = @'
154
157
$x = 1
155
158
'@
156
159
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
@@ -171,7 +174,7 @@ $x = 1
171
174
$x = @(1,2)
172
175
'@
173
176
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
174
- $ violations.Count | Should Be 1
177
+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
175
178
}
176
179
177
180
It " Should not find a violation if a space follows a comma" {
@@ -196,7 +199,7 @@ $x = @(1, 2)
196
199
$x = @{a=1;b=2}
197
200
'@
198
201
$violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
199
- $ violations.Count | Should Be 1
202
+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
200
203
}
201
204
202
205
It " Should not find a violation if a space follows a semi-colon" {
0 commit comments