Guest User

Untitled

a guest
Nov 18th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. function IsUnrolledByPipeline
  2. {
  3. param
  4. (
  5. [Parameter(Mandatory,
  6. Position=1)]
  7. [AllowNull()]
  8. [object]
  9. $InputObject
  10. )
  11. begin
  12. {
  13. function PipeIn {
  14. param
  15. (
  16. [Parameter(ValueFromPipeline)]
  17. $Pipe
  18. )
  19. process
  20. {
  21. [pscustomobject][hashtable]$PSBoundParameters
  22. }
  23. }
  24. function PipeOut {
  25. param
  26. (
  27. [Parameter(Position=1)]
  28. $Position
  29. )
  30. process
  31. {
  32. $Position
  33. }
  34. }
  35. function PipeReturn {
  36. param
  37. (
  38. [Parameter(Position=1)]
  39. $Position
  40. )
  41. process
  42. {
  43. return $Position
  44. }
  45. }
  46. }
  47. process
  48. {
  49. $after = @(
  50. ($InputObject | PipeIn).Pipe # pipe the object into a function
  51. PipeOut $InputObject # assign the object to a variable from the output of a function
  52. (PipeOut $InputObject | PipeIn).Pipe # pipe the object from the output one function to input of another
  53. PipeReturn $InputObject # assign the object to a variable from the return value of a function
  54. (PipeReturn $InputObject | PipeIn).Pipe # pipe the object from the return of one function to input of another
  55. )
  56.  
  57. if ($after -eq $null)
  58. {
  59. # the InputObject became null at least once...
  60.  
  61. if ( $null -ne $InputObject )
  62. {
  63. # ...but it didn't start as null
  64. return $true
  65. }
  66.  
  67. foreach ($v in $after)
  68. {
  69. if ( $null -ne $v )
  70. {
  71. # ... but not every time
  72. return $true
  73. }
  74. }
  75.  
  76. # ... and it turns out InputObject became null
  77. # in every case
  78. return $false
  79. }
  80.  
  81. # the InputObject never became null...
  82. foreach ( $v in $after )
  83. {
  84. if ( $v.GetHashCode() -ne $InputObject.GetHashCode() )
  85. {
  86. # ... and it became something other than itself
  87. # at least once
  88. return $true
  89. }
  90. }
  91. # ... and it remained itself in every case
  92. return $false
  93. }
  94. }
  95.  
  96. Describe IsUnrolledByPipeline {
  97. It '<n> : <o>' -TestCases @(
  98. @{n='null'; i=$null; o=$false}
  99. @{n='empty array'; i=@(); o=$true}
  100. @{n='array of nulls'; i=$null,$null; o=$true}
  101. @{n='array'; i=1,2; o=$true}
  102. @{n='nested array'; i=1,@(1,2); o=$true}
  103. @{n='empty hashtable'; i=@{}; o=$false}
  104. @{n='hashtable'; i=@{a=1};o=$false}
  105. @{n='bit array'; i=[System.Collections.BitArray]::new(0); o=$true}
  106. @{n='queue'; i=[System.Collections.Queue]::new(@(0)); o=$true}
  107. @{n='stack'; i=[System.Collections.Stack]::new(@(0)); o=$true}
  108. @{n='arrayList'; i=[System.Collections.ArrayList]::new(@(0)); o=$true}
  109. @{n='xmlElement'; i=([xml]'<a></a>').FirstChild.GetType(); o=$false}
  110. ) {
  111. param($i,$o)
  112. $r = IsUnrolledByPipeline $i
  113. $r | Should -Be $o
  114. }
  115. }
Add Comment
Please, Sign In to add comment