Files
ana-ledger/cmd/cli/flag_test.go
Bel LaPointe ca365ad39c
All checks were successful
cicd / ci (push) Successful in 1m51s
can pass duration to period
2026-03-04 08:53:02 -07:00

42 lines
1.2 KiB
Go

package cli
import (
"testing"
"time"
)
func TestPeriodSetT(t *testing.T) {
now := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC)
cases := map[string]time.Time{
"2001-02-03": time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC),
"2001-02": time.Date(2001, 2, 1, 0, 0, 0, 0, time.UTC),
"2001": time.Date(2001, 1, 1, 0, 0, 0, 0, time.UTC),
"": time.Unix(0, 0),
"1m": now.Add(1 * time.Minute),
"+1m": now.Add(1 * time.Minute),
"-1m": now.Add(-1 * time.Minute),
"1h1m": now.Add(1*time.Hour + 1*time.Minute),
"1d": now.Add(24 * time.Hour),
"+1d": now.Add(24 * time.Hour),
"-1d": now.Add(-24 * time.Hour),
"1d1m": now.Add(24*time.Hour + 1*time.Minute),
"+1d1m": now.Add(24*time.Hour + 1*time.Minute),
"-1d1m": now.Add(-1*24*time.Hour + -1*1*time.Minute),
"1y1mo1w1d1h": now.AddDate(1, 1, 7+1).Add(1 * time.Hour),
}
for input, expect := range cases {
input, expect := input, expect
t.Run(input, func(t *testing.T) {
p := &Period{}
var got time.Time
if _, err := p.setT(now, input, &got); err != nil {
t.Fatal(err)
}
if got.Unix() != expect.Unix() {
t.Errorf("expected %s but got %s", expect.String(), got.String())
}
})
}
}