-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Implement Null Coalescing and Null Coalescing assignment operators #10636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
daxian-dbw
merged 24 commits into
PowerShell:master
from
adityapatwardhan:NullAssignment
Oct 17, 2019
Merged
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ecb94dd
Implement null condtional assigment operator
adityapatwardhan 15119cf
Implement ?? operator
adityapatwardhan 0cb1592
Add QuestionDot token
adityapatwardhan cfd73f9
Fix issues after merge and test fix
adityapatwardhan 137a37b
Change Null coalescing assigned operator to ??=
adityapatwardhan 94ad01f
Make feature experimental
adityapatwardhan dee8092
Add logic for skipping tests if experimental feature is disabled
adityapatwardhan 9826e69
Fix parsing tests
adityapatwardhan 9e599c4
Address code review feedback
adityapatwardhan 8fd9572
Remove parsing test as it interfers with ternary operator
adityapatwardhan 02dc105
Add few more tests
adityapatwardhan 4e8019e
Address Rob's feedback
adityapatwardhan a9af2ab
Refactor according to feedback
adityapatwardhan 0e93e14
Add coalesce assignment
adityapatwardhan 771a86d
Add precedence flag for null coalesce operator and add tests
adityapatwardhan 1a4887e
Merge branch 'master' into NullAssignment
adityapatwardhan 73b8982
Revert formatting changes in token.cs
adityapatwardhan 36f4b85
Fix parsing test
adityapatwardhan d530fcd
More test fixes
adityapatwardhan 3f0f3d1
Check for experimental feature in parsing.tests.ps1
adityapatwardhan 8f39bbb
Updated to move Coalesce code out of Binder
adityapatwardhan dea5793
Update TokenFlag name
adityapatwardhan 4cf6ba4
Address feedback
adityapatwardhan 268a609
Address Ilya's feedback
adityapatwardhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
test/powershell/Language/Operators/NullConditional.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
Describe 'NullConditionalOperations' -Tags 'CI' { | ||
BeforeAll { | ||
|
||
$skipTest = -not $EnabledExperimentalFeatures.Contains('PSNullCoalescingOperators') | ||
|
||
if ($skipTest) { | ||
Write-Verbose "Test Suite Skipped. The test suite requires the experimental feature 'PSNullCoalescingOperators' to be enabled." -Verbose | ||
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone() | ||
$PSDefaultParameterValues["it:skip"] = $true | ||
} else { | ||
$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) } | ||
) | ||
} | ||
} | ||
|
||
AfterAll { | ||
if ($skipTest) { | ||
$global:PSDefaultParameterValues = $originalDefaultParameterValues | ||
} | ||
} | ||
|
||
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 { | ||
adityapatwardhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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' { | ||
adityapatwardhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$x = 100 | ||
$x ??= 200 | ||
$x | Should -Be 100 | ||
} | ||
|
||
It 'Rhs is a cmdlet' { | ||
$x ??= (Get-Alias -Name 'where') | ||
adityapatwardhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$x.Definition | Should -BeExactly 'Where-Object' | ||
} | ||
|
||
It 'Lhs is DBNull' { | ||
$x = [System.DBNull]::Value | ||
$x ??= 200 | ||
$x | Should -Be 200 | ||
} | ||
|
||
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 200 | ||
} | ||
|
||
It 'Error case' { | ||
$e = $null | ||
$null = [System.Management.Automation.Language.Parser]::ParseInput('1 ??= 100', [ref] $null, [ref] $e) | ||
$e[0].ErrorId | Should -BeExactly 'InvalidLeftHandSide' | ||
} | ||
} | ||
|
||
Context 'Null coalesce operator ??' { | ||
BeforeEach { | ||
$x = $null | ||
} | ||
|
||
It 'Variable does not exist' { | ||
$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' | ||
} | ||
|
||
adityapatwardhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 200 | ||
} | ||
|
||
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 200 | ||
} | ||
|
||
It 'Rhs is a get variable expression' { | ||
$x = [System.DBNull]::Value | ||
$y = 2 | ||
$x ?? $y | Should -Be 2 | ||
} | ||
|
||
It 'Lhs is a constant' { | ||
[System.DBNull]::Value ?? 2 | Should -Be 2 | ||
} | ||
|
||
It 'Both are null constants' { | ||
[System.DBNull]::Value ?? [NullString]::Value | Should -Be ([NullString]::Value) | ||
} | ||
} | ||
|
||
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' | ||
} | ||
adityapatwardhan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.