File size: 11,369 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'NullCoalesceOperations' -Tags 'CI' {
BeforeAll {
$someGuid = New-Guid
$typesTests = @(
@{ name = 'string'; valueToSet = 'hello' }
@{ name = 'dotnetType'; valueToSet = $someGuid }
@{ name = 'byte'; valueToSet = [byte]0x94 }
@{ name = 'intArray'; valueToSet = 1..2 }
@{ name = 'stringArray'; valueToSet = 'a'..'c' }
@{ name = 'emptyArray'; valueToSet = @(1, 2, 3) }
)
}
Context "Null conditional assignment operator ??=" {
It 'Variable doesnot exist' {
Remove-Variable variableDoesNotExist -ErrorAction SilentlyContinue -Force
$variableDoesNotExist ??= 1
$variableDoesNotExist | Should -Be 1
$variableDoesNotExist ??= 2
$variableDoesNotExist | Should -Be 1
}
It 'Variable exists and is null' {
$variableDoesNotExist = $null
$variableDoesNotExist ??= 2
$variableDoesNotExist | Should -Be 2
}
It 'Validate types - <name> can be set' -TestCases $typesTests {
param ($name, $valueToSet)
$x = $null
$x ??= $valueToSet
$x | Should -Be $valueToSet
}
It 'Validate hashtable can be set' {
$x = $null
$x ??= @{ 1 = '1' }
$x.Keys | Should -Be @(1)
}
It 'Validate lhs is returned' {
$x = 100
$x ??= 200
$x | Should -Be 100
}
It 'Rhs is a cmdlet' {
$x = $null
$x ??= (Get-Alias -Name 'where')
$x.Definition | Should -BeExactly 'Where-Object'
}
It 'Lhs is DBNull' {
$x = [System.DBNull]::Value
$x ??= 200
$x | Should -Be ([System.DBNull]::Value)
}
It 'Lhs is AutomationNull' {
$x = [System.Management.Automation.Internal.AutomationNull]::Value
$x ??= 200
$x | Should -Be 200
}
It 'Lhs is NullString' {
$x = [NullString]::Value
$x ??= 200
$x | Should -Be ([NullString]::Value)
}
It 'Lhs is empty string' {
$x = ''
$x ??= 20
$x | Should -BeExactly ''
}
It 'Error case' {
$e = $null
$null = [System.Management.Automation.Language.Parser]::ParseInput('1 ??= 100', [ref] $null, [ref] $e)
$e[0].ErrorId | Should -BeExactly 'InvalidLeftHandSide'
}
It 'Variable is non-null' {
$num = 10
$num ??= 20
$num | Should -Be 10
}
It 'Lhs is $?' {
{ $???=$false}
$? | Should -BeTrue
}
}
Context 'Null coalesce operator ??' {
BeforeEach {
$x = $null
}
It 'Variable does not exist' {
Remove-Variable variableDoesNotExist -ErrorAction SilentlyContinue -Force
$variableDoesNotExist ?? 100 | Should -Be 100
}
It 'Variable exists but is null' {
$x ?? 100 | Should -Be 100
}
It 'Lhs is not null' {
$x = 100
$x ?? 200 | Should -Be 100
}
It 'Lhs is a non-null constant' {
1 ?? 2 | Should -Be 1
}
It 'Lhs is `$null' {
$null ?? 'string value' | Should -BeExactly 'string value'
}
It 'Check precedence of ?? expression resolution' {
$x ?? $null ?? 100 | Should -Be 100
$null ?? $null ?? 100 | Should -Be 100
$null ?? $null ?? $null | Should -Be $null
$x ?? 200 ?? $null | Should -Be 200
$x ?? 200 ?? 300 | Should -Be 200
100 ?? $x ?? 200 | Should -Be 100
$null ?? 100 ?? $null ?? 200 | Should -Be 100
}
It 'Rhs is a cmdlet' {
$result = $x ?? (Get-Alias -Name 'where')
$result.Definition | Should -BeExactly 'Where-Object'
}
It 'Lhs is DBNull' {
$x = [System.DBNull]::Value
$x ?? 200 | Should -Be ([System.DBNull]::Value)
}
It 'Lhs is AutomationNull' {
$x = [System.Management.Automation.Internal.AutomationNull]::Value
$x ?? 200 | Should -Be 200
}
It 'Lhs is NullString' {
$x = [NullString]::Value
$x ?? 200 | Should -Be ([NullString]::Value)
}
It 'Rhs is a get variable expression' {
$x = $null
$y = 2
$x ?? $y | Should -Be 2
}
It 'Lhs is a constant' {
[System.DBNull]::Value ?? 2 | Should -Be ([System.DBNull]::Value)
}
It 'Lhs is $?' {
{$???$false} | Should -BeTrue
}
It 'Should only evaluate LHS once when it IS null' {
$testState = [pscustomobject]@{ Value = 0 }
(& { [void]$testState.Value++ }) ?? 'Nothing' | Should -BeExactly 'Nothing'
$testState.Value | Should -Be 1
}
It 'Should only evaluate LHS once when it is NOT null' {
$testState = [pscustomobject]@{ Value = 0 }
(& { 'Test'; [void]$testState.Value++ }) ?? 'Nothing' | Should -BeExactly 'Test'
$testState.Value | Should -Be 1
}
}
Context 'Null Coalesce ?? operator precedence' {
It '?? precedence over -and' {
$true -and $null ?? $true | Should -BeTrue
}
It '?? precedence over -band' {
1 -band $null ?? 1 | Should -Be 1
}
It '?? precedence over -eq' {
'x' -eq $null ?? 'x' | Should -BeTrue
$null -eq $null ?? 'x' | Should -BeFalse
}
It '?? precedence over -as' {
'abc' -as [datetime] ?? 1 | Should -BeNullOrEmpty
}
It '?? precedence over -replace' {
'x' -replace 'x',$null ?? 1 | Should -Be ([string]::empty)
}
It '+ precedence over ??' {
2 + $null ?? 3 | Should -Be 2
}
It '* precedence over ??' {
2 * $null ?? 3 | Should -Be 0
}
It '-f precedence over ??' {
"{0}" -f $null ?? 'b' | Should -Be ([string]::empty)
}
It '.. precedence ove ??' {
1..$null ?? 2 | Should -BeIn 1,0
}
}
Context 'Combined usage of null conditional operators' {
BeforeAll {
function GetNull {
return $null
}
function GetHello {
return "Hello"
}
}
BeforeEach {
$x = $null
}
It '?? and ??= used together' {
$x ??= 100 ?? 200
$x | Should -Be 100
}
It '?? and ??= chaining' {
$x ??= $x ?? (GetNull) ?? (GetHello)
$x | Should -BeExactly 'Hello'
}
It 'First two are null' {
$z ??= $null ?? 100
$z | Should -Be 100
}
}
}
Describe 'NullConditionalMemberAccess' -Tag 'CI' {
Context '?. operator tests' {
BeforeAll {
$psObj = [psobject]::new()
$psObj | Add-Member -Name 'name' -Value 'value' -MemberType NoteProperty
$psObj | Add-Member -Name 'nested' -Value @{name = 'valuenested'} -MemberType NoteProperty
$psobj2 = [psobject]::new()
$psobj2 | Add-Member -Name 'GetHello' -Value { "hello" } -MemberType ScriptMethod
$psObj | Add-Member -Name 'nestedMethod' -Value $psobj2 -MemberType NoteProperty
$array = 1..3
$hash = @{ a = 1; b = 2}
$null = New-Item -ItemType File -Path "$TestDrive/testfile.txt" -Force
}
It 'Can get member value of a non-null variable' {
${psObj}?.name | Should -BeExactly 'value'
${array}?.length | Should -Be 3
${hash}?.a | Should -Be 1
(Get-Item $TestDrive)?.EnumerateFiles()?.Name | Should -BeExactly 'testfile.txt'
[int32]::MaxValue?.ToString() | Should -BeExactly '2147483647'
}
It 'Can get null when variable is null' {
${nonExistent}?.name | Should -BeNullOrEmpty
${nonExistent}?.MyMethod() | Should -BeNullOrEmpty
(get-process -Name doesnotexist -ErrorAction SilentlyContinue)?.Id | Should -BeNullOrEmpty
}
It 'Use ?. operator multiple times in statement' {
${psObj}?.name?.nonExistent | Should -BeNullOrEmpty
${psObj}?.nonExistent?.nonExistent | Should -BeNullOrEmpty
${nonExistent}?.nonExistent?.nonExistent | Should -BeNullOrEmpty
${psObj}?.nested?.name | Should -BeExactly 'valuenested'
${psObj}?.nestedMethod?.GetHello() | Should -BeExactly 'hello'
}
It 'Use ?. on a dynamic method name' {
$methodName = 'ToLongDateString'
(Get-Date '11/11/2019')?.$methodName() | Should -BeExactly 'Monday, November 11, 2019'
${doesNotExist}?.$methodName() | Should -BeNullOrEmpty
}
It 'Use ?. on a dynamic method name that does not exist' {
$methodName = 'DoesNotExist'
{ (Get-Date '11/11/2019')?.$methodName() } | Should -Throw -ErrorId 'MethodNotFound'
}
It 'Use ?. on a dynamic method name that does not exist' {
$methodName = $null
{ (Get-Date '11/11/2019')?.$methodName() } | Should -Throw -ErrorId 'MethodNotFound'
}
It 'Use ?. on a dynamic property name' {
$propName = 'SI'
(Get-Process -Id $PID)?.$propName | Should -Be (Get-Process -id $PID).SessionId
${doesNotExist}?.$propName() | Should -BeNullOrEmpty
}
It 'Should throw error when method does not exist' {
{ ${psObj}?.nestedMethod?.NonExistent() } | Should -Throw -ErrorId 'MethodNotFound'
}
}
Context '?[] operator tests' {
BeforeAll {
$array = 1..3
$hash = @{ a = 1; b = 2}
$dateArray = @(
(Get-Date '11/1/2019'),
(Get-Date '11/2/2019'),
(Get-Date '11/3/2019'))
}
It 'Can index can call properties' {
${array}?[0] | Should -Be 1
${array}?[0,1] | Should -Be @(1,2)
${array}?[0..2] | Should -Be @(1,2,3)
${array}?[-2] | Should -Be 2
${hash}?['a'] | Should -Be 1
}
It 'Indexing in null items should be null' {
${doesnotExist}?[0] | Should -BeNullOrEmpty
${doesnotExist}?[0,1] | Should -BeNullOrEmpty
${doesnotExist}?[0..2] | Should -BeNullOrEmpty
${doesnotExist}?[-2] | Should -BeNullOrEmpty
${doesnotExist}?['a'] | Should -BeNullOrEmpty
}
It 'Can call methods on indexed items' {
${dateArray}?[0]?.ToLongDateString() | Should -BeExactly 'Friday, November 1, 2019'
}
It 'Calling a method on nonexistent item give null' {
${dateArray}?[1234]?.ToLongDateString() | Should -BeNullOrEmpty
${doesNotExist}?[0]?.MyGetMethod() | Should -BeNullOrEmpty
}
}
}
|