Skip to content

Commit

Permalink
Fix Should-Throw to handle expected message with escaped wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Aug 23, 2024
1 parent 0d3dd66 commit 7a0271f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/functions/assert/Exception/Should-Throw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function Should-Throw {
```powershell
{ throw 'error' } | Should-Throw
{ throw 'error' } | Should-Throw -ExceptionMessage 'error'
{ throw 'wildcard character []' } | Should-Throw -ExceptionMessage '*character `[`]'
{ throw 'error' } | Should-Throw -ExceptionType 'System.Management.Automation.RuntimeException'
{ throw 'error' } | Should-Throw -FullyQualifiedErrorId 'RuntimeException'
{ throw 'error' } | Should-Throw -FullyQualifiedErrorId '*Exception'
Expand Down Expand Up @@ -100,7 +101,7 @@ function Should-Throw {

$filterOnMessage = -not ([string]::IsNullOrWhiteSpace($ExceptionMessage))
if ($filterOnMessage) {
$filters += "with message '$ExceptionMessage'"
$filters += "with message like '$([System.Management.Automation.WildcardPattern]::Unescape($ExceptionMessage))'"
if ($err.ExceptionMessage -notlike $ExceptionMessage) {
$buts += "the message was '$($err.ExceptionMessage)'"
}
Expand Down
9 changes: 9 additions & 0 deletions tst/functions/assert/Exception/Should-Throw.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Describe "Should-Throw" {
It "Fails when exception does not match the message with wildcard" {
{ { throw [ArgumentException]"A is null!" } | Should-Throw -ExceptionMessage '*flabbergasted*' } | Verify-AssertionFailed
}

It "Passes when exception match the message with escaped wildcard" {
{ throw [ArgumentException]"[]" } | Should-Throw -ExceptionMessage '`[`]'
}
}

Context "Filtering with FullyQualifiedErrorId" {
Expand Down Expand Up @@ -113,6 +117,11 @@ Describe "Should-Throw" {
$err = { { throw [ArgumentException]"halt!" } | Should-Throw -ExceptionType ([System.InvalidOperationException]) -ExceptionMessage 'fail!' -FullyQualifiedErrorId 'fail!' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal "Expected an exception, of type [InvalidOperationException], with message 'fail!' and with FullyQualifiedErrorId 'fail!' to be thrown, but the exception type was [ArgumentException], the message was 'halt!' and the FullyQualifiedErrorId was 'halt!'."
}

It "Given exception that does not match on a message with escaped wildcard it returns the correct message" {
$err = { { throw [ArgumentException]"[!]" } | Should-Throw -ExceptionMessage '`[`]' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal "Expected an exception, with message like '[]' to be thrown, but the message was '[!]'."
}
}

Context "Unwrapping exception from different sources" {
Expand Down

0 comments on commit 7a0271f

Please sign in to comment.