-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp_test.go
More file actions
309 lines (271 loc) · 7.47 KB
/
help_test.go
File metadata and controls
309 lines (271 loc) · 7.47 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package verbs
import (
"strings"
"testing"
)
func interceptHelp(cli *CLI) *string {
helpText := new(string)
cli.OnHelp = func(ht string) {
*helpText = ht
}
return helpText
}
func expectHelpContains(t *testing.T, helpText, expectedHelpText string) {
if !strings.Contains(helpText, expectedHelpText) {
t.Fatalf("Expected the help text to contain \"%s\", "+
"got \"%s\"", expectedHelpText, helpText)
}
}
func TestHelpCommandErrors(t *testing.T) {
cli := setupGitCLI()
errorMessage := interceptErrors(cli)
parser := NewParser(cli)
parser.Parse(strings.Split("git help --unknown", " "))
expectError(t, *errorMessage, "unknown option '--unknown'")
parser.Parse(strings.Split("git help --force", " "))
expectError(t, *errorMessage, "option '--force' is not expected here")
parser.Parse(strings.Split("git help unknown", " "))
expectError(t, *errorMessage, "unrecognized name 'unknown'")
parser.Parse(strings.Split("git help remote add unknown", " "))
expectError(t, *errorMessage, "unexpected argument 'unknown'")
}
func TestHelpTopic(t *testing.T) {
cli := setupGitCLI()
cli.HelpTopics = []*HelpTopic{{
Keyword: "tutorial",
Text: "This is a tutorial on how to use git.",
}}
helpText := interceptHelp(cli)
parser := NewParser(cli)
parser.Parse(strings.Split("git help tutorial", " "))
expectHelpContains(t, *helpText, "how to use git")
}
func TestHelpScreenArgumentOnly(t *testing.T) {
cli := setupGrepCLI()
var helpText string
cli.OnHelp = func(text string) {
helpText = text
}
args := []string{"grep", "--help"}
NewParser(cli).Parse(args)
// Check for key elements
if !strings.Contains(helpText, "Usage:\n"+
" grep [options] PATTERN [FILE...]") {
t.Error("Help text should contain usage line")
}
if !strings.Contains(helpText, "Search for patterns in files") {
t.Error("Help text should contain summary")
}
if !strings.Contains(helpText, "Options:") {
t.Error("Help text should contain options section")
}
if !strings.Contains(helpText, "--ignore-case") {
t.Error("Help text should contain option names")
}
}
func TestHelpScreenCommandDriven(t *testing.T) {
cli := &CLI{
ProgramName: "git",
Summary: "Version control system",
Description: "Git is a distributed version control system.",
Commands: []*Command{
{
Name: "add",
Summary: "Add files to the index",
Description: "This command updates the index.",
Options: []*Option{
{
Name: "f|force",
Description: "Force add",
},
},
Args: []*Arg{
{
Name: "FILE",
Occurrence: ZeroOrMore,
Description: "Files to add",
},
},
},
{
Name: "commit",
Summary: "Record changes",
},
},
}
var helpText string
cli.OnHelp = func(text string) {
helpText = text
}
args := []string{"git", "--help"}
NewParser(cli).Parse(args)
// Check for key elements
if !strings.Contains(helpText, "Usage:\n git") {
t.Error("Help text should contain usage line")
}
if !strings.Contains(helpText, "add") {
t.Error("Help text should contain command names")
}
if !strings.Contains(helpText, "commit") {
t.Error("Help text should contain all commands")
}
}
func TestHelpScreenCommandSpecific(t *testing.T) {
cli := setupGitCLI()
var helpText string
cli.OnHelp = func(text string) {
helpText = text
}
args := strings.Split("git help remote add", " ")
result := NewParser(cli).Parse(args)
if result.Command != nil {
t.Errorf("Expected nil command, got %v", result.Command)
}
if len(result.OptsAndArgs) != 0 {
t.Errorf("Expected 0 parsed items, got %d",
len(result.OptsAndArgs))
}
// Check for command-specific help
if !strings.Contains(helpText, "Add a remote repository") {
t.Error("Help text should contain command summary")
}
if !strings.Contains(helpText, "Usage:\n git remote add") {
t.Error("Help text should contain usage")
}
if !strings.Contains(helpText, "-f") {
t.Error("Help text should contain command options")
}
}
func TestHelpScreenWithNamespaces(t *testing.T) {
cli := &CLI{
ProgramName: "git",
Summary: "Version control",
Namespaces: []*Namespace{
{
Name: "remote",
Summary: "Manage remotes",
Commands: []*Command{
{Name: "add", Summary: "Add a remote"},
{Name: "remove", Summary: "Remove a remote"},
},
},
},
}
var helpText string
cli.OnHelp = func(text string) {
helpText = text
}
args := []string{"git", "--help"}
NewParser(cli).Parse(args)
// Namespaces should appear in help
if !strings.Contains(helpText, "remote") {
t.Error("Help text should contain namespace names")
}
}
func TestArgumentFormatting(t *testing.T) {
t.Run("Required and OneOrMore arguments", func(t *testing.T) {
cli := &CLI{
ProgramName: "test",
Args: []*Arg{
{
Name: "CMD",
Description: "The command to run",
Occurrence: Optional,
},
{
Name: "ARG",
Description: "Command arguments",
Occurrence: ZeroOrMore,
},
},
}
helpText := interceptHelp(cli)
NewParser(cli).Parse([]string{"test", "--help"})
expectHelpContains(t, *helpText,
"Usage:\n test [CMD] [ARG...]\n")
expectHelpContains(t, *helpText, "\n [CMD] ")
expectHelpContains(t, *helpText, "\n [ARG...] ")
})
t.Run("Optional and OneOrMore arguments", func(t *testing.T) {
cli := &CLI{
ProgramName: "test",
Args: []*Arg{
{
Name: "FILE",
Description: "The files to copy",
Occurrence: OneOrMore,
},
{
Name: "DIR",
Description: "The directory to copy to",
Occurrence: Required,
},
},
}
helpText := interceptHelp(cli)
NewParser(cli).Parse([]string{"test", "--help"})
expectHelpContains(t, *helpText, "Usage:\n test FILE... DIR\n")
expectHelpContains(t, *helpText, "\n FILE... ")
expectHelpContains(t, *helpText, "\n DIR ")
})
}
func TestVersionHandling(t *testing.T) {
cli := &CLI{ProgramName: "test", Version: "1.2.3"}
helpText := interceptHelp(cli)
t.Run("version option", func(t *testing.T) {
NewParser(cli).Parse([]string{"test", "--version"})
expectHelpContains(t, *helpText, "1.2.3")
})
t.Run("version prompt", func(t *testing.T) {
NewParser(cli).Parse([]string{"test", "--help"})
expectHelpContains(t, *helpText,
"Run 'test --version' for version info.")
})
}
func TestWordWrapping(t *testing.T) {
var helpText string
cli := &CLI{
ProgramName: "test",
Summary: "A very long summary that should be wrapped " +
"properly when it exceeds the help text width limit",
HelpTextWidth: 40,
OnHelp: func(text string) {
helpText = text
},
}
args := []string{"test", "--help"}
NewParser(cli).Parse(args)
if helpText == "" {
t.Fatal("Expected help text, got empty string")
}
// Check that lines are wrapped (rough check)
for _, line := range strings.Split(helpText, "\n") {
// With width 40, most lines should be wrapped
// This is a basic check - more sophisticated
// tests would verify exact wrapping
if len(line) > 40 {
t.Error("Line should be wrapped")
}
}
}
func TestOptionAliases(t *testing.T) {
cli := &CLI{
ProgramName: "test",
Options: []*Option{
{Name: "v|verbose|debug", Description: "Be verbose"},
},
}
var helpText string
cli.OnHelp = func(text string) {
helpText = text
}
args := []string{"test", "--help"}
NewParser(cli).Parse(args)
// Check that aliases are shown
if !strings.Contains(helpText, "--verbose") {
t.Error("Help should show option aliases")
}
if !strings.Contains(helpText, "--debug") {
t.Error("Help should show all option aliases")
}
}