Last active
November 20, 2017 02:27
-
-
Save alx9r/fb497acbf7f6f80ff455ea4cf9b1f7b8 to your computer and use it in GitHub Desktop.
function that tests whether PowerShell unrolls an object
This file contains hidden or 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
| 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 | |
| } | |
| } | |
| $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} | |
| ) | |
| Describe IsUnrolledByPipeline { | |
| It '<n> : <o>' -TestCases $testCases { | |
| param($i,$o) | |
| $r = IsUnrolledByPipeline $i | |
| $r | Should -Be $o | |
| } | |
| } | |
| Describe '[LanguagePrimitives]::GetEnumerator()' { | |
| It '<n> : <o>' -TestCases $testCases { | |
| param($i,$o) | |
| $r = [bool][System.Management.Automation.LanguagePrimitives]::GetEnumerator($i) | |
| $r | Should -Be $o | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment