From 7304ff943ecfaa0840330b59d56886998b73c4e6 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Wed, 10 Jul 2024 20:26:40 +0200 Subject: [PATCH] Support v5 positional syntax in Should-Throw (#2536) --- src/functions/assert/Exception/Should-Throw.ps1 | 10 +++++++--- tst/functions/assert/Exception/Should-Throw.Tests.ps1 | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/functions/assert/Exception/Should-Throw.ps1 b/src/functions/assert/Exception/Should-Throw.ps1 index 6b0fdaf40..65be5112f 100644 --- a/src/functions/assert/Exception/Should-Throw.ps1 +++ b/src/functions/assert/Exception/Should-Throw.ps1 @@ -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 diff --git a/tst/functions/assert/Exception/Should-Throw.Tests.ps1 b/tst/functions/assert/Exception/Should-Throw.Tests.ps1 index 6c8e75856..aa0685363 100644 --- a/tst/functions/assert/Exception/Should-Throw.Tests.ps1 +++ b/tst/functions/assert/Exception/Should-Throw.Tests.ps1 @@ -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])