Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Aug 6, 2024
1 parent 8dfab90 commit 19ffa20
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
33 changes: 15 additions & 18 deletions source/Private/ConvertTo-ActualParameterName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function ConvertTo-ActualParameterName
# Should in Pester 5.
'Should'
{
$parametersName = @(
$parameterNames = @(
# Parameters names.
'ActualValue'
'Alias'
Expand Down Expand Up @@ -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
)
Expand Down
6 changes: 3 additions & 3 deletions source/Private/Get-ShouldCommandOperatorName.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
9 changes: 2 additions & 7 deletions source/Private/Test-PesterCommandNegated.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/Unit/Private/Get-PesterCommandParameter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Describe 'Get-PesterCommandParameter' {
$result | Should-BeEquivalent @{
ActualValue = @{
ExtentText = '$false'
Position = 3
Position = 2
Positional = $true
}
Because = @{
Expand Down

0 comments on commit 19ffa20

Please sign in to comment.