|
| 1 | +BeforeAll { |
| 2 | + Import-Module $PSScriptRoot/posh-vcpkg |
| 3 | +} |
| 4 | + |
| 5 | +Describe 'Module posh-vcpkg tests' { |
| 6 | + |
| 7 | + BeforeAll { |
| 8 | + |
| 9 | + function Complete-InputCaret { |
| 10 | + [OutputType([System.Management.Automation.CommandCompletion])] |
| 11 | + param ( |
| 12 | + [Parameter(Mandatory, Position = 0, ValueFromPipeline)] |
| 13 | + [string]$caretCursorCommand |
| 14 | + ) |
| 15 | + $positionMatches = [regex]::Matches($caretCursorCommand, '\^') |
| 16 | + if ($positionMatches.Count -ne 1) { |
| 17 | + throw 'Invalid caret cursor command, please indicate by only one ^ character' |
| 18 | + } |
| 19 | + else { |
| 20 | + $command = [string]$caretCursorCommand.Replace('^', ''); |
| 21 | + $cursorPosition = [int]$positionMatches[0].Index; |
| 22 | + return [System.Management.Automation.CommandCompletion]::CompleteInput($command, $cursorPosition, $null) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + function Expand-CompletionText { |
| 27 | + param ( |
| 28 | + [Parameter(Mandatory, Position = 0, ValueFromPipeline)] |
| 29 | + [System.Management.Automation.CommandCompletion]$CommandCompletion |
| 30 | + ) |
| 31 | + return $CommandCompletion.CompletionMatches | Select-Object -ExpandProperty CompletionText |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + Context 'Internal function tests' { |
| 37 | + |
| 38 | + It 'Complete-InputCaret 1 caret string should success' { |
| 39 | + 'aaaa^' | Complete-InputCaret | Should -Not -BeNullOrEmpty |
| 40 | + } |
| 41 | + |
| 42 | + It 'Complete-InputCaret 0 caret string should throw' { |
| 43 | + { 'aaaa' | Complete-InputCaret } | Should -Throw |
| 44 | + } |
| 45 | + |
| 46 | + It 'Complete-InputCaret 2 caret string should throw' { |
| 47 | + { 'aaaa^^' | Complete-InputCaret } | Should -Throw |
| 48 | + } |
| 49 | + |
| 50 | + It 'Expand-CompletionText self should success' { |
| 51 | + $inputStr = 'Expand-CompletionText^' |
| 52 | + $res = $inputStr | Complete-InputCaret |
| 53 | + $res | Expand-CompletionText | Should -Contain 'Expand-CompletionText' |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + Context 'Complete command name tests' { |
| 59 | + |
| 60 | + It 'Should complete command word <comment> [<caretCmd>]' -TestCases ( |
| 61 | + @{ comment = 'without word'; caretCmd = 'vcpkg ^'; expectedContain = 'version' }, |
| 62 | + @{ comment = 'with word'; caretCmd = 'vcpkg ver^'; expectedContain = 'version' }, |
| 63 | + @{ comment = 'with native exe'; caretCmd = 'vcpkg.exe ver^'; expectedContain = 'version' }, |
| 64 | + @{ comment = 'with dot slash'; caretCmd = './vcpkg ver^'; expectedContain = 'version' }, |
| 65 | + @{ comment = 'with dot backslash'; caretCmd = '.\vcpkg ver^'; expectedContain = 'version' } |
| 66 | + ) { |
| 67 | + param($caretCmd, $expectedContain, $comment) |
| 68 | + $caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + Context 'Complete command spaces tests' { |
| 74 | + |
| 75 | + It 'Should complete command <comment> [<caretCmd>]' -TestCases ( |
| 76 | + @{ comment = 'spaces without argument'; caretCmd = 'vcpkg ^'; expectedContain = 'version' }, |
| 77 | + @{ comment = 'before remaining'; caretCmd = 'vcpkg ver^'; expectedContain = 'version' }, |
| 78 | + # @{ comment = 'with trailing spaces'; caretCmd = 'vcpkg ver ^'; expectedContain = 'version' }, |
| 79 | + @{ comment = 'with leading spaces'; caretCmd = ' vcpkg ver^'; expectedContain = 'version' } |
| 80 | + ) { |
| 81 | + param($caretCmd, $expectedContain, $comment) |
| 82 | + $caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain |
| 83 | + } |
| 84 | + |
| 85 | + It 'Should complete command with trailing spaces [vcpkg ver ^]' -Skip { |
| 86 | + 'vcpkg ver ^' | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version' |
| 87 | + } |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + Context 'Complete command quotation tests' -Skip { |
| 92 | + |
| 93 | + It "Should complete command with quoted word [vcpkg 'ver'^]" { |
| 94 | + "vcpkg 'ver'^" | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version' |
| 95 | + } |
| 96 | + |
| 97 | + It "Should complete command with quoted space [vcpkg ' '^]" { |
| 98 | + "vcpkg 'ver'^" | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version' |
| 99 | + } |
| 100 | + |
| 101 | + It "Should complete command with quoted word [vcpkg 'version'^]" { |
| 102 | + "vcpkg 'ver'^" | Complete-InputCaret | Expand-CompletionText | Should -Contain 'version' |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + Context 'Complete command intermediate tests' { |
| 108 | + |
| 109 | + It 'Should complete command <comment> [<caretCmd>]' -TestCases ( |
| 110 | + @{ comment = 'end of word'; caretCmd = 'vcpkg version^'; expectedContain = 'version' }, |
| 111 | + @{ comment = 'middle of word'; caretCmd = 'vcpkg ver^sion'; expectedContain = 'version' }, |
| 112 | + @{ comment = 'front of word'; caretCmd = 'vcpkg ^version'; expectedContain = 'version' } |
| 113 | + ) { |
| 114 | + param($caretCmd, $expectedContain, $comment) |
| 115 | + $caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + Context 'Complete subcommand tests' { |
| 121 | + |
| 122 | + It 'Should complete subcommand [<expected>] from [<caretCmd>]' -TestCases ( |
| 123 | + @{ caretCmd = 'vcpkg depend^'; expected = 'depend-info' }, |
| 124 | + @{ caretCmd = 'vcpkg inst^'; expected = 'install' }, |
| 125 | + @{ caretCmd = 'vcpkg int^'; expected = 'integrate' }, |
| 126 | + @{ caretCmd = 'vcpkg rem^'; expected = 'remove' } |
| 127 | + ) { |
| 128 | + param($caretCmd, $expected) |
| 129 | + @($caretCmd | Complete-InputCaret | Expand-CompletionText)[0] | Should -BeExactly $expected |
| 130 | + } |
| 131 | + |
| 132 | + It 'Should complete subcommand two-level [powershell] from [vcpkg integrate power^]' -Skip { |
| 133 | + 'vcpkg integrate power^' | Complete-InputCaret | Expand-CompletionText | Should -Contain 'powershell' |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + Context 'Complete subcommand argument and options tests' -Skip { |
| 139 | + |
| 140 | + It 'Should complete argument [<expected>] from [<caretCmd>]' -TestCases ( |
| 141 | + @{ caretCmd = 'vcpkg install vcpkg-cmake^'; expectedContain = 'vcpkg-cmake-get-vars' }, |
| 142 | + @{ caretCmd = 'vcpkg install vcpkg-cmake --^'; expectedContain = '--dry-run' } |
| 143 | + ) { |
| 144 | + param($caretCmd, $expected) |
| 145 | + $caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + Context 'Complete complex tests' { |
| 151 | + |
| 152 | + It 'Should complete complex line [<expected>] from [<caretCmd>]' -TestCases ( |
| 153 | + @{ caretCmd = 'echo powershell | % { vcpkg ver^ $_ }; echo $?'; expectedContain = 'version' } |
| 154 | + ) { |
| 155 | + param($caretCmd, $expected) |
| 156 | + $caretCmd | Complete-InputCaret | Expand-CompletionText | Should -Contain $expectedContain |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments