MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4class CompletionResult5{6 [string]$CompletionText7 [string]$ListItemText8 [System.Management.Automation.CompletionResultType]$ResultType9 [string]$ToolTip10 [bool]$Found11 12 [bool] Equals($Other)13 {14 if ($Other -isnot [CompletionResult] -and15 $Other -isnot [System.Management.Automation.CompletionResult])16 {17 return $false18 }19 20 # Comparison is intentionally fuzzy - CompletionText and ResultType must be specified21 # but the other properties don't need to match if they aren't specified22 23 if ($this.CompletionText -cne $Other.CompletionText -or24 $this.ResultType -ne $Other.ResultType)25 {26 return $false27 }28 29 if ($this.ListItemText -cne $Other.ListItemText -and30 ![string]::IsNullOrEmpty($this.ListItemText) -and ![string]::IsNullOrEmpty($Other.ListItemText))31 {32 return $false33 }34 35 if ($this.ToolTip -cne $Other.ToolTip -and36 ![string]::IsNullOrEmpty($this.ToolTip) -and ![string]::IsNullOrEmpty($Other.ToolTip))37 {38 return $false39 }40 41 return $true42 }43}44 45class CompletionTestCase46{47 [string]$Description48 [CompletionResult[]]$ExpectedResults49 [string[]]$NotExpectedResults50 [hashtable]$TestInput51}52 53function Get-Completions54{55 [CmdletBinding()]56 param([string]$InputScript, [int]$CursorColumn, $Options = $null)57 58 if (!$PSBoundParameters.ContainsKey('CursorColumn'))59 {60 $CursorColumn = $InputScript.IndexOf('<#CURSOR#>')61 if ($CursorColumn -lt 0)62 {63 $CursorColumn = $InputScript.Length64 }65 else66 {67 $InputScript = $InputScript -replace '<#CURSOR#>',''68 }69 }70 71 $results = [System.Management.Automation.CommandCompletion]::CompleteInput(72 <#inputScript#> $InputScript,73 <#cursorColumn#> $CursorColumn,74 <#options#> $Options)75 76 return $results77}78 79function Get-CompletionTestCaseData80{81 param(82 [Parameter(ValueFromPipeline)]83 [hashtable[]]$Data)84 85 process86 {87 Write-Output ([CompletionTestCase[]]$Data)88 }89}90 91function Test-Completions92{93 param(94 [Parameter(ValueFromPipeline)]95 [CompletionTestCase[]]$TestCases,96 [string]97 $Description)98 99 process100 {101 foreach ($test in $TestCases)102 {103 Describe $test.Description -Tags "CI" {104 $hash = $Test.TestInput105 $results = Get-Completions @hash106 107 foreach ($expected in $test.ExpectedResults)108 {109 foreach ($result in $results.CompletionMatches)110 {111 112 if ($expected.Equals($result))113 {114 It "Checking for duplicates of: $($expected.CompletionText)" {115 # We should only find 1 of each expected result116 $expected.Found | Should -BeFalse117 }118 $expected.Found = $true119 }120 }121 }122 123 foreach ($expected in $test.ExpectedResults)124 {125 It "Checking for presence of expected result: $($expected.CompletionText)" {126 $expected.Found | Should -BeTrue127 }128 }129 130 foreach ($notExpected in $test.NotExpectedResults)131 {132 foreach ($result in $results.CompletionMatches)133 {134 It "Checking for results that should not be found: $notExpected" {135 $result.CompletionText | Should -Not -Be $notExpected136 }137 }138 }139 140 }141 }142 }143}144 145 