Skip to content

Commit

Permalink
Add more assertion (pester#959)
Browse files Browse the repository at this point in the history
changelog:

Add -Because parameters to all assertions Fixes pester#312
Add -BeLessOrEqual and -BeGreaterOrEqual
Add -Contain (that operates on arrays) Fixes pester#121
Add -BeLikeExactly
Add -HaveType alias to -BeOfType
Fix assertion messages in -BeOfType Fixes pester#729
Throw argument exception when -BeOfType is given type that is not loaded
Add -PassThru to -Throw to get the exception when some is thrown and passes the filters
Add -BeTrue to test for truthy values
Add -BeFalse to test for falsy values
Add -HaveCount to count stuff in collections
  • Loading branch information
nohwnd authored Dec 17, 2017
1 parent f5d2286 commit 86a9141
Show file tree
Hide file tree
Showing 39 changed files with 1,113 additions and 267 deletions.
32 changes: 16 additions & 16 deletions Functions/Assertions/Be.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -115,38 +115,39 @@ InModuleScope Pester {
#only when the objects are not equal

$string = "string"
PesterBeFailureMessage $string $string | Should BeNullOrEmpty
PesterBeFailureMessage $string $string | Should -BeNullOrEmpty
PesterBeFailureMessage $string $string | Verify-Equal ''
}

It "Outputs less verbose message for two different objects that are not strings" {
PesterBeFailureMessage 2 1 | Should Be "Expected: {1}`nBut was: {2}"
PesterBeFailureMessage 2 1 | Should -Be "Expected: {1}`nBut was: {2}"
PesterBeFailureMessage 2 1 | Verify-Equal "Expected {1}, but got {2}."
}

It "Outputs less verbose message for two different objects that are not strings, with reason" {
PesterBeFailureMessage 2 1 -Because 'reason' | Verify-Equal "Expected {1}, because reason, but got {2}."
}

It "Outputs verbose message for two strings of different length" {
PesterBeFailureMessage "actual" "expected" | Verify-Equal "Expected strings to be the same, but they were different.`nExpected length: 8`nActual length: 6`nStrings differ at index 0.`nExpected: {expected}`nBut was: {actual}`n-----------^"
}

It "Outputs verbose message for two strings of different length" {
PesterBeFailureMessage "actual" "expected" | Should Be "Expected string length 8 but was 6. Strings differ at index 0.`nExpected: {expected}`nBut was: {actual}`n-----------^"
PesterBeFailureMessage "actual" "expected" | Should -Be "Expected string length 8 but was 6. Strings differ at index 0.`nExpected: {expected}`nBut was: {actual}`n-----------^"
PesterBeFailureMessage "actual" "expected" -Because 'reason' | Verify-Equal "Expected strings to be the same, because reason, but they were different.`nExpected length: 8`nActual length: 6`nStrings differ at index 0.`nExpected: {expected}`nBut was: {actual}`n-----------^"
}

It "Outputs verbose message for two different strings of the same length" {
PesterBeFailureMessage "x" "y" | Should Be "String lengths are both 1. Strings differ at index 0.`nExpected: {y}`nBut was: {x}`n-----------^"
PesterBeFailureMessage "x" "y" | Should -Be "String lengths are both 1. Strings differ at index 0.`nExpected: {y}`nBut was: {x}`n-----------^"
PesterBeFailureMessage "x" "y" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 1.`nStrings differ at index 0.`nExpected: {y}`nBut was: {x}`n-----------^"
}

It "Replaces non-printable characters correctly" {
PesterBeFailureMessage "`n`r`b`0`tx" "`n`r`b`0`ty" | Should Be "String lengths are both 6. Strings differ at index 5.`nExpected: {\n\r\b\0\ty}`nBut was: {\n\r\b\0\tx}`n---------------------^"
PesterBeFailureMessage "`n`r`b`0`tx" "`n`r`b`0`ty" | Should -Be "String lengths are both 6. Strings differ at index 5.`nExpected: {\n\r\b\0\ty}`nBut was: {\n\r\b\0\tx}`n---------------------^"
PesterBeFailureMessage "`n`r`b`0`tx" "`n`r`b`0`ty" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 6.`nStrings differ at index 5.`nExpected: {\n\r\b\0\ty}`nBut was: {\n\r\b\0\tx}`n---------------------^"
}

It "The arrow points to the correct position when non-printable characters are replaced before the difference" {
PesterBeFailureMessage "123`n456" "123`n789" | Should Be "String lengths are both 7. Strings differ at index 4.`nExpected: {123\n789}`nBut was: {123\n456}`n----------------^"
PesterBeFailureMessage "123`n456" "123`n789" | Should -Be "String lengths are both 7. Strings differ at index 4.`nExpected: {123\n789}`nBut was: {123\n456}`n----------------^"
PesterBeFailureMessage "123`n456" "123`n789" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 7.`nStrings differ at index 4.`nExpected: {123\n789}`nBut was: {123\n456}`n----------------^"
}

It "The arrow points to the correct position when non-printable characters are replaced after the difference" {
PesterBeFailureMessage "abcd`n123" "abc!`n123" | Should Be "String lengths are both 8. Strings differ at index 3.`nExpected: {abc!\n123}`nBut was: {abcd\n123}`n--------------^"
PesterBeFailureMessage "abcd`n123" "abc!`n123" | Should -Be "String lengths are both 8. Strings differ at index 3.`nExpected: {abc!\n123}`nBut was: {abcd\n123}`n--------------^"
PesterBeFailureMessage "abcd`n123" "abc!`n123" | Verify-Equal "Expected strings to be the same, but they were different.`nString lengths are both 8.`nStrings differ at index 3.`nExpected: {abc!\n123}`nBut was: {abcd\n123}`n--------------^"
}
}
}
Expand Down Expand Up @@ -188,8 +189,7 @@ InModuleScope Pester {

Describe "PesterBeExactlyFailureMessage" {
It "Writes verbose message for strings that differ by case" {
PesterBeExactlyFailureMessage "a" "A" | Should Be "String lengths are both 1. Strings differ at index 0.`nExpected: {A}`nBut was: {a}`n-----------^"
PesterBeExactlyFailureMessage "a" "A" | Should -Be "String lengths are both 1. Strings differ at index 0.`nExpected: {A}`nBut was: {a}`n-----------^"
PesterBeExactlyFailureMessage "a" "A" -Because "reason" | Verify-Equal "Expected strings to be the same, because reason, but they were different.`nString lengths are both 1.`nStrings differ at index 0.`nExpected: {A}`nBut was: {a}`n-----------^"
}
}
}
44 changes: 25 additions & 19 deletions Functions/Assertions/Be.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Be
function PesterBe($ActualValue, $ExpectedValue, [switch] $Negate) {
function PesterBe($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because) {
[bool] $succeeded = ArraysAreEqual $ActualValue $ExpectedValue

if ($Negate) { $succeeded = -not $succeeded }
Expand All @@ -10,11 +10,11 @@ function PesterBe($ActualValue, $ExpectedValue, [switch] $Negate) {
{
if ($Negate)
{
$failureMessage = NotPesterBeFailureMessage -ActualValue $ActualValue -Expected $ExpectedValue
$failureMessage = NotPesterBeFailureMessage -ActualValue $ActualValue -Expected $ExpectedValue -Because $Because
}
else
{
$failureMessage = PesterBeFailureMessage -ActualValue $ActualValue -Expected $ExpectedValue
$failureMessage = PesterBeFailureMessage -ActualValue $ActualValue -Expected $ExpectedValue -Because $Because
}
}

Expand All @@ -24,14 +24,14 @@ function PesterBe($ActualValue, $ExpectedValue, [switch] $Negate) {
}
}

function PesterBeFailureMessage($ActualValue, $ExpectedValue) {
function PesterBeFailureMessage($ActualValue, $ExpectedValue, $Because) {
# This looks odd; it's to unroll single-element arrays so the "-is [string]" expression works properly.
$ActualValue = $($ActualValue)
$ExpectedValue = $($ExpectedValue)

if (-not (($ExpectedValue -is [string]) -and ($ActualValue -is [string])))
{
return "Expected: {$ExpectedValue}`nBut was: {$ActualValue}"
return "Expected {$ExpectedValue},$(Format-Because $Because) but got {$ActualValue}."
}
<#joining the output strings to a single string here, otherwise I get
Cannot find an overload for "Exception" and the argument count: "4".
Expand All @@ -40,11 +40,11 @@ function PesterBeFailureMessage($ActualValue, $ExpectedValue) {
This is a quickwin solution, doing the join in the Should directly might be better
way of doing this. But I don't want to mix two problems.
#>
( Get-CompareStringMessage -Expected $ExpectedValue -Actual $ActualValue ) -join "`n"
(Get-CompareStringMessage -Expected $ExpectedValue -Actual $ActualValue -Because $Because) -join "`n"
}

function NotPesterBeFailureMessage($ActualValue, $ExpectedValue) {
return "Expected: value was {$ActualValue}, but should not have been the same"
function NotPesterBeFailureMessage($ActualValue, $ExpectedValue, $Because) {
return "Expected {$ExpectedValue} to be different from the actual value,$(Format-Because $Because) but got the same value."
}

Add-AssertionOperator -Name Be `
Expand All @@ -53,7 +53,7 @@ Add-AssertionOperator -Name Be `
-SupportsArrayInput

#BeExactly
function PesterBeExactly($ActualValue, $ExpectedValue) {
function PesterBeExactly($ActualValue, $ExpectedValue, $Because) {
[bool] $succeeded = ArraysAreEqual $ActualValue $ExpectedValue -CaseSensitive

if ($Negate) { $succeeded = -not $succeeded }
Expand All @@ -64,11 +64,11 @@ function PesterBeExactly($ActualValue, $ExpectedValue) {
{
if ($Negate)
{
$failureMessage = NotPesterBeExactlyFailureMessage -ActualValue $ActualValue -ExpectedValue $ExpectedValue
$failureMessage = NotPesterBeExactlyFailureMessage -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Because $Because
}
else
{
$failureMessage = PesterBeExactlyFailureMessage -ActualValue $ActualValue -ExpectedValue $ExpectedValue
$failureMessage = PesterBeExactlyFailureMessage -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Because $Because
}
}

Expand All @@ -78,14 +78,14 @@ function PesterBeExactly($ActualValue, $ExpectedValue) {
}
}

function PesterBeExactlyFailureMessage($ActualValue, $ExpectedValue) {
function PesterBeExactlyFailureMessage($ActualValue, $ExpectedValue, $Because) {
# This looks odd; it's to unroll single-element arrays so the "-is [string]" expression works properly.
$ActualValue = $($ActualValue)
$ExpectedValue = $($ExpectedValue)

if (-not (($ExpectedValue -is [string]) -and ($ActualValue -is [string])))
{
return "Expected exactly: {$ExpectedValue}`nBut was: {$ActualValue}"
return "Expected exactly {$ExpectedValue},$(Format-Because $Because) but got {$ActualValue}."
}
<#joining the output strings to a single string here, otherwise I get
Cannot find an overload for "Exception" and the argument count: "4".
Expand All @@ -94,11 +94,11 @@ function PesterBeExactlyFailureMessage($ActualValue, $ExpectedValue) {
This is a quickwin solution, doing the join in the Should directly might be better
way of doing this. But I don't want to mix two problems.
#>
( Get-CompareStringMessage -Expected $ExpectedValue -Actual $ActualValue -CaseSensitive ) -join "`n"
(Get-CompareStringMessage -Expected $ExpectedValue -Actual $ActualValue -CaseSensitive -Because $Because) -join "`n"
}

function NotPesterBeExactlyFailureMessage($ActualValue, $ExpectedValue) {
return "Expected: value was {$ActualValue}, but should not have been exactly the same"
function NotPesterBeExactlyFailureMessage($ActualValue, $ExpectedValue, $Because) {
return "Expected {$ExpectedValue} to be different from the actual value,$(Format-Because $Because) but got exactly the same value."
}

Add-AssertionOperator -Name BeExactly `
Expand All @@ -116,7 +116,8 @@ function Get-CompareStringMessage {
[Parameter(Mandatory=$true)]
[AllowEmptyString()]
[String]$Actual,
[switch]$CaseSensitive
[switch]$CaseSensitive,
$Because
)

$ExpectedValueLength = $ExpectedValue.Length
Expand All @@ -138,12 +139,17 @@ function Get-CompareStringMessage {
[string]$output = $null
if ($null -ne $differenceIndex)
{
"Expected strings to be the same,$(Format-Because $Because) but they were different."

if ($ExpectedValue.Length -ne $actual.Length) {
"Expected string length $ExpectedValueLength but was $actualLength. Strings differ at index $differenceIndex."
"Expected length: $ExpectedValueLength"
"Actual length: $actualLength"
"Strings differ at index $differenceIndex."
}
else
{
"String lengths are both $ExpectedValueLength. Strings differ at index $differenceIndex."
"String lengths are both $ExpectedValueLength."
"Strings differ at index $differenceIndex."
}

"Expected: {{{0}}}" -f ( $ExpectedValue | Expand-SpecialCharacters )
Expand Down
87 changes: 80 additions & 7 deletions Functions/Assertions/BeGreaterThan.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,24 +1,97 @@
Set-StrictMode -Version Latest

InModuleScope Pester {
Describe "PesterBeGreaterThan" {
Describe "Should -BeGreaterThan" {
It "passes if value greater than expected" {
2 | Should BeGreaterThan 1
2 | Should -BeGreaterThan 1
2 | Should -GT 1
}

It "fails if values equal" {
3 | Should Not BeGreaterThan 3
3 | Should -Not -BeGreaterThan 3
3 | Should -Not -GT 3
{ 3 | Should BeGreaterThan 3 } | Verify-AssertionFailed
{ 3 | Should -BeGreaterThan 3 } | Verify-AssertionFailed
{ 3 | Should -GT 3 } | Verify-AssertionFailed
}

It "fails if value less than expected" {
4 | Should Not BeGreaterThan 5
4 | Should -Not -BeGreaterThan 5
4 | Should -Not -GT 5
{ 4 | Should BeGreaterThan 5 } | Verify-AssertionFailed
{ 4 | Should -BeGreaterThan 5 } | Verify-AssertionFailed
{ 4 | Should -GT 5 } | Verify-AssertionFailed
}

It "returns the correct assertion message" {
$err = { 4 | Should -BeGreaterThan 5 -Because 'reason' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal 'Expected {5} to be greater than the actual value, because reason, but got {4}.'
}
}

Describe "Should -Not -BeGreaterThan" {
It "passes if value is lower than the expected value" {
0 | Should Not BeGreaterThan 1
0 | Should -Not -BeGreaterThan 1
0 | Should -Not -GT 1
}

It "passes if value is equal to the expected value" {
1 | Should Not BeGreaterThan 1
1 | Should -Not -BeGreaterThan 1
1 | Should -Not -GT 1
}

It "fails if value is greater than the expected value" {
{ 4 | Should Not BeGreaterThan 3 } | Verify-AssertionFailed
{ 4 | Should -Not -BeGreaterThan 3 } | Verify-AssertionFailed
{ 4 | Should -Not -GT 3 } | Verify-AssertionFailed
}

It "returns the correct assertion message" {
$err = { 6 | Should -Not -BeGreaterThan 5 -Because 'reason' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal 'Expected {5} to be less or equal to the actual value, because reason, but got {6}.'
}
}

Describe "Should -BeLessOrEqual" {
It "passes if value is less than the expected value" {
0 | Should -BeLessOrEqual 1
0 | Should -LE 1
}

It "passes if value is equal to the expected value" {
1 | Should -BeLessOrEqual 1
1 | Should -LE 1
}

It "fails if value is greater than the expected value" {
{ 4 | Should -BeLessOrEqual 3 } | Verify-AssertionFailed
{ 4 | Should -LE 3 } | Verify-AssertionFailed
}

It "returns the correct assertion message" {
$err = { 6 | Should -BeLessOrEqual 5 -Because 'reason' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal 'Expected {5} to be less or equal to the actual value, because reason, but got {6}.'
}

Describe "Should -Not -BeLessOrEqual" {
It "passes if value greater than expected" {
2 | Should -Not -BeLessOrEqual 1
2 | Should -Not -LE 1
}

It "fails if values equal" {
{ 3 | Should -Not -BeLessOrEqual 3 } | Verify-AssertionFailed
{ 3 | Should -Not -LE 3 } | Verify-AssertionFailed
}

It "fails if value less than expected" {
{ 4 | Should -Not -BeLessOrEqual 5 } | Verify-AssertionFailed
{ 4 | Should -Not -LE 5 } | Verify-AssertionFailed
}

It "returns the correct assertion message" {
$err = { 4 | Should -Not -BeLessOrEqual 5 -Because 'reason' } | Verify-AssertionFailed
$err.Exception.Message | Verify-Equal 'Expected {5} to be greater than the actual value, because reason, but got {4}.'
}
}
}
}
60 changes: 38 additions & 22 deletions Functions/Assertions/BeGreaterThan.ps1
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
function PesterBeGreaterThan($ActualValue, $ExpectedValue, [switch] $Negate)
function PesterBeGreaterThan($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because)
{
[bool] $succeeded = $ActualValue -gt $ExpectedValue
if ($Negate) { $succeeded = -not $succeeded }

$failureMessage = ''
if ($Negate) {
return PesterBeLessOrEqual -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Negate:$false -Because $Because
}

if (-not $succeeded)
{
if ($Negate)
{
$failureMessage = NotPesterBeGreaterThanFailureMessage -ActualValue $ActualValue -ExpectedValue $ExpectedValue
}
else
{
$failureMessage = PesterBeGreaterThanFailureMessage -ActualValue $ActualValue -ExpectedValue $ExpectedValue
if ($ExpectedValue -ge $ActualValue) {
return New-Object psobject -Property @{
Succeeded = $false
FailureMessage = "Expected {$ExpectedValue} to be greater than the actual value,$(Format-Because $Because) but got {$ActualValue}."
}
}

return New-Object psobject -Property @{
Succeeded = $succeeded
FailureMessage = $failureMessage
Succeeded = $true
}
}

function PesterBeGreaterThanFailureMessage($ActualValue,$ExpectedValue)
{
return "Expected {$ActualValue} to be greater than {$ExpectedValue}"
}

function NotPesterBeGreaterThanFailureMessage($ActualValue,$ExpectedValue)
function PesterBeLessOrEqual($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because)
{
return "Expected {$ActualValue} to be less than or equal to {$ExpectedValue}"
if ($Negate) {
return PesterBeGreaterThan -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Negate:$false -Because $Because
}

if ($ExpectedValue -lt $ActualValue) {
return New-Object psobject -Property @{
Succeeded = $false
FailureMessage = "Expected {$ExpectedValue} to be less or equal to the actual value,$(Format-Because $Because) but got {$ActualValue}."
}
}

return New-Object psobject -Property @{
Succeeded = $true
}
}

Add-AssertionOperator -Name BeGreaterThan `
-Test $function:PesterBeGreaterThan `
-Alias 'GT'

Add-AssertionOperator -Name BeLessOrEqual `
-Test $function:PesterBeLessOrEqual `
-Alias 'LE'

#keeping tests happy
function PesterBeGreaterThanFailureMessage() { }
function NotPesterBeGreaterThanFailureMessage() { }

function PesterBeLessOrEqualFailureMessage() { }
function NotPesterBeLessOrEqualFailureMessage() { }



Loading

0 comments on commit 86a9141

Please sign in to comment.