Skip to content

Commit

Permalink
Refactor Should -Throw and Should -Not -Throw integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Aug 2, 2024
1 parent 6ed457c commit 8d715da
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
41 changes: 41 additions & 0 deletions tests/Integration/Syntax/v5/ShouldNotThrow.v5.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Describe 'Should -Not -Throw' {
Context 'When the tests are negated' {
It 'Should convert `Should -Not -Throw -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -Because ''BecauseString'' -ExpectedMessage ''MockErrorMessage'' -ActualValue { $null = 1 + 1 }` correctly' {
Should -Not -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExpectedMessage 'MockErrorMessage' -ActualValue {
$null = 1 + 1
}
}

It 'Should convert `{ $null = 1 + 1 } | Should -Not -Throw -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -Because ''BecauseString'' -ExpectedMessage ''MockErrorMessage''` correctly' {
{
$null = 1 + 1
} | Should -Not -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExpectedMessage 'MockErrorMessage'
}

It 'Should convert `$scriptBlock | Should -Not -Throw` correctly' {
$scriptBlock = {
$null = 1 + 1
}

$scriptBlock | Should -Not -Throw
}

It 'Should convert `{ $null = 1 + 1 } | Should -Not -Throw -Because ''BecauseString''` correctly' {
# Intentionally having everything on one line to test the conversion.
{ $null = 1 + 1 } | Should -Not -Throw -Because 'BecauseString'
}

It 'Should convert `{ $null = 1 + 1 } | Should -Not -Throw -Because ''BecauseString'' -ExpectedMessage ''ExpectedString''` correctly' {
# Intentionally splitting over two lines to test the conversion.
{ $null = 1 + 1 } |
Should -Not -Throw -Because 'BecauseString' -ExpectedMessage 'ExpectedString'
}

It 'Should convert `''$null = 1 + 1'' | ForEach-Object { [scriptblock]::Create($_) } | Should -Throw -Not` correctly' {
# Intentionally splitting over three lines to test the conversion.
'$null = 1 + 1' |
ForEach-Object { [scriptblock]::Create($_) } |
Should -Throw -Not
}
}
}
60 changes: 60 additions & 0 deletions tests/Integration/Syntax/v5/ShouldThrow.v5.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Describe 'Should -Throw' {
Context 'When the tests are affirming' {
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' {
Should -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExpectedMessage 'MockErrorMessage' -ActualValue {
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
}
}

It 'Should convert `Should -Throw -Because ''BecauseString'' -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -ExpectedMessage ''MockErrorMessage''` correctly' {
{
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
} | Should -Throw -Because 'BecauseString' -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -ExpectedMessage 'MockErrorMessage'
}

It 'Should convert `$scriptBlock | Should -Throw` correctly' {
$scriptBlock = { throw 'mock error' }

$scriptBlock | Should -Throw
}

It 'Should convert `{ throw ''myMessage'' } | Should -Throw -Because ''BecauseString''` correctly' {
{ throw 'myMessage' } | Should -Throw -Because 'BecauseString'
}

It 'Should convert `{ throw ''myMessage'' } | Should -Throw -Because ''BecauseString'' -ExpectedMessage ''ExpectedString''` correctly' {
{ throw 'myMessage' } |
Should -Throw -Because 'BecauseString' -ExpectedMessage 'myMessage'
}

It 'Should convert `"throw ''five''" | ForEach-Object { [scriptblock]::Create($_) } | Should -Throw -Not` correctly' {
"throw 'five'" |
ForEach-Object { [scriptblock]::Create($_) } |
Should -Throw
}

It 'Should convert `Should -Throw ''MockErrorMessage'' ''MockErrorId'' ([System.Exception]) ''BecauseString''` correctly' {
{
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
} | Should -Throw 'MockErrorMessage' 'MockErrorId' ([System.Exception]) 'BecauseString'
}

It 'Should convert `Should -Throw ''MockErrorMessage'' ''MockErrorId'' ([System.Exception])` correctly' {
{
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
} | Should -Throw 'MockErrorMessage' 'MockErrorId' ([System.Exception])
}

It 'Should convert `Should -Throw ''MockErrorMessage'' ''MockErrorId''` correctly' {
{
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
} | Should -Throw 'MockErrorMessage' 'MockErrorId'
}

It 'Should convert `Should -Throw ''MockErrorMessage''` correctly' {
{
Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop'
} | Should -Throw 'MockErrorMessage'
}
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Private/Convert-ShouldNotThrow.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Describe 'Convert-ShouldNotThrow' {
}
}

It 'Should convert `{ Write-Error -Message ''MockErrorMessage'' -ErrorId ''MockErrorId'' -Category ''InvalidOperation'' -TargetObject ''MockTargetObject'' -ErrorAction ''Stop'' } | Should -Not -Throw -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -Because ''BecauseString'' -ExceptionMessage ''MockErrorMessage''` correctly' {
It 'Should convert `{ Write-Error -Message ''MockErrorMessage'' -ErrorId ''MockErrorId'' -Category ''InvalidOperation'' -TargetObject ''MockTargetObject'' -ErrorAction ''Stop'' } | Should -Throw -Because ''BecauseString'' -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -ExpectedMessage ''MockErrorMessage''` correctly' {
InModuleScope -ScriptBlock {
$mockCommandAstPester5 = {
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Private/Convert-ShouldThrow.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Describe 'Convert-ShouldThrow' {
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' -ExpectedMessage 'MockErrorMessage'
Should -Throw -ExceptionType ([System.Exception]) -ErrorId 'MockErrorId' -Because 'BecauseString' -ExpectedMessage 'MockErrorMessage'
}.Ast.Find({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $false)

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

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' {
It 'Should convert `Should -Throw -Because ''BecauseString'' -ExceptionType ([System.Exception]) -ErrorId ''MockErrorId'' -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' -ExpectedMessage 'MockErrorMessage' -ActualValue {
Expand Down

0 comments on commit 8d715da

Please sign in to comment.