-
Notifications
You must be signed in to change notification settings - Fork 0
/
args_test.go
78 lines (73 loc) · 2.17 KB
/
args_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
package cli
import (
"fmt"
"strings"
"testing"
"github.com/awee-ai/structs"
"github.com/stretchr/testify/assert"
)
func Test_Args(t *testing.T) {
tests := []struct {
name string
args []string
structure any
expectedArgs []string
unknownArgs []string
expectedOptions map[string]any
unknownOptions map[string]any
}{
{
name: "no struct fields given produces --help with value",
args: []string{"init", "arg1", "arg2", "-v", "2", "--help", "beep", "--boop"},
expectedArgs: []string{},
unknownArgs: []string{"init", "arg1", "arg2"},
expectedOptions: map[string]any{},
unknownOptions: map[string]any{
"v": "2",
"help": "beep",
"boop": true,
},
},
{
name: "global options",
args: []string{"init", "arg1", "arg2", "-v", "2", "--help", "beep"},
structure: &GlobalOptions{},
expectedArgs: []string{},
unknownArgs: []string{"init", "arg1", "arg2", "beep"},
expectedOptions: map[string]any{
"v": "2",
"help": true,
},
unknownOptions: map[string]any{},
},
{
name: "global options",
args: []string{"init", "arg1", "arg2", "-v", "2", "--help", "beep"},
structure: &GlobalOptions{},
expectedArgs: []string{},
unknownArgs: []string{"init", "arg1", "arg2", "beep"},
expectedOptions: map[string]any{
"v": "2",
"help": true,
},
unknownOptions: map[string]any{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fmt.Println("test:", tt.name, "args: ", strings.Join(tt.args, " "))
var fields = make([]structs.Field, 0)
if tt.structure != nil {
var err error
fields, err = structs.GetStructFields(tt.structure, nil)
assert.NoError(t, err)
}
args, unknownArgs, options, unknownOptions := getCommandArgs(tt.args, fields)
// spew.Dump(args, unknownArgs, options, unknownOptions)
assert.Equal(t, tt.expectedArgs, args, "expected args")
assert.Equal(t, tt.unknownArgs, unknownArgs, "unknown args")
assert.Equal(t, tt.expectedOptions, options, "expected options")
assert.Equal(t, tt.unknownOptions, unknownOptions, "unknown options")
})
}
}