From 84da43998f7cc42cc72652333ac6e3d293abb23e Mon Sep 17 00:00:00 2001 From: tsne Date: Mon, 6 Jul 2026 16:52:20 +0200 Subject: initial --- internal/cli/duration_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 internal/cli/duration_test.go (limited to 'internal/cli/duration_test.go') diff --git a/internal/cli/duration_test.go b/internal/cli/duration_test.go new file mode 100644 index 0000000..60d446a --- /dev/null +++ b/internal/cli/duration_test.go @@ -0,0 +1,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") + } +} -- cgit v1.3