-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion_test.go
More file actions
212 lines (175 loc) · 5.02 KB
/
completion_test.go
File metadata and controls
212 lines (175 loc) · 5.02 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cli
import (
"strings"
"testing"
)
// TestCompletionBash tests bash completion functionality
func TestCompletionBash(t *testing.T) {
root := Root("myapp").
Description("Test app")
deploy := Cmd("deploy").
Description("Deploy command")
database := Cmd("database").
Description("Database command")
root.AddCommand(deploy)
root.AddCommand(database)
bash := &BashCompletion{}
t.Run("GetCompletions returns subcommands", func(t *testing.T) {
completions := bash.GetCompletions(root, nil)
if len(completions) < 2 {
t.Errorf("expected at least 2 completions, got %d", len(completions))
}
hasDeploy := false
hasDatabase := false
for _, c := range completions {
if c == "deploy" {
hasDeploy = true
}
if c == "database" {
hasDatabase = true
}
}
if !hasDeploy {
t.Error("completions should include 'deploy'")
}
if !hasDatabase {
t.Error("completions should include 'database'")
}
})
t.Run("GenerateScript returns valid bash script", func(t *testing.T) {
script := bash.GenerateScript(root)
if script == "" {
t.Error("script should not be empty")
}
if !strings.Contains(script, "myapp") {
t.Error("script should contain command name")
}
if !strings.Contains(script, "completion") {
t.Error("script should contain completion logic")
}
})
}
// TestCompletionZsh tests zsh completion functionality
func TestCompletionZsh(t *testing.T) {
root := Root("myapp")
zsh := &ZshCompletion{}
script := zsh.GenerateScript(root)
if script == "" {
t.Error("zsh script should not be empty")
}
if !strings.Contains(script, "myapp") {
t.Error("zsh script should contain command name")
}
}
// TestCompletionFish tests fish completion functionality
func TestCompletionFish(t *testing.T) {
root := Root("myapp")
fish := &FishCompletion{}
script := fish.GenerateScript(root)
if script == "" {
t.Error("fish script should not be empty")
}
if !strings.Contains(script, "myapp") {
t.Error("fish script should contain command name")
}
}
// TestCompletionPowerShell tests PowerShell completion functionality
func TestCompletionPowerShell(t *testing.T) {
root := Root("myapp")
ps := &PowerShellCompletion{}
script := ps.GenerateScript(root)
if script == "" {
t.Error("PowerShell script should not be empty")
}
if !strings.Contains(script, "myapp") {
t.Error("PowerShell script should contain command name")
}
}
// TestCompletionWithFlags tests that completions include flags
func TestCompletionWithFlags(t *testing.T) {
var verbose bool
var timeout int
root := Root("myapp").
Flag(&verbose, "verbose", "v", false, "Verbose").
Flag(&timeout, "timeout", "t", 30, "Timeout")
bash := &BashCompletion{}
completions := bash.GetCompletions(root, nil)
hasVerbose := false
hasTimeout := false
for _, c := range completions {
if c == "--verbose" || c == "-v" {
hasVerbose = true
}
if c == "--timeout" || c == "-t" {
hasTimeout = true
}
}
if !hasVerbose {
t.Error("completions should include verbose flag")
}
if !hasTimeout {
t.Error("completions should include timeout flag")
}
}
// TestCompletionHiddenCommandsExcluded tests that hidden commands are excluded
func TestCompletionHiddenCommandsExcluded(t *testing.T) {
root := Root("myapp")
visible := Cmd("deploy").
Description("Deploy command")
hidden := Cmd("debug").
Description("Debug command").
Hidden()
root.AddCommand(visible)
root.AddCommand(hidden)
bash := &BashCompletion{}
completions := bash.GetCompletions(root, nil)
for _, c := range completions {
if c == "debug" {
t.Error("hidden command 'debug' should not be in completions")
}
}
}
// TestCompletionRegistersSubcommands tests that completion registers for all subcommands
func TestCompletionRegistersSubcommands(t *testing.T) {
root := Root("myapp")
deploy := Cmd("deploy")
database := Cmd("database")
root.AddCommand(deploy)
root.AddCommand(database)
bash := &BashCompletion{}
bash.Register(root)
// Check that __bashcomplete was added to root
if _, exists := root.subcommands["__bashcomplete"]; !exists {
t.Error("root should have __bashcomplete command")
}
// Check that __bashcomplete was added to subcommands
if _, exists := deploy.subcommands["__bashcomplete"]; !exists {
t.Error("deploy should have __bashcomplete command")
}
if _, exists := database.subcommands["__bashcomplete"]; !exists {
t.Error("database should have __bashcomplete command")
}
}
// TestAddCompletion tests the AddCompletion helper
func TestAddCompletion(t *testing.T) {
root := Root("myapp")
deploy := Cmd("deploy")
root.AddCommand(deploy)
// Add all completion types
AddCompletion(root)
// Check that all completion commands were registered
completionCommands := []string{
"__bashcomplete",
"__zshcomplete",
"__fishcomplete",
"__powershellcomplete",
}
for _, cmdName := range completionCommands {
if _, exists := root.subcommands[cmdName]; !exists {
t.Errorf("root should have %s command", cmdName)
}
if _, exists := deploy.subcommands[cmdName]; !exists {
t.Errorf("deploy should have %s command", cmdName)
}
}
}