You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

exts_test.go 818 B

1234567891011121314151617181920212223242526272829303132333435
  1. package exts
  2. import (
  3. "testing"
  4. "time"
  5. jsoniter "github.com/json-iterator/go"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func Test_Duration(t *testing.T) {
  9. c := jsoniter.Config{}
  10. api := c.Froze()
  11. api.RegisterExtension(&DurationExt{})
  12. Convey("Duration", t, func() {
  13. type Test struct {
  14. D1 time.Duration `json:"d1"`
  15. D2 time.Duration `json:"d2"`
  16. D3 *time.Duration `json:"d3"`
  17. D4 *time.Duration `json:"d4"`
  18. D5 *time.Duration `json:"d5"`
  19. }
  20. var test Test
  21. err := api.Unmarshal([]byte(`{"d1":"1h", "d2": null, "d3": "2h", "d4": null}`), &test)
  22. So(err, ShouldBeNil)
  23. So(test.D1, ShouldEqual, 1*time.Hour)
  24. So(test.D2, ShouldEqual, 0)
  25. So(*test.D3, ShouldEqual, 2*time.Hour)
  26. So(test.D4, ShouldEqual, (*time.Duration)(nil))
  27. So(test.D5, ShouldEqual, (*time.Duration)(nil))
  28. })
  29. }