#Calling aliases | Pester assert that aliases exist?

2 messages · Page 1 of 1 (latest)

ornate nacelle
#

My question is
[1] How do you indirectly invoke an alias name. ( Aliases of the function )
[2] Is there another method that's more pester-idiomatic?

this didn't work

$some_al = gcm 'Pk.AnyTrue' -CommandType Alias
& $some_al @('foo', 'bar')
# Exception: Uhandled compare mode: & !
# technically it worked but $MyInvocation.InvocationName == String.Empty
# in the debug term, or '&' in the variables view

I want to automatically assert every alias is defined. Any unhandled / missing versions will throw

function Picky.TestBools {
    [Alias('Picky.AllTrue', 'Picky.AnyTrue')]
    param()
    switch( $MyInvocation.InvocationName) {
        'Picky.AllTrue' { ... }   
        'Picky.AnyTrue' { ... } 
        default { throw "UnhandledAlias: $( $MyInvocation.AliasName )"}
    }
}

I want to test that passing by pipeline or parameter is defined for every alias.
Maybe this should be split into another step. One validates aliases exist. One validates both parametersets resolve.

Describe 'Picky.TestBools' { 
    beforeAll { 
         $all_aliases = ( Get-Command Picky.TestBools ).
               ScriptBlock.Attributes.Where({$_ -is [Alias]}).AliasNames
    } 
    it 'Name' -ForEach (
        $all_aliases
        # 'Picky.AllTrue', 'Picky.NoneTrue'
    ) { 
        $AliasName = $_ 
        $data = @( $True, $false ) 
        { $data | & $AliasName   } | Should -not -throw
        { & $AliasName -In $data } | Should -not -throw
    }
}
kindred snow
#

could Invoke-Expression. You're fundamentally tied to the command line usage of the alias name.