Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Get-ChildException approach to supporting assertion on child exceptions #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions Assert.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ FunctionsToExport = @(
'Assert-NotLike'
'Assert-StringEqual'
'Assert-StringNotEqual'
'Get-ChildException'
)

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
Expand Down
1 change: 1 addition & 0 deletions Assert.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ Export-ModuleMember -Function @(
'Assert-NotLike'
'Assert-StringEqual'
'Assert-StringNotEqual'
'Get-ChildException'
)
2 changes: 1 addition & 1 deletion src/Exception/Assert-Throw.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function Assert-Throw {
throw [Assertions.AssertionException]$Message
}

$ScriptBlock
$err.ErrorRecord
}

function Get-Error ($ErrorRecord) {
Expand Down
43 changes: 43 additions & 0 deletions src/Exception/Get-ChildException.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Get-ChildException
{
param
(
[switch]
$Recurse,

[Parameter(Mandatory,
ValueFromPipeline)]
[System.Management.Automation.ErrorRecord]
$ErrorRecord
)
process
{
$ErrorRecord.Exception | Get-ChildExceptionImpl -Recurse:$Recurse
}
}

function Get-ChildExceptionImpl
{
param
(
[switch]
$Recurse,

[Parameter(Mandatory,
ValueFromPipeline)]
[System.Exception]
$Exception
)
process
{
if ($null -eq $Exception.InnerException)
{
return
}
$Exception.InnerException
if ( $Recurse )
{
$Exception.InnerException | Get-ChildExceptionImpl -Recurse:$Recurse
}
}
}
7 changes: 7 additions & 0 deletions tst/Exception/Assert-Throw.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ Describe "Assert-Throw" {
{ Get-Item "non-existing" } | Assert-Throw
}
}

Context 'output' {
It 'outputs ErrorRecord' {
$r = {throw} | Assert-Throw
$r | Should -BeOfType ([System.Management.Automation.ErrorRecord])
}
}
}

Describe "General try catch behavior" {
Expand Down
69 changes: 69 additions & 0 deletions tst/Exception/Get-ChildException.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
InModuleScope Assert {
Describe Get-ChildException {
Mock Get-ChildExceptionImpl -Verifiable {'return value'}
try
{
throw 'message'
}
catch
{
$r = $_ | Get-ChildException -Recurse
}
It 'invokes Get-ChildExceptionImpl' {
Assert-MockCalled Get-ChildExceptionImpl -Times 1 -Exactly {
($Exception.Message -eq 'message') -and
$Recurse
}
}
It 'returns value Get-ChildExceptionImpl' {
$r | Verify-Equal 'return value'
}
}
Describe Get-ChildExceptionImpl {
Context 'no InnerException' {
$r = [System.Exception]::new('message') |
Get-ChildExceptionImpl
It 'returns null' {
$r | Verify-Null
}
}
Context 'has InnerException' {
$r = [System.Exception]::new(
'outer',
[System.Exception]::new('inner')
) |
Get-ChildExceptionImpl
It 'returns InnerException' {
$r.Message | Verify-Equal 'inner'
}
}
Context 'nested InnerException' {
$e = [System.Exception]::new(
'outermost',
[System.Exception]::new(
'middle-outer',
[System.Exception]::new(
'middle-inner',
[System.Exception]::new('innermost')
)
)
)
Context 'don''t -Recurse' {
$r = $e | Get-ChildExceptionImpl
It 'returns only first descendant' {
$r.Count | Verify-Equal 1
$r.Message | Verify-Equal 'middle-outer'
}
}
Context '-Recurse' {
$r = $e | Get-ChildExceptionImpl -Recurse
It 'returns all descendents, outermost-first' {
$r.Count | Verify-Equal 3
$r[0].Message | Verify-Equal 'middle-outer'
$r[1].Message | Verify-Equal 'middle-inner'
$r[2].Message | Verify-Equal 'innermost'
}
}
}
}
}