-
Notifications
You must be signed in to change notification settings - Fork 0
/
getopt_fixture_test.go
255 lines (228 loc) · 5.49 KB
/
getopt_fixture_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
package getopt
import (
"encoding/json"
"errors"
"fmt"
"os"
"slices"
"testing"
)
const fixturePath = "testdata/fixtures.json"
func TestGetOpt_Fixtures(t *testing.T) {
fixtureFile, err := os.Open(fixturePath)
if err != nil {
t.Fatalf("error opening fixtures file: %v", err)
}
defer fixtureFile.Close()
decoder := json.NewDecoder(fixtureFile)
// read open bracket
_, err = decoder.Token()
if err != nil {
t.Fatalf("error decoding cases: %v", err)
}
// while the array contains values
for decoder.More() {
var f fixture
if err := decoder.Decode(&f); err != nil {
t.Fatalf("error decoding fixture: %v", err)
}
testName := fmt.Sprintf("%s %s %s)", f.Label, funcString(f.Func), modeString(f.Mode))
t.Run(testName, func(t *testing.T) {
assertFixture(t, f)
})
}
// read closing bracket
_, err = decoder.Token()
if err != nil {
t.Fatalf("error decoding cases: %v", err)
}
}
func funcString(f Func) string {
switch f {
case FuncGetOpt:
return "getopt"
case FuncGetOptLong:
return "getopt_long"
case FuncGetOptLongOnly:
return "getopt_long_only"
default:
return "unknown"
}
}
func modeString(m Mode) string {
switch m {
case ModeGNU:
return "gnu"
case ModePOSIX:
return "posix"
case ModeInOrder:
return "inorder"
default:
return "unknown"
}
}
func assertFixture(t testing.TB, f fixture) {
t.Helper()
s := NewState(f.Args)
c := Config{
Opts: f.Opts,
LongOpts: f.LongOpts,
Mode: f.Mode,
Func: f.Func,
}
for iter, want := range f.WantResults {
res, err := s.GetOpt(c)
if want.Err == nil {
if err != nil {
t.Errorf("iter %d, wanted no error, but got %q", iter, err)
}
} else {
if err == nil {
t.Errorf("iter %d, wanted an error, but didn't get one", iter)
} else if !errors.Is(err, want.Err) {
t.Errorf("iter %d, got error %q, but wanted %q", iter, err, want.Err)
}
}
if res.Char != want.Char {
t.Errorf("iter %d, got Char %q, but wanted %q", iter, res.Char, want.Char)
}
if res.Name != want.Name {
t.Errorf("iter %d, got Name %q, but wanted %q", iter, res.Name, want.Name)
}
if res.OptArg != want.OptArg {
t.Errorf("iter %d, got OptArg %q, but wanted %q", iter, res.OptArg, want.OptArg)
}
}
if s.optInd != f.WantOptInd {
t.Errorf("got optInd %d, but wanted %d", s.optInd, f.WantOptInd)
}
if !slices.Equal(s.args, f.WantArgs) {
t.Errorf("got Args %+q, but wanted %+q", s.args, f.WantArgs)
}
}
type fixtureResult struct {
Char rune
Name string
OptArg string
Err error
}
type fixture struct {
Label string `json:"label"`
Func Func `json:"func"`
Mode Mode `json:"mode"`
Args []string `json:"args"`
Opts []Opt `json:"opts"`
LongOpts []LongOpt `json:"lopts"`
WantArgs []string `json:"want_args"`
WantOptInd int `json:"want_optind"`
WantResults []fixtureResult `json:"want_results"`
}
func (f *fixture) UnmarshalJSON(data []byte) error {
type alias fixture
aux := &struct {
Func string `json:"func"`
Mode string `json:"mode"`
Opts []json.RawMessage `json:"opts"`
LongOpts []json.RawMessage `json:"lopts"`
WantResults []json.RawMessage `json:"want_results"`
*alias
}{
alias: (*alias)(f),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
switch aux.Func {
case "getopt":
f.Func = FuncGetOpt
case "getopt_long":
f.Func = FuncGetOptLong
case "getopt_long_only":
f.Func = FuncGetOptLongOnly
default:
return fmt.Errorf("invalid Func: %s", aux.Func)
}
switch aux.Mode {
case "gnu":
f.Mode = ModeGNU
case "posix":
f.Mode = ModePOSIX
case "inorder":
f.Mode = ModeInOrder
}
f.Opts = make([]Opt, len(aux.Opts))
for i, raw := range aux.Opts {
var jsonOpt struct {
Char int `json:"char"`
HasArg string `json:"has_arg"`
}
err := json.Unmarshal(raw, &jsonOpt)
if err != nil {
return err
}
f.Opts[i].Char = rune(jsonOpt.Char)
f.Opts[i].HasArg, err = parseHasArg(jsonOpt.HasArg)
if err != nil {
return err
}
}
f.LongOpts = make([]LongOpt, len(aux.LongOpts))
for i, raw := range aux.LongOpts {
var jsonLongOpt struct {
Name string `json:"name"`
HasArg string `json:"has_arg"`
}
err := json.Unmarshal(raw, &jsonLongOpt)
if err != nil {
return err
}
f.LongOpts[i].Name = jsonLongOpt.Name
f.LongOpts[i].HasArg, err = parseHasArg(jsonLongOpt.HasArg)
if err != nil {
return err
}
}
f.WantResults = make([]fixtureResult, len(aux.WantResults))
for i, raw := range aux.WantResults {
var jsonResult struct {
Char int `json:"char"`
Name string `json:"name"`
OptArg string `json:"optarg"`
Err string `json:"err"`
}
if err := json.Unmarshal(raw, &jsonResult); err != nil {
return err
}
f.WantResults[i].Char = rune(jsonResult.Char)
f.WantResults[i].Name = jsonResult.Name
f.WantResults[i].OptArg = jsonResult.OptArg
f.WantResults[i].Err = parseErr(jsonResult.Err)
}
return nil
}
func parseErr(err string) error {
switch err {
case "done":
return ErrDone
case "unknown_opt":
return ErrUnknownOpt
case "missing_opt_arg":
return ErrMissingOptArg
case "illegal_opt_arg":
return ErrIllegalOptArg
default:
return nil
}
}
func parseHasArg(str string) (HasArg, error) {
switch str {
case "no_argument":
return NoArgument, nil
case "required_argument":
return RequiredArgument, nil
case "optional_argument":
return OptionalArgument, nil
default:
return 0, fmt.Errorf("invalid HasArg: %q", str)
}
}