-
Notifications
You must be signed in to change notification settings - Fork 4
/
json_test.go
66 lines (62 loc) · 1.39 KB
/
json_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
package otils
import (
"encoding/json"
"testing"
"time"
)
func TestNullableString(t *testing.T) {
type testNullableString struct {
S NullableString
}
tests := []struct {
name string
s string
expected NullableString
}{
{"normal", `{"s": "foo"}`, NullableString("foo")},
{"null", `{"s": null}`, NullableString("")},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tn := testNullableString{S: "init"}
if err := json.Unmarshal([]byte(tc.s), &tn); err != nil {
t.Error(err)
}
if tn.S != tc.expected {
t.Errorf("unexpected result, want: %q, got: %q", tc.expected, tn.S)
}
})
}
}
func TestNullableTime(t *testing.T) {
type testNullableTime struct {
T *NullableTime
}
now := NullableTime(time.Now())
tests := []struct {
name string
s string
isNull bool
expected NullableTime
}{
{"null", `{"T": null}`, true, NullableTime(now)},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tn := testNullableTime{T: &now}
if err := json.Unmarshal([]byte(tc.s), &tn); err != nil {
t.Error(err)
}
if tc.isNull && tn.T != nil {
t.Errorf("expected nil, got non-nil")
}
if !tc.isNull && time.Time(*tn.T).UnixNano() != time.Time(tc.expected).UnixNano() {
t.Errorf("unexpected result, want: %v, got: %v", tc.expected, *tn.T)
}
})
}
}