Repository: PowerShell/PowerShell
File: test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 (PowerShell)

Code before:
    It 'Should complete abbreviated function' {
        function Test-AbbreviatedFunctionExpansion {}
        $res = (TabExpansion2 -inputScript 't-afe' -cursorColumn 't-afe'.Length).CompletionMatches.CompletionText
        $res.Count | Should -BeGreaterOrEqual 1
        $res | Should -BeExactly 'Test-AbbreviatedFunctionExpansion'
    }

    It 'Should complete native exe' -Skip:(!$IsWindows) {
        $res = TabExpansion2 -inputScript 'notep' -cursorColumn 'notep'.Length
        $res.CompletionMatches[0].CompletionText | Should -BeExactly 'notepad.exe'
    }

    It 'Should not include duplicate command results' {
        $OldModulePath = $env:PSModulePath
        $tempDir = Join-Path -Path $TestDrive -ChildPath "TempPsModuleDir"
        try
        {
            $ModuleDirs = @(
                Join-Path $tempDir "TestModule1\1.0"
                Join-Path $tempDir "TestModule1\1.1"
                Join-Path $tempDir "TestModule2\1.0"
            )
            foreach ($Dir in $ModuleDirs)
            {
                $NewDir = New-Item -Path $Dir -ItemType Directory -Force
                $ModuleName = $NewDir.Parent.Name
                Set-Content -Value 'MyTestFunction{}' -LiteralPath "$($NewDir.FullName)\$ModuleName.psm1"
                New-ModuleManifest -Path "$($NewDir.FullName)\$ModuleName.psd1" -RootModule "$ModuleName.psm1" -FunctionsToExport "MyTestFunction" -ModuleVersion $NewDir.Name
            }

            $env:PSModulePath += [System.IO.Path]::PathSeparator + $tempDir            
            $Res = TabExpansion2 -inputScript MyTestFunction
            $Res.CompletionMatches.Count | Should -Be 2
            $SortedMatches = $Res.CompletionMatches.CompletionText | Sort-Object
            $SortedMatches[0] | Should -Be "TestModule1\MyTestFunction"
            $SortedMatches[1] | Should -Be "TestModule2\MyTestFunction"
        }
        finally
        {
            $env:PSModulePath = $OldModulePath
            if (Test-Path -LiteralPath $tempDir)
            {
                Remove-Item $tempDir -Recurse -Force
            }
        }
    }

    It 'Should complete dotnet method' {
        $res = TabExpansion2 -inputScript '(1).ToSt' -cursorColumn '(1).ToSt'.Length
        $res.CompletionMatches[0].CompletionText | Should -BeExactly 'ToString('
    }

Review comment (refactor):
1. I agree it is useful to remove duplicates for the _same_ modules with different versions.
2. I'd prefer to get variants with full names (`TestModule1\MyTestFunction` and `TestModule2\MyTestFunction`) for different modules.
3. In original issue there are two cases - here I see test for one case only.

Code after:
    It 'Should complete abbreviated function' {
        function Test-AbbreviatedFunctionExpansion {}
        $res = (TabExpansion2 -inputScript 't-afe' -cursorColumn 't-afe'.Length).CompletionMatches.CompletionText
        $res.Count | Should -BeGreaterOrEqual 1
        $res | Should -BeExactly 'Test-AbbreviatedFunctionExpansion'
    }

    It 'Should complete module by shortname' {
        $res = TabExpansion2 -inputScript 'Get-Module -ListAvailable -Name Host'
        $res.CompletionMatches[0].CompletionText | Should -BeExactly 'Microsoft.PowerShell.Host'
    }

    It 'Should complete native exe' -Skip:(!$IsWindows) {
        $res = TabExpansion2 -inputScript 'notep' -cursorColumn 'notep'.Length
        $res.CompletionMatches[0].CompletionText | Should -BeExactly 'notepad.exe'
    }

    It 'Should not include duplicate command results' {
        $OldModulePath = $env:PSModulePath
        $tempDir = Join-Path -Path $TestDrive -ChildPath "TempPsModuleDir"
        try
        {
            $ModuleDirs = @(
                Join-Path $tempDir "TestModule1\1.0"
                Join-Path $tempDir "TestModule1\1.1"
                Join-Path $tempDir "TestModule2\1.0"
            )
            foreach ($Dir in $ModuleDirs)
            {
                $NewDir = New-Item -Path $Dir -ItemType Directory -Force
                $ModuleName = $NewDir.Parent.Name
                Set-Content -Value 'MyTestFunction{}' -LiteralPath "$($NewDir.FullName)\$ModuleName.psm1"
                New-ModuleManifest -Path "$($NewDir.FullName)\$ModuleName.psd1" -RootModule "$ModuleName.psm1" -FunctionsToExport "MyTestFunction" -ModuleVersion $NewDir.Name
            }

            $env:PSModulePath += [System.IO.Path]::PathSeparator + $tempDir            
            $Res = TabExpansion2 -inputScript MyTestFunction
            $Res.CompletionMatches.Count | Should -Be 2
            $SortedMatches = $Res.CompletionMatches.CompletionText | Sort-Object
            $SortedMatches[0] | Should -Be "TestModule1\MyTestFunction"
            $SortedMatches[1] | Should -Be "TestModule2\MyTestFunction"
        }
        finally
        {
            $env:PSModulePath = $OldModulePath
        }
    }

    It 'Should complete dotnet method' {
        $res = TabExpansion2 -inputScript '(1).ToSt' -cursorColumn '(1).ToSt'.Length
        $res.CompletionMatches[0].CompletionText | Should -BeExactly 'ToString('