2017-04-10 18:54:12 +02:00
|
|
|
package runtime
|
2017-01-29 18:30:08 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDuration(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var tests = []struct {
|
|
|
|
input string
|
|
|
|
err string
|
|
|
|
duration time.Duration
|
|
|
|
}{
|
|
|
|
{"", "invalid duration: \"\"", 0},
|
2018-01-02 09:50:00 +01:00
|
|
|
{"1x", "invalid duration unit \"x\"", 0},
|
2017-01-29 18:30:08 +01:00
|
|
|
{"1s", "", time.Second},
|
|
|
|
{"73s", "", time.Second * 73},
|
|
|
|
{"1m", "", time.Minute},
|
|
|
|
{"73m", "", time.Minute * 73},
|
|
|
|
{"1h", "", time.Hour},
|
|
|
|
{"43h", "", time.Hour * 43},
|
|
|
|
{"1d", "", time.Hour * 24},
|
|
|
|
{"8d", "", time.Hour * 24 * 8},
|
|
|
|
{"1w", "", time.Hour * 24 * 7},
|
|
|
|
{"52w", "", time.Hour * 24 * 7 * 52},
|
|
|
|
{"1y", "", time.Hour * 24 * 365},
|
|
|
|
{"3y", "", time.Hour * 24 * 365 * 3},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
|
|
|
d := Duration{}
|
2017-04-10 18:54:12 +02:00
|
|
|
err := d.UnmarshalTOML(test.input)
|
2017-01-29 18:30:08 +01:00
|
|
|
duration := d.Duration
|
|
|
|
|
|
|
|
if test.err == "" {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(test.duration, duration)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(err, test.err)
|
|
|
|
}
|
|
|
|
}
|
2017-10-12 15:25:00 +02:00
|
|
|
|
|
|
|
d := Duration{}
|
|
|
|
err := d.UnmarshalTOML(3)
|
|
|
|
assert.Error(err)
|
|
|
|
assert.Contains(err.Error(), "invalid duration")
|
|
|
|
|
|
|
|
err = d.UnmarshalTOML("am")
|
|
|
|
assert.Error(err)
|
2018-01-02 09:50:00 +01:00
|
|
|
assert.EqualError(err, "unable to parse duration \"am\": strconv.Atoi: parsing \"a\": invalid syntax")
|
2017-01-29 18:30:08 +01:00
|
|
|
}
|