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 cli
import (
"testing"
"time"
)
func TestDurationValueParses(t *testing.T) {
cases := map[string]duration{
"30d": {days: 30},
"7d": {days: 7},
"1y2m10d": {years: 1, months: 2, days: 10},
"2m": {months: 2},
"1y": {years: 1},
"0d": {days: 0},
}
for in, want := range cases {
var v duration
if err := v.Set(in); err != nil {
t.Fatalf("Set(%q): %v", in, err)
}
if v != want {
t.Fatalf("Set(%q) = %+v, want %+v", in, v, want)
}
}
}
func TestDurationValueRejectsBadInput(t *testing.T) {
for _, in := range []string{"", "d", "5", "5x", "1h", "1m2m", "-3d", "y", "5 d", "99999999999999999999d"} {
var v duration
if err := v.Set(in); err == nil {
t.Fatalf("Set(%q): expected error", in)
}
}
}
func TestDurationValueMonthIsCalendarMonth(t *testing.T) {
var v duration
if err := v.Set("1m"); err != nil {
t.Fatal(err)
}
now := time.Date(2024, time.March, 31, 0, 0, 0, 0, time.UTC)
// A calendar-aware month back from March 31 lands in February, not "31 days".
got := v.cutoff(now)
if got.Month() != time.February && got.Month() != time.March {
t.Fatalf("cutoff month = %v, want calendar-aware", got.Month())
}
if got.Equal(now.AddDate(0, 0, -30)) {
t.Fatalf("cutoff must not use a fixed 30-day month")
}
}
|