Skip to content

Commit 357e79c

Browse files
committed
Add missing operator tests for switch parameter explicit $false handling
- Add tests for -LT, -NE, -GE, -LE, -NotLike, -NotMatch, -NotContains, -NotIn, -IsNot - Fix escaping of $false in test names for consistent display
1 parent e748593 commit 357e79c

1 file changed

Lines changed: 73 additions & 19 deletions

File tree

‎test/powershell/Modules/Microsoft.PowerShell.Core/Where-Object.Tests.ps1‎

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Describe "Where-Object" -Tags "CI" {
143143
$Result[1] | Should -Be $dynObj
144144
}
145145

146-
Context "Switch parameter explicit $false handling" {
146+
Context "Switch parameter explicit `$false handling" {
147147
BeforeAll {
148148
$testData = @(
149149
[PSCustomObject]@{ Name = 'Alice'; Age = 25; City = 'New York' }
@@ -153,76 +153,130 @@ Describe "Where-Object" -Tags "CI" {
153153
}
154154

155155
It "-EQ:`$false should behave like default boolean evaluation" {
156-
# With -EQ:False, the parameter is not specified, so default boolean evaluation applies
156+
# With -EQ:$false, the parameter is not specified, so default boolean evaluation applies
157157
# "... | Where-Object Name" is equivalent to "... | Where-Object {$true -eq $_.Name}"
158158
# Non-empty strings are truthy
159159
$result = $testData | Where-Object Name -EQ:$false
160160
$result | Should -HaveCount 3
161161
}
162162

163-
It "-GT:$false should behave like default boolean evaluation" {
164-
# With -GT:False, the parameter is not specified, so default boolean evaluation applies
163+
It "-GT:`$false should behave like default boolean evaluation" {
164+
# With -GT:$false, the parameter is not specified, so default boolean evaluation applies
165165
$result = $testData | Where-Object Age -GT:$false
166166
$result | Should -HaveCount 3
167167
}
168168

169-
It "-Like:$false should behave like default boolean evaluation" {
170-
# With -Like:False, the parameter is not specified, so default boolean evaluation applies
169+
It "-LT:`$false should behave like default boolean evaluation" {
170+
# With -LT:$false, the parameter is not specified, so default boolean evaluation applies
171+
$result = $testData | Where-Object Age -LT:$false
172+
$result | Should -HaveCount 3
173+
}
174+
175+
It "-NE:`$false should behave like default boolean evaluation" {
176+
# With -NE:$false, the parameter is not specified, so default boolean evaluation applies
177+
$result = $testData | Where-Object Name -NE:$false
178+
$result | Should -HaveCount 3
179+
}
180+
181+
It "-GE:`$false should behave like default boolean evaluation" {
182+
# With -GE:$false, the parameter is not specified, so default boolean evaluation applies
183+
$result = $testData | Where-Object Age -GE:$false
184+
$result | Should -HaveCount 3
185+
}
186+
187+
It "-LE:`$false should behave like default boolean evaluation" {
188+
# With -LE:$false, the parameter is not specified, so default boolean evaluation applies
189+
$result = $testData | Where-Object Age -LE:$false
190+
$result | Should -HaveCount 3
191+
}
192+
193+
It "-NotLike:`$false should behave like default boolean evaluation" {
194+
# With -NotLike:$false, the parameter is not specified, so default boolean evaluation applies
195+
$result = $testData | Where-Object City -NotLike:$false
196+
$result | Should -HaveCount 3
197+
}
198+
199+
It "-NotMatch:`$false should behave like default boolean evaluation" {
200+
# With -NotMatch:$false, the parameter is not specified, so default boolean evaluation applies
201+
$result = $testData | Where-Object Name -NotMatch:$false
202+
$result | Should -HaveCount 3
203+
}
204+
205+
It "-NotContains:`$false should behave like default boolean evaluation" {
206+
# With -NotContains:$false, the parameter is not specified, so default boolean evaluation applies
207+
$result = $testData | Where-Object Name -NotContains:$false
208+
$result | Should -HaveCount 3
209+
}
210+
211+
It "-NotIn:`$false should behave like default boolean evaluation" {
212+
# With -NotIn:$false, the parameter is not specified, so default boolean evaluation applies
213+
$result = $testData | Where-Object Name -NotIn:$false
214+
$result | Should -HaveCount 3
215+
}
216+
217+
It "-IsNot:`$false should behave like default boolean evaluation" {
218+
# With -IsNot:$false, the parameter is not specified, so default boolean evaluation applies
219+
$result = $testData | Where-Object Name -IsNot:$false
220+
$result | Should -HaveCount 3
221+
}
222+
223+
It "-Like:`$false should behave like default boolean evaluation" {
224+
# With -Like:$false, the parameter is not specified, so default boolean evaluation applies
171225
$result = $testData | Where-Object City -Like:$false
172226
$result | Should -HaveCount 3
173227
}
174228

175-
It "-Match:$false should behave like default boolean evaluation" {
176-
# With -Match:False, the parameter is not specified, so default boolean evaluation applies
229+
It "-Match:`$false should behave like default boolean evaluation" {
230+
# With -Match:$false, the parameter is not specified, so default boolean evaluation applies
177231
$result = $testData | Where-Object Name -Match:$false
178232
$result | Should -HaveCount 3
179233
}
180234

181-
It "-Contains:$false should behave like default boolean evaluation" {
182-
# With -Contains:False, the parameter is not specified, so default boolean evaluation applies
235+
It "-Contains:`$false should behave like default boolean evaluation" {
236+
# With -Contains:$false, the parameter is not specified, so default boolean evaluation applies
183237
# "... | Where-Object Name" checks if Name property is truthy
184238
$result = $testData | Where-Object Name -Contains:$false
185239
$result | Should -HaveCount 3
186240
}
187241

188-
It "-In:$false should behave like default boolean evaluation" {
189-
# With -In:False, the parameter is not specified, so default boolean evaluation applies
242+
It "-In:`$false should behave like default boolean evaluation" {
243+
# With -In:$false, the parameter is not specified, so default boolean evaluation applies
190244
# "... | Where-Object Name" checks if Name property is truthy
191245
$result = $testData | Where-Object Name -In:$false
192246
$result | Should -HaveCount 3
193247
}
194248

195-
It "-Is:$false should behave like default boolean evaluation" {
196-
# With -Is:False, the parameter is not specified, so default boolean evaluation applies
249+
It "-Is:`$false should behave like default boolean evaluation" {
250+
# With -Is:$false, the parameter is not specified, so default boolean evaluation applies
197251
# "... | Where-Object Name" checks if Name property is truthy
198252
$result = $testData | Where-Object Name -Is:$false
199253
$result | Should -HaveCount 3
200254
}
201255

202-
It "-Not:$false should behave like default boolean evaluation" {
256+
It "-Not:`$false should behave like default boolean evaluation" {
203257
$testData2 = @(
204258
[PSCustomObject]@{ Name = ''; Value = 1 }
205259
[PSCustomObject]@{ Name = 'Test'; Value = 2 }
206260
)
207-
# With -Not:False, the parameter is not specified, so default boolean evaluation applies
261+
# With -Not:$false, the parameter is not specified, so default boolean evaluation applies
208262
# "... | Where-Object Name" returns objects where Name is truthy (non-empty)
209263
$result = $testData2 | Where-Object Name -Not:$false
210264
$result | Should -HaveCount 1
211265
$result.Name | Should -Be 'Test'
212266
}
213267

214-
It "-GT:$false with -Value should throw an error requiring an operator" {
268+
It "-GT:`$false with -Value should throw an error requiring an operator" {
215269
# When a switch parameter is set to $false with -Value specified,
216270
# it should throw an error because no valid operator is active
217271
{ $testData | Where-Object Age -GT:$false -Value 25 } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'
218272
}
219273

220-
It "-EQ:$false with -Value should throw an error requiring an operator" {
274+
It "-EQ:`$false with -Value should throw an error requiring an operator" {
221275
# Similar behavior for -EQ:$false with -Value
222276
{ $testData | Where-Object Name -EQ:$false -Value 'Alice' } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'
223277
}
224278

225-
It "-Like:$false with -Value should throw an error requiring an operator" {
279+
It "-Like:`$false with -Value should throw an error requiring an operator" {
226280
# Similar behavior for -Like:$false with -Value
227281
{ $testData | Where-Object Name -Like:$false -Value 'A*' } | Should -Throw -ErrorId 'OperatorNotSpecified,Microsoft.PowerShell.Commands.WhereObjectCommand'
228282
}

0 commit comments

Comments
 (0)