From 6ef4bc12026fa9356171522b6ff7900c9146b559 Mon Sep 17 00:00:00 2001 From: linka Date: Tue, 28 Jun 2022 18:07:54 +0800 Subject: [PATCH] fix: add command help, fixed variable wrong export --- README.md | 4 +- src/Completion.ps1 | 209 ++++++++++++++ .../Register-Completion.psd1 | 6 +- src/Register-Completion.psm1 | 10 + .../Register-Completion.ps1 | 116 -------- .../Register-Completion.psm1 | 3 - .../Register-Completion.Tests.ps1 | 267 +++++++++--------- 7 files changed, 364 insertions(+), 251 deletions(-) create mode 100644 src/Completion.ps1 rename src/{Register-Completion => }/Register-Completion.psd1 (95%) create mode 100644 src/Register-Completion.psm1 delete mode 100644 src/Register-Completion/Register-Completion.ps1 delete mode 100644 src/Register-Completion/Register-Completion.psm1 rename {src/test => test}/Register-Completion.Tests.ps1 (51%) diff --git a/README.md b/README.md index 34ff938..a998430 100644 --- a/README.md +++ b/README.md @@ -67,8 +67,8 @@ Register-Completion rc_object " `Register-Completion` module export the variables: -+ `$cache_all_completion`: cache all the avaliable completion -+ `$cache_command_list`: cache the register completion list ++ `$CacheAllCompletions`: cache all the avaliable completion ++ `$CacheCommands`: cache the register completion list ## License diff --git a/src/Completion.ps1 b/src/Completion.ps1 new file mode 100644 index 0000000..1870a9e --- /dev/null +++ b/src/Completion.ps1 @@ -0,0 +1,209 @@ +[hashtable]$CacheAllCompletions = @{} +[hashtable]$CacheCommands = @{} + +<# +.SYNOPSIS + Convert to hashtable format. +.DESCRIPTION + Recursive conversion of data to hashtable format. hashtable values will be converted to hashtable. + e.g. + @{arg1 = "arg1_1"} -> @{arg1 = @{arg1_1 = ""}} +.PARAMETER InputObject + Input data, support for basic data types +.EXAMPLE + ConvertTo-Hash "arg" + Convert string to hashtable format +.EXAMPLE + ConvertTo-Hash 100 + Convert number to hashtable format +.EXAMPLE + ConvertTo-Hash "['hello','world']" + Convert Javascript array to hashtable format +.EXAMPLE + ConvertTo-Hash "[{arg: {arg_1: 'arg_1_1'}}]" + Convert Javascript array object to hashtable format +.EXAMPLE + ConvertTo-Hash "[{arg: {arg_1: {arg_1_1: ['arg_1_1_1', 'arg_1_1_2']}}}]" + Convert Javascript nested array object to hashtable format +.EXAMPLE + ConvertTo-Hash "[100, 'hello', {arg1: 'arg1_1'}, ['arg2', 'arg3']]" + Convert Javascript array to hashtable format +.EXAMPLE + ConvertTo-Hash @("arg1", "arg2") + Convert array to hashtable format +.EXAMPLE + ConvertTo-Hash @("arg1", @{arg2 = "arg2_1"; arg3 = @("arg3_1", "arg3_2")}) + Convert nested array to hashtable format +.EXAMPLE + @("arg1", "arg2") | ConvertTo-Hash + Convert array to hashtable format by pipeline input +.INPUTS + None. +.OUTPUTS + System.Collections.Hashtable +#> +function ConvertTo-Hash { + Param($InputObject) + + if (!$InputObject) { + return "" + } + + [hashtable]$hash = @{} + $inputType = $InputObject.getType() + + if ($inputType -eq [hashtable]) { + $InputObject.Keys | ForEach-Object { $hash[$_] = ConvertTo-Hash $InputObject[$_] } + } + elseif ($inputType -eq [Object[]]) { + $InputObject | ForEach-Object { $hash += ConvertTo-Hash $_ } + } + else { + try { + $json = ConvertFrom-Json -InputObject $InputObject -AsHashtable + if ($json.getType() -in [hashtable],[Object[]]) { + $hash = ConvertTo-Hash $json + } + else { + $hash.Add($json, "") + } + } + catch { + $hash.Add($InputObject, "") + } + } + return $hash +} + +<# +.SYNOPSIS + Get the completion keys. +.DESCRIPTION + According to the input word and data, return the corresponding command keys. + it usually used in the cmdlet `Register-ArgumentCompleter`, when provide datasets, it will return the right completion keys. +.PARAMETER Word + The input word. From `$wordToComplete` +.PARAMETER Ast + The input data. From `$commandAst` +.PARAMETER HashList + The datasets, support basic data types. +.EXAMPLE + Get-CompletionKeys "" "case" "hello","world" + Returns `hello` and `world` +.EXAMPLE + Get-CompletionKeys "h" "case h" "hello","world" + Returns `hello` +.EXAMPLE + Get-CompletionKeys "" "case h" "hello","world" + Returns None. +.INPUTS + None. +.OUTPUTS + System.Array +#> +function Get-CompletionKeys { + Param([string]$Word, $Ast, $HashList) + + if (!$HashList) { + return @() + } + + $arr = $Ast.ToString().Split().ToLower() | Where-Object { $null -ne $_ } + + # Empty, need to return children completion keys + if (!$Word) { + [string]$key = ($arr -join ".").trim(".") + $keyLevel = $arr + } + # Character, need to return sibling completion keys + else { + [string]$key = (($arr | Select-Object -SkipLast 1) -join ".").trim(".") + $keyLevel = $key | ForEach-Object { $_.split(".") } + } + + if (!$CacheAllCompletions.ContainsKey($key)) { + $map = ConvertTo-Hash $HashList + $prefix = "" + $keyLevel | ForEach-Object { + if ($prefix) { + $map = $map[$_] + $prefix = "$prefix.$($_)" + } + else { + $prefix = $_ + } + if (!$CacheAllCompletions.ContainsKey($prefix)) { + $CacheAllCompletions[$prefix] = $map.Keys + } + } + } + + $CacheAllCompletions[$key] | + Where-Object { $_ -Like "*$Word*" } | + Sort-Object -Property @{Expression = { $_.ToString().StartsWith($Word) }; Descending = $true }, @{Expression = { $_.ToString().indexOf($Word) }; Descending = $false }, @{Expression = { $_ }; Descending = $false } +} + +function Remove-Completion { + Param([string]$Command) + + $CacheCommands.Remove($Command) + $CacheAllCompletions.Clone().Keys | + Where-Object { $_.StartsWith("$Command.") -or ($_ -eq $Command) } | + ForEach-Object { $CacheAllCompletions.Remove($_) } +} + +<# +.SYNOPSIS + Register a completion. +.DESCRIPTION + Register a completion. provide the command name and the completion datasets. when type the command name, and press `Tab`, it will show the completion keys. +.PARAMETER Command + The command name. +.PARAMETER HashList + The datasets, support basic data types. +.PARAMETER Force + Enable replaced the existing completion. default is false. +.EXAMPLE + New-Completion demo "hello","world" + Register a completion with command name `demo` and datasets `hello`、`world`. + Press `demo ` will get `demo hello` +.EXAMPLE + New-Completion demo "100" -Force + Replace the existing completion with command name `demo` and datasets `100`. + Press `demo ` will get `demo 100` +.INPUTS + None. +.OUTPUTS + None. +#> +function New-Completion { + Param( + [string]$Command, + $HashList, + [switch]$Force = $false + ) + + if ($CacheCommands.ContainsKey($Command)) { + if ($Force) { + Remove-Completion $Command + } + else { + return + } + } + $CacheCommands.Add($Command, $HashList) + + Register-ArgumentCompleter -Native -CommandName $Command -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new() + + $cmd = $commandAst.CommandElements[0].Value + $cmdHashList = $CacheCommands[$cmd] + + if ($null -ne $cmdHashList) { + Get-CompletionKeys $wordToComplete $commandAst $cmdHashList | + ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } + } + } +} + diff --git a/src/Register-Completion/Register-Completion.psd1 b/src/Register-Completion.psd1 similarity index 95% rename from src/Register-Completion/Register-Completion.psd1 rename to src/Register-Completion.psd1 index 9e72970..1b4f8de 100644 --- a/src/Register-Completion/Register-Completion.psd1 +++ b/src/Register-Completion.psd1 @@ -69,13 +69,13 @@ PowerShellVersion = '5.0' # NestedModules = @() # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. -FunctionsToExport = 'ConvertTo-Hash', 'Get-CompletionKeys', 'Register-Completion' +FunctionsToExport = 'New-Completion', 'Get-CompletionKeys', 'ConvertTo-Hash' # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @() # Variables to export from this module -VariablesToExport = 'cache_all_completion', 'cache_command_list' +VariablesToExport = 'CacheAllCompletions', 'CacheCommands' # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. AliasesToExport = @() @@ -123,7 +123,7 @@ PrivateData = @{ } # End of PrivateData hashtable # HelpInfo URI of this module -# HelpInfoURI = '' +HelpInfoURI = 'https://github.com/aliuq/Register-Completion' # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. # DefaultCommandPrefix = '' diff --git a/src/Register-Completion.psm1 b/src/Register-Completion.psm1 new file mode 100644 index 0000000..2d26586 --- /dev/null +++ b/src/Register-Completion.psm1 @@ -0,0 +1,10 @@ +if (Get-Module New-Completion) { return } + +. $PSScriptRoot\Completion.ps1 + +$exportModuleMemberParams = @{ + Function = @('New-Completion', 'Get-CompletionKeys', 'ConvertTo-Hash') + Variable = @('CacheAllCompletions', 'CacheCommands') +} +Export-ModuleMember @exportModuleMemberParams + diff --git a/src/Register-Completion/Register-Completion.ps1 b/src/Register-Completion/Register-Completion.ps1 deleted file mode 100644 index 74d13cf..0000000 --- a/src/Register-Completion/Register-Completion.ps1 +++ /dev/null @@ -1,116 +0,0 @@ -[hashtable]$cache_all_completion = @{} -[hashtable]$cache_command_list = @{} -$PSVersion = $PSVersionTable.PSVersion - -function ConvertTo-Hash { - Param([PSCustomObject]$InputObject) - [hashtable]$hash = @{} - - if (!$InputObject) { - return "" - } - - $input_type = $InputObject.getType() - - if ($input_type -eq [hashtable]) { - $InputObject.Keys | ForEach-Object { $hash[$_] = ConvertTo-Hash $InputObject[$_] } - } - elseif ($input_type -eq [Object[]]) { - $InputObject | ForEach-Object { $hash += ConvertTo-Hash $_ } - } - else { - try { - $json = ConvertFrom-Json -InputObject $InputObject -AsHashtable - $json_type = $json.getType() - if ($json_type -in [hashtable],[Object[]]) { - $hash = ConvertTo-Hash $json - } - else { - $hash.Add($json, "") - } - } - catch { - $hash.Add($InputObject, "") - } - } - return $hash -} - -function Get-CompletionKeys { - Param($word, $ast, $hash_list) - - if (!$hash_list) { - return @() - } - - $arr = $ast.ToString().Split().ToLower() | Where-Object { $_ -ne $null } - - # Empty, need to return children completion keys - if (!$word) { - [string]$key = ($arr -join ".").trim(".") - $key_level = $arr - } - # Character, need to return sibling completion keys - else { - [string]$key = (($arr | Select-Object -SkipLast 1) -join ".").trim(".") - $key_level = $key | ForEach-Object { $_.split(".") } - } - - if (!$cache_all_completion.ContainsKey($key)) { - $map = ConvertTo-Hash $hash_list - $prefix = "" - $key_level | ForEach-Object { - if ($prefix) { - $map = $map[$_] - $prefix = $prefix + "." + $_ - } - else { - $prefix = $_ - } - if (!$cache_all_completion.ContainsKey($prefix)) { - $cache_all_completion[$prefix] = $map.Keys - } - } - } - - $cache_all_completion[$key] | - Where-Object { $_ -Like "*$word*" } | - Sort-Object -Property @{Expression = { $_.ToString().StartsWith($wordToComplete) }; Descending = $true }, @{Expression = { $_.ToString().indexOf($wordToComplete) }; Descending = $false }, @{Expression = { $_ }; Descending = $false } -} - -function Remove-Completion { - Param([Parameter(Mandatory)][string]$command) - - $cache_command_list.Remove($command) - $cache_all_completion.Clone().Keys | - Where-Object { $_.StartsWith("$command.") -or ($_ -eq $command) } | - ForEach-Object { $cache_all_completion.Remove($_) } -} - -function Register-Completion { - Param($command, $hash_list, [switch]$Force = $false) - - if ($cache_command_list.ContainsKey($command)) { - if ($Force) { - Remove-Completion $command - } - else { - return - } - } - $cache_command_list.Add($command, $hash_list) - - Register-ArgumentCompleter -Native -CommandName $command -ScriptBlock { - param($wordToComplete, $commandAst, $cursorPosition) - [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new() - - $cmd = $commandAst.CommandElements[0].Value - $hash_list = $cache_command_list[$cmd] - - if ($null -ne $hash_list) { - Get-CompletionKeys $wordToComplete $commandAst $hash_list | - ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) } - } - } -} - diff --git a/src/Register-Completion/Register-Completion.psm1 b/src/Register-Completion/Register-Completion.psm1 deleted file mode 100644 index e586fa6..0000000 --- a/src/Register-Completion/Register-Completion.psm1 +++ /dev/null @@ -1,3 +0,0 @@ -if (Get-Module Register-Completion) { return } - -. $PSScriptRoot\Register-Completion.ps1 diff --git a/src/test/Register-Completion.Tests.ps1 b/test/Register-Completion.Tests.ps1 similarity index 51% rename from src/test/Register-Completion.Tests.ps1 rename to test/Register-Completion.Tests.ps1 index 1060f46..b5e4b89 100644 --- a/src/test/Register-Completion.Tests.ps1 +++ b/test/Register-Completion.Tests.ps1 @@ -1,72 +1,87 @@ BeforeAll { - . "$pwd\src\Register-Completion\Register-Completion.ps1" + . "$pwd\src\Completion.ps1" function Compare-Hashtable { Param( - [Hashtable]$hash_object_1, - [Hashtable]$hash_object_2 + [Hashtable]$HashObjectOne, + [Hashtable]$HashObjectTwo ) - if ($hash_object_1.Count -ne $hash_object_2.Count) { + if ($HashObjectOne.Count -ne $HashObjectTwo.Count) { return $false } - $result = $true + $flag = $true - foreach ($key in $hash_object_1.Keys) { - $hash_1 = $hash_object_1[$key] - $hash_2 = $hash_object_2[$key] + foreach ($key in $HashObjectOne.Keys) { + $hashOne = $HashObjectOne[$key] + $hashTwo = $HashObjectTwo[$key] - if ($hash_1.getType() -in [object[]],[hashtable]) { - $result = Compare-Hashtable $hash_1 $hash_2 + if ($hashOne.getType() -in [object[]],[hashtable]) { + $flag = Compare-Hashtable $hashOne $hashTwo } - elseif ([string]::IsNullOrEmpty($hash_1) -And [string]::IsNullOrEmpty($hash_2)) { + elseif ([string]::IsNullOrEmpty($hashOne) -And [string]::IsNullOrEmpty($hashTwo)) { continue } - elseif ($hash_1 -ne $hash_2) { - $result = $false + elseif ($hashOne -ne $hashTwo) { + $flag = $false break } } - return $result + return $flag } } Describe "ConvertTo-Hash" { - It "convert " -ForEach @( - @{ type = "string"; src = "arg"; expected = @{arg = ""} } - @{ type = "number"; src = 100; expected = @{100 = ""} } - @{ type = "number string"; src = "100"; expected = @{100 = ""} } - @{ type = "array number"; src = "[100]"; expected = @{100 = ""} } - @{ type = "array number"; src = "[100,101]"; expected = @{100 = ""; 101 = ""} } - @{ type = "array string"; src = "['hello']"; expected = @{hello = ""} } - @{ type = "array string"; src = "['hello','world']"; expected = @{hello = ""; world = ""} } - @{ type = "array object"; src = "[{arg: 'arg_1'}]"; expected = @{arg = @{arg_1 = ""}} } + It "Convert " -ForEach @( + @{ Type = "string"; Src = "arg"; Expected = @{arg = ""} } + @{ Type = "number"; Src = 100; Expected = @{100 = ""} } + @{ Type = "number string"; Src = "100"; Expected = @{100 = ""} } + @{ Type = "array number"; Src = "[100]"; Expected = @{100 = ""} } + @{ Type = "array number"; Src = "[100,101]"; Expected = @{100 = ""; 101 = ""} } + @{ Type = "array string"; Src = "['hello']"; Expected = @{hello = ""} } + @{ Type = "array string"; Src = "['hello','world']"; Expected = @{hello = ""; world = ""} } + @{ Type = "array object"; Src = "[{arg: 'arg_1'}]"; Expected = @{arg = @{arg_1 = ""}} } @{ - type = "array nested object"; - src = "[{arg: {arg_1: 'arg_1_1'}}]"; - expected = @{arg = @{arg_1 = @{arg_1_1 = ""}}} + Type = "array nested object"; + Src = "[{arg: {arg_1: 'arg_1_1'}}]"; + Expected = @{arg = @{arg_1 = @{arg_1_1 = ""}}} } @{ - type = "array nested object array"; - src = "[{arg: {arg_1: {arg_1_1: ['arg_1_1_1', 'arg_1_1_2']}}}]"; - expected = @{arg = @{arg_1 = @{arg_1_1 = @{arg_1_1_1 = ""; arg_1_1_2 = ""}}}} + Type = "array nested object array"; + Src = "[{arg: {arg_1: {arg_1_1: ['arg_1_1_1', 'arg_1_1_2']}}}]"; + Expected = @{arg = @{arg_1 = @{arg_1_1 = @{arg_1_1_1 = ""; arg_1_1_2 = ""}}}} } @{ - type = "array number、string、object、array"; - src = "[100, 'hello', {arg1: 'arg1_1'}, ['arg2', 'arg3']]"; - expected = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = ""} + Type = "array number、string、object、array"; + Src = "[100, 'hello', {arg1: 'arg1_1'}, ['arg2', 'arg3']]"; + Expected = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = ""} } @{ - type = "hashtable number、string、hashtable、list"; - src = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = ""} - expected = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = ""} + Type = "hashtable number、string、hashtable、list"; + Src = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = ""}; + Expected = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = ""} } @{ - type = "object number、string、object、array"; - src = "{a:1,b:2,c:['c1','c2',{c3:{c3_1:'c3_1_1',c3_2:['c3_2_1','c3_2_2']}}]}"; - expected = @{a = @{1 = ""}; b = @{2 = ""}; c = @{c1 = ""; c2 = ""; c3 = @{c3_1 = @{c3_1_1 = ""}; c3_2 = @{c3_2_1 = ""; c3_2_2 = ""}}}} + Type = "powershell list"; + Src = @("arg1", "arg2"); + Expected = @{arg1 = ""; arg2 = ""} + } + @{ + Type = "powershell list hashtable"; + Src = @("arg1", @{arg2 = "arg2_1"; arg3 = @("arg3_1", "arg3_2")}); + Expected = @{arg1 = ""; arg2 = @{arg2_1 = ""}; arg3 = @{arg3_1 = ""; arg3_2 = ""}} + } + @{ + Type = "hashtable nested data"; + Src = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = @("arg3_1", "arg3_2")}; + Expected = @{100 = ""; hello = ""; arg1 = @{arg1_1 = ""}; arg2 = ""; arg3 = @{arg3_1 = ""; arg3_2 = ""}} + } + @{ + Type = "object number、string、object、array"; + Src = "{a:1,b:2,c:['c1','c2',{c3:{c3_1:'c3_1_1',c3_2:['c3_2_1','c3_2_2']}}]}"; + Expected = @{a = @{1 = ""}; b = @{2 = ""}; c = @{c1 = ""; c2 = ""; c3 = @{c3_1 = @{c3_1_1 = ""}; c3_2 = @{c3_2_1 = ""; c3_2_2 = ""}}}} } ) { $hash = ConvertTo-Hash $src @@ -77,8 +92,8 @@ Describe "ConvertTo-Hash" { Describe "Test Cases" { It "test number" { $list = 100 - Register-Completion case $list - $cache_command_list["case"] | Should -Be 100 + New-Completion case $list + $CacheCommands["case"] | Should -Be 100 Get-CompletionKeys "" "case" $list | Should -Be @(100) Get-CompletionKeys "1" "case 1" $list | Should -Be @(100) @@ -86,20 +101,20 @@ Describe "Test Cases" { Get-CompletionKeys "2" "case 2" $list | Should -Be @() Get-CompletionKeys "" "case 2" $list | Should -Be @() - $cache_all_completion["case"] | Should -Be $((ConvertTo-Hash $list).Keys) - Register-Completion case 1001 - $cache_command_list["case"] | Should -Be 100 - Register-Completion case 1001 -Force - $cache_command_list.case | Should -Be 1001 + $CacheAllCompletions["case"] | Should -Be $((ConvertTo-Hash $list).Keys) + New-Completion case 1001 + $CacheCommands["case"] | Should -Be 100 + New-Completion case 1001 -Force + $CacheCommands.case | Should -Be 1001 Remove-Completion case - $cache_command_list["case"] | Should -Be $null - $cache_all_completion["case"] | Should -Be $null + $CacheCommands["case"] | Should -Be $null + $CacheAllCompletions["case"] | Should -Be $null } It "test number string" { $list = "100" - Register-Completion case $list - $cache_command_list["case"] | Should -Be "100" + New-Completion case $list + $CacheCommands["case"] | Should -Be "100" Get-CompletionKeys "" "case" $list | Should -Be @("100") Get-CompletionKeys "1" "case 1" $list | Should -Be @("100") @@ -107,20 +122,20 @@ Describe "Test Cases" { Get-CompletionKeys "2" "case 2" $list | Should -Be @() Get-CompletionKeys "" "case 2" $list | Should -Be @() - $cache_all_completion["case"] | Should -Be $((ConvertTo-Hash $list).Keys) - Register-Completion case "1001" - $cache_command_list["case"] | Should -Be "100" - Register-Completion case "1001" -Force - $cache_command_list.case | Should -Be "1001" + $CacheAllCompletions["case"] | Should -Be $((ConvertTo-Hash $list).Keys) + New-Completion case "1001" + $CacheCommands["case"] | Should -Be "100" + New-Completion case "1001" -Force + $CacheCommands.case | Should -Be "1001" Remove-Completion case - $cache_command_list["case"] | Should -Be $null - $cache_all_completion["case"] | Should -Be $null + $CacheCommands["case"] | Should -Be $null + $CacheAllCompletions["case"] | Should -Be $null } It "test string" { $list = "world" - Register-Completion case $list - $cache_command_list["case"] | Should -Be "world" + New-Completion case $list + $CacheCommands["case"] | Should -Be "world" Get-CompletionKeys "" "case" $list | Should -Be @("world") Get-CompletionKeys "w" "case w" $list | Should -Be @("world") @@ -128,32 +143,30 @@ Describe "Test Cases" { Get-CompletionKeys "c" "case c" $list | Should -Be @() Get-CompletionKeys "" "case c" $list | Should -Be @() - $cache_all_completion["case"] | Should -Be $((ConvertTo-Hash $list).Keys) - Register-Completion case "world new" - $cache_command_list["case"] | Should -Be "world" - Register-Completion case "world new" -Force - $cache_command_list.case | Should -Be "world new" + $CacheAllCompletions["case"] | Should -Be $((ConvertTo-Hash $list).Keys) + New-Completion case "world new" + $CacheCommands["case"] | Should -Be "world" + New-Completion case "world new" -Force + $CacheCommands.case | Should -Be "world new" Remove-Completion case - $cache_command_list["case"] | Should -Be $null - $cache_all_completion["case"] | Should -Be $null + $CacheCommands["case"] | Should -Be $null + $CacheAllCompletions["case"] | Should -Be $null } It "test " -ForEach @( @{ - type = "js array"; - list = "['arg1','arg2','arg3']" - list_new = "['arg1_new','arg2_new','arg3_new']" + Type = "js array"; + List = "['arg1','arg2','arg3']" + ListNew = "['arg1_new','arg2_new','arg3_new']" } @{ - type = "powershell list"; - list = "arg1","arg2","arg3" - list_new = "arg1_new","arg2_new","arg3_new" + Type = "powershell list"; + List = "arg1","arg2","arg3" + ListNew = "arg1_new","arg2_new","arg3_new" } ) { - $list = "['arg1','arg2','arg3']" - $list_new = "['arg1_new','arg2_new','arg3_new']" - Register-Completion case $list - $cache_command_list["case"] | Should -Be $list + New-Completion case $list + $CacheCommands["case"] | Should -Be $list Get-CompletionKeys "" "case" $list | Should -Be @("arg1","arg2","arg3") Get-CompletionKeys "2" "case 2" $list | Should -Be @("arg2") @@ -163,20 +176,20 @@ Describe "Test Cases" { Get-CompletionKeys "arg1" "case arg1" $list | Should -Be @("arg1") Get-CompletionKeys "" "case arg1" $list | Should -Be @() - $cache_all_completion["case"] | Should -Be $((ConvertTo-Hash $list).Keys) - Register-Completion case $list_new - $cache_command_list["case"] | Should -Be $list - Register-Completion case $list_new -Force - $cache_command_list.case | Should -Be $list_new + $CacheAllCompletions["case"] | Should -Be $((ConvertTo-Hash $list).Keys) + New-Completion case $listNew + $CacheCommands["case"] | Should -Be $list + New-Completion case $listNew -Force + $CacheCommands.case | Should -Be $listNew Remove-Completion case - $cache_command_list["case"] | Should -Be $null - $cache_all_completion["case"] | Should -Be $null + $CacheCommands["case"] | Should -Be $null + $CacheAllCompletions["case"] | Should -Be $null } It "test " -ForEach @( @{ - type = "js object"; - list = "{ + Type = "js object"; + List = "{ 'arg1': 'arg1_1', 'arg2': 'arg2_2', 'arg3': { @@ -184,7 +197,7 @@ Describe "Test Cases" { 'arg3_2': 'arg3_2_1' } }"; - list_new = "{ + ListNew = "{ 'arg1': 'arg1_1_new', 'arg2_new': 'arg2_2', 'arg3': { @@ -194,8 +207,8 @@ Describe "Test Cases" { }" } @{ - type = "powershell hashtable"; - list = @{ + Type = "powershell hashtable"; + List = @{ arg1 = "arg1_1"; arg2 = "arg2_2"; arg3 = @{ @@ -203,7 +216,7 @@ Describe "Test Cases" { arg3_2 = "arg3_2_1" } }; - list_new = @{ + ListNew = @{ arg1 = "arg1_1_new"; arg2_new = "arg2_2"; arg3 = @{ @@ -213,8 +226,8 @@ Describe "Test Cases" { } } ) { - Register-Completion case $list - $cache_command_list["case"] | Should -Be $list + New-Completion case $list + $CacheCommands["case"] | Should -Be $list Get-CompletionKeys "" "case" $list | Should -Be @("arg1","arg2","arg3") Get-CompletionKeys "2" "case 2" $list | Should -Be @("arg2") @@ -224,20 +237,20 @@ Describe "Test Cases" { Get-CompletionKeys "1" "case arg3 arg3_1 1" $list | Should -Be @("arg3_1_1") Get-CompletionKeys "" "case arg3 arg3_1 1" $list | Should -Be @() - $cache_all_completion["case"] | Should -Be $((ConvertTo-Hash $list).Keys) - Register-Completion case $list_new - $cache_command_list["case"] | Should -Be $list - Register-Completion case $list_new -Force - $cache_command_list.case | Should -Be $list_new + $CacheAllCompletions["case"] | Should -Be $((ConvertTo-Hash $list).Keys) + New-Completion case $listNew + $CacheCommands["case"] | Should -Be $list + New-Completion case $listNew -Force + $CacheCommands.case | Should -Be $listNew Remove-Completion case - $cache_command_list["case"] | Should -Be $null - $cache_all_completion["case"] | Should -Be $null + $CacheCommands["case"] | Should -Be $null + $CacheAllCompletions["case"] | Should -Be $null } It "test " -ForEach @( @{ - type = "js object array"; - list = "{ + Type = "js object array"; + List = "{ 'arg1': [ 'arg1_1', { arg1_2: 'arg1_2_1' } @@ -248,7 +261,7 @@ Describe "Test Cases" { 'arg3_2': 'arg3_2_1' } }"; - list_new = "{ + ListNew = "{ 'arg1_new': [ 'arg1_1', { arg1_2: 'arg1_2_1' } @@ -261,8 +274,8 @@ Describe "Test Cases" { }" } @{ - type = "powershell hashtable list"; - list = @{ + Type = "powershell hashtable list"; + List = @{ arg1 = "arg1_1",@{arg1_2 = "arg1_2_1"}; arg2 = "arg2_2"; arg3 = @{ @@ -270,7 +283,7 @@ Describe "Test Cases" { arg3_2 = "arg3_2_1" } }; - list_new = @{ + ListNew = @{ arg1_new = "arg1_1",@{arg1_2 = "arg1_2_1"}; arg2 = "arg2_2_new"; arg3 = @{ @@ -280,8 +293,8 @@ Describe "Test Cases" { } } ) { - Register-Completion case $list - $cache_command_list["case"] | Should -Be $list + New-Completion case $list + $CacheCommands["case"] | Should -Be $list Get-CompletionKeys "" "case" $list | Should -Be @("arg1","arg2","arg3") Get-CompletionKeys "2" "case 2" $list | Should -Be @("arg2") @@ -297,46 +310,46 @@ Describe "Test Cases" { Get-CompletionKeys "2" "case arg3 arg3_1 2" $list | Should -Be @("arg3_1_2") Get-CompletionKeys "" "case arg3 arg3_1 2" $list | Should -Be @() - $cache_all_completion["case"] | Should -Be $((ConvertTo-Hash $list).Keys) - Register-Completion case $list_new - $cache_command_list["case"] | Should -Be $list - Register-Completion case $list_new -Force - $cache_command_list.case | Should -Be $list_new + $CacheAllCompletions["case"] | Should -Be $((ConvertTo-Hash $list).Keys) + New-Completion case $listNew + $CacheCommands["case"] | Should -Be $list + New-Completion case $listNew -Force + $CacheCommands.case | Should -Be $listNew Remove-Completion case - $cache_command_list["case"] | Should -Be $null - $cache_all_completion["case"] | Should -Be $null + $CacheCommands["case"] | Should -Be $null + $CacheAllCompletions["case"] | Should -Be $null } It "test " -ForEach @( @{ - type = "js array object"; - list = "[ + Type = "js array object"; + List = "[ 'arg1', { 'arg2': 'arg2_2' }, ['arg3', {'arg4': 'arg4_1'}] ]"; - list_new = "[ + ListNew = "[ 'arg1', { 'arg2': 'arg2_2_new' }, ['arg3', {'arg4_new': 'arg4_1'}] ]" } @{ - type = "powershell list hashtable"; - list = @( + Type = "powershell list hashtable"; + List = @( 'arg1', @{ arg2 = "arg2_2" }, @('arg3', @{arg4 = "arg4_1"}) ); - list_new = @( + ListNew = @( 'arg1', @{ arg2 = "arg2_2_new" }, @('arg3', @{arg4_new = "arg4_1"}) ) } ) { - Register-Completion case $list - $cache_command_list["case"] | Should -Be $list + New-Completion case $list + $CacheCommands["case"] | Should -Be $list Get-CompletionKeys "" "case" $list | Should -Be @("arg1","arg2","arg3","arg4") Get-CompletionKeys "2" "case 2" $list | Should -Be @("arg2") @@ -347,14 +360,14 @@ Describe "Test Cases" { Get-CompletionKeys "" "case arg4" $list | Should -Be @("arg4_1") Get-CompletionKeys "" "case arg4 2" $list | Should -Be @() - $cache_all_completion["case"] | Should -Be $((ConvertTo-Hash $list).Keys) - Register-Completion case $list_new - $cache_command_list["case"] | Should -Be $list - Register-Completion case $list_new -Force - $cache_command_list.case | Should -Be $list_new + $CacheAllCompletions["case"] | Should -Be $((ConvertTo-Hash $list).Keys) + New-Completion case $listNew + $CacheCommands["case"] | Should -Be $list + New-Completion case $listNew -Force + $CacheCommands.case | Should -Be $listNew Remove-Completion case - $cache_command_list["case"] | Should -Be $null - $cache_all_completion["case"] | Should -Be $null + $CacheCommands["case"] | Should -Be $null + $CacheAllCompletions["case"] | Should -Be $null } }