Skip to content

Commit

Permalink
Avoid nested array when using Should -ActualValue (pester#2315)
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten authored May 6, 2023
1 parent 08998af commit bd7bfe7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Pester.Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,10 @@ function Fold-Run {
}
}
}

function IsPSEnumerable($Object) {
# https://github.com/pester/Pester/issues/1200#issuecomment-493043683
# PowerShell doesn't consider all IEnumerable an enumerable, ex. string is excluded.
$enumerator = [System.Management.Automation.LanguagePrimitives]::GetEnumerator($Object)
return $null -ne $enumerator
}
9 changes: 8 additions & 1 deletion src/functions/assertions/Should.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ function Should {
}

process {
$inputArray.Add($ActualValue)
# If array was provided using -ActualValue @(1,2,3), unroll like pipeline for consistent behaviour
if (-not $PSCmdlet.MyInvocation.ExpectingInput -and (IsPSEnumerable -Object $ActualValue)) {
foreach ($object in $ActualValue) {
$inputArray.Add($object)
}
} else {
$inputArray.Add($ActualValue)
}
}

end {
Expand Down
6 changes: 6 additions & 0 deletions tst/functions/assertions/Should.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ InPesterModuleScope {
@($item1, $item2) | Should -Not -BeNullOrEmpty
}

It 'works consistenly with array-input using pipeline or -ActualValue' {
# https://github.com/pester/Pester/issues/2314
@(1, 2, 3) | Should -Be -ExpectedValue @(1, 2, 3)
Should -Be -ActualValue @(1, 2, 3) -ExpectedValue @(1, 2, 3)
}

It "can handle exception thrown assertions" {
{ foo } | Should -Throw
}
Expand Down

0 comments on commit bd7bfe7

Please sign in to comment.