-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconsole_test.go
More file actions
319 lines (296 loc) · 8.01 KB
/
console_test.go
File metadata and controls
319 lines (296 loc) · 8.01 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
310
311
312
313
314
315
316
317
318
319
package command
import (
"os"
"path/filepath"
"testing"
)
func TestFilterPositionalArgs_legacyDoesNotEatNextToken(t *testing.T) {
// 原规则:-b 不吞 bar,bar 仍为位置参数
got := filterPositionalArgs([]string{"-a=1", "foo", "-b", "bar", "baz"})
want := []string{"foo", "bar", "baz"}
if len(got) != len(want) {
t.Fatalf("len got %d want %d", len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("got[%d]=%q want %q", i, got[i], want[i])
}
}
}
func TestFilterPositionalArgs_doubleDash(t *testing.T) {
got := filterPositionalArgs([]string{"-x=1", "a", "--", "-y", "b"})
want := []string{"a", "-y", "b"}
if len(got) != len(want) {
t.Fatalf("len got %d want %d: %#v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("got[%d]=%q want %q", i, got[i], want[i])
}
}
}
func TestParsed_dashAgeSpaceDoesNotBindValue(t *testing.T) {
c := testConsole()
inp := Input{
console: c,
Has: map[string]bool{},
Argument: map[string]string{},
Option: map[string][]string{},
}
cfg := Argument{
Option: []ArgParam{{Name: "age", Default: "0", Description: ""}},
}
err := inp.Parsed(cfg, []string{"--age", "20"})
if err != nil {
t.Fatal(err)
}
if inp.GetOption("age") != "" {
t.Fatalf("want empty age, got %q", inp.GetOption("age"))
}
}
func TestParsed_longFormEqSetsOption(t *testing.T) {
c := testConsole()
inp := Input{
console: c,
Has: map[string]bool{},
Argument: map[string]string{},
Option: map[string][]string{},
}
cfg := Argument{
Option: []ArgParam{{Name: "age", Default: "0", Description: ""}},
}
err := inp.Parsed(cfg, []string{"--age=99"})
if err != nil {
t.Fatal(err)
}
if inp.GetOption("age") != "99" {
t.Fatalf("got %q", inp.GetOption("age"))
}
}
func TestArgTokenSet(t *testing.T) {
m := argTokenSet([]string{"a", "b", "a"})
if _, ok := m["a"]; !ok {
t.Fatal("expected a")
}
if _, ok := m["b"]; !ok {
t.Fatal("expected b")
}
if len(m) != 2 {
t.Fatalf("unique tokens: got %d want 2", len(m))
}
}
// testConsole 无 -h 回调,避免单测中触发 os.Exit
func testConsole() *Console {
return &Console{
MapCommand: map[string]MapCommand{},
baseOption: []ArgParam{},
baseHas: []ArgParam{
{Name: "-d", Description: "daemon"},
},
}
}
func TestParsed_baseHasAndHas(t *testing.T) {
c := testConsole()
inp := Input{
console: c,
Has: map[string]bool{},
Argument: map[string]string{},
Option: map[string][]string{},
}
cfg := Argument{
Has: []ArgParam{{Name: "one", Description: ""}},
}
err := inp.Parsed(cfg, []string{"-d", "one"})
if err != nil {
t.Fatal(err)
}
if !inp.GetHas("-d") || !inp.GetHas("one") {
t.Fatalf("Has: %#v", inp.Has)
}
}
func TestParsedOptions_multiDefaultSameName(t *testing.T) {
c := testConsole()
inp := Input{
console: c,
Has: map[string]bool{},
Argument: map[string]string{},
Option: map[string][]string{},
}
cfg := Argument{
Option: []ArgParam{
{Name: "age", Default: "18"},
{Name: "age", Default: "24"},
},
}
inp.ParsedOptions(cfg, []string{})
got := inp.GetOptions("age")
if len(got) != 2 || got[0] != "18" || got[1] != "24" {
t.Fatalf("age options: %#v", got)
}
}
func TestIniLoad_typesAndRawStringFallback(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "c.ini")
content := "; c\n[sec]\n" +
"k=42\n" +
"f=3.14\n" +
"b=true\n" +
"raw=notanumber\n"
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
var i ini
if err := i.Load(path); err != nil {
t.Fatal(err)
}
if i.GetInt("sec.k", 0) != 42 {
t.Fatalf("int got %v", i.GetInt("sec.k", 0))
}
if i.GetString("sec.raw", "") != "notanumber" {
t.Fatalf("raw string got %q", i.GetString("sec.raw", ""))
}
if !i.GetBool("sec.b", false) {
t.Fatal("expected bool true")
}
f := i.config["sec.f"].(float64)
if f < 3.13 || f > 3.15 {
t.Fatalf("float got %v", f)
}
}
func TestIniLoad_missingFileIsOK(t *testing.T) {
var i ini
err := i.Load(filepath.Join(t.TempDir(), "none.ini"))
if err != nil {
t.Fatal(err)
}
}
// demoIniCmd 用于 Console + INI 集成测试
type demoIniCmd struct{}
func (demoIniCmd) Configure() Configure {
return Configure{
Name: "demo",
Description: "ini default",
Input: Argument{
Option: []ArgParam{
{Name: "url", Description: "", Default: "fallback"},
{Name: "port", Description: "", Default: "9000"},
},
},
}
}
func (demoIniCmd) Execute(input Input) {}
func TestAddCommand_iniOverridesOptionDefault(t *testing.T) {
dir := t.TempDir()
iniPath := filepath.Join(dir, "app.ini")
if err := os.WriteFile(iniPath, []byte("url=\"from-ini\"\nport=\"6000\"\n"), 0644); err != nil {
t.Fatal(err)
}
c := New()
c.SetConfig(iniPath)
if err := c.IniConfig(); err != nil {
t.Fatal(err)
}
c.AddCommand(demoIniCmd{})
mc := c.MapCommand["demo"]
opts := mc.CommandConfig.Input.Option
if len(opts) < 2 {
t.Fatalf("options: %d", len(opts))
}
if opts[0].Default != "from-ini" {
t.Fatalf("url default got %q", opts[0].Default)
}
if opts[1].Default != "6000" {
t.Fatalf("port default got %q", opts[1].Default)
}
}
func TestParsed_missingRequiredArgument(t *testing.T) {
c := testConsole()
inp := Input{console: c, Has: map[string]bool{}, Argument: map[string]string{}, Option: map[string][]string{}}
cfg := Argument{
Argument: []ArgParam{{Name: "a", Description: ""}, {Name: "b", Description: ""}},
}
err := inp.Parsed(cfg, []string{"only-one"})
if err == nil {
t.Fatal("expected error")
}
}
func TestParsed_commandLineOverridesIniDefault(t *testing.T) {
c := testConsole()
// 模拟 AddCommand 后的 Default 已由 ini 写成 from-ini
inp := Input{console: c, Has: map[string]bool{}, Argument: map[string]string{}, Option: map[string][]string{}}
cfg := Argument{
Option: []ArgParam{{Name: "url", Default: "from-ini", Description: ""}},
}
if err := inp.Parsed(cfg, []string{"-url=cli"}); err != nil {
t.Fatal(err)
}
if inp.GetOption("url") != "cli" {
t.Fatalf("got %q", inp.GetOption("url"))
}
}
func TestParsed_repeatOptionAppendsSlice(t *testing.T) {
c := testConsole()
inp := Input{console: c, Has: map[string]bool{}, Argument: map[string]string{}, Option: map[string][]string{}}
cfg := Argument{
Option: []ArgParam{{Name: "tag", Default: "", Description: ""}},
}
if err := inp.Parsed(cfg, []string{"-tag=a", "-tag=b"}); err != nil {
t.Fatal(err)
}
got := inp.GetOptions("tag")
if len(got) != 2 || got[0] != "a" || got[1] != "b" {
t.Fatalf("%#v", got)
}
}
func TestParsed_IsDaemonDoubleHyphen(t *testing.T) {
c := &Console{
MapCommand: map[string]MapCommand{},
baseOption: []ArgParam{},
baseHas: []ArgParam{
{Name: "-d", Description: ""},
{Name: "--d", Description: ""},
},
}
inp := Input{console: c, Has: map[string]bool{}, Argument: map[string]string{}, Option: map[string][]string{}}
if err := inp.Parsed(Argument{}, []string{"--d"}); err != nil {
t.Fatal(err)
}
if !inp.IsDaemon() {
t.Fatal("IsDaemon should be true for --d")
}
}
func TestParsed_mixedOptionAndPositionalOrder(t *testing.T) {
c := testConsole()
inp := Input{console: c, Has: map[string]bool{}, Argument: map[string]string{}, Option: map[string][]string{}}
cfg := Argument{
Argument: []ArgParam{{Name: "x", Description: ""}, {Name: "y", Description: ""}},
Option: []ArgParam{{Name: "n", Default: "0", Description: ""}},
}
if err := inp.Parsed(cfg, []string{"-n=5", "a", "b"}); err != nil {
t.Fatal(err)
}
if inp.GetArgument("x") != "a" || inp.GetArgument("y") != "b" {
t.Fatalf("%v %v", inp.GetArgument("x"), inp.GetArgument("y"))
}
if inp.GetOption("n") != "5" {
t.Fatalf("n=%q", inp.GetOption("n"))
}
}
func TestInput_GetFilePath(t *testing.T) {
inp := Input{FilePath: "/tmp/prog"}
if inp.GetFilePath() != "/tmp/prog" {
t.Fatal(inp.GetFilePath())
}
}
func TestGetOptions_unknownKeyEmptySlice(t *testing.T) {
inp := Input{Option: map[string][]string{}}
if len(inp.GetOptions("nope")) != 0 {
t.Fatal()
}
}
func TestGetArgument_unknownEmpty(t *testing.T) {
inp := Input{Argument: map[string]string{}}
if inp.GetArgument("nope") != "" {
t.Fatal()
}
}