Functions/Assertions/FileContentMatchExactly.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 PesterFileContentMatchExactly($ActualValue, $ExpectedContent, [switch] $Negate, [String] $Because) {
    $succeeded = (@(& $SafeCommands['Get-Content'] -Encoding UTF8 $ActualValue) -cmatch $ExpectedContent).Count -gt 0

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

    $failureMessage = ''

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

    return & $SafeCommands['New-Object'] psobject -Property @{
        Succeeded      = $succeeded
        FailureMessage = $failureMessage
    }
}

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

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

Add-AssertionOperator -Name FileContentMatchExactly `
                      -Test $function:PesterFileContentMatchExactly