Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function IsUnrolledByPipeline
- {
- param
- (
- [Parameter(Mandatory,
- Position=1)]
- [AllowNull()]
- [object]
- $InputObject
- )
- begin
- {
- function PipeIn {
- param
- (
- [Parameter(ValueFromPipeline)]
- $Pipe
- )
- process
- {
- [pscustomobject][hashtable]$PSBoundParameters
- }
- }
- function PipeOut {
- param
- (
- [Parameter(Position=1)]
- $Position
- )
- process
- {
- $Position
- }
- }
- function PipeReturn {
- param
- (
- [Parameter(Position=1)]
- $Position
- )
- process
- {
- return $Position
- }
- }
- }
- process
- {
- $after = @(
- ($InputObject | PipeIn).Pipe # pipe the object into a function
- PipeOut $InputObject # assign the object to a variable from the output of a function
- (PipeOut $InputObject | PipeIn).Pipe # pipe the object from the output one function to input of another
- PipeReturn $InputObject # assign the object to a variable from the return value of a function
- (PipeReturn $InputObject | PipeIn).Pipe # pipe the object from the return of one function to input of another
- )
- if ($after -eq $null)
- {
- # the InputObject became null at least once...
- if ( $null -ne $InputObject )
- {
- # ...but it didn't start as null
- return $true
- }
- foreach ($v in $after)
- {
- if ( $null -ne $v )
- {
- # ... but not every time
- return $true
- }
- }
- # ... and it turns out InputObject became null
- # in every case
- return $false
- }
- # the InputObject never became null...
- foreach ( $v in $after )
- {
- if ( $v.GetHashCode() -ne $InputObject.GetHashCode() )
- {
- # ... and it became something other than itself
- # at least once
- return $true
- }
- }
- # ... and it remained itself in every case
- return $false
- }
- }
- Describe IsUnrolledByPipeline {
- It '<n> : <o>' -TestCases @(
- @{n='null'; i=$null; o=$false}
- @{n='empty array'; i=@(); o=$true}
- @{n='array of nulls'; i=$null,$null; o=$true}
- @{n='array'; i=1,2; o=$true}
- @{n='nested array'; i=1,@(1,2); o=$true}
- @{n='empty hashtable'; i=@{}; o=$false}
- @{n='hashtable'; i=@{a=1};o=$false}
- @{n='bit array'; i=[System.Collections.BitArray]::new(0); o=$true}
- @{n='queue'; i=[System.Collections.Queue]::new(@(0)); o=$true}
- @{n='stack'; i=[System.Collections.Stack]::new(@(0)); o=$true}
- @{n='arrayList'; i=[System.Collections.ArrayList]::new(@(0)); o=$true}
- @{n='xmlElement'; i=([xml]'<a></a>').FirstChild.GetType(); o=$false}
- ) {
- param($i,$o)
- $r = IsUnrolledByPipeline $i
- $r | Should -Be $o
- }
- }
Add Comment
Please, Sign In to add comment