@@ -200,7 +200,7 @@ Some-Command -Param1 @{
200
200
}
201
201
}
202
202
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" {
204
204
BeforeAll {
205
205
$ruleConfiguration .' NoEmptyLineBefore' = $false
206
206
$ruleConfiguration .' IgnoreOneLineBlock' = $false
@@ -220,27 +220,48 @@ else {
220
220
$violations.Count | Should Be 1
221
221
}
222
222
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 " {
224
224
$def = @'
225
- if ($true)
226
- {
225
+ if ($true) {
227
226
$true
228
227
}
229
- else
230
- {
228
+ else {
231
229
$false
232
230
}
233
231
'@
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
236
240
}
237
241
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 = @'
240
244
if ($true) {
241
245
$true
246
+ }else {
247
+ $false
242
248
}
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 {
244
265
$false
245
266
}
246
267
'@
@@ -250,6 +271,56 @@ if ($true) {
250
271
} else {
251
272
$false
252
273
}
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
+ }
253
324
'@
254
325
Invoke-Formatter - ScriptDefinition $def - Settings $settings | Should Be $expected
255
326
}
0 commit comments