Skip to content

Commit 4069f2a

Browse files
committed
Add tests for handling comment run boundaries and indentation resets
1 parent 91e54c2 commit 4069f2a

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

‎Tests/Rules/UseConsistentIndentation.tests.ps1‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,70 @@ function foo {
254254
Should -Be $expectedScriptDefinition
255255
}
256256

257+
It 'Should treat a blank line as a boundary between comment runs with <Kind> indentation' -TestCases @(
258+
@{ Kind = 'space'; Indentation = ' ' }
259+
@{ Kind = 'tab'; Indentation = "`t" }
260+
) {
261+
param([string] $Kind, [string] $Indentation)
262+
$scriptDefinition = @(
263+
'if ($a) {'
264+
"$Indentation# first"
265+
"$Indentation$Indentation# nested"
266+
''
267+
"$Indentation$Indentation# second"
268+
"$Indentation$Indentation$Indentation# nested"
269+
'}'
270+
) -join "`n"
271+
$expectedScriptDefinition = @(
272+
'if ($a) {'
273+
"$Indentation# first"
274+
"$Indentation$Indentation# nested"
275+
''
276+
"$Indentation# second"
277+
"$Indentation$Indentation# nested"
278+
'}'
279+
) -join "`n"
280+
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
281+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
282+
283+
$formatted = Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings
284+
$formatted | Should -Be $expectedScriptDefinition
285+
Invoke-Formatter -ScriptDefinition $formatted -Settings $settings |
286+
Should -Be $expectedScriptDefinition
287+
}
288+
289+
It 'Should restart comment-run indentation after executable code with <Kind> indentation' -TestCases @(
290+
@{ Kind = 'space'; Indentation = ' ' }
291+
@{ Kind = 'tab'; Indentation = "`t" }
292+
) {
293+
param([string] $Kind, [string] $Indentation)
294+
$scriptDefinition = @(
295+
'if ($a) {'
296+
"$Indentation# first"
297+
"$Indentation$Indentation# nested"
298+
"${Indentation}Write-Output 'between'"
299+
"$Indentation$Indentation# second"
300+
"$Indentation$Indentation$Indentation# nested"
301+
'}'
302+
) -join "`n"
303+
$expectedScriptDefinition = @(
304+
'if ($a) {'
305+
"$Indentation# first"
306+
"$Indentation$Indentation# nested"
307+
"${Indentation}Write-Output 'between'"
308+
"$Indentation# second"
309+
"$Indentation$Indentation# nested"
310+
'}'
311+
) -join "`n"
312+
$settings.Rules.PSUseConsistentIndentation.Kind = $Kind
313+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'
314+
315+
$formatted = Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings
316+
$formatted | Should -Be $expectedScriptDefinition
317+
Invoke-Formatter -ScriptDefinition $formatted -Settings $settings |
318+
Should -Be $expectedScriptDefinition
319+
}
320+
257321
It 'Should preserve relative indentation in comments after a line continuation' {
258322
$scriptDefinition = @'
259323
Write-Output `

0 commit comments

Comments
 (0)