PSPesterTestHelpers.psm1

function Assert-PTHMandatoryParameter {
    <#
    .SYNOPSIS
    Asserts that a function has only the mandatory parameters we pass.
    .DESCRIPTION
    Asserts that a function has only the mandatory parameters we pass. Returns true if it does and false otherwise.
    .EXAMPLE
    Assert-MandatoryParameter -FunctionName 'Get-MyFunction' -ParameterName 'Name'
 
    Asserts that the function 'Get-MyFunction' has only one mandatory parameter called 'Name'.
    .NOTES
    Author : Paul Broadwith https://github.com/pauby
    History: 2019-01-09 - pauby - Initial release
    .LINK
    https://github.com/pauby/pspestertesthelpers/blob/master/docs/Get-PTHMandatoryParameter.md
#>

    [OutputType([System.Boolean])]
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory)]
        [string]
        $FunctionName,

        [Parameter(Mandatory)]
        [string[]]
        $ParameterName
    )

    # this will find a functions mandatory parameters that are not in the $ParameterName array.
    # if anything is found then it means the mandatory parameters we're testing for are different.
    $unknownParameters = Get-PHFunctionParameter -Name $FunctionName | Where-Object {
        $_.Value.Attributes.Mandatory -eq $true -and $ParameterName -notcontains $_.Key
    }

    # if $unknownParameters is 0 then that is $false so we must negate it to mean $true as not finding any parameters different is a pass
    -not [bool]$unknownParameters
}
function Get-PTHFunctionParameter {
    <#
    .SYNOPSIS
        Gets the parameters and their properties for a specified function.
    .DESCRIPTION
        Gets the parameters and their properties for a specified function. The
        function must exist within the 'Function:' provider so will not work on
        cmdlets.
    .EXAMPLE
        Get-PTHFunctionParameter -Name 'Get-FunctionParameters'
 
        Returns the properties of each function parameter for the 'Import-Module'
        function excluding the common parameters for Advanced Functions.
    .EXAMPLE
        Get-PTHFunctionParameter -Name 'Get-FunctionParameters' -Exclude ''
 
        Returns the properties of each function parameter for the 'Import-Module'
        function excluding no parameter names.
    .NOTES
        Author : Paul Broadwith (https://github.com/pauby)
        History : 2018-03-17 - pauby - Initial release
    .LINK
        https://github.com/pauby/pspestertesthelpers/blob/master/docs/Get-PTHFunctionParameter.md
    #>

    [CmdletBinding()]
    Param (
        # Name of the function. The function must exist within the 'Function:'
        # provider or an exception will be thrown.
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Name,

        # Array of parameter names to exclude. By default the Advanced Functions
        # common parameters are excluded. Pass an empty array to have all
        # parameters returned.
        [AllowEmptyCollection()]
        [string[]]
        $Exclude = @('Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable')
    )

    if (-not $PSBoundParameters.ContainsKey('Verbose')) {
        $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference')
    }

    try {
        (Get-Item -Path "Function:\$Name").Parameters.GetEnumerator() | Where-Object { $Exclude -notcontains $_.key}
    }
    catch {
        throw "Cannot find function '$Name' loaded in the current session."
    }
}