Repository: PowerShell/PowerShell
File: test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Alias.Tests.ps1 (PowerShell)

Code before:
            $result.Definition | Should -BeExactly "foo"
            $result.Description | Should -BeNullOrEmpty
            $result.Options | Should -BeExactly "None"

            $result=Get-Alias -Name ABCD -Scope "0"
            $result.Name | Should -BeExactly "ABCD"
            $result.Definition | Should -BeExactly "foo"
            $result.Description | Should -BeNullOrEmpty
            $result.Options | Should -BeExactly "None"
    }

    It "Test get-alias with Definition parameter" {
        $returnObject = Get-Alias -Definition Get-Command
        For($i = 0; $i -lt $returnObject.Length;$i++)
        {
            $returnObject[$i] | Should -Not -BeNullOrEmpty
            $returnObject[$i].CommandType | Should -Be 'Alias'
            $returnObject[$i].Definition | Should -Be 'Get-Command'
        }
    }

    # Below test was suggested by GitHub Copilot to aid with fixing issue #25616
    It "Get-Alias DisplayName should always show AliasName -> ReferencedCommand for all aliases" {
    # Clean up any existing aliases
    if (Get-Alias -Name Test-MyAlias -ErrorAction SilentlyContinue) { Remove-Item Alias:Test-MyAlias }
    if (Get-Alias -Name tma -ErrorAction SilentlyContinue) { Remove-Item Alias:tma }

    # Create a Verb-Noun style alias "Test-MyAlias" pointing to Get-Command
    Set-Alias -Name Test-MyAlias -Value Get-Command
    # Create a short alias for the above "tma" -> "Test-MyAlias"
    Set-Alias -Name tma -Value Test-MyAlias

    $aliases = Get-Alias Test-MyAlias, tma

    $aliases | ForEach-Object {
        # The DisplayName property should always be in the format: Name -> [Definition or ReferencedCommand]
        $_.DisplayName | Should -Be "$($_.Name) -> Get-Command"
    }

    # Clean up
    Remove-Item Alias:Test-MyAlias
    Remove-Item Alias:tma
}
}

Describe "Get-Alias" -Tags "CI" {
    It "Should have a return type of System.Array when gal returns more than one object" {
        $val1=(Get-Alias a*)
        $val2=(Get-Alias c*)
        $i=0


Review comment (suggestion):
No need to check aliases before removing - Remove-Itrm -ErrorAction SilentlyContinue should work well.

Code after:
            $result.Definition | Should -BeExactly "foo"
            $result.Description | Should -BeNullOrEmpty
            $result.Options | Should -BeExactly "None"

            $result=Get-Alias -Name ABCD -Scope "0"
            $result.Name | Should -BeExactly "ABCD"
            $result.Definition | Should -BeExactly "foo"
            $result.Description | Should -BeNullOrEmpty
            $result.Options | Should -BeExactly "None"
    }

    It "Test get-alias with Definition parameter" {
        $returnObject = Get-Alias -Definition Get-Command
        For($i = 0; $i -lt $returnObject.Length;$i++)
        {
            $returnObject[$i] | Should -Not -BeNullOrEmpty
            $returnObject[$i].CommandType | Should -Be 'Alias'
            $returnObject[$i].Definition | Should -Be 'Get-Command'
        }
    }

    It "Get-Alias DisplayName should always show AliasName -> ResolvedCommand for all aliases" {
        Set-Alias -Name Test-MyAlias -Value Get-Command -Force
        Set-Alias -Name tma -Value Test-MyAlias -force
        $aliases = Get-Alias Test-MyAlias, tma
        $aliases | ForEach-Object {
            $_.DisplayName | Should -Be "$($_.Name) -> Get-Command"
        }
        $aliases.Name.foreach{Remove-Item Alias:$_ -ErrorAction SilentlyContinue}
    }
}

Describe "Get-Alias" -Tags "CI" {
    It "Should have a return type of System.Array when gal returns more than one object" {
        $val1=(Get-Alias a*)
        $val2=(Get-Alias c*)
        $i=0

        $val1 | Should -Not -BeNullOrEmpty
        $val2 | Should -Not -BeNullOrEmpty

        $val1 | ForEach-Object{ $i++};
        if($i -lt 2) {
            $val1 | Should -BeOfType System.Management.Automation.CommandInfo
        }
        else
        {
            ,$val1 | Should -BeOfType System.Array
        }

        $val2 | ForEach-Object{ $i++};