TypeName.Tests.ps1

using namespace System.Collections.Generic

Describe 'Get-TypeName Test' {
    $isVersionGe60 = $PSVersionTable['PSVersion'] -ge '6.0'
    Context 'Get-TypeName $null' {
        It 'Null' {
            Get-TypeName $null | Should -Be 'Null'
        }
    }
    Context 'Get-TypeName (New-Object -ComObject WScript.Shell)' {
        It 'IWshShell3' {
            Get-TypeName (New-Object -ComObject WScript.Shell) | Should -Be 'IWshShell3'
        }
    }
    Context 'Get-TypeName (New-Object -ComObject Shell.Application)' {
        if ($isVersionGe60) {
            It 'like System.__ComObject#*' {
                Get-TypeName (New-Object -ComObject Shell.Application) | Should -BeLike 'System.__ComObject#*'
            }
        } else {
            It 'match ^IShellDispatch\d+$' {
                Get-TypeName (New-Object -ComObject Shell.Application) | Should -Match '^IShellDispatch\d+$'
            }
        }
    }
    Context 'Get-TypeName (Get-CimInstance Win32_OperatingSystem)' {
        It 'root/cimv2:Win32_OperatingSystem' {
            Get-TypeName (Get-CimInstance Win32_OperatingSystem) | Should -Be 'root/cimv2:Win32_OperatingSystem'
        }
    }
    if (!$isVersionGe60) {
        Context 'Get-TypeName (Get-WmiObject Win32_ComputerSystem)' {
            It 'root\cimv2:Win32_ComputerSystem' {
                Get-TypeName (Get-WmiObject Win32_ComputerSystem) | Should -Be 'root\cimv2:Win32_ComputerSystem'
            }
        }
    }
    Context 'Get-TypeName ([pscustomobject]@{ pstypename = ''Foo'' })' {
        It 'Foo' {
            Get-TypeName ([pscustomobject]@{ pstypename = 'Foo' }) | Should -Be 'Foo'
        }
    }
    Context 'Get-TypeName ([pscustomobject]@{ })' {
        It 'System.Management.Automation.PSCustomObject' {
            Get-TypeName ([pscustomobject]@{ }) | Should -Be 'System.Management.Automation.PSCustomObject'
        }
    }
    Context 'Get-TypeName ([System.Object]::new())' {
        It 'System.Object' {
            $obj = [System.Object]::new()
            $obj.pstypenames.Insert(0, 'Obj')
            Get-TypeName $obj | Should -Be 'System.Object'
        }
    }
    Context 'Get-TypeName ''abc''' {
        It 'System.String' {
            Get-TypeName 'abc' | Should -Be 'System.String'
        }
    }
    Context 'Get-TypeName ([int[]]@(0, 1, 2))' {
        It 'System.Int32[]' {
            Get-TypeName ([int[]]@(0, 1, 2)) | Should -Be 'System.Int32[]'
        }
    }
    Context 'Get-TypeName ([Dictionary[[string], [int]]]::new())' {
        It 'System.Collections.Generic.Dictionary`2' {
            Get-TypeName ([Dictionary[[string], [int]]]::new()) |
                Should -Be 'System.Collections.Generic.Dictionary`2[System.String,System.Int32]'
        }
    }
    Context 'Get-TypeName ([List[int][]]@([List[int]]::new()))' {
        It 'System.Collections.Generic.List`1[System.Int32][]' {
            Get-TypeName ([List[int][]]@([List[int]]::new())) |
                Should -Be 'System.Collections.Generic.List`1[System.Int32][]'
        }
    }
    Context 'Get-TypeName ([List[int[, ]]]::new())' {
        It 'System.Collections.Generic.List`1[System.Int32[,]]' {
            Get-TypeName ([List[int[, ]]]::new()) |
                Should -Be 'System.Collections.Generic.List`1[System.Int32[,]]'
        }
    }
    Context 'Get-TypeName -ProgId Scripting.FileSystemObject' {
        It 'IFileSystem3' {
            Get-TypeName -ProgId Scripting.FileSystemObject | Should -Be 'IFileSystem3'
        }
    }
    Context 'Get-TypeName -ProgId ???' {
        It 'throw System.Runtime.InteropServices.COMException' {
            { Get-TypeName -ProgId ??? } | Should -Throw -ExceptionType System.Runtime.InteropServices.COMException
        }
    }
    Context 'Get-TypeName -ProgId ''''' {
        It 'throw System.Management.Automation.ParameterBindingException' {
            { Get-TypeName -ProgId '' } |
                Should -Throw -ExceptionType System.Management.Automation.ParameterBindingException
        }
    }
}