TypeName.Tests.ps1

#Requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '4.0.0' }

using namespace System.Collections.Generic
using namespace System.Management.Automation

[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingWMICmdlet', '')]
param()

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

Describe 'Get-PsTypeName Test' {
    Context 'int' {
        It 'int' {
            Get-PsTypeName int | Should -BeExactly 'int'
        }
    }
    Context 'foobar' {
        It '$null' {
            Get-PsTypeName foobar | Should -BeExactly $null
        }
    }
    Context 'System.String' {
        It 'string' {
            Get-PsTypeName System.String | Should -BeExactly 'string'
        }
    }
    # System.の省略
    Context 'Management.Automation.PSObject' {
        It 'psobject' {
            Get-PsTypeName Management.Automation.PSObject | Should -BeExactly 'psobject'
        }
    }
    Context 'System.Poo' {
        It '$null' {
            Get-PsTypeName System.Poo | Should -BeExactly $null
        }
    }
    Context '''''' {
        It 'throw ParameterBindingException' {
            { Get-PsTypeName '' } | Should -Throw -ExceptionType ([ParameterBindingException])
        }
    }
    Context '1.0, $null, ''foo'' | Get-TypeName | Get-PsTypeName' {
        It 'double, string' {
            1.0, $null, 'foo' |
                Get-TypeName -ErrorAction SilentlyContinue |
                Get-PsTypeName |
                Should -BeExactly @('double', 'string')
        }
    }
    Context '$null | Get-PsTypeName' {
        It 'throw ParameterBindingException' {
            { $null | Get-PsTypeName -ErrorAction Stop } | Should -Throw -ExceptionType ([ParameterBindingException])
        }
    }
}