Functions/Assertions/Contain.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function PesterContain($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because)
{
    [bool] $succeeded = $ActualValue -contains $ExpectedValue
    if ($Negate) { $succeeded = -not $succeeded }

    if (-not $succeeded)
    {
        if ($Negate)
        {
            return New-Object psobject -Property @{
                Succeeded      = $false
                FailureMessage = "Expected {$ExpectedValue} to not be found in collection [$($ActualValue -join ',')],$(Format-Because $Because) but it was found."
            }
        } else {
            return New-Object psobject -Property @{
                Succeeded      = $false
                FailureMessage = "Expected {$ExpectedValue} to be found in collection [$($ActualValue -join ',')],$(Format-Because $Because) but it was not found."
            }
        }
    }

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

Add-AssertionOperator -Name Contain `
                      -Test $function:PesterContain `
                      -SupportsArrayInput

function PesterContainFailureMessage() { }
function NotPesterContainFailureMessage() {}