Skip to content

Commit

Permalink
test for IList per discussion in pester#785
Browse files Browse the repository at this point in the history
self-limit recursion depth to cause fast(er) failure in case cyclic object graphs are encountered
  • Loading branch information
alx9r committed Aug 16, 2017
1 parent 89340b8 commit e8268be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Functions/Assertions/Be.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,19 @@ InModuleScope Pester {
{abc} | Should -CEQ "abc"
}

It 'Does not overflow on IEnumerable' {
It 'Does not overflow on cyclic object graph' {
# see https://github.com/pester/Pester/issues/785
$doc = [xml]'<?xml version="1.0" encoding="UTF-8" standalone="no" ?><root></root>'
$doc | Should be $doc
}

Context 'recursion limit' {
Mock IsIList { $true }
It 'throws exception when self-imposed recursion limit is reached' {
$doc = [xml]''
{ $doc | Should be $doc } | Should throw 'reached recursion depth limit'
}
}
}

Describe "PesterBeFailureMessage" {
Expand Down
14 changes: 10 additions & 4 deletions Functions/Assertions/Be.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,14 @@ function ArraysAreEqual
param (
[object[]] $First,
[object[]] $Second,
[switch] $CaseSensitive
[switch] $CaseSensitive,
[int] $RecursionLimit = 100
)
$recursionDepth++
if ( $recursionDepth -gt $RecursionLimit )
{
throw "reached recursion depth limit of $RecursionLimit when comparing arrays $First and $Second"
}

# Do not remove the subexpression @() operators in the following two lines; doing so can cause a
# silly error in PowerShell v3. (Null Reference exception from the PowerShell engine in a
Expand All @@ -196,7 +202,7 @@ function ArraysAreEqual

for ($i = 0; $i -lt $First.Count; $i++)
{
if ((IsCollection $First[$i]) -or (IsCollection $Second[$i]))
if ((IsIList $First[$i]) -or (IsIList $Second[$i]))
{
if (-not (ArraysAreEqual -First $First[$i] -Second $Second[$i] -CaseSensitive:$CaseSensitive))
{
Expand Down Expand Up @@ -231,11 +237,11 @@ function ArrayOrSingleElementIsNullOrEmpty
return $null -eq $Array -or $Array.Count -eq 0 -or ($Array.Count -eq 1 -and $null -eq $Array[0])
}

function IsCollection
function IsIList
{
param ([object] $InputObject)

return $InputObject -is [Array]
return [bool]$InputObject.GetType().GetInterface('IList')
}

function ReplaceValueInArray
Expand Down

0 comments on commit e8268be

Please sign in to comment.