-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
179 lines (174 loc) · 3.62 KB
/
Copy pathmain_test.go
File metadata and controls
179 lines (174 loc) · 3.62 KB
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
package main
import (
"errors"
"flag"
"slices"
"testing"
"time"
)
func TestParseCmd(t *testing.T) {
tests := []struct {
name string
input string
wantCmd string
wantArgs []string
wantErr bool
}{
{
name: "simple command",
input: "tr",
wantCmd: "tr",
wantArgs: []string{},
},
{
name: "multi-arg",
input: "tr a-z A-Z",
wantCmd: "tr",
wantArgs: []string{"a-z", "A-Z"},
},
{
name: "skips empty args from extra spaces",
input: "echo hello",
wantCmd: "echo",
wantArgs: []string{"hello"},
},
{
name: "trims leading and trailing spaces",
input: " tr a-z A-Z ",
wantCmd: "tr",
wantArgs: []string{"a-z", "A-Z"},
},
{
name: "empty",
input: "",
wantErr: true,
},
{
name: "whitespace only",
input: " ",
wantErr: true,
},
{
name: "tabs and spaces only",
input: " \t ",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd, args, err := parseCmd(tt.input)
if (err != nil) != tt.wantErr {
t.Fatalf("parseCmd(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
}
if tt.wantErr {
return
}
if cmd != tt.wantCmd {
t.Errorf("cmd = %q, want %q", cmd, tt.wantCmd)
}
if !slices.Equal(args, tt.wantArgs) {
t.Errorf("args = %#v, want %#v", args, tt.wantArgs)
}
})
}
}
func TestParseFlags(t *testing.T) {
tests := []struct {
name string
args []string
want config
wantErr bool
errIs error
}{
{
name: "defaults with cmd",
args: []string{"--cmd", "tr a-z A-Z"},
want: config{
addr: "127.0.0.1:3131",
cmd: "tr",
cmdArgs: []string{"a-z", "A-Z"},
},
},
{
name: "all flags",
args: []string{
"-q",
"--debug",
"--addr", "0.0.0.0:4000",
"--cmd", "tee",
"--io-timeout", "5s",
},
want: config{
addr: "0.0.0.0:4000",
cmd: "tee",
cmdArgs: []string{},
ioTimeout: 5 * time.Second,
quiet: true,
debug: true,
},
},
{
name: "quiet long form",
args: []string{"--quiet", "--cmd", "tee"},
want: config{
addr: "127.0.0.1:3131",
cmd: "tee",
cmdArgs: []string{},
quiet: true,
},
},
{
name: "debug only",
args: []string{"--debug", "--cmd", "tee"},
want: config{
addr: "127.0.0.1:3131",
cmd: "tee",
cmdArgs: []string{},
debug: true,
},
},
{
name: "missing cmd",
args: []string{"--addr", "127.0.0.1:3131"},
wantErr: true,
},
{
name: "help",
args: []string{"-h"},
wantErr: true,
errIs: flag.ErrHelp,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseFlags(tt.args)
if (err != nil) != tt.wantErr {
t.Fatalf("parseFlags(%v) error = %v, wantErr %v", tt.args, err, tt.wantErr)
}
if tt.errIs != nil && !errors.Is(err, tt.errIs) {
t.Fatalf("error = %v, want %v", err, tt.errIs)
}
if tt.wantErr {
return
}
if got.addr != tt.want.addr {
t.Errorf("addr = %q, want %q", got.addr, tt.want.addr)
}
if got.cmd != tt.want.cmd {
t.Errorf("cmd = %q, want %q", got.cmd, tt.want.cmd)
}
if !slices.Equal(got.cmdArgs, tt.want.cmdArgs) {
t.Errorf("cmdArgs = %#v, want %#v", got.cmdArgs, tt.want.cmdArgs)
}
if got.ioTimeout != tt.want.ioTimeout {
t.Errorf("ioTimeout = %v, want %v", got.ioTimeout, tt.want.ioTimeout)
}
if got.quiet != tt.want.quiet {
t.Errorf("quiet = %v, want %v", got.quiet, tt.want.quiet)
}
if got.debug != tt.want.debug {
t.Errorf("debug = %v, want %v", got.debug, tt.want.debug)
}
})
}
}