-
Notifications
You must be signed in to change notification settings - Fork 0
/
opt_test.go
140 lines (126 loc) · 3.1 KB
/
opt_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
package opt
import (
"os"
"strings"
"testing"
)
// The testArgs is example of the test args.
type testArgs struct {
Host string `opt:"host" alt:"h" def:"localhost"`
Port int `opt:"p" alt:"port" def:"8080"`
UserName string // default opt == user-name
Ignored bool `opt:"-"`
Verbose bool `opt:"v" alt:"verb" def:"true"`
Path string `opt:"0"`
PosSlcie []int `opt:"[]"`
PosArray [8]int `opt:"[]"`
Help string `opt:"?"`
}
// TestLongFlags tests Unmarshal with long flags.
func TestLongFlags(t *testing.T) {
split := func(str string) []string { return strings.Split(str, ":") }
tests := []struct {
Args []string
Expected testArgs
}{
{
// ./app --host=0.0.0.0 --port 80 --user-name John --no-verb
split("./app:--host=0.0.0.0:--port:80:--user-name:John:--no-verb"),
testArgs{
Host: "0.0.0.0",
Port: 80,
UserName: "John",
Verbose: false,
},
},
{
// ./app --HOST localhost --User-Name="John Smith"
split("./app:--HOST:localhost:--User-Name:John Smith"),
testArgs{
Host: "localhost",
Port: 8080,
UserName: "John Smith",
Verbose: true,
},
},
{
// ./app --port 80 --verb=false
split("./app:--port:80:--verb=false"),
testArgs{
Host: "localhost",
Port: 80,
UserName: "",
Verbose: false,
},
},
}
for _, test := range tests {
os.Args = test.Args
args := testArgs{}
if err := Unmarshal(&args); err != nil {
t.Error(err)
}
if args.Host != test.Expected.Host {
t.Errorf("expected %s but %s", test.Expected.Host, args.Host)
}
if args.Port != test.Expected.Port {
t.Errorf("expected %d but %d", test.Expected.Port, args.Port)
}
if args.UserName != test.Expected.UserName {
t.Errorf(
"expected %s but %s",
test.Expected.UserName,
args.UserName,
)
}
if args.Verbose != test.Expected.Verbose {
t.Errorf(
"expected %v but %v",
test.Expected.Verbose,
args.Verbose,
)
}
}
}
// TestShortFlags tests Unmarshal with short flags.
func TestShortFlags(t *testing.T) {
args := testArgs{}
split := func(str string) []string { return strings.Split(str, ":") }
tests := []struct {
Args []string
Expected testArgs
}{
{
// ./app -h 127.0.0.1 -p 80
split("./app:-h:127.0.0.1:-p80"),
testArgs{Host: "127.0.0.1", Port: 80},
},
{
// ./app --HOST localhost --User-Name="John Smith"
split("./app:--HOST:localhost:--User-Name:John Smith"),
testArgs{Host: "localhost", Port: 8080, UserName: "John Smith"},
},
}
for _, test := range tests {
os.Args = test.Args
if err := Unmarshal(&args); err != nil {
t.Error(err)
}
if args.Host != test.Expected.Host {
t.Errorf("expected %s but %s", test.Expected.Host, args.Host)
}
if args.Port != test.Expected.Port {
t.Errorf("expected %d but %d", test.Expected.Port, args.Port)
}
}
}
// TestUnmarshalErrors tests Unmarshal with errors.
func TestUnmarshalErrors(t *testing.T) {
args := struct {
Host string
}{}
os.Args = []string{"./apt", "-d", "-p"} // flags d and p doesn't exist
if err := Unmarshal(&args); err == nil {
t.Error("expected an error")
}
}