Skip to content

Commit

Permalink
Support v5 positional syntax in Should-Throw (#2536)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten authored Jul 10, 2024
1 parent 5f5251d commit 7304ff9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/functions/assert/Exception/Should-Throw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ function Should-Throw {
param (
[Parameter(ValueFromPipeline = $true, Mandatory = $true)]
[ScriptBlock]$ScriptBlock,
[Type]$ExceptionType,
[Parameter(Position = 0)]
[String]$ExceptionMessage,
[Parameter(Position = 1)]
[String]$FullyQualifiedErrorId,
[Switch]$AllowNonTerminatingError,
[String]$Because
[Parameter(Position = 2)]
[Type]$ExceptionType,
[Parameter(Position = 3)]
[String]$Because,
[Switch]$AllowNonTerminatingError
)

$collectedInput = Collect-Input -ParameterInput $ScriptBlock -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput
Expand Down
5 changes: 5 additions & 0 deletions tst/functions/assert/Exception/Should-Throw.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Describe "Should-Throw" {
{ { Write-Error "fail!" } | Should-Throw -AllowNonTerminatingError } | Verify-AssertionFailed
}

It 'Supports same positional parameters as Should -Throw' {
{ Write-Error -Message 'MockErrorMessage' -ErrorId 'MockErrorId' -Category 'InvalidOperation' -TargetObject 'MockTargetObject' -ErrorAction 'Stop' } |
Should-Throw 'MockErrorMessage' 'MockErrorId' ([Microsoft.PowerShell.Commands.WriteErrorException]) 'MockBecauseString'
}

Context "Filtering with exception type" {
It "Passes when exception has the expected type" {
{ throw [ArgumentException]"A is null!" } | Should-Throw -ExceptionType ([ArgumentException])
Expand Down

0 comments on commit 7304ff9

Please sign in to comment.