tests/extensions/HaveProperty.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function HaveProperty
{
    [CmdletBinding()]
    param (
        $ActualValue,
        
        [string]
        $PropertyName,
        
        $WithValue,
        
        [switch]
        $Negate
    )
    end
    {
        $shouldTestValue = $PSBoundParameters.ContainsKey('WithValue')
        if ($null -eq $ActualValue)
        {
            if ($shouldTestValue)
            {
                if ($Negate.IsPresent)
                {
                    $failureMessage = 'Expected: value "{0}" to contain the property "{1}" where the value was not "{2}" but the input object was null.' -f $ActualValue, $PropertyName, $WithValue
                }
                else
                {
                    $failureMessage = 'Expected: value "{0}" to contain the property "{1}" where the value was "{2}" but the input object was null.' -f $ActualValue, $PropertyName, $WithValue
                }
            }
            else
            {
                if ($Negate.IsPresent)
                {
                    $failureMessage = 'Expected: value "{0}" to not contain the property "{1}" but the input object was null.' -f $ActualValue, $PropertyName
                }
                else
                {
                    $failureMessage = 'Expected: value "{0}" to contain the property "{1}" but the input object was null.' -f $ActualValue, $PropertyName
                }
            }
            
            return [PSCustomObject]@{
                Succeeded       = $false
                FailureMessage = $failureMessage
            }
        }
        
        $property = $ActualValue.psobject.Properties[$PropertyName]
        $hasProperty = [bool]$property
        if (-not $shouldTestValue)
        {
            $succeeded = $hasProperty
            if ($Negate.IsPresent)
            {
                $succeeded = -not $succeeded
            }
            
            if (-not $succeeded)
            {
                if ($Negate.IsPresent)
                {
                    $failureMessage = 'Expected: value "{0}" to not contain the property "{1}" but it did.' -f $ActualValue, $PropertyName
                }
                else
                {
                    $failureMessage = 'Expected: value "{0}" to contain the property "{1}" but it did not.' -f $ActualValue, $PropertyName
                }
            }
            
            return [PSCustomObject]@{
                Succeeded       = $succeeded
                FailureMessage = $failureMessage
            }
        }
        
        if (-not $hasProperty)
        {
            if ($Negate.IsPresent)
            {
                $failureMessage = 'Expected: value "{0}" to contain the property "{1}" where the value was not "{2}" but the property did not exist.' -f $ActualValue, $PropertyName, $WithValue
            }
            else
            {
                $failureMessage = 'Expected: value "{0}" to contain the property "{1}" where the value was "{2}" but the property did not exist.' -f $ActualValue, $PropertyName, $WithValue
            }
            
            return [PSCustomObject]@{
                Succeeded       = $false
                FailureMessage = $failureMessage
            }
        }
        
        $succeeded = $WithValue -eq $property.Value
        if ($Negate.IsPresent)
        {
            $succeeded = -not $succeeded
            $failureMessage = 'Expected: value "{0}" to contain the property "{1}" where the value was not "{2}" but it was.' -f $ActualValue, $PropertyName, $WithValue
        }
        else
        {
            $failureMessage = 'Expected: value "{0}" to contain the property "{1}" where the value was not "{2}" but the actual value was "{3}".' -f $ActualValue, $PropertyName, $WithValue, $property.Value
        }
        
        [PSCustomObject]@{
            Succeeded       = $succeeded
            FailureMessage = $failureMessage
        }
    }
}
Add-ShouldOperator -Name HaveProperty -Test $function:HaveProperty