-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
51 lines (47 loc) · 1.17 KB
/
main_test.go
File metadata and controls
51 lines (47 loc) · 1.17 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
package main
import (
"testing"
"time"
)
func TestParseMaxAge(t *testing.T) {
now := time.Now()
cases := []struct {
in string
wantD time.Duration
wantOK bool
}{
{"12h", 12 * time.Hour, true},
{"30d", 30 * 24 * time.Hour, true},
{"4w", 4 * 7 * 24 * time.Hour, true},
{"6mo", 6 * 30 * 24 * time.Hour, true},
{"2y", 2 * 365 * 24 * time.Hour, true},
{"1h", 1 * time.Hour, true},
{"", 0, false},
{"foo", 0, false},
{"5x", 0, false},
{"5m", 0, false}, // ambiguous — months use "mo"
{"0d", 0, false}, // zero rejected
{"-1d", 0, false}, // negative rejected
{"1.5y", 0, false},
{"y", 0, false},
{"10", 0, false},
}
for _, tc := range cases {
got, err := parseMaxAge(tc.in)
if tc.wantOK {
if err != nil {
t.Errorf("parseMaxAge(%q): unexpected error %v", tc.in, err)
continue
}
delta := now.Sub(got)
// Allow 1s of slack since now is recomputed inside parseMaxAge.
if delta < tc.wantD-time.Second || delta > tc.wantD+time.Second {
t.Errorf("parseMaxAge(%q): delta %v, want ~%v", tc.in, delta, tc.wantD)
}
} else {
if err == nil {
t.Errorf("parseMaxAge(%q): want error, got %v", tc.in, got)
}
}
}
}