-
Notifications
You must be signed in to change notification settings - Fork 53
/
docopts_test.go
369 lines (341 loc) · 7.95 KB
/
docopts_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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// vim: set ts=4 sw=4 sts=4 noet:
//
// unit test for docopts.go
//
package main
import (
"bytes"
"errors"
"reflect"
"strings"
"testing"
// our json loader for common_input_test.json
"fmt"
"github.com/docopt/docopts/test_json_load"
)
func TestShellquote(t *testing.T) {
tables := []struct {
input string
expect string
}{
{"pipo", "pipo"},
{"i''i", "i'\\'''\\''i"},
{"'pipo'", "'\\''pipo'\\''"},
}
for _, table := range tables {
str := Shellquote(table.input)
if str != table.expect {
t.Errorf("Shellquote error, got: %s, want: %s.", str, table.expect)
}
}
}
func TestIsBashIdentifier(t *testing.T) {
tables := []struct {
input string
expect bool
}{
{"pipo", true},
{"i''i", false},
{"'\\''pipo'\\''", false},
{"OK", true},
{"ARGS", true},
// unsecable space at first char
{" ARGS", false},
{"123", false},
{"var%%", false},
{"varname ", false},
{"var name", false},
{"", false},
{"--", false},
}
for _, table := range tables {
res := IsBashIdentifier(table.input)
if res != table.expect {
t.Errorf("IsBashIdentifier for '%s', got: %v, want: %v.", table.input, res, table.expect)
}
}
}
func TestIsArray(t *testing.T) {
tables := []struct {
input interface{}
expect bool
}{
{[]string{"pipo", "molo", "--clip"}, true},
{"pipo", false},
{42, false},
{[3]int{1, 2, 3}, true},
}
for _, table := range tables {
rt := reflect.TypeOf(table.input)
res := IsArray(rt)
if res != table.expect {
t.Errorf("IsArray for '%v', got: %v, want: %v.", table.input, res, table.expect)
}
}
}
func TestPrint_bash_args(t *testing.T) {
// replace out (os.Stdout) by a buffer
bak := out
out = new(bytes.Buffer)
defer func() { out = bak }()
//tables := []struct{
// input map[string]interface{}
// expect []string
//}{
// {
// map[string]interface{}{ "FILE" : []string{"pipo", "molo", "toto"} },
// []string{
// "declare -A args",
// "args['FILE,0']='pipo'",
// "args['FILE,1']='molo'",
// "args['FILE,2']='toto'",
// "args['FILE,#']=3",
// },
// },
// {
// map[string]interface{}{ "--counter" : 2 },
// []string{
// "declare -A args",
// "args['--counter']=2",
// },
// },
// {
// map[string]interface{}{ "--counter" : "2" },
// []string{
// "declare -A args",
// "args['--counter']='2'",
// },
// },
// {
// map[string]interface{}{ "bool" : true },
// []string{
// "declare -A args",
// "args['bool']=true",
// },
// },
//}
d := &Docopts{
Global_prefix: "",
Mangle_key: true,
Output_declare: true,
}
tables, _ := test_json_loader.Load_json("./common_input_test.json")
for _, table := range tables {
d.Print_bash_args("args", table.Input)
res := out.(*bytes.Buffer).String()
expect := strings.Join(table.Expect_args[:], "\n") + "\n"
if res != expect {
t.Errorf("Print_bash_args for '%v'\ngot: '%v'\nwant: '%v'\n", table.Input, res, expect)
}
out.(*bytes.Buffer).Reset()
}
}
func TestTo_bash(t *testing.T) {
tables := []struct {
input interface{}
expect string
}{
{"pipo", "'pipo'"},
{"i''i", "'i'\\'''\\''i'"},
{123, "123"},
{nil, ""},
{"", "''"},
{[]string{"pipo", "molo"}, "('pipo' 'molo')"},
{true, "true"},
}
for _, table := range tables {
res := To_bash(table.input)
if res != table.expect {
t.Errorf("To_bash for '%s', got: %v, want: %v.", table.input, res, table.expect)
}
}
}
// helpers compose no-mangle output for matching test
func rewrite_not_mangled(input map[string]interface{}) string {
var out string
for _, k := range Sort_args_keys(input) {
v := input[k]
out += fmt.Sprintf("%s=%s\n", k, To_bash(v))
}
return out
}
func rewrite_prefix(prefix string, expected []string) string {
var out string
for _, l := range expected {
out += fmt.Sprintf("%s_%s\n", prefix, l)
}
return out
}
func TestPrint_bash_global(t *testing.T) {
// replace out (os.Stdout) by a buffer
bak := out
out = new(bytes.Buffer)
defer func() { out = bak }()
// now loads test from a JSON file
tables, _ := test_json_loader.Load_json("./common_input_test.json")
// static tables format
//tables := []struct{
// input map[string]interface{}
// expect []string
//}{
// {
// map[string]interface{}{ "FILE" : []string{"pipo", "molo", "toto"} },
// []string{
// "FILE=('pipo' 'molo' 'toto')",
// },
// },
// {
// map[string]interface{}{ "--counter" : 2 },
// []string{
// "counter=2",
// },
// },
// {
// map[string]interface{}{ "--counter" : "2" },
// []string{
// "counter='2'",
// },
// },
// {
// map[string]interface{}{ "bool" : true },
// []string{
// "bool=true",
// },
// },
//}
var err error
// with Mangle_key
d := &Docopts{
Global_prefix: "",
Mangle_key: true,
}
for _, table := range tables {
err = d.Print_bash_global(table.Input)
if err != nil {
t.Errorf("Print_bash_global doesn't return nil for err: %v\n", err)
}
res := out.(*bytes.Buffer).String()
expect := strings.Join(table.Expect_global[:], "\n") + "\n"
if res != expect {
t.Errorf("Print_bash_global for '%v'\ngot: '%v'\nwant: '%v'\n", table.Input, res, expect)
}
out.(*bytes.Buffer).Reset()
}
// without Mangle_key --no-mangle
d = &Docopts{
Global_prefix: "",
Mangle_key: false,
}
for _, table := range tables {
err = d.Print_bash_global(table.Input)
if err != nil {
t.Errorf("Print_bash_global doesn't return nil for err: %v\n", err)
}
res := out.(*bytes.Buffer).String()
expect := rewrite_not_mangled(table.Input)
if res != expect {
t.Errorf("Mangle_key false: Print_bash_global for '%v'\ngot: '%v'\nwant: '%v'\n", table.Input, res, expect)
}
out.(*bytes.Buffer).Reset()
}
// with Mangle_key and Global_prefix
d = &Docopts{
Global_prefix: "ARGS",
Mangle_key: true,
}
for _, table := range tables {
err = d.Print_bash_global(table.Input)
if err != nil {
t.Errorf("Print_bash_global doesn't return nil for err: %v\n", err)
}
res := out.(*bytes.Buffer).String()
var expect string
if len(table.Expect_global_prefix) > 0 {
// if special case was provided
expect = strings.Join(table.Expect_global_prefix[:], "\n") + "\n"
} else {
expect = rewrite_prefix("ARGS", table.Expect_global)
}
if res != expect {
t.Errorf("with prefix: Print_bash_global for '%v'\ngot: '%v'\nwant: '%v'\n", table.Input, res, expect)
}
out.(*bytes.Buffer).Reset()
}
// with Mangle_key plus name collision
input_args := make(map[string]interface{})
input_args["--long-option"] = true
input_args["<long-option>"] = "dummy_value"
err = d.Print_bash_global(input_args)
if err == nil {
t.Errorf("Print_bash_global expecting err on duplicate Mangle_key options")
}
out.(*bytes.Buffer).Reset()
}
type Expected struct {
s string
e error
}
func TestName_mangle(t *testing.T) {
tables := []struct {
input string
expect Expected
}{
{
"FILE",
Expected{s: "FILE", e: nil},
},
{
"--counter",
Expected{s: "counter", e: nil},
},
{
"--counter-strike",
Expected{s: "counter_strike", e: nil},
},
{
"--",
Expected{s: "", e: errors.New("fail")},
},
{
"<key_word>",
Expected{s: "key_word", e: nil},
},
{
"<key-word>",
Expected{s: "key_word", e: nil},
},
{
"-A",
Expected{s: "A", e: nil},
},
{
"-9",
Expected{s: "", e: errors.New("fail")},
},
{
"CamelCase",
Expected{s: "CamelCase", e: nil},
},
{
"SCREAMING_SNAKE_CASE",
Expected{s: "SCREAMING_SNAKE_CASE", e: nil},
},
{
"l-i-s-p",
Expected{s: "l_i_s_p", e: nil},
},
}
d := &Docopts{
Global_prefix: "",
Mangle_key: true,
}
for _, table := range tables {
res, err := d.Name_mangle(table.input)
if table.expect.e != nil && err == nil {
t.Errorf("Name_mangle for '%v'\ngot: '%v'\nwant: '%v'\n", table.input, err, table.expect.e)
}
if res != table.expect.s {
t.Errorf("Name_mangle for '%v'\ngot: '%v'\nwant: '%v'\n", table.input, res, table.expect.s)
}
}
}