goext/timeext/parser_test.go

71 lines
2.8 KiB
Go
Raw Permalink Normal View History

2022-12-07 23:15:16 +01:00
package timeext
import (
"testing"
"time"
)
func TestParseDurationShortString(t *testing.T) {
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromSeconds(1), "1s")
tst.AssertPDSSEqual(t, FromSeconds(1), "1sec")
tst.AssertPDSSEqual(t, FromSeconds(1), "1second")
tst.AssertPDSSEqual(t, FromSeconds(1), "1seconds")
tst.AssertPDSSEqual(t, FromSeconds(100), "100second")
tst.AssertPDSSEqual(t, FromSeconds(100), "100seconds")
tst.AssertPDSSEqual(t, FromSeconds(1883639.77), "1883639.77second")
tst.AssertPDSSEqual(t, FromSeconds(1883639.77), "1883639.77seconds")
tst.AssertPDSSEqual(t, FromSeconds(50), "50s")
tst.AssertPDSSEqual(t, FromSeconds(50), "50sec")
tst.AssertPDSSEqual(t, FromSeconds(1), "1second")
tst.AssertPDSSEqual(t, FromSeconds(50), "50seconds")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromMinutes(10), "10m")
tst.AssertPDSSEqual(t, FromMinutes(10), "10min")
tst.AssertPDSSEqual(t, FromMinutes(1), "1minute")
tst.AssertPDSSEqual(t, FromMinutes(10), "10minutes")
tst.AssertPDSSEqual(t, FromMinutes(10.5), "10.5minutes")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromMilliseconds(100), "100ms")
tst.AssertPDSSEqual(t, FromMilliseconds(100), "100milliseconds")
tst.AssertPDSSEqual(t, FromMilliseconds(100), "100millisecond")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromNanoseconds(99235), "99235ns")
tst.AssertPDSSEqual(t, FromNanoseconds(99235), "99235nanoseconds")
tst.AssertPDSSEqual(t, FromNanoseconds(99235), "99235nanosecond")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromMicroseconds(99235), "99235us")
tst.AssertPDSSEqual(t, FromMicroseconds(99235), "99235microseconds")
tst.AssertPDSSEqual(t, FromMicroseconds(99235), "99235microsecond")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromHours(1), "1h")
tst.AssertPDSSEqual(t, FromHours(1), "1hour")
tst.AssertPDSSEqual(t, FromHours(2), "2hours")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromDays(1), "1d")
tst.AssertPDSSEqual(t, FromDays(1), "1day")
tst.AssertPDSSEqual(t, FromDays(10), "10days")
tst.AssertPDSSEqual(t, FromDays(1), "1days")
tst.AssertPDSSEqual(t, FromDays(10), "10day")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromDays(1)+FromMinutes(10), "1d10m")
tst.AssertPDSSEqual(t, FromDays(1)+FromMinutes(10)+FromSeconds(200), "1d10m200sec")
tst.AssertPDSSEqual(t, FromDays(1)+FromMinutes(10), "1d:10m")
tst.AssertPDSSEqual(t, FromDays(1)+FromMinutes(10), "1d 10m")
tst.AssertPDSSEqual(t, FromDays(1)+FromMinutes(10), "1d,10m")
tst.AssertPDSSEqual(t, FromDays(1)+FromMinutes(10), "1d, 10m")
tst.AssertPDSSEqual(t, FromDays(1)+FromSeconds(1000), "1d 1000seconds")
2022-12-07 23:15:16 +01:00
2023-03-31 13:33:06 +02:00
tst.AssertPDSSEqual(t, FromDays(1), "86400s")
2022-12-07 23:15:16 +01:00
}
func assertPDSSEqual(t *testing.T, expected time.Duration, fmt string) {
actual, err := ParseDurationShortString(fmt)
if err != nil {
t.Errorf("ParseDurationShortString('%s') failed with %v", fmt, err)
}
if actual != expected {
t.Errorf("values differ: Actual: '%v', Expected: '%v'", actual.String(), expected.String())
}
}