-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
260 lines (239 loc) · 5.5 KB
/
types_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
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
package fcli_test
import (
"context"
"errors"
"flag"
"fmt"
"strings"
"sync"
"testing"
"github.com/berquerant/fcli"
"github.com/stretchr/testify/assert"
)
func testTargetFunctionCallTarget() {}
func TestTargetFunctionCallName(t *testing.T) {
s, err := fcli.NewTargetFunction(testTargetFunctionCallTarget)
if !assert.Nil(t, err) {
return
}
assert.Equal(t, s.Name(), "testTargetFunctionCallTarget")
}
type targetFunctionTestcaseResult struct {
result []any
sync.Mutex
}
var targetFunctionTestcaseResultInstance targetFunctionTestcaseResult
func getTargetFunctionTestcaseResult() []any { return targetFunctionTestcaseResultInstance.result }
func setTargetFunctionTestcaseResult(v []any) { targetFunctionTestcaseResultInstance.result = v }
type targetFunctionCallTestcase struct {
name string
f any // func(...)
args []string
wantArgsP func(*testing.T, []any)
newErr error
callErr error
}
func variadic(_ int, _ ...any) {}
func withOutputParam() any { return nil }
func unsupportedInputParamType(_ uintptr) {}
func withoutInputParams() {}
func singleIntInput(i int) {
setTargetFunctionTestcaseResult([]any{i})
}
func allBasic(b bool,
i int, i8 int8, i16 int16, i32 int32, i64 int64,
u uint, u8 uint8, u16 uint16, u32 uint32, u64 uint64,
f32 float32, f64 float64,
str string) {
setTargetFunctionTestcaseResult([]any{
b,
i, i8, i16, i32, i64,
u, u8, u16, u32, u64,
f32, f64,
str,
})
}
type stringList struct {
list []string
}
func (*stringList) UnmarshalFlag(v string) (fcli.CustomFlagUnmarshaller, error) {
return &stringList{
list: strings.Split(v, ","),
}, nil
}
type failUnmarshaller struct{}
func (*failUnmarshaller) UnmarshalFlag(_ string) (fcli.CustomFlagUnmarshaller, error) {
return nil, fmt.Errorf("fail unmarshaller")
}
func customStringList(list *stringList) {
setTargetFunctionTestcaseResult([]any{list})
}
func customFlagFailure(v *failUnmarshaller) {}
func int8LimitCheck(i8 int8) {}
func withContextAndError(ctx context.Context) error {
v := ctx.Value("ctx")
setTargetFunctionTestcaseResult([]any{v})
return nil
}
var errReturnError = errors.New("return error")
func returnError() error { return errReturnError }
func TestTargetFunctionCall(t *testing.T) {
fcli.SetVerboseLevel(2)
defer fcli.SetVerboseLevel(0)
for _, tc := range []targetFunctionCallTestcase{
{
name: "not a function",
f: 1,
newErr: fcli.ErrBadTargetFunction,
},
{
name: "variadic",
f: variadic,
newErr: fcli.ErrBadTargetFunction,
},
{
name: "with output params not error",
f: withOutputParam,
newErr: fcli.ErrBadTargetFunction,
},
{
name: "unsupported input param type",
f: unsupportedInputParamType,
newErr: fcli.ErrBadTargetFunction,
},
{
name: "no input params",
f: withoutInputParams,
wantArgsP: func(_ *testing.T, _ []any) {},
},
{
name: "return error",
f: returnError,
callErr: errReturnError,
},
{
name: "context value",
f: withContextAndError,
wantArgsP: func(t *testing.T, v []any) {
assert.Equal(t, []any{1}, v)
},
},
{
name: "int",
f: singleIntInput,
args: []string{
"-i", "1",
},
wantArgsP: func(t *testing.T, v []any) {
assert.Equal(t, []any{1}, v)
},
},
{
name: "int default",
f: singleIntInput,
args: []string{},
wantArgsP: func(t *testing.T, v []any) {
assert.Equal(t, []any{0}, v)
},
},
{
name: "int call failure",
f: singleIntInput,
args: []string{
"-i", "INT",
},
callErr: fcli.ErrCallFailure,
},
{
name: "all basic",
f: allBasic,
args: []string{
"-b",
"-i", "1",
"-i8", "8",
"-i16", "16",
"-i32", "32",
"-i64", "64",
"-u", "2",
"-u8", "9",
"-u16", "17",
"-u32", "33",
"-u64", "65",
"-f32", "37.1",
"-f64", "67.2",
"-str", "all-basic",
},
wantArgsP: func(t *testing.T, v []any) {
want := []any{
true,
int(1),
int8(8),
int16(16),
int32(32),
int64(64),
uint(2),
uint8(9),
uint16(17),
uint32(33),
uint64(65),
float32(37.1),
float64(67.2),
"all-basic",
}
assert.Equal(t, want, v)
},
},
{
name: "string list",
f: customStringList,
args: []string{
"-list", "a,b,c",
},
wantArgsP: func(t *testing.T, v []any) {
if !assert.Equal(t, 1, len(v), v) {
return
}
x := v[0]
y, ok := x.(*stringList)
if !assert.True(t, ok, x) {
return
}
assert.Equal(t, []string{"a", "b", "c"}, y.list)
},
},
{
name: "custom flag failure",
f: customFlagFailure,
args: []string{"-v", "fail"},
callErr: fcli.ErrCallFailure,
},
{
name: "int8 limit",
f: int8LimitCheck,
args: []string{
"-i8", "128",
},
callErr: fcli.ErrCallFailure,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
targetFunctionTestcaseResultInstance.Lock()
defer targetFunctionTestcaseResultInstance.Unlock()
s, err := fcli.NewTargetFunction(tc.f, fcli.WithErrorHandling(flag.ContinueOnError))
assert.ErrorIs(t, err, tc.newErr, "new error")
if tc.newErr != nil {
t.Logf("new error %v", err)
return
}
err = s.CallWithContext(context.WithValue(context.TODO(), "ctx", 1), tc.args)
assert.ErrorIs(t, err, tc.callErr, "call error")
if tc.callErr != nil {
t.Logf("call error %v", err)
return
}
tc.wantArgsP(t, getTargetFunctionTestcaseResult())
setTargetFunctionTestcaseResult(nil)
})
}
}