Skip to content
Closed
14 changes: 14 additions & 0 deletions src/System.Management.Automation/engine/lang/parserutils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,13 @@ private static object ReplaceOperatorImpl(ExecutionContext context, string input
/// <returns>The result of the operator.</returns>
Comment thread
KirkMunro marked this conversation as resolved.
internal static object IsOperator(ExecutionContext context, IScriptExtent errorPosition, object left, object right)
{
if (right is null)
{
return left is null
? _TrueObject
: _FalseObject;
}

object lval = PSObject.Base(left);
object rval = PSObject.Base(right);

Expand Down Expand Up @@ -1077,6 +1084,13 @@ internal static object IsOperator(ExecutionContext context, IScriptExtent errorP
/// <returns>The result of the operator.</returns>
internal static object IsNotOperator(ExecutionContext context, IScriptExtent errorPosition, object left, object right)
{
if (right is null)
{
return left is null
? _FalseObject
: _TrueObject;
}

object lval = PSObject.Base(left);
object rval = PSObject.Base(right);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,11 @@ internal static Type ResolveTypeName(ITypeName typeName, IScriptExtent errorPos)

internal static bool IsInstance(object left, object right)
{
if (right is null)
{
return left is null;
Comment thread
KirkMunro marked this conversation as resolved.
Outdated
}

object lval = PSObject.Base(left);
object rval = PSObject.Base(right);

Expand Down
16 changes: 16 additions & 0 deletions test/powershell/Language/Operators/ComparisonOperator.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ Describe "ComparisonOperator" -tag "CI" {
param($lhs, $operator, $rhs)
Invoke-Expression "$lhs $operator $rhs" | Should -BeFalse
}

It 'Should succeed in comparing null: <lhs> <operator> $null' -TestCases @(
@{lhs = '([Array]$null)'; operator = '-is'; rhs = '$null' }
@{lhs = '[pscustomobject]@{foo=1}'; operator = '-isnot'; rhs = '$null' }
) {
param($lhs, $operator, $rhs)
Invoke-Expression "$lhs $operator $rhs" | Should -BeTrue
}

It 'Should fail in comparing null: <lhs> <operator> $null' -TestCases @(
@{lhs = '([Array]$null)'; operator = '-isnot'; rhs = '$null' }
@{lhs = '[pscustomobject]@{foo=1}'; operator = '-is'; rhs = '$null' }
) {
param($lhs, $operator, $rhs)
Invoke-Expression "$lhs $operator $rhs" | Should -BeFalse
}
}

Describe "Bytewise Operator" -tag "CI" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ Describe "Where-Object" -Tags "CI" {
IPAddress = "192.168.0.1"
NumberOfCores = 1
Drives = 'C','D'
NICCards = @('Broadcom')
},
[PSCustomObject]@{
ComputerName = "BGP-5678"
IPAddress = ""
NumberOfCores = 2
Drives = 'C','D','E'
NICCards = $null
},
[PSCustomObject]@{
ComputerName = "MGC-9101"
NumberOfCores = 3
Drives = 'C'
NICCards = $null
}
)
}
Expand Down Expand Up @@ -142,4 +145,15 @@ Describe "Where-Object" -Tags "CI" {
$Result[0] | Should -Be $dynObj
$Result[1] | Should -Be $dynObj
}

It 'Where-Object Prop -Is $null' {
$Result = $Computers | Where-Object NICCards -Is $null
$Result | Should -HaveCount 2
}

It 'Where-Object Prop -IsNot $null' {
$Result = $Computers | Where-Object NICCards -IsNot $null
$Result | Should -HaveCount 1
}

}