Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Functions/Assertions/PesterThrow.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ $here = Split-Path -Parent $MyInvocation.MyCommand.Path
. "$here\Test-Assertion.ps1"
. "$here\PesterThrow.ps1"


Describe "PesterThrow" {

It "returns true if the statement throws an exception" {
Expand Down Expand Up @@ -64,7 +63,7 @@ Describe 'PesterThrowFailureMessage' {
$expectedErrorMessage = 'some expected message'
PesterThrow { throw $unexpectedErrorMessage } $expectedErrorMessage > $null
$result = PesterThrowFailureMessage $unexpectedErrorMessage $expectedErrorMessage
$result | Should Be "Expected: the expression to throw an exception with message {$expectedErrorMessage}, an exception was raised, message was {$unexpectedErrorMessage}"
$result | Should Match "^Expected: the expression to throw an exception with message {$expectedErrorMessage}, an exception was raised, message was {$unexpectedErrorMessage}`n from $([RegEx]::Escape("$here\PesterThrow.Tests.ps1")):\d+ char:\d+"
}

It 'returns true if the actual message is the same as the expected message' {
Expand All @@ -80,12 +79,12 @@ Describe 'NotPesterThrowFailureMessage' {
$expectedErrorMessage = 'some expected message'
PesterThrow { throw $unexpectedErrorMessage } $expectedErrorMessage > $null
$result = NotPesterThrowFailureMessage $unexpectedErrorMessage $expectedErrorMessage
$result | Should Be "Expected: the expression not to throw an exception with message {$expectedErrorMessage}, an exception was raised, message was {$unexpectedErrorMessage}"
$result | Should Match "^Expected: the expression not to throw an exception with message {$expectedErrorMessage}, an exception was raised, message was {$unexpectedErrorMessage}`n from $([RegEx]::Escape("$here\PesterThrow.Tests.ps1")):\d+ char:\d+"
}

It 'returns true if the actual message is the same as the expected message' {
PesterThrow { throw 'error message' } > $null
$result = NotPesterThrowFailureMessage 'error message'
$result | Should Be 'Expected: the expression not to throw an exception. Message was {error message}'
$result | Should Match "^Expected: the expression not to throw an exception. Message was {error message}`n from $([RegEx]::Escape("$here\PesterThrow.Tests.ps1")):\d+ char:\d+"
}
}
17 changes: 11 additions & 6 deletions Functions/Assertions/PesterThrow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function PesterThrow([scriptblock] $script, $expectedErrorMessage) {
} catch {
$Script:ActualExceptionWasThrown = $true
$Script:ActualExceptionMessage = $_.Exception.Message
$Script:ActualExceptionLine = Get-ExceptionLineInfo $_.InvocationInfo
}

if ($ActualExceptionWasThrown) {
Expand All @@ -27,21 +28,25 @@ function Get-DoMessagesMatch($value, $expected) {
return $value.Contains($expected)
}

function Get-ExceptionLineInfo($info) {
return ($info.PositionMessage -replace "^At ","from ")

}

function PesterThrowFailureMessage($value, $expected) {
if ($expected) {
return "Expected: the expression to throw an exception with message {{{0}}}, an exception was {2}raised, message was {{{1}}}" -f
$expected, $ActualExceptionMessage,(@{$true="";$false="not "}[$ActualExceptionWasThrown])
return "Expected: the expression to throw an exception with message {{{0}}}, an exception was {2}raised, message was {{{1}}}`n {3}" -f
$expected, $ActualExceptionMessage,(@{$true="";$false="not "}[$ActualExceptionWasThrown]),($ActualExceptionLine -replace "`n","`n ")
} else {
return "Expected: the expression to throw an exception"
}
}

function NotPesterThrowFailureMessage($value, $expected) {
if ($expected) {
return "Expected: the expression not to throw an exception with message {{{0}}}, an exception was {2}raised, message was {{{1}}}" -f
$expected, $ActualExceptionMessage,(@{$true="";$false="not "}[$ActualExceptionWasThrown])
return "Expected: the expression not to throw an exception with message {{{0}}}, an exception was {2}raised, message was {{{1}}}`n {3}" -f
$expected, $ActualExceptionMessage,(@{$true="";$false="not "}[$ActualExceptionWasThrown]),($ActualExceptionLine -replace "`n","`n ")
} else {
return "Expected: the expression not to throw an exception. Message was {{{0}}}" -f $ActualExceptionMessage
return "Expected: the expression not to throw an exception. Message was {{{0}}}`n {1}" -f $ActualExceptionMessage,($ActualExceptionLine -replace "`n","`n ")
}
}