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+
27$ruleName = " PSUseWhitespace"
38$ruleConfiguration = @ {
49 Enable = $true
@@ -18,28 +23,26 @@ $settings = @{
1823Describe " UseWhitespace" {
1924 Context " When an open brace follows a keyword" {
2025 BeforeAll {
21-
2226 $ruleConfiguration.CheckOpenBrace = $true
2327 $ruleConfiguration.CheckOpenParen = $false
2428 $ruleConfiguration.CheckOperator = $false
2529 $ruleConfiguration.CheckSeparator = $false
2630 }
2731
2832 It " Should find a violation if an open brace does not follow whitespace" {
29- $def = @'
33+ $def = @'
3034if ($true){}
3135'@
3236 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
33- $ violations.Count | Should Be 1
37+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
3438 }
3539
3640 It " Should not find violation if an open brace follows a whitespace" {
37- $def = @'
41+ $def = @'
3842if($true) {}
3943'@
4044 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
4145 $violations.Count | Should Be 0
42-
4346 }
4447
4548 }
@@ -53,15 +56,15 @@ if($true) {}
5356 }
5457
5558 It " Should find violation in an if statement" {
56- $def = @'
59+ $def = @'
5760if($true) {}
5861'@
5962 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
60- $ violations.Count | Should Be 1
63+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
6164 }
6265
6366 It " Should not find a violation in a function definition" {
64- $def = @'
67+ $def = @'
6568function foo($param1) {
6669
6770}
@@ -71,7 +74,7 @@ function foo($param1) {
7174 }
7275
7376 It " Should not find a violation in a param block" {
74- $def = @'
77+ $def = @'
7578function foo() {
7679 param( )
7780}
@@ -81,7 +84,7 @@ function foo() {
8184 }
8285
8386 It " Should not find a violation in a nested open paren" {
84- $def = @'
87+ $def = @'
8588function foo($param) {
8689 ((Get-Process))
8790}
@@ -91,7 +94,7 @@ function foo($param) {
9194 }
9295
9396 It " Should not find a violation on a method call" {
94- $def = @'
97+ $def = @'
9598$x.foo("bar")
9699'@
97100 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
@@ -108,39 +111,39 @@ $x.foo("bar")
108111 }
109112
110113 It " Should find a violation if no whitespace around an assignment operator" {
111- $def = @'
114+ $def = @'
112115$x=1
113116'@
114117 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
115- $ violations.Count | Should Be 1
118+ Test-CorrectionExtentFromContent $def $ violations 1 ' = ' ' = '
116119 }
117120
118121 It " Should find a violation if no whitespace before an assignment operator" {
119- $def = @'
122+ $def = @'
120123$x= 1
121124'@
122125 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
123- $ violations.Count | Should Be 1
126+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
124127 }
125128
126129 It " Should find a violation if no whitespace after an assignment operator" {
127- $def = @'
130+ $def = @'
128131$x =1
129132'@
130133 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
131- $ violations.Count | Should Be 1
134+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
132135 }
133136
134137 It " Should find a violation if there is a whitespaces not of size 1 around an assignment operator" {
135- $def = @'
138+ $def = @'
136139$x = 1
137140'@
138141 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
139- $ violations.Count | Should Be 1
142+ Test-CorrectionExtentFromContent $def $ violations 1 ' = ' ' = '
140143 }
141144
142145 It " Should not find violation if there are whitespaces of size 1 around an assignment operator" {
143- $def = @'
146+ $def = @'
144147$x = @"
145148"abc"
146149"@
@@ -150,7 +153,7 @@ $x = @"
150153 }
151154
152155 It " Should not find violation if there are whitespaces of size 1 around an assignment operator for here string" {
153- $def = @'
156+ $def = @'
154157$x = 1
155158'@
156159 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
@@ -171,7 +174,7 @@ $x = 1
171174$x = @(1,2)
172175'@
173176 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
174- $ violations.Count | Should Be 1
177+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
175178 }
176179
177180 It " Should not find a violation if a space follows a comma" {
@@ -196,7 +199,7 @@ $x = @(1, 2)
196199$x = @{a=1;b=2}
197200'@
198201 $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
199- $ violations.Count | Should Be 1
202+ Test-CorrectionExtentFromContent $def $ violations 1 ' ' ' '
200203 }
201204
202205 It " Should not find a violation if a space follows a semi-colon" {
0 commit comments