Functions/Assertions/BeLessThan.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
function PesterBeLessThan($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because)
{
    if ($Negate) {
        return PesterBeGreaterOrEqual -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Negate:$false -Because $Because
    }

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

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


function PesterBeGreaterOrEqual($ActualValue, $ExpectedValue, [switch] $Negate, [string] $Because)
{
    if ($Negate) {
        return PesterBeLessThan -ActualValue $ActualValue -ExpectedValue $ExpectedValue -Negate:$false -Because $Because
    }

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

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

Add-AssertionOperator -Name  BeLessThan `
                      -Test  $function:PesterBeLessThan `
                      -Alias 'LT'

Add-AssertionOperator -Name  BeGreaterOrEqual `
                      -Test  $function:PesterBeGreaterOrEqual `
                      -Alias 'GE'

#keeping tests happy
function PesterBeLessThanFailureMessage() {  }
function NotPesterBeLessThanFailureMessage() { }

function PesterBeGreaterOrEqualFailureMessage() {  }
function NotPesterBeGreaterOrEqualFailureMessage() { }