mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Ensure null-coalescing LHS is evaluated only once (#12667)
This commit is contained in:
@@ -172,6 +172,18 @@ Describe 'NullCoalesceOperations' -Tags 'CI' {
|
||||
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' {
|
||||
|
||||
Reference in New Issue
Block a user