Skip to content

Commit d8f3c15

Browse files
committed
Update testing
1 parent 306b10d commit d8f3c15

File tree

1 file changed

+147
-52
lines changed

1 file changed

+147
-52
lines changed

command_line_parser_test.go

Lines changed: 147 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
11
package cli
22

33
import (
4-
"net"
5-
"reflect"
64
"testing"
75
)
86

9-
func TestCommandlineParse(t *testing.T) {
10-
var (
11-
oBool bool
12-
oInt int
13-
oInt64 int64
14-
oFloat32 float32
15-
oString string
16-
oStringList []string
17-
oIntList []int
18-
oNetIP net.IP
19-
oNetIPMask net.IPMask
20-
)
7+
func TestCommandlineParseBoolFlag(t *testing.T) {
8+
var b1, b2, b3, b4 bool
219

2210
cl := &commandline{
2311
flags: []*Flag{
24-
{Name: "b", Value: &oBool},
25-
{Name: "i, int", Value: &oInt},
26-
{Name: "int64", Value: &oInt64},
27-
{Name: "f, f32", Value: &oFloat32},
28-
{Name: "s", Value: &oString},
29-
{Name: "string-list", Value: &oStringList},
30-
{Name: "int-list", Value: &oIntList},
31-
{Name: "ip", Value: &oNetIP},
32-
{Name: "ipmask", Value: &oNetIPMask},
12+
{Name: "b1", Value: &b1},
13+
{Name: "b2", Value: &b2},
14+
{Name: "b3", Value: &b3},
15+
{Name: "b4", Value: &b4, DefValue: "true"},
3316
},
3417
}
3518

@@ -39,48 +22,160 @@ func TestCommandlineParse(t *testing.T) {
3922
}
4023

4124
args := []string{
42-
"-b",
43-
"-i=12",
44-
"--int64", "12345",
45-
"-f0.1",
46-
"-s='a b'",
47-
"--string-list=a",
48-
"--string-list", "b",
49-
"--int-list=0",
50-
"--int-list", "1",
51-
"--ip=127.0.0.1",
52-
"--ipmask=255.255.0.0",
25+
"--b1",
26+
"--b2=false",
27+
"--b3=on",
5328
}
29+
5430
err := cl.parse(args)
5531
if err != nil {
5632
t.Fatal(err)
5733
}
5834

59-
if oBool != true {
60-
t.Error("flag bool value is wrong:", oBool)
35+
if b1 != true {
36+
t.Error("b1 != true")
6137
}
62-
if oInt != 12 {
63-
t.Error("flag int value is wrong:", oInt)
38+
if b2 != false {
39+
t.Error("b2 != false")
6440
}
65-
if oInt64 != 12345 {
66-
t.Error("flag int64 value is wrong:", oInt64)
41+
if b3 != true {
42+
t.Error("b3 != true")
6743
}
68-
if oFloat32 != 0.1 {
69-
t.Error("flag float32 value is wrong:", oFloat32)
44+
if b4 != true {
45+
t.Error("b4 != true")
7046
}
71-
if oString != "a b" {
72-
t.Error("flag string value is wrong:", oString)
47+
}
48+
49+
func TestCommandlineParseStringFlag(t *testing.T) {
50+
var a, b, c string
51+
var s1, s2, s3, s4 string
52+
53+
cl := &commandline{
54+
flags: []*Flag{
55+
{Name: "s1", Value: &s1},
56+
{Name: "s2", Value: &s2},
57+
{Name: "s3", Value: &s3},
58+
{Name: "s4", Value: &s4},
59+
{Name: "a", Value: &a},
60+
{Name: "b", Value: &b},
61+
{Name: "c", Value: &c},
62+
},
7363
}
74-
if !reflect.DeepEqual(oStringList, []string{"a", "b"}) {
75-
t.Error("flag string list value is wrong:", oStringList)
64+
65+
// initialize flags
66+
for _, f := range cl.flags {
67+
f.initialize()
7668
}
77-
if !reflect.DeepEqual(oIntList, []int{0, 1}) {
78-
t.Error("flag int list value is wrong:", oIntList)
69+
70+
args := []string{
71+
"--s1=val",
72+
`--s2="val"`,
73+
`--s3='val'`,
74+
"--s4", "val",
75+
76+
"-a", "val",
77+
"-b=val",
78+
"-cval",
7979
}
80-
if oNetIP.String() != "127.0.0.1" {
81-
t.Error("flag net.ip value is wrong:", oNetIP.String())
80+
81+
err := cl.parse(args)
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
86+
if a != "val" {
87+
t.Error("wrong: -x arg")
88+
}
89+
if b != "val" {
90+
t.Error("wrong -x=arg")
91+
}
92+
if c != "val" {
93+
t.Error("wrong -xarg")
94+
}
95+
96+
if s1 != "val" {
97+
t.Error("wrong: --long=arg")
98+
}
99+
if s2 != "val" {
100+
t.Error(`wrong --long="arg"`)
101+
}
102+
if s3 != "val" {
103+
t.Error(`wrong --long='arg'`)
104+
}
105+
if s4 != "val" {
106+
t.Error("wrong: --long arg")
107+
}
108+
}
109+
110+
func TestCommandlineParseRawFlag(t *testing.T) {
111+
cl := &commandline{}
112+
113+
args := []string{"--", "-a", "--bb"}
114+
115+
err := cl.parse(args)
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
120+
if len(cl.args) != 2 {
121+
t.Error("wrong parse raw: -- arg ...")
122+
}
123+
}
124+
125+
func TestCommandlineParseFlagNotFound(t *testing.T) {
126+
cl := &commandline{}
127+
128+
args := []string{"--test"}
129+
130+
err := cl.parse(args)
131+
if err == nil {
132+
t.Fatal("no error found")
133+
}
134+
if err.Error() != `unrecognized option '--test'` {
135+
t.Fatalf("unexpected error: %v", err)
136+
}
137+
}
138+
139+
func TestCommandlineParseCommand(t *testing.T) {
140+
cl := &commandline{
141+
commands: []*Command{
142+
{
143+
Name: "debug",
144+
},
145+
},
146+
}
147+
148+
args := []string{"debug", "-a", "--bb"}
149+
150+
err := cl.parse(args)
151+
if err != nil {
152+
t.Fatal(err)
153+
}
154+
155+
if cl.command == nil {
156+
t.Fatal("no command found")
157+
}
158+
if cl.command.Name != "debug" {
159+
t.Fatal("wrong parsed command")
160+
}
161+
}
162+
163+
func TestCommandlineParseCommandNotFound(t *testing.T) {
164+
cl := &commandline{
165+
commands: []*Command{
166+
{
167+
Name: "debug",
168+
},
169+
},
170+
}
171+
172+
args := []string{"help"}
173+
174+
err := cl.parse(args)
175+
if err != nil {
176+
t.Fatal(err)
82177
}
83-
if oNetIPMask.String() != "ffff0000" {
84-
t.Error("flag net.ipmask value is wrong:", oNetIPMask.String())
178+
if cl.command != nil {
179+
t.Fatal("command should be not found")
85180
}
86181
}

0 commit comments

Comments
 (0)