Skip to content

Commit

Permalink
Convert-ShouldThrow: Fix ExpectedMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Jul 11, 2024
1 parent 3891393 commit 43aa0cf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Public commands:
- `Convert-PesterSyntax`
- GitHub templates.

### Fixed

- `Convert-ShouldThrow`
- Correctly convert parameter `ExpectedMessage`.
19 changes: 14 additions & 5 deletions source/Private/Convert-ShouldThrow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Should -Throw [[-ActualValue] <Object>] [[-ExpectedMessage] <string>] [[-ErrorId] <string>] [[-ExceptionType] <type>] [[-Because] <string>] [-Not] [-PassThru]
Positional parameters:
Position 1: ExceptionMessage
Position 1: ExpectedMessage
Position 2: ErrorId
Position 3: ExceptionType
Position 4: Because
Expand Down Expand Up @@ -124,7 +124,7 @@ function Convert-ShouldThrow
'PassThru'
)
PositionalParameter = @(
'ExceptionMessage'
'ExpectedMessage'
'ErrorId'
'ExceptionType'
'Because'
Expand All @@ -150,9 +150,9 @@ function Convert-ShouldThrow
If a previous positional parameter is missing then the ones behind
it cannot be set to positional.
#>
if ($commandParameters.ExceptionMessage)
if ($commandParameters.ExpectedMessage)
{
$commandParameters.ExceptionMessage.Positional = $true
$commandParameters.ExpectedMessage.Positional = $true

if ($commandParameters.ErrorId)
{
Expand All @@ -171,7 +171,7 @@ function Convert-ShouldThrow
}
}

$newExtentText += $commandParameters.ExceptionMessage.Positional ? (' {0}' -f $commandParameters.ExceptionMessage.ExtentText) : ''
$newExtentText += $commandParameters.ExpectedMessage.Positional ? (' {0}' -f $commandParameters.ExpectedMessage.ExtentText) : ''
$newExtentText += $commandParameters.ErrorId.Positional ? (' {0}' -f $commandParameters.ErrorId.ExtentText) : ''
$newExtentText += $commandParameters.ExceptionType.Positional ? (' {0}' -f $commandParameters.ExceptionType.ExtentText) : ''
$newExtentText += $commandParameters.Because.Positional ? (' {0}' -f $commandParameters.Because.ExtentText) : ''
Expand All @@ -188,6 +188,15 @@ function Convert-ShouldThrow

switch ($currentParameter)
{
'ExpectedMessage'
{
$parameterNames += @{
ExceptionMessage = 'ExpectedMessage'
}

break
}

'ErrorId'
{
$parameterNames += @{
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/Private/Convert-ShouldThrow.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ Describe 'Convert-ShouldThrow' {
}
}

It 'Should convert `Should -Throw -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -Because ''BecauseString'' -ExceptionMessage ''MockErrorMessage''` correctly' {
It 'Should convert `Should -Throw -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -Because ''BecauseString'' -ExpectedMessage ''MockErrorMessage''` correctly' {
InModuleScope -ScriptBlock {
$mockCommandAstPester5 = {
Should -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExceptionMessage 'MockErrorMessage'
Should -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExpectedMessage 'MockErrorMessage'
}.Ast.Find({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $false)

$result = Convert-ShouldThrow -CommandAst $mockCommandAstPester5
Expand All @@ -129,10 +129,10 @@ Describe 'Convert-ShouldThrow' {
}
}

It 'Should convert `Should -Throw -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -Because ''BecauseString'' -ExceptionMessage ''MockErrorMessage'' -ActualValue { Write-Error -Message ''MockErrorMessage'' -ErrorId ''MockErrorId'' -Category ''InvalidOperation'' -TargetObject ''MockTargetObject'' -ErrorAction ''Stop'' }` correctly' {
It 'Should convert `Should -Throw -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -Because ''BecauseString'' -ExpectedMessage ''MockErrorMessage'' -ActualValue { Write-Error -Message ''MockErrorMessage'' -ErrorId ''MockErrorId'' -Category ''InvalidOperation'' -TargetObject ''MockTargetObject'' -ErrorAction ''Stop'' }` correctly' {
InModuleScope -ScriptBlock {
$mockCommandAstPester5 = {
Should -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExceptionMessage 'MockErrorMessage' -ActualValue {
Should -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExpectedMessage 'MockErrorMessage' -ActualValue {
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
}
}.Ast.Find({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $false)
Expand Down Expand Up @@ -175,10 +175,10 @@ Describe 'Convert-ShouldThrow' {

Context 'When tests should always use positional parameters' {
Context 'When the tests are affirming' {
It 'Should convert `Should -Throw -Because ''BecauseString'' -ActualValue ''ActualString'' -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -ExceptionMessage ''MockErrorMessage''` correctly' {
It 'Should convert `Should -Throw -Because ''BecauseString'' -ActualValue ''ActualString'' -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -ExpectedMessage ''MockErrorMessage''` correctly' {
InModuleScope -ScriptBlock {
$mockCommandAstPester5 = {
Should -Throw -Because 'BecauseString' -ActualValue 'ActualString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExceptionMessage 'MockErrorMessage'
Should -Throw -Because 'BecauseString' -ActualValue 'ActualString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExpectedMessage 'MockErrorMessage'
}.Ast.Find({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $false)

$result = Convert-ShouldThrow -CommandAst $mockCommandAstPester5 -UsePositionalParameters
Expand Down

0 comments on commit 43aa0cf

Please sign in to comment.