MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "ComparisonOperator" -Tag "CI" {4 5 It "Should be <result> for <lhs> <operator> <rhs>" -TestCases @(6 @{lhs = 1; operator = "-lt"; rhs = 2; result = $true},7 @{lhs = 1; operator = "-gt"; rhs = 2; result = $false},8 @{lhs = 1; operator = "-le"; rhs = 2; result = $true},9 @{lhs = 1; operator = "-le"; rhs = 1; result = $true},10 @{lhs = 1; operator = "-ge"; rhs = 2; result = $false},11 @{lhs = 1; operator = "-ge"; rhs = 1; result = $true},12 @{lhs = 1; operator = "-eq"; rhs = 1; result = $true},13 @{lhs = 1; operator = "-ne"; rhs = 2; result = $true},14 @{lhs = "'abc'"; operator = "-ceq"; rhs = "'abc'"; result = $true}15 @{lhs = "'abc'"; operator = "-ceq"; rhs = "'Abc'"; result = $false}16 @{lhs = 1; operator = "-and"; rhs = 1; result = $true},17 @{lhs = 1; operator = "-and"; rhs = 0; result = $false},18 @{lhs = 0; operator = "-and"; rhs = 0; result = $false},19 @{lhs = 1; operator = "-or"; rhs = 1; result = $true},20 @{lhs = 1; operator = "-or"; rhs = 0; result = $true},21 @{lhs = 0; operator = "-or"; rhs = 0; result = $false}22 ) {23 param($lhs, $operator, $rhs, $result)24 Invoke-Expression "$lhs $operator $rhs" | Should -Be $result25 }26 27 It "Should be <result> for <operator> <rhs>" -TestCases @(28 @{operator = "-not "; rhs = "1"; result = $false},29 @{operator = "-not "; rhs = "0"; result = $true},30 @{operator = "! "; rhs = "1"; result = $false},31 @{operator = "! "; rhs = "0"; result = $true},32 @{operator = "!"; rhs = "1"; result = $false},33 @{operator = "!"; rhs = "0"; result = $true}34 ) {35 param($operator, $rhs, $result)36 Invoke-Expression "$operator$rhs" | Should -Be $result37 }38 39 It "Should be <result> for <lhs> <operator> <rhs>" -TestCases @(40 @{lhs = "'Hello'"; operator = "-contains"; rhs = "'Hello'"; result = $true},41 @{lhs = "'Hello'"; operator = "-notcontains"; rhs = "'Hello'"; result = $false},42 @{lhs = "'Hello','world'"; operator = "-ccontains"; rhs = "'hello'"; result = $false},43 @{lhs = "'Hello','world'"; operator = "-ccontains"; rhs = "'Hello'"; result = $true}44 @{lhs = "'Hello','world'"; operator = "-cnotcontains"; rhs = "'Hello'"; result = $false}45 @{lhs = "'Hello world'"; operator = "-match"; rhs = "'Hello*'"; result = $true},46 @{lhs = "'Hello world'"; operator = "-like"; rhs = "'Hello*'"; result = $true},47 @{lhs = "'Hello world'"; operator = "-notmatch"; rhs = "'Hello*'"; result = $false},48 @{lhs = "'Hello world'"; operator = "-notlike"; rhs = "'Hello*'"; result = $false}49 ) {50 param($lhs, $operator, $rhs, $result)51 Invoke-Expression "$lhs $operator $rhs" | Should -Be $result52 }53 54 It "Should return error if right hand is not a valid type: 'hello' <operator> <type>" -TestCases @(55 @{operator = "-is"; type = "'foo'"; expectedError='RuntimeException,Microsoft.PowerShell.Commands.InvokeExpressionCommand'},56 @{operator = "-isnot"; type = "'foo'"; expectedError='RuntimeException,Microsoft.PowerShell.Commands.InvokeExpressionCommand'},57 @{operator = "-is"; type = "[foo]"; expectedError='TypeNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand'},58 @{operator = "-isnot"; type = "[foo]"; expectedError='TypeNotFound,Microsoft.PowerShell.Commands.InvokeExpressionCommand'}59 ) {60 param($operator, $type, $expectedError)61 { Invoke-Expression "'Hello' $operator $type" } | Should -Throw -ErrorId $expectedError62 }63 64 It "Should succeed in comparing type: <lhs> <operator> <rhs>" -TestCases @(65 @{lhs = '[pscustomobject]@{foo=1}'; operator = '-is'; rhs = '[pscustomobject]'},66 @{lhs = '[pscustomobject]@{foo=1}'; operator = '-is'; rhs = '[psobject]'},67 @{lhs = '"hello"'; operator = '-is'; rhs = "[string]"},68 @{lhs = '"hello"'; operator = '-is'; rhs = "[system.string]"},69 @{lhs = '100'; operator = '-is'; rhs = "[int]"},70 @{lhs = '100'; operator = '-is'; rhs = "[system.int32]"},71 @{lhs = '"hello"'; operator = '-isnot'; rhs = "[int]"}72 ) {73 param($lhs, $operator, $rhs)74 Invoke-Expression "$lhs $operator $rhs" | Should -BeTrue75 }76 77 It "Should fail in comparing type: <lhs> <operator> <rhs>" -TestCases @(78 @{lhs = '[pscustomobject]@{foo=1}'; operator = '-is'; rhs = '[string]'},79 @{lhs = '"hello"'; operator = '-is'; rhs = "[psobject]"},80 @{lhs = '"hello"'; operator = '-isnot'; rhs = "[string]"}81 ) {82 param($lhs, $operator, $rhs)83 Invoke-Expression "$lhs $operator $rhs" | Should -BeFalse84 }85 86 It "Should be <result> for backtick comparison <lhs> <operator> <rhs>" -TestCases @(87 @{ lhs = 'abc`def'; operator = '-like'; rhs = 'abc`def'; result = $false }88 @{ lhs = 'abc`def'; operator = '-like'; rhs = 'abc``def'; result = $true }89 @{ lhs = 'abc`def'; operator = '-like'; rhs = 'abc````def'; result = $false }90 @{ lhs = 'abc``def'; operator = '-like'; rhs = 'abc````def'; result = $true }91 @{ lhs = 'abc`def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc`def'); result = $true }92 @{ lhs = 'abc`def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc``def'); result = $false }93 @{ lhs = 'abc``def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc``def'); result = $true }94 @{ lhs = 'abc``def'; operator = '-like'; rhs = [WildcardPattern]::Escape('abc````def'); result = $false }95 ) {96 param($lhs, $operator, $rhs, $result)97 $expression = "'$lhs' $operator '$rhs'"98 Invoke-Expression $expression | Should -Be $result99 }100}101 102Describe "Bytewise Operator" -Tag "CI" {103 104 It "Test -bor on enum with [byte] as underlying type" {105 $result = [System.Security.AccessControl.AceFlags]::ObjectInherit -bxor `106 [System.Security.AccessControl.AceFlags]::ContainerInherit107 $result.ToString() | Should -BeExactly "ObjectInherit, ContainerInherit"108 }109 110 It "Test -bor on enum with [int] as underlying type" {111 $result = [System.Management.Automation.CommandTypes]::Alias -bor `112 [System.Management.Automation.CommandTypes]::Application113 $result.ToString() | Should -BeExactly "Alias, Application"114 }115 116 It "Test -band on enum with [byte] as underlying type" {117 $result = [System.Security.AccessControl.AceFlags]::ObjectInherit -band `118 [System.Security.AccessControl.AceFlags]::ContainerInherit119 $result.ToString() | Should -BeExactly "None"120 }121 122 It "Test -band on enum with [int] as underlying type" {123 $result = [System.Management.Automation.CommandTypes]::Alias -band `124 [System.Management.Automation.CommandTypes]::All125 $result.ToString() | Should -BeExactly "Alias"126 }127 128 It "Test -bxor on enum with [byte] as underlying type" {129 $result = [System.Security.AccessControl.AceFlags]::ObjectInherit -bxor `130 [System.Security.AccessControl.AceFlags]::ContainerInherit131 $result.ToString() | Should -BeExactly "ObjectInherit, ContainerInherit"132 }133 134 It "Test -bxor on enum with [int] as underlying type" {135 $result = [System.Management.Automation.CommandTypes]::Alias -bxor `136 [System.Management.Automation.CommandTypes]::Application137 $result.ToString() | Should -BeExactly "Alias, Application"138 }139}140 