@@ -200,7 +200,7 @@ Some-Command -Param1 @{
200200 }
201201 }
202202
203- Context " When a close brace should be followed by a new line" {
203+ Context " When a close brace should not be followed by a new line" {
204204 BeforeAll {
205205 $ruleConfiguration .' NoEmptyLineBefore' = $false
206206 $ruleConfiguration .' IgnoreOneLineBlock' = $false
@@ -220,27 +220,48 @@ else {
220220 $violations.Count | Should Be 1
221221 }
222222
223- It " Should not find a violation if a branching statement is on a new line if open brance is not on same line " {
223+ It " Should correct violation by cuddling the else branch statement " {
224224 $def = @'
225- if ($true)
226- {
225+ if ($true) {
227226 $true
228227}
229- else
230- {
228+ else {
231229 $false
232230}
233231'@
234- $violations = Invoke-ScriptAnalyzer - ScriptDefinition $def - Settings $settings
235- $violations.Count | Should Be 0
232+ $expected = @'
233+ if ($true) {
234+ $true
235+ } else {
236+ $false
237+ }
238+ '@
239+ Invoke-Formatter - ScriptDefinition $def - Settings $settings | Should Be $expected
236240 }
237241
238- It " Should correct violation by cuddling the else branch statement " {
239- $def = @'
242+ It " Should correct violation if the close brace and following keyword are apart by less than a space " {
243+ $def = @'
240244if ($true) {
241245 $true
246+ }else {
247+ $false
242248}
243- else {
249+ '@
250+ $expected = @'
251+ if ($true) {
252+ $true
253+ } else {
254+ $false
255+ }
256+ '@
257+ Invoke-Formatter - ScriptDefinition $def - Settings $settings | Should Be $expected
258+ }
259+
260+ It " Should correct violation if the close brace and following keyword are apart by more than a space" {
261+ $def = @'
262+ if ($true) {
263+ $true
264+ } else {
244265 $false
245266}
246267'@
@@ -250,6 +271,56 @@ if ($true) {
250271} else {
251272 $false
252273}
274+ '@
275+ Invoke-Formatter - ScriptDefinition $def - Settings $settings | Should Be $expected
276+ }
277+
278+ It " Should correct violations in an if-else-elseif block" {
279+ $def = @'
280+ $x = 1
281+ if ($x -eq 1) {
282+ "1"
283+ }
284+ elseif ($x -eq 2) {
285+ "2"
286+ }
287+ else {
288+ "3"
289+ }
290+ '@
291+ $expected = @'
292+ $x = 1
293+ if ($x -eq 1) {
294+ "1"
295+ } elseif ($x -eq 2) {
296+ "2"
297+ } else {
298+ "3"
299+ }
300+ '@
301+ Invoke-Formatter - ScriptDefinition $def - Settings $settings | Should Be $expected
302+ }
303+
304+ It " Should correct violations in a try-catch-finally block" {
305+ $def = @'
306+ try {
307+ "try"
308+ }
309+ catch {
310+ "catch"
311+ }
312+ finally {
313+ "finally"
314+ }
315+ '@
316+ $expected = @'
317+ try {
318+ "try"
319+ } catch {
320+ "catch"
321+ } finally {
322+ "finally"
323+ }
253324'@
254325 Invoke-Formatter - ScriptDefinition $def - Settings $settings | Should Be $expected
255326 }
0 commit comments