37 lines
1.2 KiB
Go
37 lines
1.2 KiB
Go
package daterange
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func intPointer(value int) *int { return &value }
|
|
|
|
func TestCalendarWindow(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
now time.Time
|
|
from int
|
|
fromDay *int
|
|
to int
|
|
toDay *int
|
|
wantStart, wantEnd string
|
|
}{
|
|
{"ordinary", time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC), 2, nil, 3, nil, "2026-02-01", "2026-03-31"},
|
|
{"wrap before end", time.Date(2026, 1, 10, 0, 0, 0, 0, time.UTC), 11, nil, 2, nil, "2025-11-01", "2026-02-28"},
|
|
{"wrap before start", time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC), 11, nil, 2, nil, "2026-11-01", "2027-02-28"},
|
|
{"clamps invalid month day", time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), 2, intPointer(31), 2, intPointer(31), "2026-02-28", "2026-02-28"},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
start, end := CalendarWindow(test.now, test.from, test.fromDay, test.to, test.toDay)
|
|
if got := start.Format("2006-01-02"); got != test.wantStart {
|
|
t.Errorf("start=%s", got)
|
|
}
|
|
if got := end.Format("2006-01-02"); got != test.wantEnd {
|
|
t.Errorf("end=%s", got)
|
|
}
|
|
})
|
|
}
|
|
}
|