Repository navigation
Expand file tree
/
Copy pathflags_test.go
More file actions
295 lines (258 loc) · 6.71 KB
/
Copy pathflags_test.go
File metadata and controls
295 lines (258 loc) · 6.71 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
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
package main
import (
"testing"
"time"
)
// ── flagStr ──
func TestFlagStr_present(t *testing.T) {
val, rest := flagStr([]string{"--code", "12345", "--foo", "bar"}, "--code")
if val != "12345" {
t.Errorf("expected 12345, got %q", val)
}
if len(rest) != 2 || rest[0] != "--foo" || rest[1] != "bar" {
t.Errorf("unexpected rest: %v", rest)
}
}
func TestFlagStr_absent(t *testing.T) {
args := []string{"--foo", "bar"}
val, rest := flagStr(args, "--code")
if val != "" {
t.Errorf("expected empty, got %q", val)
}
if len(rest) != len(args) {
t.Errorf("rest should be unchanged: %v", rest)
}
}
func TestFlagStr_atEnd(t *testing.T) {
// flag without value at end of args — ignored
val, rest := flagStr([]string{"--code"}, "--code")
if val != "" {
t.Errorf("expected empty, got %q", val)
}
if len(rest) != 1 {
t.Errorf("unexpected rest: %v", rest)
}
}
// ── flagBool ──
func TestFlagBool_present(t *testing.T) {
ok, rest := flagBool([]string{"--unread", "--limit", "5"}, "--unread")
if !ok {
t.Error("expected true")
}
if len(rest) != 2 {
t.Errorf("unexpected rest: %v", rest)
}
}
func TestFlagBool_absent(t *testing.T) {
args := []string{"--limit", "5"}
ok, rest := flagBool(args, "--unread")
if ok {
t.Error("expected false")
}
if len(rest) != 2 {
t.Errorf("rest should be unchanged: %v", rest)
}
}
// ── flagInt ──
func TestFlagInt_present(t *testing.T) {
n, rest := flagInt([]string{"--limit", "42"}, "--limit", 10)
if n != 42 {
t.Errorf("expected 42, got %d", n)
}
if len(rest) != 0 {
t.Errorf("unexpected rest: %v", rest)
}
}
func TestFlagInt_absent(t *testing.T) {
n, _ := flagInt([]string{"--foo", "bar"}, "--limit", 10)
if n != 10 {
t.Errorf("expected default 10, got %d", n)
}
}
func TestFlagInt_invalid(t *testing.T) {
// invalid value → default returned, no panic
n, _ := flagInt([]string{"--limit", "notanumber"}, "--limit", 7)
if n != 7 {
t.Errorf("expected default 7, got %d", n)
}
}
// ── positional ──
func TestPositional_mixed(t *testing.T) {
args := []string{"durov", "--unread", "--limit", "5"}
got := positional(args)
if len(got) != 1 || got[0] != "durov" {
t.Errorf("unexpected positionals: %v", got)
}
}
func TestPositional_boolFlagBeforeValueFlag(t *testing.T) {
// Regression: old positional() ate --limit because --unread was bool.
// Fix: a "--flag" only skips the next token if it doesn't start with "-".
// So "--unread --limit 5": --limit is NOT consumed as --unread's value.
args := []string{"--unread", "--limit", "5"}
got := positional(args)
// "5" must NOT leak into positionals
if len(got) != 0 {
t.Errorf("unexpected positionals: %v (want [])", got)
}
}
func TestPositional_noFlags(t *testing.T) {
args := []string{"search", "messages", "hello"}
got := positional(args)
if len(got) != 3 {
t.Errorf("unexpected positionals: %v", got)
}
}
func TestPositional_empty(t *testing.T) {
got := positional(nil)
if len(got) != 0 {
t.Errorf("expected empty, got %v", got)
}
}
// ── extractGlobalFlags ──
func TestExtractGlobalFlags_account(t *testing.T) {
acc, _, rem := extractGlobalFlags([]string{"--account", "+79001234567", "me"})
if acc != "+79001234567" {
t.Errorf("expected +79001234567, got %q", acc)
}
if len(rem) != 1 || rem[0] != "me" {
t.Errorf("unexpected remaining: %v", rem)
}
}
func TestExtractGlobalFlags_shortAccount(t *testing.T) {
acc, _, _ := extractGlobalFlags([]string{"-a", "+79001234567", "me"})
if acc != "+79001234567" {
t.Errorf("expected +79001234567, got %q", acc)
}
}
func TestExtractGlobalFlags_timeout(t *testing.T) {
_, timeout, _ := extractGlobalFlags([]string{"--timeout", "30", "dialogs"})
if timeout != 30*time.Second {
t.Errorf("expected 30s, got %v", timeout)
}
}
func TestExtractGlobalFlags_invalidTimeout(t *testing.T) {
// invalid timeout → zero duration, no panic
_, timeout, _ := extractGlobalFlags([]string{"--timeout", "bad", "dialogs"})
if timeout != 0 {
t.Errorf("expected 0, got %v", timeout)
}
}
// ── parseSince ──
func TestParseSince_hours(t *testing.T) {
cutoff, err := parseSince("2h")
if err != nil {
t.Fatal(err)
}
diff := time.Since(cutoff)
if diff < 1*time.Hour || diff > 3*time.Hour {
t.Errorf("expected ~2h ago, got diff=%v", diff)
}
}
func TestParseSince_minutes(t *testing.T) {
cutoff, err := parseSince("30m")
if err != nil {
t.Fatal(err)
}
diff := time.Since(cutoff)
if diff < 25*time.Minute || diff > 35*time.Minute {
t.Errorf("expected ~30m ago, got diff=%v", diff)
}
}
func TestParseSince_days(t *testing.T) {
cutoff, err := parseSince("7d")
if err != nil {
t.Fatal(err)
}
diff := time.Since(cutoff)
if diff < 6*24*time.Hour || diff > 8*24*time.Hour {
t.Errorf("expected ~7d ago, got diff=%v", diff)
}
}
func TestParseSince_invalid(t *testing.T) {
cases := []string{"bad", "0d", "-1h", ""}
for _, s := range cases {
_, err := parseSince(s)
if err == nil {
t.Errorf("parseSince(%q) expected error, got nil", s)
}
}
}
func TestExtractGlobalFlags_noGlobals(t *testing.T) {
acc, timeout, rem := extractGlobalFlags([]string{"dialogs", "--unread"})
if acc != "" || timeout != 0 {
t.Errorf("expected no globals, got acc=%q timeout=%v", acc, timeout)
}
if len(rem) != 2 {
t.Errorf("unexpected remaining: %v", rem)
}
}
// ── parseDuration ──
func TestParseDuration_hours(t *testing.T) {
d, err := parseDuration("2h")
if err != nil {
t.Fatal(err)
}
if d != 2*time.Hour {
t.Errorf("expected 2h, got %v", d)
}
}
func TestParseDuration_minutes(t *testing.T) {
d, err := parseDuration("30m")
if err != nil {
t.Fatal(err)
}
if d != 30*time.Minute {
t.Errorf("expected 30m, got %v", d)
}
}
func TestParseDuration_days(t *testing.T) {
d, err := parseDuration("7d")
if err != nil {
t.Fatal(err)
}
if d != 7*24*time.Hour {
t.Errorf("expected 7*24h, got %v", d)
}
}
func TestParseDuration_oneDayEdge(t *testing.T) {
d, err := parseDuration("1d")
if err != nil {
t.Fatal(err)
}
if d != 24*time.Hour {
t.Errorf("expected 24h, got %v", d)
}
}
func TestParseDuration_seconds(t *testing.T) {
d, err := parseDuration("90s")
if err != nil {
t.Fatal(err)
}
if d != 90*time.Second {
t.Errorf("expected 90s, got %v", d)
}
}
func TestParseDuration_invalid(t *testing.T) {
cases := []string{"bad", "0d", "-1h", "", "0m", "0s"}
for _, s := range cases {
_, err := parseDuration(s)
if err == nil {
t.Errorf("parseDuration(%q) expected error, got nil", s)
}
}
}
func TestParseDuration_zeroDays(t *testing.T) {
_, err := parseDuration("0d")
if err == nil {
t.Error("parseDuration(0d) expected error")
}
}
func TestParseDuration_whitespace(t *testing.T) {
d, err := parseDuration(" 1h ")
if err != nil {
t.Fatal(err)
}
if d != time.Hour {
t.Errorf("expected 1h, got %v", d)
}
}