duration_test.go 659 B

123456789101112131415161718192021222324252627282930313233
  1. package duration_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. "github.com/v2fly/v2ray-core/v4/infra/conf/cfgcommon/duration"
  7. )
  8. type testWithDuration struct {
  9. Duration duration.Duration
  10. }
  11. func TestDurationJSON(t *testing.T) {
  12. expected := &testWithDuration{
  13. Duration: duration.Duration(time.Hour),
  14. }
  15. data, err := json.Marshal(expected)
  16. if err != nil {
  17. t.Error(err)
  18. return
  19. }
  20. actual := &testWithDuration{}
  21. err = json.Unmarshal(data, &actual)
  22. if err != nil {
  23. t.Error(err)
  24. return
  25. }
  26. if actual.Duration != expected.Duration {
  27. t.Errorf("expected: %s, actual: %s", time.Duration(expected.Duration), time.Duration(actual.Duration))
  28. }
  29. }