-
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |