Functions/Assertions/FileContentMatchMultiline.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
function PesterFileContentMatchMultiline($ActualValue, $ExpectedContent, [switch] $Negate, [String] $Because) {
    $succeeded = [bool] ((Get-Content $ActualValue -delim ([char]0)) -match $ExpectedContent)

    if ($Negate) { $succeeded = -not $succeeded }

    $failureMessage = ''

    if (-not $succeeded)
    {
        if ($Negate)
        {
            $failureMessage = NotPesterFileContentMatchMultilineFailureMessage -ActualValue $ActualValue -ExpectedContent $ExpectedContent -Because $Because
        }
        else
        {
            $failureMessage = PesterFileContentMatchMultilineFailureMessage -ActualValue $ActualValue -ExpectedContent $ExpectedContent -Because $Because
        }
    }

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

function PesterFileContentMatchMultilineFailureMessage($ActualValue, $ExpectedContent, $Because) {
    return "Expected {$ExpectedContent} to be found in file '$ActualValue',$(Format-Because $Because) but it was not found."
}

function NotPesterFileContentMatchMultilineFailureMessage($ActualValue, $ExpectedContent, $Because) {
    return "Expected {$ExpectedContent} to not be found in file '$ActualValue',$(Format-Because $Because) but it was found."
}

Add-AssertionOperator -Name  FileContentMatchMultiline `
                      -Test  $function:PesterFileContentMatchMultiline