-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclic_test.go
190 lines (169 loc) · 3.6 KB
/
clic_test.go
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
package clic
import (
"bytes"
"context"
"errors"
"fmt"
"reflect"
"testing"
"github.com/daved/clic/operandset"
)
func defaultPtrs[T any](args ...T) []any {
var ptrs []any
for _, arg := range args {
a := arg // must copy
ptrs = append(ptrs, &a)
}
return ptrs
}
func TestClicParse(t *testing.T) {
type parseScope struct {
name string
clicFn func(*bytes.Buffer, *[]any) *Clic
}
scopeA := parseScope{
name: "clic subcmd-opt opnd0-req opnd1-opt",
clicFn: func(buf *bytes.Buffer, ptrs *[]any) *Clic {
return NewCmdClic(buf, "myapp", nil,
NewCmdClic(buf, "subcmd",
func(os *operandset.OperandSet) {
*ptrs = defaultPtrs("default0", "default1")
os.Operand((*ptrs)[0], true, "first_opnd", "")
os.Operand((*ptrs)[1], false, "second_opnd", "")
},
),
)
},
}
scopeB := parseScope{
name: "clic subcmd-req opnd0-req opnd1-opt",
clicFn: func(buf *bytes.Buffer, ptrs *[]any) *Clic {
c := NewCmdClic(buf, "myapp", nil,
NewCmdClic(buf, "subcmd",
func(os *operandset.OperandSet) {
*ptrs = defaultPtrs("default0", "default1")
os.Operand((*ptrs)[0], true, "first_opnd", "")
os.Operand((*ptrs)[1], false, "second_opnd", "")
},
),
)
c.SubRequired = true
return c
},
}
tt := []struct {
scope parseScope
name string
args []string
out string
vals []any
cause error
}{
{
scope: scopeA,
name: "subcmd-one opnds-both",
args: []string{
"myapp", "subcmd", "--info=flagval",
"first", "second",
},
out: "subcmd",
vals: []any{"first", "second"},
},
{
scope: scopeA,
name: "subcmd-one opnds-none",
args: []string{
"myapp", "subcmd", "--info=flagval",
},
cause: CauseOperandMissing,
},
{
scope: scopeA,
name: "subcmd-one opnds-first",
args: []string{
"myapp", "subcmd", "--info=flagval",
"first",
},
out: "subcmd",
vals: []any{"first", "default1"},
},
{
scope: scopeB,
name: "subcmd-one opnds-both",
args: []string{
"myapp", "subcmd", "--info=flagval",
"first", "second",
},
out: "subcmd",
vals: []any{"first", "second"},
},
{
scope: scopeB,
name: "subcmd-none opnds-both",
args: []string{
"myapp", "--info=flagval",
"first", "second",
},
cause: CauseSubRequired,
},
{
scope: scopeB,
name: "subcmd-none opnds-none",
args: []string{
"myapp",
},
cause: CauseSubRequired,
},
}
for _, tc := range tt {
t.Run(tc.scope.name+"/"+tc.name, func(t *testing.T) {
buf := &bytes.Buffer{}
var ptrs []any
c := tc.scope.clicFn(buf, &ptrs)
err := c.Parse(tc.args[1:])
if !errors.Is(err, tc.cause) {
t.Fatalf("parse error: got %v, want %v", err, tc.cause)
}
if err != nil {
return
}
_ = c.HandleResolvedCmd(context.Background())
out := buf.String()
if out != tc.out {
t.Fatalf("output: got: %v, want: %v", out, tc.out)
}
for i, ptr := range ptrs {
got := reflect.ValueOf(ptr).Elem()
want := reflect.ValueOf(tc.vals[i])
if !got.Equal(want) {
t.Fatalf("vals: got: %v, want: %v", got, want)
}
}
})
}
}
type Cmd struct {
buf *bytes.Buffer
name string
}
type setupFunc func(*operandset.OperandSet)
func NewCmdClic(buf *bytes.Buffer, name string, fn setupFunc, subs ...*Clic) *Clic {
cmd := &Cmd{
buf: buf,
name: name,
}
var info string
var num int
c := New(cmd, name, subs...)
c.Flag(&info, "info|i", "")
c.Flag(&num, "num|n", "")
if fn != nil {
fn(c.OperandSet)
}
return c
}
func (cmd *Cmd) HandleCommand(ctx context.Context) error {
cmd.buf.Reset()
fmt.Fprintf(cmd.buf, "%s", cmd.name)
return nil
}