Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Apr 7, 2024
1 parent 6575ca4 commit 50182d9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/functions/assert/Common/Collect-Input.ps1
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
}
}
62 changes: 62 additions & 0 deletions tst/functions/assert/Common/Collect-Input.Tests.ps1
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)
}
}
}

0 comments on commit 50182d9

Please sign in to comment.