Fix bytewise operators to work on enum types with 'System.Byte' as the underlying type (#2411)

This commit is contained in:
Dongbo Wang 2016-10-03 15:24:39 -07:00 committed by Jason Shirk
parent f62dd4bf54
commit 5fbde6eb40
2 changed files with 45 additions and 3 deletions

View file

@ -2940,13 +2940,15 @@ namespace System.Management.Automation.Language
if (numericTarget != null && numericArg != null)
{
var expr = exprGenerator(numericTarget.Expression.Cast(target.LimitType).Cast(opType),
numericArg.Expression.Cast(numericArg.LimitType).Cast(opType)).Cast(typeof(object));
var expr = exprGenerator(numericTarget.Expression.Cast(numericTarget.LimitType).Cast(opType),
numericArg.Expression.Cast(numericArg.LimitType).Cast(opType));
if (target.LimitType.GetTypeInfo().IsEnum)
{
expr = expr.Cast(target.LimitType).Cast(typeof(object));
expr = expr.Cast(target.LimitType);
}
expr = expr.Cast(typeof(object));
return new DynamicMetaObject(expr, numericTarget.CombineRestrictions(numericArg));
}
}

View file

@ -69,3 +69,43 @@ Describe "ComparisonOperator" -tag "CI" {
"Hello world" -notlike "Hello*" | Should Be $false
}
}
Describe "Bytewise Operator" -tag "CI" {
It "Test -bor on enum with [byte] as underlying type" {
$result = [System.Security.AccessControl.AceFlags]::ObjectInherit -bxor `
[System.Security.AccessControl.AceFlags]::ContainerInherit
$result.ToString() | Should Be "ObjectInherit, ContainerInherit"
}
It "Test -bor on enum with [int] as underlying type" {
$result = [System.Management.Automation.CommandTypes]::Alias -bor `
[System.Management.Automation.CommandTypes]::Application
$result.ToString() | Should Be "Alias, Application"
}
It "Test -band on enum with [byte] as underlying type" {
$result = [System.Security.AccessControl.AceFlags]::ObjectInherit -band `
[System.Security.AccessControl.AceFlags]::ContainerInherit
$result.ToString() | Should Be "None"
}
It "Test -band on enum with [int] as underlying type" {
$result = [System.Management.Automation.CommandTypes]::Alias -band `
[System.Management.Automation.CommandTypes]::All
$result.ToString() | Should Be "Alias"
}
It "Test -bxor on enum with [byte] as underlying type" {
$result = [System.Security.AccessControl.AceFlags]::ObjectInherit -bxor `
[System.Security.AccessControl.AceFlags]::ContainerInherit
$result.ToString() | Should Be "ObjectInherit, ContainerInherit"
}
It "Test -bxor on enum with [int] as underlying type" {
$result = [System.Management.Automation.CommandTypes]::Alias -bxor `
[System.Management.Automation.CommandTypes]::Application
$result.ToString() | Should Be "Alias, Application"
}
}