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

Code before:

                $dynParam = [System.Management.Automation.RuntimeDefinedParameter]::new('Foo', [string], $attrCollection)

                $paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
                $paramDictionary.Add('Foo', $dynParam)
                $paramDictionary
            }

            end {}
        }

        $expected = '[string] Foo - Howdy partner'
        $Script = 'Test-Function -'
        $res = (TabExpansion2 -inputScript $Script).CompletionMatches | Where-Object CompletionText -eq '-Foo'
        $res.Count | Should -Be 1
        $res.CompletionText | Should -BeExactly '-Foo'
        $res.ToolTip | Should -BeExactly $expected
    }

    It 'Should have type and name for parameter without help message' {
        Function Test-Function {
            param (
                [Parameter()]
                $WithAttribute,

                $WithoutAttribute
            )
        }

        $Script = 'Test-Function -'
        $res = (TabExpansion2 -inputScript $Script).CompletionMatches |
            Where-Object CompletionText -in '-WithAttribute', '-WithoutAttribute' |
            Sort-Object CompletionText
        $res.Count | Should -Be 2

        $res.CompletionText[0] | Should -BeExactly '-WithAttribute'
        $res.ToolTip[0] | Should -BeExactly '[Object] WithAttribute'

        $res.CompletionText[1] | Should -BeExactly '-WithoutAttribute'
        $res.ToolTip[1] | Should -BeExactly '[Object] WithoutAttribute'
    }

    It 'Should include help resource message in parameter tool tip by direct match' {
        $expected = '[string] Activity - Text to describe the activity for which progress is being reported.'
        $Script = 'Write-Progress -Activity'
        $res = (TabExpansion2 -inputScript $Script).CompletionMatches
        $res.Count | Should -Be 1
        $res.CompletionText | Should -BeExactly '-Activity'
        $res.ToolTip | Should -BeExactly $expected
    }


Review comment (suggestion):
A bit confuse - look like an attribute in script below:
```suggestion
                [Parameter()]
                $WithAttributeParam,

                $WithoutAttributeParam
```

Code after:
param([ValidatePattern(
^)])
'@
        $CursorIndex = $TestString.IndexOf('^')
        $res = TabExpansion2 -cursorColumn $CursorIndex -inputScript $TestString.Remove($CursorIndex, 1)
        $Expected = ([ValidatePattern].GetProperties() | Where-Object {$_.CanWrite}).Name -join ','
        $res.CompletionMatches.CompletionText -join ',' | Should -BeExactly $Expected
    }

    It 'Should complete attribute argument with partially written name in incomplete param block' {
        $TestString =  'param([ValidatePattern(op^)]'
        $CursorIndex = $TestString.IndexOf('^')
        $res = TabExpansion2 -cursorColumn $CursorIndex -inputScript $TestString.Remove($CursorIndex, 1)
        $res.CompletionMatches[0].CompletionText | Should -BeExactly 'Options'
    }

    It 'Should complete attribute argument for incomplete standalone attribute' {
        $res = TabExpansion2 -inputScript '[ValidatePattern('
        $Expected = ([ValidatePattern].GetProperties() | Where-Object {$_.CanWrite}).Name -join ','
        $res.CompletionMatches.CompletionText -join ',' | Should -BeExactly $Expected
    }

    It 'Should complete argument for second parameter' {
        $res = TabExpansion2 -inputScript 'Get-ChildItem -Path $HOME -ErrorAction '
        $res.CompletionMatches[0].CompletionText | Should -BeExactly Break
    }

    It 'Should complete argument with validateset attribute after comma' {
        $TestString = 'function Test-ValidateSet{Param([ValidateSet("Cat","Dog")]$Param1,$Param2)};Test-ValidateSet -Param1 Dog, -Param2'
        $res = TabExpansion2 -inputScript $TestString -cursorColumn ($TestString.LastIndexOf(',') + 1)
        $res.CompletionMatches[0].CompletionText | Should -BeExactly Cat
    }

    It 'Should complete cim ETS member added by shortname' -Skip:(!$IsWindows -or (Test-IsWinServer2012R2) -or (Test-IsWindows2016)) {
        $res = TabExpansion2 -inputScript '(Get-NetFirewallRule).Nam'
        $res.CompletionMatches[0].CompletionText | Should -BeExactly 'Name'
    }

    It 'Should complete variable assigned with Data statement' {
        $TestString = 'data MyDataVar {"Hello"};$MyDatav'
        $res = TabExpansion2 -inputScript $TestString
        $res.CompletionMatches[0].CompletionText | Should -BeExactly '$MyDataVar'
    }

    It 'Should complete global variable without scope' {
        $res = TabExpansion2 -inputScript '$Global:MyTestVar = "Hello";$MyTestV'
        $res.CompletionMatches[0].CompletionText | Should -BeExactly '$MyTestVar'
    }

    It 'Should complete previously assigned variable in using: scope' {
        $res = TabExpansion2 -inputScript '$MyTestVar = "Hello";$Using:MyTestv'