-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration_test.go
112 lines (96 loc) · 3.47 KB
/
duration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package jsonutil_test
import (
"errors"
"reflect"
"strconv"
"testing"
"time"
"github.com/MarkRosemaker/jsonutil"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
)
func TestDuration(t *testing.T) {
type testDuration struct {
Duration time.Duration `json:"duration"`
DurationPointer *time.Duration `json:"durationPointer"`
DurationOmitZero time.Duration `json:"durationOmitZero,omitzero"`
DurationPointerOmitEmpty *time.Duration `json:"durationPointerOmitEmpty,omitempty"`
}
jsonOpts := json.JoinOptions(
json.WithMarshalers(json.MarshalFuncV2(jsonutil.DurationMarshalIntSeconds)),
json.WithUnmarshalers(json.UnmarshalFuncV2(jsonutil.DurationUnmarshalIntSeconds)),
)
t.Run("EOF", func(t *testing.T) {
out := &testDuration{}
errSyn := &jsontext.SyntacticError{}
if err := json.Unmarshal([]byte(`{"duration":`), out, jsonOpts); err == nil {
t.Fatalf("expected error")
} else if !errors.As(err, &errSyn) {
t.Fatalf("expected error to be a semantic error, got: %v", err)
} else if want := `unexpected EOF`; errSyn.Err.Error() != want {
t.Fatalf("expected syntactic error be %s, got: %#v", want, errSyn.Err)
}
})
t.Run("not an int", func(t *testing.T) {
out := &testDuration{}
errSem := &json.SemanticError{}
if err := json.Unmarshal([]byte(`{"duration": "3"}`), out, jsonOpts); err == nil {
t.Fatalf("expected error")
} else if !errors.As(err, &errSem) {
t.Fatalf("expected error to be a semantic error, got: %v", err)
} else if tpInt := reflect.TypeFor[int64](); errSem.GoType != tpInt {
t.Fatalf("expected semantic error to have type %s, got: %s", tpInt, errSem.GoType)
}
})
t.Run("null", func(t *testing.T) {
out := &testDuration{}
if err := json.Unmarshal([]byte(`{"duration":null,"durationPointer":null,"durationOmitZero":null,"durationPointerOmitEmpty":null}`), out, jsonOpts); err != nil {
t.Errorf("unexpected error: %v", err)
}
})
u := 30 * time.Second
for i, tc := range []struct {
in testDuration
out string
}{
{testDuration{}, `{"duration":0,"durationPointer":null}`},
{
testDuration{Duration: u, DurationPointer: &u, DurationOmitZero: u, DurationPointerOmitEmpty: &u},
`{"duration":30,"durationPointer":30,"durationOmitZero":30,"durationPointerOmitEmpty":30}`,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b, err := json.Marshal(tc.in, jsonOpts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(b) != tc.out {
t.Fatalf("want: %s, got: %s", tc.out, string(b))
}
var out testDuration
if err := json.Unmarshal(b, &out, jsonOpts); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := out.Duration.String(), tc.in.Duration.String(); got != want {
t.Fatalf("want: %s, got: %s", want, got)
}
if got, want := out.DurationOmitZero.String(), tc.in.DurationOmitZero.String(); got != want {
t.Fatalf("want: %s, got: %s", want, got)
}
for _, tt := range []struct{ got, want *time.Duration }{
{out.DurationPointer, tc.in.DurationPointer},
{out.DurationPointerOmitEmpty, tc.in.DurationPointerOmitEmpty},
} {
if tt.got == nil && tt.want != nil {
t.Fatalf("expected non-nil URLPointer")
} else if tt.got != nil && tt.want == nil {
t.Fatalf("expected nil URLPointer")
} else if tt.got != nil && tt.want != nil {
if tt.got.String() != tt.want.String() {
t.Fatalf("want: %s, got: %s", tt.want, tt.got)
}
}
}
})
}
}