package service import ( "encoding/json" "testing" "time" "github.com/stretchr/testify/require" ) func TestDurationMarshal(t *testing.T) { tests := []struct { arg Duration exp string }{ { Duration{time.Second}, "\"1s\"", }, { Duration{time.Minute}, "\"1m0s\"", }, } for _, test := range tests { v, err := json.Marshal(test.arg) require.NoError(t, err) require.Equal(t, test.exp, string(v)) } } func TestDurationUnmarshal(t *testing.T) { tests := []struct { arg string exp Duration }{ { "\"1s\"", Duration{time.Second}, }, { "\"1m0s\"", Duration{time.Minute}, }, } for _, test := range tests { var v Duration err := json.Unmarshal([]byte(test.arg), &v) require.NoError(t, err) require.Equal(t, test.exp.Duration, v.Duration) } }