From 8d715da002dc0723c4d06a49e374b2ac42b025b3 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 2 Aug 2024 15:58:49 +0200 Subject: [PATCH] Refactor Should -Throw and Should -Not -Throw integration tests --- .../Syntax/v5/ShouldNotThrow.v5.tests.ps1 | 41 +++++++++++++ .../Syntax/v5/ShouldThrow.v5.tests.ps1 | 60 +++++++++++++++++++ .../Private/Convert-ShouldNotThrow.tests.ps1 | 2 +- .../Private/Convert-ShouldThrow.tests.ps1 | 4 +- 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/Integration/Syntax/v5/ShouldNotThrow.v5.tests.ps1 create mode 100644 tests/Integration/Syntax/v5/ShouldThrow.v5.tests.ps1 diff --git a/tests/Integration/Syntax/v5/ShouldNotThrow.v5.tests.ps1 b/tests/Integration/Syntax/v5/ShouldNotThrow.v5.tests.ps1 new file mode 100644 index 0000000..c547d33 --- /dev/null +++ b/tests/Integration/Syntax/v5/ShouldNotThrow.v5.tests.ps1 @@ -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 + } + } +} diff --git a/tests/Integration/Syntax/v5/ShouldThrow.v5.tests.ps1 b/tests/Integration/Syntax/v5/ShouldThrow.v5.tests.ps1 new file mode 100644 index 0000000..3696def --- /dev/null +++ b/tests/Integration/Syntax/v5/ShouldThrow.v5.tests.ps1 @@ -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' + } + } +} diff --git a/tests/Unit/Private/Convert-ShouldNotThrow.tests.ps1 b/tests/Unit/Private/Convert-ShouldNotThrow.tests.ps1 index 7565a54..0c3a854 100644 --- a/tests/Unit/Private/Convert-ShouldNotThrow.tests.ps1 +++ b/tests/Unit/Private/Convert-ShouldNotThrow.tests.ps1 @@ -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 = { { diff --git a/tests/Unit/Private/Convert-ShouldThrow.tests.ps1 b/tests/Unit/Private/Convert-ShouldThrow.tests.ps1 index b1b2be5..6cf4803 100644 --- a/tests/Unit/Private/Convert-ShouldThrow.tests.ps1 +++ b/tests/Unit/Private/Convert-ShouldThrow.tests.ps1 @@ -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 @@ -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 {