# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. Describe "Using of ternary operator" -Tags CI { BeforeAll { $testCases = @( ## Condition: variable and constant expressions @{ Script = { $true ? 1 : 2 }; ExpectedValue = 1 } @{ Script = { $true? ?1 :2 }; ExpectedValue = 2 } @{ Script = { ${true}?1:2 }; ExpectedValue = 1 } @{ Script = { 1 ? 1kb : 0xf }; ExpectedValue = 1kb } @{ Script = { 0 ?1kb:0xf }; ExpectedValue = 15 } @{ Script = { 's' ?1kb:0xf }; ExpectedValue = 1kb } @{ Script = { $null ?1kb:0xf }; ExpectedValue = 15 } @{ Script = { '' ?1kb:0xf }; ExpectedValue = 15 } ## Condition: other primary expressions @{ Script = { 1,2,3,4 ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { @(1,2,3,4) ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { @{name = 'name'} ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { @{name = 'name'}.name ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { @{name = 'name'}.Contains('name') ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { (Test-Path Env:\NonExist) ? 'true' : 'false' }; ExpectedValue = 'false' } @{ Script = { (Test-Path Env:\PSModulePath) ? 'true' : 'false' }; ExpectedValue = 'true' } @{ Script = { $($p = Get-Process -Id $PID; $p.Id -eq $PID) ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { ($a = 1) ? 2 : 3 }; ExpectedValue = 2 } @{ Script = { $($a = 1) ? 2 : 3 }; ExpectedValue = 3 } @{ Script = { (Write-Warning -Message warning -WarningAction SilentlyContinue) ? 1 : 2 }; ExpectedValue = 2 } @{ Script = { (Write-Error -Message error -ErrorAction SilentlyContinue) ? 1 : 2 }; ExpectedValue = 2 } ## Condition: unary and binary expression expressions @{ Script = { -not $IsCoreCLR ? 'Desktop' : 'Core' }; ExpectedValue = 'Core' } @{ Script = { $PSEdition -eq 'Core' ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { $IsCoreCLR -and (Get-Process -Id $PID).Id -eq $PID ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { $IsCoreCLR -and 'pwsh' -match 'p.*h' ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } @{ Script = { 1,2,3 -contains 2 ? 'Core' : 'Desktop' }; ExpectedValue = 'Core' } ## Nested ternary expressions @{ Script = { $IsCoreCLR ? $false ? 'nested-if-true' : 'nested-if-false' : 'if-false' }; ExpectedValue = 'nested-if-false' } @{ Script = { $IsCoreCLR ? $false ? 'nested-if-true' : $true ? 'nested-nested-if-true' : 'nested-nested-if-false' : 'if-false' }; ExpectedValue = 'nested-nested-if-true' } ## Binary operator has higher precedence order than ternary @{ Script = { !$IsCoreCLR ? 'Core' : 'Desktop' -EQ 'Core' }; ExpectedValue = !$IsCoreCLR ? 'Core' : ('Desktop' -eq 'Core') } @{ Script = { ($IsCoreCLR ? 'Core' : 'Desktop') -eq 'Core' }; ExpectedValue = $true } ) } AfterAll { if ($skipTest) { $global:PSDefaultParameterValues = $originalDefaultParameterValues } } It "Ternary expression '