|
- package exts
-
- import (
- "testing"
- "time"
-
- jsoniter "github.com/json-iterator/go"
- . "github.com/smartystreets/goconvey/convey"
- )
-
- func Test_Duration(t *testing.T) {
- c := jsoniter.Config{}
- api := c.Froze()
- api.RegisterExtension(&DurationExt{})
-
- Convey("Duration", t, func() {
- type Test struct {
- D1 time.Duration `json:"d1"`
- D2 time.Duration `json:"d2"`
- D3 *time.Duration `json:"d3"`
- D4 *time.Duration `json:"d4"`
- D5 *time.Duration `json:"d5"`
- }
-
- var test Test
- err := api.Unmarshal([]byte(`{"d1":"1h", "d2": null, "d3": "2h", "d4": null}`), &test)
- So(err, ShouldBeNil)
- So(test.D1, ShouldEqual, 1*time.Hour)
- So(test.D2, ShouldEqual, 0)
- So(*test.D3, ShouldEqual, 2*time.Hour)
- So(test.D4, ShouldEqual, (*time.Duration)(nil))
- So(test.D5, ShouldEqual, (*time.Duration)(nil))
- })
-
- }
|