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

Code before:
                $sortedSetExpectedNouns.Add($noun) | Out-Null
            }

            $completionText -join ' ' | Should -BeExactly ($sortedSetExpectedNouns -join ' ')
        }
    }

    Context "Get-ExperimentalFeature -Name parameter completion" {
        BeforeAll {
            function GetExperimentalFeatureNames([switch]$SingleQuote, [switch]$DoubleQuote) {
                $features = (Get-ExperimentalFeature).Name

                if ($SingleQuote) {
                    return ($features | ForEach-Object { "'$_'" })
                }
                elseif ($DoubleQuote) {
                    return ($features | ForEach-Object { """$_""" })
                }

                return $features
            }

            $allExperimentalFeatures = GetExperimentalFeatureNames
            $allExperimentalFeaturesSingleQuote = GetExperimentalFeatureNames -SingleQuote
            $allExperimentalFeaturesDoubleQuote = GetExperimentalFeatureNames -DoubleQuote
            $experimentalFeaturesStartingWithPS = $allExperimentalFeatures | Where-Object { $_ -like 'PS*'}
            $experimentalFeaturesStartingWithPSSingleQuote = $allExperimentalFeaturesSingleQuote | Where-Object { $_ -like "'PS*" }
            $experimentalFeaturesStartingWithPSDoubleQuote = $allExperimentalFeaturesDoubleQuote | Where-Object { $_ -like """PS*" }
            $experimentalFeaturesStartingWithPSNative = $allExperimentalFeatures | Where-Object { $_ -like 'PSNative*'}
            $experimentalFeaturesStartingWithPSNativeSingleQuote = $allExperimentalFeaturesSingleQuote | Where-Object { $_ -like "'PSNative*" }
            $experimentalFeaturesStartingWithPSNativeDoubleQuote = $allExperimentalFeaturesDoubleQuote | Where-Object { $_ -like """PSNative*" }
        }

        It "Should complete Name for '<TextInput>'" -TestCases @(
            @{ TextInput = "Get-ExperimentalFeature -Name "; ExpectedExperimentalFeatureNames = $allExperimentalFeatures }
            @{ TextInput = "Get-ExperimentalFeature -Name '"; ExpectedExperimentalFeatureNames = $allExperimentalFeaturesSingleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name """; ExpectedExperimentalFeatureNames = $allExperimentalFeaturesDoubleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name PS"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPS }
            @{ TextInput = "Get-ExperimentalFeature -Name 'PS"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPSSingleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name ""PS"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPSDoubleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name PSNative"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPSNative }
            @{ TextInput = "Get-ExperimentalFeature -Name 'PSNative"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPSNativeSingleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name ""PSNative"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPSNativeDoubleQuote }
        ) {
            param($TextInput, $ExpectedExperimentalFeatureNames)
            $res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
            $completionText = $res.CompletionMatches.CompletionText
            $completionText -join ' ' | Should -BeExactly (($ExpectedExperimentalFeatureNames | Sort-Object -Unique) -join ' ')
        }
    }


Review comment (suggestion):
The names are not permanent. I don't want us to have to fix this test every time the list of current features changes. I think we should take the first name and use it for tests, and the test should complete successfully even if the list is empty.

Code after:
                $sortedSetExpectedNouns.Add($noun) | Out-Null
            }

            $completionText -join ' ' | Should -BeExactly ($sortedSetExpectedNouns -join ' ')
        }
    }

    Context "Get-ExperimentalFeature -Name parameter completion" {
        BeforeAll {
            function GetExperimentalFeatureNames([switch]$SingleQuote, [switch]$DoubleQuote) {
                $features = (Get-ExperimentalFeature).Name

                if ($SingleQuote) {
                    return ($features | ForEach-Object { "'$_'" })
                }
                elseif ($DoubleQuote) {
                    return ($features | ForEach-Object { """$_""" })
                }

                return $features
            }

            $allExperimentalFeatures = GetExperimentalFeatureNames
            $allExperimentalFeaturesSingleQuote = GetExperimentalFeatureNames -SingleQuote
            $allExperimentalFeaturesDoubleQuote = GetExperimentalFeatureNames -DoubleQuote
            $experimentalFeaturesStartingWithPS = $allExperimentalFeatures | Where-Object { $_ -like 'PS*'}
            $experimentalFeaturesStartingWithPSSingleQuote = $allExperimentalFeaturesSingleQuote | Where-Object { $_ -like "'PS*" }
            $experimentalFeaturesStartingWithPSDoubleQuote = $allExperimentalFeaturesDoubleQuote | Where-Object { $_ -like """PS*" }
        }

        It "Should complete Name for '<TextInput>'" -TestCases @(
            @{ TextInput = "Get-ExperimentalFeature -Name "; ExpectedExperimentalFeatureNames = $allExperimentalFeatures }
            @{ TextInput = "Get-ExperimentalFeature -Name '"; ExpectedExperimentalFeatureNames = $allExperimentalFeaturesSingleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name """; ExpectedExperimentalFeatureNames = $allExperimentalFeaturesDoubleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name PS"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPS }
            @{ TextInput = "Get-ExperimentalFeature -Name 'PS"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPSSingleQuote }
            @{ TextInput = "Get-ExperimentalFeature -Name ""PS"; ExpectedExperimentalFeatureNames = $experimentalFeaturesStartingWithPSDoubleQuote }
        ) {
            param($TextInput, $ExpectedExperimentalFeatureNames)
            $res = TabExpansion2 -inputScript $TextInput -cursorColumn $TextInput.Length
            $completionText = $res.CompletionMatches.CompletionText
            $completionText -join ' ' | Should -BeExactly (($ExpectedExperimentalFeatureNames | Sort-Object -Unique) -join ' ')
        }
    }

    Context "Format cmdlet's View paramter completion" {
        BeforeAll {
            $viewDefinition = @'
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
  <ViewDefinitions>