Private/Get-PoshspecParam.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
<#
.SYNOPSIS
    Returns an object which is used with Invoke-PoshspecExpression to execute a Pester 'It' block with the generated name and test expression values.
.EXAMPLE
    Get-PoshspecParam -TestName File -TestExpression {'C:\Temp'} -Target 'C:\Temp' -Should { Should -Exist }
 
    Name : File 'C:\Temp' Should -Exist
    Expression : 'C:\Temp' | Should -Exist
#>

function Get-PoshspecParam {
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param(
        [Parameter(Mandatory, ParameterSetName = "Default")]
        [Parameter(Mandatory, ParameterSetName = "PropertyExpression")]
        [string]
        $TestName,

        [Parameter(Mandatory, ParameterSetName = "Default")]
        [Parameter(Mandatory, ParameterSetName = "PropertyExpression")]
        [string]
        $TestExpression,

        [Parameter(Mandatory, ParameterSetName = "Default")]
        [Parameter(Mandatory, ParameterSetName = "PropertyExpression")]
        [string]
        $Target,

        [Parameter(ParameterSetName = "Default")]
        [string]
        $FriendlyName,

        [Parameter(ParameterSetName = "Default")]
        [string]
        $Property,

        [Parameter(Mandatory, ParameterSetName = "PropertyExpression")]
        [string]
        $PropertyExpression,

        [Parameter(ParameterSetName = "Default")]
        [Parameter(ParameterSetName = "PropertyExpression")]
        [string]
        $Qualifier,

        [Parameter(Mandatory, ParameterSetName = "Default")]
        [Parameter(Mandatory, ParameterSetName = "PropertyExpression")]
        [scriptblock]
        $Should
    )

    $assertion = $Should.ToString().Trim()
    $expressionString = $TestExpression.ToString().Trim()
    $PropertyExpression = $PropertyExpression.ToString().Trim()

    if ($PSBoundParameters.ContainsKey("PropertyExpression")) {
        $expressionString = $ExecutionContext.InvokeCommand.ExpandString($expressionString)
        $expressionString = Expand-PoshspecTestExpression $expressionString $PropertyExpression

        if ($PropertyExpression -like '*.*') {
            $lastIndexOfPeriod = $PropertyExpression.LastIndexOf('.')
            $Qualifier = $PropertyExpression.substring(0, $lastIndexOfPeriod)
            $NewProperty = $PropertyExpression.substring($lastIndexOfPeriod + 1)
            $nameString = "{0} property '{1}' for '{2}' at '{3}' {4}" -f $TestName, $NewProperty, $Target, $Qualifier, $assertion
        }
        else {
            $nameString = "{0} property '{1}' for '{2}' {3}" -f $TestName, $PropertyExpression, $Target, $assertion
        }

        $expressionString += " | $assertion"
        Write-Output -InputObject ([PSCustomObject]@{Name = $nameString; Expression = $expressionString})
    }
    else {
        if (-not $PSBoundParameters.ContainsKey("FriendlyName")) {
            $FriendlyName = $Target
        }

        $expressionString = $TestExpression.ToString().Trim()

        if ($PSBoundParameters.ContainsKey("Property")) {
            $expressionString += " | Select-Object -ExpandProperty '$Property'"

            if ($PSBoundParameters.ContainsKey("Qualifier")) {
                $nameString = "{0} property '{1}' for '{2}' at '{3}' {4}" -f $TestName, $Property, $FriendlyName, $Qualifier, $assertion
            }
            else {
                $nameString = "{0} property '{1}' for '{2}' {3}" -f $TestName, $Property, $FriendlyName, $assertion
            }
        }
        else {
            $nameString = "{0} '{1}' {2}" -f $TestName, $FriendlyName, $assertion
        }

        $expressionString += " | $assertion"
        $expressionString = $ExecutionContext.InvokeCommand.ExpandString($expressionString)

        Write-Output -InputObject ([PSCustomObject]@{
                Name       = $nameString
                Expression = $expressionString
            })
    }
}