add a test for DurationPretty

This commit is contained in:
Anton Kaliaev 2018-10-16 13:38:27 +04:00
parent e9f30e1f22
commit 56f943e890
No known key found for this signature in database
GPG Key ID: 7B6881D965918214

View File

@ -0,0 +1,31 @@
package time
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDurationPretty(t *testing.T) {
testCases := []struct {
jsonBytes []byte
expErr bool
expValue time.Duration
}{
{[]byte(`"10s"`), false, 10 * time.Second},
{[]byte(`"48h0m0s"`), false, 48 * time.Hour},
{[]byte(`"10kkk"`), true, 0},
{[]byte(`"kkk"`), true, 0},
}
for i, tc := range testCases {
var d DurationPretty
if tc.expErr {
assert.Error(t, d.UnmarshalJSON(tc.jsonBytes), "#%d", i)
} else {
assert.NoError(t, d.UnmarshalJSON(tc.jsonBytes), "#%d", i)
assert.Equal(t, tc.expValue, d.Duration, "#%d", i)
}
}
}