Functions/Assertions/BeGreaterThan.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function PesterBeGreaterThan($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because)
{
    if ($Negate) {
        return PesterBeLessOrEqual -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Negate:$false -Because $Because
    }

    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      = $true
    }
}


function PesterBeLessOrEqual($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because)
{
    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() { }