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

Code before:

        It "Should complete Verb parameter for '<TextInput>'" -Skip:(!([System.Management.Automation.Platform]::IsWindowsDesktop)) -TestCases @(
            @{ TextInput = 'Start-Process -Verb '; ExpectedVerbs = '' }
            @{ TextInput = "Start-Process -FilePath $cmdPath -Verb "; ExpectedVerbs =  $cmdVerbs -join ' ' }
            @{ TextInput = "Start-Process -FilePath $cmdPath -Verb '"; ExpectedVerbs =  $cmdVerbsSingleQuote -join ' ' }
            @{ TextInput = "Start-Process -FilePath $cmdPath -Verb """; ExpectedVerbs =   $cmdVerbsDoubleQuote -join ' ' }
            @{ TextInput = "Start-Process -FilePath $exePath -Verb "; ExpectedVerbs = $exeVerbs -join ' ' }
            @{ TextInput = "Start-Process -FilePath $exePath -Verb run"; ExpectedVerbs = $exeVerbsStartingWithRun -join ' ' }
            @{ TextInput = "Start-Process -FilePath $exePath -Verb 'run"; ExpectedVerbs = $exeVerbsStartingWithRunSingleQuote -join ' ' }
            @{ TextInput = "Start-Process -FilePath $exePath -Verb ""run"; ExpectedVerbs = $exeVerbsStartingWithRunDoubleQuote -join ' ' }
            @{ TextInput = "Start-Process -FilePath $powerShellExeWithNoExtension -Verb "; ExpectedVerbs = $exeVerbs -join ' ' }
            @{ TextInput = "Start-Process -FilePath $txtPath -Verb "; ExpectedVerbs = $txtVerbs -join ' ' }
            @{ TextInput = "Start-Process -FilePath $wavPath -Verb "; ExpectedVerbs = $wavVerbs -join ' ' }
            @{ TextInput = "Start-Process -FilePath $docxPath -Verb "; ExpectedVerbs = $docxVerbs -join ' ' }
            @{ TextInput = "Start-Process -FilePath $fileWithNoExtensionPath -Verb "; ExpectedVerbs = $fileWithNoExtensionVerbs -join ' ' }
        ) {
            param($TextInput, $ExpectedVerbs)
            $res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
            $completionText = $res.CompletionMatches.CompletionText | Sort-Object
            $completionText -join ' ' | Should -BeExactly $ExpectedVerbs
        }
    }

    Context 'Scope parameter completion' {
        BeforeAll {
            $allScopes = 'Global Local Script'
            $allScopesSingleQuote = "'Global' 'Local' 'Script'"
            $allScopesDoubleQuote = """Global"" ""Local"" ""Script"""
            $globalScope = 'Global'
            $globalScopeSingleQuote = "'Global'"
            $globalScopeDoubleQuote = """Global"""
            $localScope = 'Local'
            $localScopeSingleQuote = "'Local'"
            $localScopeDoubleQuote = """Local"""
            $scriptScope = 'Script'
            $scriptScopeSingleQuote = "'Script'"
            $scriptScopeDoubleQuote = """Script"""
            $allScopeCommands = 'Clear-Variable', 'Export-Alias', 'Get-Alias', 'Get-PSDrive', 'Get-Variable', 'Import-Alias', 'New-Alias', 'New-PSDrive', 'New-Variable', 'Remove-Alias', 'Remove-PSDrive', 'Remove-Variable', 'Set-Alias', 'Set-Variable'
        }

        It "Should complete '<ParameterInput>' for '<Commands>'" -TestCases @(
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope "; ExpectedScopes = $allScopes }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope '"; ExpectedScopes = $allScopesSingleQuote }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope """; ExpectedScopes = $allScopesDoubleQuote }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope G"; ExpectedScopes = $globalScope }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope 'G"; ExpectedScopes = $globalScopeSingleQuote }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope ""G"; ExpectedScopes = $globalScopeDoubleQuote }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope Lo"; ExpectedScopes = $localScope }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope 'Lo"; ExpectedScopes = $localScopeSingleQuote }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope ""Lo"; ExpectedScopes = $localScopeDoubleQuote }
            @{ Commands = $allScopeCommands; ParameterInput = "-Scope Scr"; ExpectedScopes = $scriptScope }

Review comment (suggestion):
It is better to check all the cases explicitly to avoid regression in any form (in tests below too):
```suggestion
        if ($SingleMatch) {
            $Script = 'Test-Function -ParamWithHelp'
            $res = (TabExpansion2 -inputScript $Script).CompletionMatches

            $res.Count | Should -Be 1
            $res.CompletionText | Should -BeExactly '-ParamWithHelp'
            $res.ToolTip | Should -BeExactly $expected
        }
        else {
            $Script = 'Test-Function -'
            $res = (TabExpansion2 -inputScript $Script).CompletionMatches | Where-Object CompletionText -eq '-ParamWithHelp'

            $res.Count | Should -Be 2
            $res[0].CompletionText | Should -BeExactly '-ParamWithHelp'
            $res[0].ToolTip | Should -BeExactly $expected

            $res[1].CompletionText | Should -BeExactly '-ParamWithoutHelp'
            $res[1].ToolTip | Should -BeExactly '[Object] ParamWithoutHelp'

        }
```

Code after:
    ) {
        param ($SingleMatch)

        Function Test-Function {
            param (
                [Parameter(HelpMessage = 'Some help message')]
                $ParamWithHelp,

                $ParamWithoutHelp
            )
        }

        $expected = '[Object] ParamWithHelp - Some help message'

        if ($SingleMatch) {
            $Script = 'Test-Function -ParamWithHelp'
            $res = (TabExpansion2 -inputScript $Script).CompletionMatches
        }
        else {
            $Script = 'Test-Function -'
            $res = (TabExpansion2 -inputScript $Script).CompletionMatches | Where-Object CompletionText -eq '-ParamWithHelp'
        }

        $res.Count | Should -Be 1
        $res.CompletionText | Should -BeExactly '-ParamWithHelp'
        $res.ToolTip | Should -BeExactly $expected
    }

    It 'Should include parameter help resource message in tool tip - SingleMatch <SingleMatch>' -TestCases @(
        @{ SingleMatch = $true }
        @{ SingleMatch = $false }
    ) {
        param ($SingleMatch)

        $expected = '`[string`] Activity - *'

        if ($SingleMatch) {
            $Script = 'Write-Progress -Activity'
            $res = (TabExpansion2 -inputScript $Script).CompletionMatches
        }
        else {
            $Script = 'Write-Progress -'
            $res = (TabExpansion2 -inputScript $Script).CompletionMatches | Where-Object CompletionText -eq '-Activity'
        }

        $res.Count | Should -Be 1
        $res.CompletionText | Should -BeExactly '-Activity'
        $res.ToolTip | Should -BeLikeExactly $expected
    }

    It 'Should skip empty parameter HelpMessage with multiple parameters - SingleMatch <SingleMatch>' -TestCases @(