MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Add-TestDynamicType4 5Describe "Where-Object" -Tags "CI" {6 BeforeAll {7 $Computers = @(8 [PSCustomObject]@{9 ComputerName = "SPC-1234"10 IPAddress = "192.168.0.1"11 NumberOfCores = 112 Drives = 'C','D'13 },14 [PSCustomObject]@{15 ComputerName = "BGP-5678"16 IPAddress = ""17 NumberOfCores = 218 Drives = 'C','D','E'19 },20 [PSCustomObject]@{21 ComputerName = "MGC-9101"22 NumberOfCores = 323 Drives = 'C'24 }25 )26 }27 28 It "Where-Object -Not Prop" {29 $Result = $Computers | Where-Object -Not 'IPAddress'30 $Result | Should -HaveCount 231 }32 33 It 'Where-Object -FilterScript {$true -ne $_.Prop}' {34 $Result = $Computers | Where-Object -FilterScript {$true -ne $_.IPAddress}35 $Result | Should -HaveCount 236 }37 38 It "Where-Object Prop" {39 $Result = $Computers | Where-Object 'IPAddress'40 $Result | Should -HaveCount 141 }42 43 It 'Where-Object -FilterScript {$true -eq $_.Prop}' {44 $Result = $Computers | Where-Object -FilterScript {$true -eq $_.IPAddress}45 $Result | Should -HaveCount 146 }47 48 It 'Where-Object -FilterScript {$_.Prop -contains Value}' {49 $Result = $Computers | Where-Object -FilterScript {$_.Drives -contains 'D'}50 $Result | Should -HaveCount 251 }52 53 It 'Where-Object Prop -contains Value' {54 $Result = $Computers | Where-Object Drives -Contains 'D'55 $Result | Should -HaveCount 256 }57 58 It 'Where-Object -FilterScript {$_.Prop -in $Array}' {59 $Array = 'SPC-1234','BGP-5678'60 $Result = $Computers | Where-Object -FilterScript {$_.ComputerName -in $Array}61 $Result | Should -HaveCount 262 }63 64 It 'Where-Object $Array -in Prop' {65 $Array = 'SPC-1234','BGP-5678'66 $Result = $Computers | Where-Object ComputerName -In $Array67 $Result | Should -HaveCount 268 }69 70 It 'Where-Object -FilterScript {$_.Prop -ge 2}' {71 $Result = $Computers | Where-Object -FilterScript {$_.NumberOfCores -ge 2}72 $Result | Should -HaveCount 273 }74 75 It 'Where-Object Prop -ge 2' {76 $Result = $Computers | Where-Object NumberOfCores -GE 277 $Result | Should -HaveCount 278 }79 80 It 'Where-Object -FilterScript {$_.Prop -gt 2}' {81 $Result = $Computers | Where-Object -FilterScript {$_.NumberOfCores -gt 2}82 $Result | Should -HaveCount 183 }84 85 It 'Where-Object Prop -gt 2' {86 $Result = $Computers | Where-Object NumberOfCores -GT 287 $Result | Should -HaveCount 188 }89 90 It 'Where-Object -FilterScript {$_.Prop -le 2}' {91 $Result = $Computers | Where-Object -FilterScript {$_.NumberOfCores -le 2}92 $Result | Should -HaveCount 293 }94 95 It 'Where-Object Prop -le 2' {96 $Result = $Computers | Where-Object NumberOfCores -LE 297 $Result | Should -HaveCount 298 }99 100 It 'Where-Object -FilterScript {$_.Prop -lt 2}' {101 $Result = $Computers | Where-Object -FilterScript {$_.NumberOfCores -lt 2}102 $Result | Should -HaveCount 1103 }104 105 It 'Where-Object Prop -lt 2' {106 $Result = $Computers | Where-Object NumberOfCores -LT 2107 $Result | Should -HaveCount 1108 }109 110 It 'Where-Object -FilterScript {$_.Prop -Like Value}' {111 $Result = $Computers | Where-Object -FilterScript {$_.ComputerName -like 'MGC-9101'}112 $Result | Should -HaveCount 1113 }114 115 It 'Where-Object Prop -like Value' {116 $Result = $Computers | Where-Object ComputerName -Like 'MGC-9101'117 $Result | Should -HaveCount 1118 }119 120 It 'Where-Object -FilterScript {$_.Prop -Match Pattern}' {121 $Result = $Computers | Where-Object -FilterScript {$_.ComputerName -match '^MGC.+'}122 $Result | Should -HaveCount 1123 }124 125 It 'Where-Object Prop -like Value' {126 $Result = $Computers | Where-Object ComputerName -Match '^MGC.+'127 $Result | Should -HaveCount 1128 }129 130 It 'Where-Object should handle dynamic (DLR) objects' {131 $dynObj = [TestDynamic]::new()132 $Result = $dynObj, $dynObj | Where-Object FooProp -EQ 123133 $Result | Should -HaveCount 2134 $Result[0] | Should -Be $dynObj135 $Result[1] | Should -Be $dynObj136 }137 138 It 'Where-Object should handle dynamic (DLR) objects, even without property name hint' {139 $dynObj = [TestDynamic]::new()140 $Result = $dynObj, $dynObj | Where-Object HiddenProp -EQ 789141 $Result | Should -HaveCount 2142 $Result[0] | Should -Be $dynObj143 $Result[1] | Should -Be $dynObj144 }145 146 Context "Switch parameter explicit `$false handling" {147 BeforeAll {148 $testData = @(149 [PSCustomObject]@{ Name = 'Alice'; Age = 25; City = 'New York' }150 [PSCustomObject]@{ Name = 'Bob'; Age = 30; City = 'Boston' }151 [PSCustomObject]@{ Name = 'Charlie'; Age = 35; City = 'Chicago' }152 )153 }154 155 It "-EQ:`$false should behave like default boolean evaluation" {156 # With -EQ:$false, the parameter is not specified, so default boolean evaluation applies157 # "... | Where-Object Name" is equivalent to "... | Where-Object {$true -eq $_.Name}"158 # Non-empty strings are truthy159 $result = $testData | Where-Object Name -EQ:$false160 $result | Should -HaveCount 3161 }162 163 It "-GT:`$false should behave like default boolean evaluation" {164 # With -GT:$false, the parameter is not specified, so default boolean evaluation applies165 $result = $testData | Where-Object Age -GT:$false166 $result | Should -HaveCount 3167 }168 169 It "-LT:`$false should behave like default boolean evaluation" {170 # With -LT:$false, the parameter is not specified, so default boolean evaluation applies171 $result = $testData | Where-Object Age -LT:$false172 $result | Should -HaveCount 3173 }174 175 It "-NE:`$false should behave like default boolean evaluation" {176 # With -NE:$false, the parameter is not specified, so default boolean evaluation applies177 $result = $testData | Where-Object Name -NE:$false178 $result | Should -HaveCount 3179 }180 181 It "-GE:`$false should behave like default boolean evaluation" {182 # With -GE:$false, the parameter is not specified, so default boolean evaluation applies183 $result = $testData | Where-Object Age -GE:$false184 $result | Should -HaveCount 3185 }186 187 It "-LE:`$false should behave like default boolean evaluation" {188 # With -LE:$false, the parameter is not specified, so default boolean evaluation applies189 $result = $testData | Where-Object Age -LE:$false190 $result | Should -HaveCount 3191 }192 193 It "-NotLike:`$false should behave like default boolean evaluation" {194 # With -NotLike:$false, the parameter is not specified, so default boolean evaluation applies195 $result = $testData | Where-Object City -NotLike:$false196 $result | Should -HaveCount 3197 }198 199 It "-NotMatch:`$false should behave like default boolean evaluation" {200 # With -NotMatch:$false, the parameter is not specified, so default boolean evaluation applies201 $result = $testData | Where-Object Name -NotMatch:$false202 $result | Should -HaveCount 3203 }204 205 It "-NotContains:`$false should behave like default boolean evaluation" {206 # With -NotContains:$false, the parameter is not specified, so default boolean evaluation applies207 $result = $testData | Where-Object Name -NotContains:$false208 $result | Should -HaveCount 3209 }210 211 It "-NotIn:`$false should behave like default boolean evaluation" {212 # With -NotIn:$false, the parameter is not specified, so default boolean evaluation applies213 $result = $testData | Where-Object Name -NotIn:$false214 $result | Should -HaveCount 3215 }216 217 It "-IsNot:`$false should behave like default boolean evaluation" {218 # With -IsNot:$false, the parameter is not specified, so default boolean evaluation applies219 $result = $testData | Where-Object Name -IsNot:$false220 $result | Should -HaveCount 3221 }222 223 It "-Like:`$false should behave like default boolean evaluation" {224 # With -Like:$false, the parameter is not specified, so default boolean evaluation applies225 $result = $testData | Where-Object City -Like:$false226 $result | Should -HaveCount 3227 }228 229 It "-Match:`$false should behave like default boolean evaluation" {230 # With -Match:$false, the parameter is not specified, so default boolean evaluation applies231 $result = $testData | Where-Object Name -Match:$false232 $result | Should -HaveCount 3233 }234 235 It "-Contains:`$false should behave like default boolean evaluation" {236 # With -Contains:$false, the parameter is not specified, so default boolean evaluation applies237 # "... | Where-Object Name" checks if Name property is truthy238 $result = $testData | Where-Object Name -Contains:$false239 $result | Should -HaveCount 3240 }241 242 It "-In:`$false should behave like default boolean evaluation" {243 # With -In:$false, the parameter is not specified, so default boolean evaluation applies244 # "... | Where-Object Name" checks if Name property is truthy245 $result = $testData | Where-Object Name -In:$false246 $result | Should -HaveCount 3247 }248 249 It "-Is:`$false should behave like default boolean evaluation" {250 # With -Is:$false, the parameter is not specified, so default boolean evaluation applies251 # "... | Where-Object Name" checks if Name property is truthy252 $result = $testData | Where-Object Name -Is:$false253 $result | Should -HaveCount 3254 }255 256 It "-Not:`$false should behave like default boolean evaluation" {257 $testData2 = @(258 [PSCustomObject]@{ Name = ''; Value = 1 }259 [PSCustomObject]@{ Name = 'Test'; Value = 2 }260 )261 # With -Not:$false, the parameter is not specified, so default boolean evaluation applies262 # "... | Where-Object Name" returns objects where Name is truthy (non-empty)263 $result = $testData2 | Where-Object Name -Not:$false264 $result | Should -HaveCount 1265 $result.Name | Should -Be 'Test'266 }267 268 It "-GT:`$false with -Value should throw an error requiring an operator" {269 # When a switch parameter is set to $false with -Value specified,270 # it should throw an error because no valid operator is active271 { $testData | Where-Object Age -GT:$false -Value 25 } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'272 }273 274 It "-EQ:`$false with -Value should throw an error requiring an operator" {275 # Similar behavior for -EQ:$false with -Value276 { $testData | Where-Object Name -EQ:$false -Value 'Alice' } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'277 }278 279 It "-Like:`$false with -Value should throw an error requiring an operator" {280 # Similar behavior for -Like:$false with -Value281 { $testData | Where-Object Name -Like:$false -Value 'A*' } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'282 }283 284 It "-CEQ:`$false should behave like default boolean evaluation" {285 # With -CEQ:$false, the parameter is not specified, so default boolean evaluation applies286 $result = $testData | Where-Object Name -CEQ:$false287 $result | Should -HaveCount 3288 }289 290 It "-CNE:`$false should behave like default boolean evaluation" {291 # With -CNE:$false, the parameter is not specified, so default boolean evaluation applies292 $result = $testData | Where-Object Name -CNE:$false293 $result | Should -HaveCount 3294 }295 296 It "-CGT:`$false should behave like default boolean evaluation" {297 # With -CGT:$false, the parameter is not specified, so default boolean evaluation applies298 $result = $testData | Where-Object Age -CGT:$false299 $result | Should -HaveCount 3300 }301 302 It "-CLT:`$false should behave like default boolean evaluation" {303 # With -CLT:$false, the parameter is not specified, so default boolean evaluation applies304 $result = $testData | Where-Object Age -CLT:$false305 $result | Should -HaveCount 3306 }307 308 It "-CGE:`$false should behave like default boolean evaluation" {309 # With -CGE:$false, the parameter is not specified, so default boolean evaluation applies310 $result = $testData | Where-Object Age -CGE:$false311 $result | Should -HaveCount 3312 }313 314 It "-CLE:`$false should behave like default boolean evaluation" {315 # With -CLE:$false, the parameter is not specified, so default boolean evaluation applies316 $result = $testData | Where-Object Age -CLE:$false317 $result | Should -HaveCount 3318 }319 320 It "-CLike:`$false should behave like default boolean evaluation" {321 # With -CLike:$false, the parameter is not specified, so default boolean evaluation applies322 $result = $testData | Where-Object City -CLike:$false323 $result | Should -HaveCount 3324 }325 326 It "-CNotLike:`$false should behave like default boolean evaluation" {327 # With -CNotLike:$false, the parameter is not specified, so default boolean evaluation applies328 $result = $testData | Where-Object City -CNotLike:$false329 $result | Should -HaveCount 3330 }331 332 It "-CMatch:`$false should behave like default boolean evaluation" {333 # With -CMatch:$false, the parameter is not specified, so default boolean evaluation applies334 $result = $testData | Where-Object Name -CMatch:$false335 $result | Should -HaveCount 3336 }337 338 It "-CNotMatch:`$false should behave like default boolean evaluation" {339 # With -CNotMatch:$false, the parameter is not specified, so default boolean evaluation applies340 $result = $testData | Where-Object Name -CNotMatch:$false341 $result | Should -HaveCount 3342 }343 344 It "-CContains:`$false should behave like default boolean evaluation" {345 # With -CContains:$false, the parameter is not specified, so default boolean evaluation applies346 $result = $testData | Where-Object Name -CContains:$false347 $result | Should -HaveCount 3348 }349 350 It "-CNotContains:`$false should behave like default boolean evaluation" {351 # With -CNotContains:$false, the parameter is not specified, so default boolean evaluation applies352 $result = $testData | Where-Object Name -CNotContains:$false353 $result | Should -HaveCount 3354 }355 356 It "-CIn:`$false should behave like default boolean evaluation" {357 # With -CIn:$false, the parameter is not specified, so default boolean evaluation applies358 $result = $testData | Where-Object Name -CIn:$false359 $result | Should -HaveCount 3360 }361 362 It "-CNotIn:`$false should behave like default boolean evaluation" {363 # With -CNotIn:$false, the parameter is not specified, so default boolean evaluation applies364 $result = $testData | Where-Object Name -CNotIn:$false365 $result | Should -HaveCount 3366 }367 368 It "-CEQ:`$false with -Value should throw an error requiring an operator" {369 # Similar behavior for -CEQ:$false with -Value370 { $testData | Where-Object Name -CEQ:$false -Value 'Alice' } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'371 }372 373 It "-CGT:`$false with -Value should throw an error requiring an operator" {374 # Similar behavior for -CGT:$false with -Value375 { $testData | Where-Object Age -CGT:$false -Value 25 } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'376 }377 378 It "-CLike:`$false with -Value should throw an error requiring an operator" {379 # Similar behavior for -CLike:$false with -Value380 { $testData | Where-Object Name -CLike:$false -Value 'A*' } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'381 }382 }383}384 