-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add support for Should operators: - BeLike - BeLikeExactly
- Loading branch information
Showing
7 changed files
with
1,711 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<# | ||
.SYNOPSIS | ||
Converts a command `Should -BeLike` to the specified Pester syntax. | ||
.DESCRIPTION | ||
The Convert-ShouldBeLike function is used to convert a command `Should -BeLike` to | ||
the specified Pester syntax. | ||
.PARAMETER CommandAst | ||
The CommandAst object representing the command to be converted. | ||
.PARAMETER Pester6 | ||
Specifies that the command should be converted to Pester version 6 syntax. | ||
.PARAMETER UseNamedParameters | ||
Specifies whether to use named parameters in the converted syntax. | ||
.PARAMETER UsePositionalParameters | ||
Specifies whether to use positional parameters in the converted syntax, | ||
where supported. | ||
.EXAMPLE | ||
$commandAst = [System.Management.Automation.Language.Parser]::ParseInput('Should -BeLike "Test*"') | ||
Convert-ShouldBeLike -CommandAst $commandAst -Pester6 | ||
This example converts the `Should -BeLike "Test*"` command to Pester 6 syntax. | ||
.NOTES | ||
Pester 5 Syntax: | ||
Should -BeLike [[-ActualValue] <Object>] [[-ExpectedValue] <Object>] [[-Because] <string>] [-Not] | ||
Positional parameters: | ||
Position 1: ExpectedValue | ||
Position 2: Because | ||
Position 3: ActualValue | ||
Pester 6 Syntax: | ||
Should-BeLikeString [[-Actual] <Object>] [-Expected] <String> [-CaseSensitive] [-Because <String>] | ||
Should-NotBeLikeString [[-Actual] <Object>] [-Expected] <String> [-CaseSensitive] [-Because <String>] | ||
Positional parameters: | ||
Position 1: Expected | ||
Position 2: Actual | ||
#> | ||
function Convert-ShouldBeLike | ||
{ | ||
[CmdletBinding()] | ||
[OutputType([System.String])] | ||
param | ||
( | ||
[Parameter(Mandatory = $true)] | ||
[System.Management.Automation.Language.CommandAst] | ||
$CommandAst, | ||
|
||
[Parameter(Mandatory = $true, ParameterSetName = 'Pester6')] | ||
[System.Management.Automation.SwitchParameter] | ||
$Pester6, | ||
|
||
[Parameter()] | ||
[System.Management.Automation.SwitchParameter] | ||
$UseNamedParameters, | ||
|
||
[Parameter()] | ||
[System.Management.Automation.SwitchParameter] | ||
$UsePositionalParameters | ||
) | ||
|
||
$assertBoundParameterParameters = @{ | ||
BoundParameterList = $PSBoundParameters | ||
MutuallyExclusiveList1 = @('UseNamedParameters') | ||
MutuallyExclusiveList2 = @('UsePositionalParameters') | ||
} | ||
|
||
Assert-BoundParameter @assertBoundParameterParameters | ||
|
||
Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ParsingCommandAst -f $CommandAst.Extent.Text) | ||
|
||
# Determine if the command is negated | ||
$isNegated = Test-PesterCommandNegated -CommandAst $CommandAst | ||
|
||
$sourceSyntaxVersion = Get-PesterCommandSyntaxVersion -CommandAst $CommandAst | ||
|
||
# Parse the command elements and convert them to Pester 6 syntax | ||
if ($PSCmdlet.ParameterSetName -eq 'Pester6') | ||
{ | ||
Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertingFromTo -f $sourceSyntaxVersion, '6') | ||
|
||
# Add the correct Pester command based on negation | ||
if ($isNegated) | ||
{ | ||
$newExtentText = 'Should-NotBeLikeString' | ||
} | ||
else | ||
{ | ||
$newExtentText = 'Should-BeLikeString' | ||
} | ||
|
||
$getPesterCommandParameterParameters = @{ | ||
CommandAst = $CommandAst | ||
CommandName = 'Should' | ||
IgnoreParameter = @( | ||
'BeLike' | ||
'Not' | ||
) | ||
PositionalParameter = @( | ||
'ExpectedValue' | ||
'Because' | ||
'ActualValue' | ||
) | ||
} | ||
|
||
$commandParameters = Get-PesterCommandParameter @getPesterCommandParameterParameters | ||
|
||
# Parameter 'Because' is only supported as named parameter in Pester 6 syntax. | ||
if ($commandParameters.Because) | ||
{ | ||
$commandParameters.Because.Positional = $false | ||
} | ||
|
||
# Determine if named or positional parameters should be forcibly used | ||
if ($UseNamedParameters.IsPresent) | ||
{ | ||
$commandParameters.Keys.ForEach({ $commandParameters.$_.Positional = $false }) | ||
} | ||
elseif ($UsePositionalParameters.IsPresent) | ||
{ | ||
# First set all to named parameters | ||
$commandParameters.Keys.ForEach({ $commandParameters.$_.Positional = $false }) | ||
|
||
<# | ||
If a previous positional parameter is missing then the ones behind | ||
it cannot be set to positional. | ||
#> | ||
if ($commandParameters.ExpectedValue) | ||
{ | ||
$commandParameters.ExpectedValue.Positional = $true | ||
|
||
if ($commandParameters.ActualValue) | ||
{ | ||
$commandParameters.ActualValue.Positional = $true | ||
} | ||
} | ||
} | ||
|
||
$newExtentText += $commandParameters.ExpectedValue.Positional ? (' {0}' -f $commandParameters.ExpectedValue.ExtentText) : '' | ||
$newExtentText += $commandParameters.ActualValue.Positional ? (' {0}' -f $commandParameters.ActualValue.ExtentText) : '' | ||
|
||
# Holds the new parameter names so they can be added in alphabetical order. | ||
$parameterNames = @() | ||
|
||
foreach ($currentParameter in $commandParameters.Keys) | ||
{ | ||
if ($commandParameters.$currentParameter.Positional -eq $true) | ||
{ | ||
continue | ||
} | ||
|
||
switch ($currentParameter) | ||
{ | ||
'ActualValue' | ||
{ | ||
$parameterNames += @{ | ||
Actual = 'ActualValue' | ||
} | ||
|
||
break | ||
} | ||
|
||
'ExpectedValue' | ||
{ | ||
$parameterNames += @{ | ||
Expected = 'ExpectedValue' | ||
} | ||
|
||
break | ||
} | ||
|
||
default | ||
{ | ||
$parameterNames += @{ | ||
$currentParameter = $currentParameter | ||
} | ||
|
||
break | ||
} | ||
} | ||
} | ||
|
||
# This handles the named parameters in the command elements, added in alphabetical order. | ||
foreach ($currentParameter in $parameterNames.Keys | Sort-Object) | ||
{ | ||
$originalParameterName = $parameterNames.$currentParameter | ||
|
||
$newExtentText += ' -{0} {1}' -f $currentParameter, $commandParameters.$originalParameterName.ExtentText | ||
} | ||
} | ||
|
||
Write-Debug -Message ($script:localizedData.Convert_Should_Debug_ConvertedCommand -f $CommandAst.Extent.Text, $newExtentText) | ||
|
||
return $newExtentText | ||
} |
Oops, something went wrong.