diff --git a/src/functions/assert/Common/Collect-Input.ps1 b/src/functions/assert/Common/Collect-Input.ps1 index ab2facc29..d10433d85 100644 --- a/src/functions/assert/Common/Collect-Input.ps1 +++ b/src/functions/assert/Common/Collect-Input.ps1 @@ -1,8 +1,19 @@ function Collect-Input ($ParameterInput, $PipelineInput, $IsInPipeline) { if ($IsInPipeline) { - $PipelineInput + # We are called like this: 1 | Assert-Equal -Expected 1, we will get $local:Input in $PipelineInput and $true in $IsInPipeline (coming from $MyInvocation.ExpectingInput). + + if ($PipelineInput.Count -eq 0) { + # When calling @() | Assert-Equal -Expected 1, the engine will special case it, and we will get empty array $local:Input, fix that + # by returning empty array wrapped in array. + , @() + } + else { + # This is array of all the input, when we output it, the function will unwrap it. So we get the raw input on the output. + $PipelineInput + } } else { - $ParameterInput + # This is exactly what was provided to the ActualParmeter, wrap it in array so the function return can unwrap it. + , $ParameterInput } } diff --git a/tst/functions/assert/Common/Collect-Input.Tests.ps1 b/tst/functions/assert/Common/Collect-Input.Tests.ps1 new file mode 100644 index 000000000..1cccba7ed --- /dev/null +++ b/tst/functions/assert/Common/Collect-Input.Tests.ps1 @@ -0,0 +1,62 @@ +Set-StrictMode -Version Latest + +InPesterModuleScope { + Describe "Collect-Input" { + BeforeAll { + function Assert-PassThru { + # This is how all Assert-* functions look inside, here we just collect $Actual and return it. + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')] + param ( + [Parameter(ValueFromPipeline = $true)] + $Actual + ) + + + $Actual = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsInPipeline $MyInvocation.ExpectingInput + + $Actual + } + } + + It "Given `$null through pipeline it returns `$null" { + $in = $null | Assert-PassThru + + Verify-True ($null -eq $in) + } + + It "Given @() through pipeline it returns array with 0 items" { + $in = @() | Assert-PassThru + + Verify-True ($in.GetType().Name -eq 'Object[]') + Verify-True ($in.Count -eq 0) + } + + It "Given @() through pipeline it returns array with 0 items" { + $in = Assert-PassThru -Actual @() + + Verify-True ($in.GetType().Name -eq 'Object[]') + Verify-True ($in.Count -eq 0) + } + + It "Given @() through pipeline it returns array with 0 items" { + $in = Assert-PassThru -Actual $null + + Verify-True ($in.GetType().Name -eq 'Object[]') + Verify-True ($in.Count -eq 0) + } + + It "Given @() through pipeline it returns array with 0 items" { + $in = Assert-PassThru -Actual 1, 2 + + Verify-True ($in.GetType().Name -eq 'Object[]') + Verify-True ($in.Count -eq 0) + } + + It "Given @() through pipeline it returns array with 0 items" { + $in = 1, 2 | Assert-PassThru + + Verify-True ($in.GetType().Name -eq 'Object[]') + Verify-True ($in.Count -eq 0) + } + } +}