2018-01-12 17:30:54 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2018-05-14 17:32:27 +02:00
|
|
|
"fmt"
|
2018-01-12 17:30:54 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check it satisfies the interface
|
|
|
|
var _ pflag.Value = (*Duration)(nil)
|
|
|
|
|
|
|
|
func TestParseDuration(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
2018-03-12 21:52:42 +01:00
|
|
|
want time.Duration
|
2018-01-12 17:30:54 +01:00
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{"0", 0, false},
|
|
|
|
{"", 0, true},
|
2018-03-12 21:52:42 +01:00
|
|
|
{"1ms", time.Millisecond, false},
|
|
|
|
{"1s", time.Second, false},
|
|
|
|
{"1m", time.Minute, false},
|
2018-05-14 17:32:27 +02:00
|
|
|
{"1.5m", (3 * time.Minute) / 2, false},
|
2018-03-12 21:52:42 +01:00
|
|
|
{"1h", time.Hour, false},
|
|
|
|
{"1d", time.Hour * 24, false},
|
|
|
|
{"1w", time.Hour * 24 * 7, false},
|
|
|
|
{"1M", time.Hour * 24 * 30, false},
|
|
|
|
{"1y", time.Hour * 24 * 365, false},
|
|
|
|
{"1.5y", time.Hour * 24 * 365 * 3 / 2, false},
|
|
|
|
{"-1s", -time.Second, false},
|
|
|
|
{"1.s", time.Second, false},
|
2018-01-12 17:30:54 +01:00
|
|
|
{"1x", 0, true},
|
2018-03-12 21:52:42 +01:00
|
|
|
{"off", time.Duration(DurationOff), false},
|
2018-05-14 17:32:27 +02:00
|
|
|
{"1h2m3s", time.Hour + 2*time.Minute + 3*time.Second, false},
|
2018-01-12 17:30:54 +01:00
|
|
|
} {
|
|
|
|
duration, err := ParseDuration(test.in)
|
|
|
|
if test.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2018-03-12 21:52:42 +01:00
|
|
|
assert.Equal(t, test.want, duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationString(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in time.Duration
|
|
|
|
want string
|
|
|
|
}{
|
2018-04-06 20:04:31 +02:00
|
|
|
{time.Duration(0), "0s"},
|
2018-03-12 21:52:42 +01:00
|
|
|
{time.Second, "1s"},
|
|
|
|
{time.Minute, "1m0s"},
|
2018-05-14 17:32:27 +02:00
|
|
|
{time.Millisecond, "1ms"},
|
|
|
|
{time.Second, "1s"},
|
|
|
|
{(3 * time.Minute) / 2, "1m30s"},
|
|
|
|
{time.Hour, "1h0m0s"},
|
|
|
|
{time.Hour * 24, "1d"},
|
|
|
|
{time.Hour * 24 * 7, "1w"},
|
|
|
|
{time.Hour * 24 * 30, "1M"},
|
|
|
|
{time.Hour * 24 * 365, "1y"},
|
|
|
|
{time.Hour * 24 * 365 * 3 / 2, "1.5y"},
|
|
|
|
{-time.Second, "-1s"},
|
|
|
|
{time.Second, "1s"},
|
2018-03-12 21:52:42 +01:00
|
|
|
{time.Duration(DurationOff), "off"},
|
2018-05-14 17:32:27 +02:00
|
|
|
{time.Hour + 2*time.Minute + 3*time.Second, "1h2m3s"},
|
|
|
|
{time.Hour * 24, "1d"},
|
|
|
|
{time.Hour * 24 * 7, "1w"},
|
|
|
|
{time.Hour * 24 * 30, "1M"},
|
|
|
|
{time.Hour * 24 * 365, "1y"},
|
|
|
|
{time.Hour * 24 * 365 * 3 / 2, "1.5y"},
|
|
|
|
{-time.Hour * 24 * 365 * 3 / 2, "-1.5y"},
|
2018-03-12 21:52:42 +01:00
|
|
|
} {
|
|
|
|
got := Duration(test.in).String()
|
|
|
|
assert.Equal(t, test.want, got)
|
2018-05-14 17:32:27 +02:00
|
|
|
// Test the reverse
|
|
|
|
reverse, err := ParseDuration(test.want)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, test.in, reverse)
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 17:32:27 +02:00
|
|
|
|
|
|
|
func TestDurationScan(t *testing.T) {
|
|
|
|
var v Duration
|
|
|
|
n, err := fmt.Sscan(" 17m ", &v)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, n)
|
|
|
|
assert.Equal(t, Duration(17*60*time.Second), v)
|
|
|
|
}
|