From 150d74d2064637d3196eded4ab13994c4940b07e Mon Sep 17 00:00:00 2001 From: "Steve Lee [MSFT]" Date: Mon, 3 Jul 2017 15:16:31 -0700 Subject: [PATCH 1/2] added test coverage for -notcontains, -is, -isnot operators --- .../Operators/ComparisonOperator.Tests.ps1 | 43 +++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 index 16ee0a67758..bc035cc5289 100644 --- a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 @@ -47,9 +47,13 @@ Describe "ComparisonOperator" -tag "CI" { !0 | Should Be $true } - It "Should be $true for 'Hello','world' -contains 'Hello'" { - $arr= 'Hello','world' - $arr -contains 'Hello' | Should Be $true + It "Should be for 'Hello','world' " -TestCases @( + @{result = $true; operator = "-contains"; rhs = "'Hello'"}, + @{result = $false; operator = "-notcontains"; rhs = "'Hello'"} + ) { + param($result, $operator, $rhs) + $arr= "'Hello','world'" + Invoke-Expression "$arr $operator $rhs" | Should Be $result } It "Should be $false for 'Hello','world' -ccontains 'hello' and $true for 'Hello','world' -ccontains 'Hello'" { $arr= 'Hello','world' @@ -68,6 +72,39 @@ Describe "ComparisonOperator" -tag "CI" { It "Should be $false for 'Hello world' -notlike 'Hello*'" { "Hello world" -notlike "Hello*" | Should Be $false } + + # -Is/-IsNot operator + It "Should return error if right hand is not a valid type: 'hello' " -TestCases @( + @{operator = "-is"; type = "'foo'"}, + @{operator = "-isnot"; type = "'foo'"}, + @{operator = "-is"; type = "[foo]"}, + @{operator = "-isnot"; type = "[foo]"} + ) { + param($operator, $type) + { Invoke-Expression "'Hello' $operator $type" | Should Be ErrorId "RuntimeException" } + } + + It "Should succeed in comparing type: " -TestCases @( + @{lhs = '[pscustomobject]@{foo=1}'; operator = '-is'; rhs = '[pscustomobject]'}, + @{lhs = '[pscustomobject]@{foo=1}'; operator = '-is'; rhs = '[psobject]'}, + @{lhs = '"hello"'; operator = '-is'; rhs = "[string]"}, + @{lhs = '"hello"'; operator = '-is'; rhs = "[system.string]"}, + @{lhs = '100'; operator = '-is'; rhs = "[int]"}, + @{lhs = '100'; operator = '-is'; rhs = "[system.int32]"}, + @{lhs = '"hello"'; operator = '-isnot'; rhs = "[int]"} + ) { + param($lhs, $operator, $rhs) + Invoke-Expression "$lhs $operator $rhs" | Should Be $true + } + + It "Should fail in comparing type: " -TestCases @( + @{lhs = '[pscustomobject]@{foo=1}'; operator = '-is'; rhs = '[string]'}, + @{lhs = '"hello"'; operator = '-is'; rhs = "[psobject]"}, + @{lhs = '"hello"'; operator = '-isnot'; rhs = "[string]"} + ) { + param($lhs, $operator, $rhs) + Invoke-Expression "$lhs $operator $rhs" | Should Be $false + } } From aba74b6f0794825d5bbf54ce24d3064e9ee5049a Mon Sep 17 00:00:00 2001 From: SteveL-MSFT Date: Fri, 7 Jul 2017 09:34:30 -0700 Subject: [PATCH 2/2] cleaned up tests to use -TestCases, added missing -cnotcontains test --- .../Operators/ComparisonOperator.Tests.ps1 | 112 +++++++----------- 1 file changed, 43 insertions(+), 69 deletions(-) diff --git a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 index bc035cc5289..703b8c203a7 100644 --- a/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 +++ b/test/powershell/Language/Operators/ComparisonOperator.Tests.ps1 @@ -1,79 +1,54 @@ Describe "ComparisonOperator" -tag "CI" { - It "Should be $true for 1 -lt 2" { - 1 -lt 2 | Should Be $true - } - It "Should be $false for 1 -gt 2" { - 1 -gt 2 | Should Be $false - } - It "Should be $true for 1 -le 2" { - 1 -le 2 | Should Be $true - } - It "Should be $true for 1 -le 1" { - 1 -le 1 | Should Be $true - } - It "Should be $false for 1 -ge 2" { - 1 -ge 2 | Should Be $false - } - It "Should be $true for 1 -ge 1" { - 1 -ge 1 | Should Be $true - } - It "Should be $true for 1 -eq 1" { - 1 -eq 1 | Should Be $true - } - It "Should be $true for 'abc' -ceq 'abc' and $false for 'abc' -ceq 'Abc'" { - 'abc' -ceq 'abc' | Should Be $true - 'abc' -ceq 'Abc' | Should Be $false - } - It "Should be $true for 1 -ne 2" { - 1 -ne 2 | Should Be $true - } - It "Should be $true for 1 -and 1, $false for 1 -and 0, $false for 0 -and 0" { - 1 -and 1 | Should Be $true - 1 -and 0 | Should Be $false - 0 -and 0 | Should Be $false - } - It "Should be $true for 1 -or 1, $true for 1 -or 0, $false for 0 -or 0" { - 1 -or 1 | Should Be $true - 1 -or 0 | Should Be $true - 0 -or 0 | Should Be $false - } - It "Should be $false for -not 1, $true for -not 0" { - -not 1 | Should Be $false - -not 0 | Should Be $true - } - It "Should be $false for !1, $true for !0" { - !1 | Should Be $false - !0 | Should Be $true + It "Should be for " -TestCases @( + @{lhs = 1; operator = "-lt"; rhs = 2; result = $true}, + @{lhs = 1; operator = "-gt"; rhs = 2; result = $false}, + @{lhs = 1; operator = "-le"; rhs = 2; result = $true}, + @{lhs = 1; operator = "-le"; rhs = 1; result = $true}, + @{lhs = 1; operator = "-ge"; rhs = 2; result = $false}, + @{lhs = 1; operator = "-ge"; rhs = 1; result = $true}, + @{lhs = 1; operator = "-eq"; rhs = 1; result = $true}, + @{lhs = 1; operator = "-ne"; rhs = 2; result = $true}, + @{lhs = "'abc'"; operator = "-ceq"; rhs = "'abc'"; result = $true} + @{lhs = "'abc'"; operator = "-ceq"; rhs = "'Abc'"; result = $false} + @{lhs = 1; operator = "-and"; rhs = 1; result = $true}, + @{lhs = 1; operator = "-and"; rhs = 0; result = $false}, + @{lhs = 0; operator = "-and"; rhs = 0; result = $false}, + @{lhs = 1; operator = "-or"; rhs = 1; result = $true}, + @{lhs = 1; operator = "-or"; rhs = 0; result = $true}, + @{lhs = 0; operator = "-or"; rhs = 0; result = $false} + ) { + param($lhs, $operator, $rhs, $result) + Invoke-Expression "$lhs $operator $rhs" | Should Be $result } - It "Should be for 'Hello','world' " -TestCases @( - @{result = $true; operator = "-contains"; rhs = "'Hello'"}, - @{result = $false; operator = "-notcontains"; rhs = "'Hello'"} + It "Should be for " -TestCases @( + @{operator = "-not "; rhs = "1"; result = $false}, + @{operator = "-not "; rhs = "0"; result = $true}, + @{operator = "! "; rhs = "1"; result = $false}, + @{operator = "! "; rhs = "0"; result = $true}, + @{operator = "!"; rhs = "1"; result = $false}, + @{operator = "!"; rhs = "0"; result = $true} ) { - param($result, $operator, $rhs) - $arr= "'Hello','world'" - Invoke-Expression "$arr $operator $rhs" | Should Be $result - } - It "Should be $false for 'Hello','world' -ccontains 'hello' and $true for 'Hello','world' -ccontains 'Hello'" { - $arr= 'Hello','world' - $arr -ccontains 'hello' | Should Be $false - $arr -ccontains 'Hello' | Should Be $true - } - It "Should be $true for 'Hello world' -match 'Hello*'" { - "Hello world" -match "Hello*" | Should Be $true + param($operator, $rhs, $result) + Invoke-Expression "$operator$rhs" | Should Be $result } - It "Should be $true for 'Hello world' -like 'Hello*'" { - "Hello world" -like "Hello*" | Should Be $true - } - It "Should be $false for 'Hello world' -notmatch 'Hello*'" { - "Hello world" -notmatch "Hello*" | Should Be $false - } - It "Should be $false for 'Hello world' -notlike 'Hello*'" { - "Hello world" -notlike "Hello*" | Should Be $false + + It "Should be for " -TestCases @( + @{lhs = "'Hello'"; operator = "-contains"; rhs = "'Hello'"; result = $true}, + @{lhs = "'Hello'"; operator = "-notcontains"; rhs = "'Hello'"; result = $false}, + @{lhs = "'Hello','world'"; operator = "-ccontains"; rhs = "'hello'"; result = $false}, + @{lhs = "'Hello','world'"; operator = "-ccontains"; rhs = "'Hello'"; result = $true} + @{lhs = "'Hello','world'"; operator = "-cnotcontains"; rhs = "'Hello'"; result = $false} + @{lhs = "'Hello world'"; operator = "-match"; rhs = "'Hello*'"; result = $true}, + @{lhs = "'Hello world'"; operator = "-like"; rhs = "'Hello*'"; result = $true}, + @{lhs = "'Hello world'"; operator = "-notmatch"; rhs = "'Hello*'"; result = $false}, + @{lhs = "'Hello world'"; operator = "-notlike"; rhs = "'Hello*'"; result = $false} + ) { + param($lhs, $operator, $rhs, $result) + Invoke-Expression "$lhs $operator $rhs" | Should Be $result } - # -Is/-IsNot operator It "Should return error if right hand is not a valid type: 'hello' " -TestCases @( @{operator = "-is"; type = "'foo'"}, @{operator = "-isnot"; type = "'foo'"}, @@ -107,7 +82,6 @@ Describe "ComparisonOperator" -tag "CI" { } } - Describe "Bytewise Operator" -tag "CI" { It "Test -bor on enum with [byte] as underlying type" {