tests/TestUtils.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
param(
    [Parameter(HelpMessage="The block of tests to run in the scope of the module")]
    [ScriptBlock]$TestScope = $null
)
Write-Verbose "TestScope:$TestScope"
Function Test-ParamIsMandatory {
    param(
        [Parameter(Mandatory=$true)]
        [string]$CommandName,
        [Parameter(Mandatory=$true)]
        [string]$Parameter,
        [string]$SetName = ''
    )

    $cmd = Get-Command $CommandName
    $attr = $cmd.Parameters.$Parameter.Attributes | Where-Object { $_.TypeId -eq [System.Management.Automation.ParameterAttribute] }
    if($SetName) {
        $attr = $attr | Where-Object { $_.ParameterSetName -eq $SetName }
    }
    $attr.Mandatory
}

Function Test-ParamValidateSet {
    param(
        [Parameter(Mandatory=$true)]
        [string]$CommandName,
        [Parameter(Mandatory=$true)]
        [string]$Parameter,
        [string[]]$Values
    )

    $cmd = Get-Command $CommandName
    $attr = $cmd.Parameters.$Parameter.Attributes | Where-Object { $_.TypeId -eq [System.Management.Automation.ValidateSetAttribute] }

    Compare-Sets $attr.ValidValues $Values
}

Function Compare-Sets {
    param(
        [Parameter(Mandatory=$true)]
        [psobject[]]$Left,
        [Parameter(Mandatory=$true)]
        [psobject[]]$Right
    )

    $results = Compare-Object $Left $Right
    if($results){
        $formatter = {
            $obj = New-Object psobject -Property @{
                State = if($_.SideIndicator.Contains('>')) { 'Missing' } else { 'Extra' }
                Value = $_.InputObject
            }

            Write-Output "$($Obj.State) => $($obj.Value)"
        }

        $results | ForEach-Object $formatter
        return $false
    }

    $true
}