From 5fbde6eb401da40c4f5a3e97f5ccfa860c852d28 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 3 Oct 2016 15:24:39 -0700 Subject: [PATCH] Fix bytewise operators to work on enum types with 'System.Byte' as the underlying type (#2411) --- .../engine/runtime/Binding/Binders.cs | 8 ++-- .../Operators/ComparisonOperator.Tests.ps1 | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs index 7449aef575..f84ce6e55a 100644 --- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs +++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs @@ -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)); } } diff --git a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 index acc247a992..1b9db849f9 100644 --- a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 @@ -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" + } +}