Skip to content

Commit 7b0a58b

Browse files
committed
✅ Add test for pipelinevariable
1 parent bbbb4ee commit 7b0a58b

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/powershell/engine/ParameterBinding/ParameterBinding.Tests.ps1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,62 @@ Describe "Parameter Binding Tests" -Tags "CI" {
256256
DynamicParamTest -PipelineVariable bar | ForEach-Object { $bar } | Should -Be "hi"
257257
}
258258

259+
Context "PipelineVariable Behaviour" {
260+
261+
BeforeAll {
262+
function Write-PipelineVariable {
263+
[CmdletBinding()]
264+
[OutputType([int])]
265+
param(
266+
[Parameter(ValueFromPipeline)]
267+
$a
268+
)
269+
begin { 1 }
270+
process { 2 }
271+
end { 3 }
272+
}
273+
274+
$testScripts = @(
275+
@{
276+
CmdletType = 'Script Cmdlet'
277+
Script = {
278+
1..3 |
279+
Write-PipelineVariable -PipelineVariable pipe |
280+
Select-Object -Property @(
281+
@{ Name = "PipelineVariableSet"; Expression = { $null -ne $pipe ? $true : $false } }
282+
@{ Name = "PipelineVariable"; Expression = { $pipe } }
283+
)
284+
}
285+
}
286+
@{
287+
CmdletType = 'Compiled Cmdlet'
288+
Script = {
289+
1..3 |
290+
Write-PipelineVariable |
291+
ForEach-Object { $_ } -PipelineVariable pipe |
292+
Select-Object -Property @(
293+
@{ Name = "PipelineVariableSet"; Expression = { $null -ne $pipe ? $true : $false } }
294+
@{ Name = "PipelineVariable"; Expression = { $pipe } }
295+
)
296+
}
297+
}
298+
)
299+
}
300+
301+
AfterAll {
302+
Remove-Item -Path 'function:Write-PipelineVariable'
303+
}
304+
305+
It 'should set the pipeline variable every time for a <CmdletType>' -TestCases $testScripts {
306+
param($Script, $CmdletType)
307+
308+
$result = & $Script
309+
$result.Count | Should -Be 5
310+
$result.PipelineVariableSet | Should -Not -Contain $false
311+
$result.PipelineVariable | Should -Be 1, 2, 2, 2, 3
312+
}
313+
}
314+
259315
Context "Use automatic variables as default value for parameters" {
260316
BeforeAll {
261317
## Explicit use of 'CmdletBinding' make it a script cmdlet

0 commit comments

Comments
 (0)