File size: 3,989 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

class CompletionResult
{
    [string]$CompletionText
    [string]$ListItemText
    [System.Management.Automation.CompletionResultType]$ResultType
    [string]$ToolTip
    [bool]$Found

    [bool] Equals($Other)
    {
        if ($Other -isnot [CompletionResult] -and
            $Other -isnot [System.Management.Automation.CompletionResult])
        {
            return $false
        }

        # Comparison is intentionally fuzzy - CompletionText and ResultType must be specified
        # but the other properties don't need to match if they aren't specified

        if ($this.CompletionText -cne $Other.CompletionText -or
            $this.ResultType -ne $Other.ResultType)
        {
            return $false
        }

        if ($this.ListItemText -cne $Other.ListItemText -and
            ![string]::IsNullOrEmpty($this.ListItemText) -and ![string]::IsNullOrEmpty($Other.ListItemText))
        {
            return $false
        }

        if ($this.ToolTip -cne $Other.ToolTip -and
            ![string]::IsNullOrEmpty($this.ToolTip) -and ![string]::IsNullOrEmpty($Other.ToolTip))
        {
            return $false
        }

        return $true
    }
}

class CompletionTestCase
{
    [string]$Description
    [CompletionResult[]]$ExpectedResults
    [string[]]$NotExpectedResults
    [hashtable]$TestInput
}

function Get-Completions
{
    [CmdletBinding()]
    param([string]$InputScript, [int]$CursorColumn, $Options = $null)

    if (!$PSBoundParameters.ContainsKey('CursorColumn'))
    {
        $CursorColumn = $InputScript.IndexOf('<#CURSOR#>')
        if ($CursorColumn -lt 0)
        {
            $CursorColumn = $InputScript.Length
        }
        else
        {
            $InputScript = $InputScript -replace '<#CURSOR#>',''
        }
    }

    $results = [System.Management.Automation.CommandCompletion]::CompleteInput(
        <#inputScript#>  $InputScript,
        <#cursorColumn#> $CursorColumn,
        <#options#>      $Options)

    return $results
}

function Get-CompletionTestCaseData
{
    param(
        [Parameter(ValueFromPipeline)]
        [hashtable[]]$Data)

    process
    {
        Write-Output ([CompletionTestCase[]]$Data)
    }
}

function Test-Completions
{
    param(
        [Parameter(ValueFromPipeline)]
        [CompletionTestCase[]]$TestCases,
        [string]
        $Description)

    process
    {
        foreach ($test in $TestCases)
        {
            Describe $test.Description -Tags "CI" {
                $hash = $Test.TestInput
                $results = Get-Completions @hash

                foreach ($expected in $test.ExpectedResults)
                {
                    foreach ($result in $results.CompletionMatches)
                    {

                        if ($expected.Equals($result))
                        {
                            It "Checking for duplicates of: $($expected.CompletionText)" {
                                # We should only find 1 of each expected result
                                $expected.Found | Should -BeFalse
                            }
                            $expected.Found = $true
                        }
                    }
                }

                foreach ($expected in $test.ExpectedResults)
                {
                    It "Checking for presence of expected result: $($expected.CompletionText)" {
                        $expected.Found | Should -BeTrue
                    }
                }

                foreach ($notExpected in $test.NotExpectedResults)
                {
                    foreach ($result in $results.CompletionMatches)
                    {
                        It "Checking for results that should not be found: $notExpected" {
                            $result.CompletionText | Should -Not -Be $notExpected
                        }
                    }
                }

            }
        }
    }
}