Skip to content

Commit 4811c94

Browse files
author
Kapil Borle
committed
Add more tests
1 parent 6e039bd commit 4811c94

1 file changed

Lines changed: 107 additions & 9 deletions

File tree

Tests/Rules/UseSupportsShouldProcess.tests.ps1

Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,92 @@ function foo {
2222
[bool] $whatif
2323
)
2424
}
25+
'@
26+
$s = " "
27+
$expectedCorrection = @"
28+
function foo {
29+
[CmdletBinding(SupportsShouldProcess)]
30+
param(
31+
$s$s$s$s$s$s$s$s
32+
)
33+
}
34+
"@
35+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
36+
$violations.Count | Should Be 1
37+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
38+
}
39+
40+
It "Should return valid correction text if whatif the first parameter" {
41+
$def = @'
42+
function foo {
43+
param(
44+
[bool] $whatif,
45+
$param1
46+
)
47+
}
48+
'@
49+
$s = " "
50+
$expectedCorrection = @'
51+
function foo {
52+
[CmdletBinding(SupportsShouldProcess)]
53+
param(
54+
$param1
55+
)
56+
}
57+
'@
58+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
59+
$violations.Count | Should Be 1
60+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
61+
}
62+
63+
It "Should return valid correction text if whatif is in the middle" {
64+
$def = @'
65+
function foo {
66+
param(
67+
$param1,
68+
[bool] $whatif,
69+
$param2
70+
)
71+
}
72+
'@
73+
$s = " "
74+
$expectedCorrection = @'
75+
function foo {
76+
[CmdletBinding(SupportsShouldProcess)]
77+
param(
78+
$param1,
79+
$param2
80+
)
81+
}
2582
'@
2683
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
2784
$violations.Count | Should Be 1
28-
Test-CorrectionExtentFromContent $def $violations 1 '[bool] $whatif' ''
85+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
86+
}
87+
88+
89+
It "Should return valid correction text if whatif the last parameter" {
90+
$def = @'
91+
function foo {
92+
param(
93+
$param1,
94+
[bool] $whatif
95+
)
96+
}
97+
'@
98+
$s = " "
99+
$expectedCorrection = @'
100+
function foo {
101+
[CmdletBinding(SupportsShouldProcess)]
102+
param(
103+
$param1
104+
)
105+
}
106+
'@
107+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
108+
$violations.Count | Should Be 1
109+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
110+
29111
}
30112

31113
It "Should find violation if only Confirm is added" {
@@ -36,9 +118,20 @@ function foo {
36118
)
37119
}
38120
'@
121+
122+
# We use this odd construct because the editor auto-removes the trailing whitespaces.
123+
$s = " "
124+
$expectedCorrection = @"
125+
function foo {
126+
[CmdletBinding(SupportsShouldProcess)]
127+
param(
128+
$s$s$s$s$s$s$s$s
129+
)
130+
}
131+
"@
39132
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
40133
$violations.Count | Should Be 1
41-
Test-CorrectionExtentFromContent $def $violations 1 '[bool] $confirm' ''
134+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
42135
}
43136

44137
It "Should find violation if both Whatif and Confirm are added" {
@@ -50,13 +143,18 @@ function foo {
50143
)
51144
}
52145
'@
53-
$expectedViolationText = @'
54-
[bool] $confirm,
55-
[bool] $whatif
56-
'@
57-
$violation = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
58-
$violation.Count | Should Be 1
59-
$violation[0].SuggestedCorrections.Count | Should Be 2
146+
$s = " "
147+
$expectedCorrection = @"
148+
function foo {
149+
[CmdletBinding(SupportsShouldProcess)]
150+
param(
151+
$s$s$s$s$s$s$s$s
152+
)
153+
}
154+
"@
155+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
156+
$violations.Count | Should Be 1
157+
$violations[0].SuggestedCorrections[0].Text | Should Be $expectedCorrection
60158
# TODO Make test-correction extent take more than 1 corrections
61159
# or modify the rule such that it outputs only 1 correction.
62160
# Test-CorrectionExtentFromContent $def $violation 2 $expectedViolationText ''

0 commit comments

Comments
 (0)