github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/pkg/types/duration_test.go (about) 1 package types 2 3 import ( 4 "encoding/json" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestMarshalJSOn(t *testing.T) { 12 13 s := struct { 14 D Duration 15 }{ 16 D: Duration(time.Second), 17 } 18 19 j, err := json.Marshal(&s) 20 assert.NoError(t, err) 21 assert.Equal(t, `{"D":"1s"}`, string(j)) 22 } 23 24 func TestUnmarshalJSON(t *testing.T) { 25 26 s := struct { 27 D Duration 28 }{} 29 30 err := json.Unmarshal([]byte(`{"D":"1s"}`), &s) 31 assert.NoError(t, err) 32 assert.Equal(t, Duration(time.Second), s.D) 33 34 err = json.Unmarshal([]byte(`{"D":"200ms"}`), &s) 35 assert.NoError(t, err) 36 assert.Equal(t, Duration(time.Millisecond*200), s.D) 37 }