diff --git a/Engine/Settings/CodeFormattingOTPS.psd1 b/Engine/Settings/CodeFormattingOTPS.psd1 new file mode 100644 index 000000000..83514c7f9 --- /dev/null +++ b/Engine/Settings/CodeFormattingOTPS.psd1 @@ -0,0 +1,55 @@ +@{ + IncludeRules = @( + 'PSPlaceOpenBrace', + 'PSPlaceCloseBrace', + 'PSUseConsistentWhitespace', + 'PSUseConsistentIndentation', + 'PSAlignAssignmentStatement', + 'PSUseCorrectCasing' + ) + + Rules = @{ + PSPlaceOpenBrace = @{ + Enable = $true + OnSameLine = $true + NewLineAfter = $true + IgnoreOneLineBlock = $true + } + + PSPlaceCloseBrace = @{ + Enable = $true + NewLineAfter = $true + IgnoreOneLineBlock = $true + NoEmptyLineBefore = $false + } + + PSUseConsistentIndentation = @{ + Enable = $true + Kind = 'space' + PipelineIndentation = 'IncreaseIndentationForFirstPipeline' + IndentationSize = 4 + } + + PSUseConsistentWhitespace = @{ + Enable = $true + CheckInnerBrace = $true + CheckOpenBrace = $true + CheckOpenParen = $true + CheckOperator = $true + CheckPipe = $true + CheckPipeForRedundantWhitespace = $false + CheckSeparator = $true + CheckParameter = $false + IgnoreAssignmentOperatorInsideHashTable = $true + } + + PSAlignAssignmentStatement = @{ + Enable = $true + CheckHashtable = $true + } + + PSUseCorrectCasing = @{ + Enable = $true + } + } +} diff --git a/Tests/Rules/CodeFormattingPresets.tests.ps1 b/Tests/Rules/CodeFormattingPresets.tests.ps1 new file mode 100644 index 000000000..949b7c191 --- /dev/null +++ b/Tests/Rules/CodeFormattingPresets.tests.ps1 @@ -0,0 +1,149 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +BeforeAll { + $testRootDirectory = Split-Path -Parent $PSScriptRoot + Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1") + + $Allman = @' +enum Color +{ + Black, + White +} + +function Test-Code +{ + [CmdletBinding()] + param + ( + [int] $ParameterOne + ) + end + { + if (10 -gt $ParameterOne) + { + "Greater" + } + else + { + "Lesser" + } + } +} +'@ + + $OTBS = @' +enum Color { + Black, + White +} + +function Test-Code { + [CmdletBinding()] + param( + [int] $ParameterOne + ) + end { + if (10 -gt $ParameterOne) { + "Greater" + } else { + "Lesser" + } + } +} +'@ + $OTPS = @' +enum Color { + Black, + White +} + +function Test-Code { + [CmdletBinding()] + param( + [int] $ParameterOne + ) + end { + if (10 -gt $ParameterOne) { + "Greater" + } + else { + "Lesser" + } + } +} +'@ + $Stroustrup = @' +enum Color { + Black, + White +} + +function Test-Code +{ + [CmdletBinding()] + param( + [int] $ParameterOne + ) + end { + if (10 -gt $ParameterOne) { + "Greater" + } + else { + "Lesser" + } + } +} +'@ +} + +Describe "CodeFormattingPresets" { + Context "Allman" { + It "To Allman from OTBS" { + Invoke-Formatter -ScriptDefinition $OTBS -Settings 'CodeFormattingAllman' | Should -Be $Allman + } + It "To Allman from OTPS" { + Invoke-Formatter -ScriptDefinition $OTPS -Settings 'CodeFormattingAllman' | Should -Be $Allman + } + It "To Allman from Stroustrup" { + Invoke-Formatter -ScriptDefinition $Stroustrup -Settings 'CodeFormattingAllman' | Should -Be $Allman + } + } + + Context "OTBS" { + It "To OTBS from Allman" { + Invoke-Formatter -ScriptDefinition $Allman -Settings 'CodeFormattingOTBS' | Should -Be $OTBS + } + It "To OTBS from OTPS" { + Invoke-Formatter -ScriptDefinition $OTPS -Settings 'CodeFormattingOTBS' | Should -Be $OTBS + } + It "To OTBS from Stroustrup" { + Invoke-Formatter -ScriptDefinition $Stroustrup -Settings 'CodeFormattingOTBS' | Should -Be $OTBS + } + } + + Context "OTPS" { + It "To OTPS from Allman" { + Invoke-Formatter -ScriptDefinition $Allman -Settings 'CodeFormattingOTPS' | Should -Be $OTPS + } + It "To OTPS from OTBS" { + Invoke-Formatter -ScriptDefinition $OTBS -Settings 'CodeFormattingOTPS' | Should -Be $OTPS + } + It "To OTPS from Stroustrup" { + Invoke-Formatter -ScriptDefinition $Stroustrup -Settings 'CodeFormattingOTPS' | Should -Be $OTPS + } + } + + Context "Stroustrup" { + It "To Stroustrup from Allman" { + Invoke-Formatter -ScriptDefinition $Allman -Settings 'CodeFormattingStroustrup' | Should -Be $Stroustrup + } + It "To Stroustrup from OTBS" { + Invoke-Formatter -ScriptDefinition $OTBS -Settings 'CodeFormattingStroustrup' | Should -Be $Stroustrup + } + It "To Stroustrup from OTPS" { + Invoke-Formatter -ScriptDefinition $OTPS -Settings 'CodeFormattingStroustrup' | Should -Be $Stroustrup + } + } +} \ No newline at end of file diff --git a/Tests/Rules/PlaceCloseBrace.tests.ps1 b/Tests/Rules/PlaceCloseBrace.tests.ps1 index 85ccb8979..0b2a8321d 100644 --- a/Tests/Rules/PlaceCloseBrace.tests.ps1 +++ b/Tests/Rules/PlaceCloseBrace.tests.ps1 @@ -383,4 +383,54 @@ if ($true) { $violations.Count | Should -Be 0 } } + + Context "When formatting presets handles if else" { + BeforeAll { + $AllmanDefinition = @" +if (`$true) +{ + 'yes' +} +else +{ + 'no' +} +"@ + $OTBSDefinition = @" +if (`$true) { + 'yes' +} else { + 'no' +} +"@ + $OTPSAndStroustrupDefinition = @" +if (`$true) { + 'yes' +} +else { + 'no' +} +"@ + } + + It "Allman should have all opening and closing braces on a new line" { + Invoke-Formatter -ScriptDefinition $OTBSDefinition -Settings 'CodeFormattingAllman' | Should -Be $AllmanDefinition + Invoke-Formatter -ScriptDefinition $OTPSAndStroustrupDefinition -Settings 'CodeFormattingAllman' | Should -Be $AllmanDefinition + } + + It "OTBS should place else on same line as the if closing bracket" { + Invoke-Formatter -ScriptDefinition $AllmanDefinition -Settings 'CodeFormattingOTBS' | Should -Be $OTBSDefinition + Invoke-Formatter -ScriptDefinition $OTPSAndStroustrupDefinition -Settings 'CodeFormattingOTBS' | Should -Be $OTBSDefinition + } + + It "OTPS should place else on a new line after the if closing bracket" { + Invoke-Formatter -ScriptDefinition $AllmanDefinition -Settings 'CodeFormattingOTPS' | Should -Be $OTPSAndStroustrupDefinition + Invoke-Formatter -ScriptDefinition $OTBSDefinition -Settings 'CodeFormattingOTPS' | Should -Be $OTPSAndStroustrupDefinition + } + + It "Stroustrup should place else on a new line after the if closing bracket" { + Invoke-Formatter -ScriptDefinition $AllmanDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $OTPSAndStroustrupDefinition + Invoke-Formatter -ScriptDefinition $OTBSDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $OTPSAndStroustrupDefinition + } + } } diff --git a/Tests/Rules/PlaceOpenBrace.tests.ps1 b/Tests/Rules/PlaceOpenBrace.tests.ps1 index 4c8206102..424e511b2 100644 --- a/Tests/Rules/PlaceOpenBrace.tests.ps1 +++ b/Tests/Rules/PlaceOpenBrace.tests.ps1 @@ -56,6 +56,7 @@ foreach ($x in $y) { Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTPS' | Should -Be $expected } It "Should correct violation when brace should be on the same line and take comment into account" { @@ -73,6 +74,7 @@ foreach ($x in $y) { # useful comment Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTPS' | Should -Be $expected } It "Should correct violation when the brace should be on the next line and take comment into account" { @@ -90,6 +92,7 @@ foreach ($x in $y) { # useful comment Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingStroustrup' | Should -Be $expected Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTBS' | Should -Be $expected + Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings 'CodeFormattingOTPS' | Should -Be $expected } }