Skip to content

Commit bd7bfe7

Browse files
authored
Avoid nested array when using Should -ActualValue (#2315)
1 parent 08998af commit bd7bfe7

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/Pester.Utility.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,10 @@ function Fold-Run {
384384
}
385385
}
386386
}
387+
388+
function IsPSEnumerable($Object) {
389+
# https://github.com/pester/Pester/issues/1200#issuecomment-493043683
390+
# PowerShell doesn't consider all IEnumerable an enumerable, ex. string is excluded.
391+
$enumerator = [System.Management.Automation.LanguagePrimitives]::GetEnumerator($Object)
392+
return $null -ne $enumerator
393+
}

src/functions/assertions/Should.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ function Should {
120120
}
121121

122122
process {
123-
$inputArray.Add($ActualValue)
123+
# If array was provided using -ActualValue @(1,2,3), unroll like pipeline for consistent behaviour
124+
if (-not $PSCmdlet.MyInvocation.ExpectingInput -and (IsPSEnumerable -Object $ActualValue)) {
125+
foreach ($object in $ActualValue) {
126+
$inputArray.Add($object)
127+
}
128+
} else {
129+
$inputArray.Add($ActualValue)
130+
}
124131
}
125132

126133
end {

tst/functions/assertions/Should.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ InPesterModuleScope {
2828
@($item1, $item2) | Should -Not -BeNullOrEmpty
2929
}
3030

31+
It 'works consistenly with array-input using pipeline or -ActualValue' {
32+
# https://github.com/pester/Pester/issues/2314
33+
@(1, 2, 3) | Should -Be -ExpectedValue @(1, 2, 3)
34+
Should -Be -ActualValue @(1, 2, 3) -ExpectedValue @(1, 2, 3)
35+
}
36+
3137
It "can handle exception thrown assertions" {
3238
{ foo } | Should -Throw
3339
}

0 commit comments

Comments
 (0)