Initial public release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseDuration(t *testing.T) {
|
|
tests := []struct {
|
|
raw string
|
|
want int64
|
|
wantErr error
|
|
}{
|
|
{"30s", 30, nil},
|
|
{"30S", 30, nil},
|
|
{"29s", 0, errDurationRange},
|
|
{"1m", 60, nil},
|
|
{"10m", 600, nil},
|
|
{"1h", 3600, nil},
|
|
{"1H", 3600, nil},
|
|
{"1h30m", 5400, nil},
|
|
{"1m30s", 90, nil},
|
|
{"1d", 86400, nil},
|
|
{"1w", 604800, nil},
|
|
{"1D2H", 93600, nil},
|
|
{"365d", MaxDuration, nil},
|
|
{"365d1s", 0, errDurationRange},
|
|
{"366d", 0, errDurationRange},
|
|
{"0s", 0, errDurationRange},
|
|
{"30m0s", 1800, nil},
|
|
{"1", 60, nil},
|
|
{"29", 1740, nil},
|
|
{"0", 0, errDurationRange},
|
|
{"525600", MaxDuration, nil},
|
|
{"525601", 0, errDurationRange},
|
|
{"", 0, errDurationFormat},
|
|
{"abc", 0, errDurationFormat},
|
|
{"1x", 0, errDurationFormat},
|
|
{"h", 0, errDurationFormat},
|
|
{"1h30", 0, errDurationFormat},
|
|
{"-5m", 0, errDurationFormat},
|
|
{"1.5h", 0, errDurationFormat},
|
|
{" 5m", 0, errDurationFormat},
|
|
{"5m ", 0, errDurationFormat},
|
|
{"5 s", 0, errDurationFormat},
|
|
{"m5", 0, errDurationFormat},
|
|
{"999999999999999999999999h", 0, errDurationRange},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(fmt.Sprintf("%q", test.raw), func(t *testing.T) {
|
|
got, err := ParseDuration(test.raw)
|
|
if !errors.Is(err, test.wantErr) {
|
|
t.Fatalf("ParseDuration(%q) error = %v, want %v", test.raw, err, test.wantErr)
|
|
}
|
|
if got != test.want {
|
|
t.Fatalf("ParseDuration(%q) = %d, want %d", test.raw, got, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseDurationBounds(t *testing.T) {
|
|
if seconds, err := ParseDuration("30s"); err != nil || seconds != MinDuration {
|
|
t.Fatalf("minimum duration = %d, %v; want %d", seconds, err, MinDuration)
|
|
}
|
|
if seconds, err := ParseDuration("365d"); err != nil || seconds != MaxDuration {
|
|
t.Fatalf("maximum duration = %d, %v; want %d", seconds, err, MaxDuration)
|
|
}
|
|
if _, err := ParseDuration("365d1m"); !errors.Is(err, errDurationRange) {
|
|
t.Fatalf("one minute above maximum: error = %v, want range error", err)
|
|
}
|
|
}
|