From 19ffa20e56474fb2aec2d85793721adf151816b2 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Tue, 6 Aug 2024 10:27:16 +0200 Subject: [PATCH] Clean up --- .../Private/ConvertTo-ActualParameterName.ps1 | 33 +++++++++---------- .../Private/Get-ShouldCommandOperatorName.ps1 | 6 ++-- source/Private/Test-PesterCommandNegated.ps1 | 9 ++--- .../Get-PesterCommandParameter.Tests.ps1 | 2 +- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/source/Private/ConvertTo-ActualParameterName.ps1 b/source/Private/ConvertTo-ActualParameterName.ps1 index b57ac49..1eb7ccb 100644 --- a/source/Private/ConvertTo-ActualParameterName.ps1 +++ b/source/Private/ConvertTo-ActualParameterName.ps1 @@ -44,7 +44,7 @@ function ConvertTo-ActualParameterName # Should in Pester 5. 'Should' { - $parametersName = @( + $parameterNames = @( # Parameters names. 'ActualValue' 'Alias' @@ -125,32 +125,29 @@ function ConvertTo-ActualParameterName } # Try to match exact name. - $result = $parametersName -match "^$NamedParameter$" + $result = $parameterNames -match "^$NamedParameter$" + + # Try to match abbreviated name. + $result = $result ? $result : $parameterNames -match "^$NamedParameter" if (-not $result) { - # Try to match abbreviated name. - $result = $parametersName -match "^$NamedParameter" - - if ($result.Count -gt 1) - { - $PSCmdlet.ThrowTerminatingError( - [System.Management.Automation.ErrorRecord]::new( - ($script:localizedData.AmbiguousNamedParameter -f $NamedParameter, $CommandName), - 'CTAPN0001', # cSpell: disable-line - [System.Management.Automation.ErrorCategory]::InvalidOperation, - $NamedParameter - ) + $PSCmdlet.ThrowTerminatingError( + [System.Management.Automation.ErrorRecord]::new( + ($script:localizedData.UnknownNamedParameter -f $NamedParameter, $CommandName), + 'CTAPN0002', # cSpell: disable-line + [System.Management.Automation.ErrorCategory]::InvalidOperation, + $NamedParameter ) - } + ) } - if (-not $result) + if (${result}?.Count -gt 1) { $PSCmdlet.ThrowTerminatingError( [System.Management.Automation.ErrorRecord]::new( - ($script:localizedData.UnknownNamedParameter -f $NamedParameter, $CommandName), - 'CTAPN0002', # cSpell: disable-line + ($script:localizedData.AmbiguousNamedParameter -f $NamedParameter, $CommandName), + 'CTAPN0001', # cSpell: disable-line [System.Management.Automation.ErrorCategory]::InvalidOperation, $NamedParameter ) diff --git a/source/Private/Get-ShouldCommandOperatorName.ps1 b/source/Private/Get-ShouldCommandOperatorName.ps1 index 675ba7a..438961f 100644 --- a/source/Private/Get-ShouldCommandOperatorName.ps1 +++ b/source/Private/Get-ShouldCommandOperatorName.ps1 @@ -85,14 +85,14 @@ function Get-ShouldCommandOperatorName $node ) - return $node -is [System.Management.Automation.Language.CommandParameterAst] -and ($node.ParameterName -in $possibleShouldOperator -or $node.ParameterName -in $possibleShouldOperatorAlias.Keys) + return $node -is [System.Management.Automation.Language.CommandParameterAst] -and ($node.ParameterName -in $possibleShouldOperator -or ($possibleShouldOperatorAlias -and $node.ParameterName -in $possibleShouldOperatorAlias.Keys)) }, $true) - if ($shouldOperatorAsts.ParameterName -in $possibleShouldOperatorAlias.Keys) + if (${shouldOperatorAsts}?.ParameterName -in ${possibleShouldOperatorAlias}?.Keys) { return $possibleShouldOperatorAlias[$shouldOperatorAsts.ParameterName] } - return $shouldOperatorAsts.ParameterName + return ${shouldOperatorAsts}?.ParameterName } } diff --git a/source/Private/Test-PesterCommandNegated.ps1 b/source/Private/Test-PesterCommandNegated.ps1 index c27893a..0b73fc0 100644 --- a/source/Private/Test-PesterCommandNegated.ps1 +++ b/source/Private/Test-PesterCommandNegated.ps1 @@ -46,19 +46,14 @@ function Test-PesterCommandNegated $CommandAst = $CommandAst.Parent.PipelineElements[-1] } - $negateCommandParameterAst = $CommandAst.CommandElements | + $negateCommandParameterAst = ${CommandAst}?.CommandElements | Where-Object -FilterScript { $_ -is [System.Management.Automation.Language.CommandParameterAst] ` -and $_.ParameterName -eq 'Not' ` -and $_.Argument.Extent.Text -ne '$false' } - $negated = $false - - if ($negateCommandParameterAst) - { - $negated = $true - } + $negated = $negateCommandParameterAst ? $true : $false return $negated } diff --git a/tests/Unit/Private/Get-PesterCommandParameter.Tests.ps1 b/tests/Unit/Private/Get-PesterCommandParameter.Tests.ps1 index 6e2348b..0de93e5 100644 --- a/tests/Unit/Private/Get-PesterCommandParameter.Tests.ps1 +++ b/tests/Unit/Private/Get-PesterCommandParameter.Tests.ps1 @@ -208,7 +208,7 @@ Describe 'Get-PesterCommandParameter' { $result | Should-BeEquivalent @{ ActualValue = @{ ExtentText = '$false' - Position = 3 + Position = 2 Positional = $true } Because = @{