forked from guregu/null
-
Notifications
You must be signed in to change notification settings - Fork 34
/
json_test.go
294 lines (238 loc) · 5.76 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package null
import (
"bytes"
"encoding/json"
"testing"
)
var (
jsonJSON = []byte(`"hello"`)
)
func TestJSONFrom(t *testing.T) {
t.Parallel()
i := JSONFrom([]byte(`"hello"`))
assertJSON(t, i, "JSONFrom()")
zero := JSONFrom(nil)
if zero.Valid {
t.Error("JSONFrom(nil)", "is valid, but should be invalid")
}
zero = JSONFrom([]byte{})
if !zero.Valid {
t.Error("JSONFrom([]byte{})", "is invalid, but should be valid")
}
}
func TestJSONFromPtr(t *testing.T) {
t.Parallel()
n := []byte(`"hello"`)
iptr := &n
i := JSONFromPtr(iptr)
assertJSON(t, i, "JSONFromPtr()")
null := JSONFromPtr(nil)
assertNullJSON(t, null, "JSONFromPtr(nil)")
}
type Test struct {
Name string
Age int
}
func TestMarshal(t *testing.T) {
t.Parallel()
var i JSON
test := &Test{Name: "hello", Age: 15}
err := i.Marshal(test)
if err != nil {
t.Error(err)
}
if !bytes.Equal(i.JSON, []byte(`{"Name":"hello","Age":15}`)) {
t.Errorf("Mismatch between received and expected, got: %s", string(i.JSON))
}
if i.Valid == false {
t.Error("Expected valid true, got Valid false")
}
err = i.Marshal(nil)
if err != nil {
t.Error(err)
}
if i.Valid == true {
t.Error("Expected Valid false, got Valid true")
}
}
func TestUnmarshal(t *testing.T) {
t.Parallel()
var i JSON
test := &Test{}
err := i.Unmarshal(test)
if err != nil {
t.Error(err)
}
x := &Test{Name: "hello", Age: 15}
err = i.Marshal(x)
if err != nil {
t.Error(err)
}
if !bytes.Equal(i.JSON, []byte(`{"Name":"hello","Age":15}`)) {
t.Errorf("Mismatch between received and expected, got: %s", string(i.JSON))
}
err = i.Unmarshal(test)
if err != nil {
t.Error(err)
}
if test.Age != 15 {
t.Errorf("Expected 15, got %d", test.Age)
}
if test.Name != "hello" {
t.Errorf("Expected name, got %s", test.Name)
}
}
func TestUnmarshalJSON(t *testing.T) {
t.Parallel()
var i JSON
err := json.Unmarshal(jsonJSON, &i)
maybePanic(err)
assertJSON(t, i, "[]byte json")
var ni JSON
err = ni.UnmarshalJSON([]byte{})
if err != nil {
t.Error(err)
}
if ni.Valid == false {
t.Errorf("expected Valid to be true, got false")
}
if !bytes.Equal(ni.JSON, nil) {
t.Errorf("Expected JSON to be nil, but was not: %#v %#v", ni.JSON, []byte(nil))
}
var null JSON
err = null.UnmarshalJSON(nil)
if err == nil {
t.Error("passing a nil should fail")
}
}
func TestUnmarshalJSONInStruct(t *testing.T) {
t.Parallel()
type testStruct struct {
Val JSON `json:"val,omitempty"`
}
// In this case UnmarshalJSON is never called and it should not be
// considered set nor valid.
t1 := testStruct{}
err := json.Unmarshal([]byte(`{}`), &t1)
if err != nil {
t.Error(err)
}
if t1.Val.Set {
t.Error("should not be set, no value was given")
}
if t1.Val.Valid {
t.Error("should not be valid, no value was given")
}
// In this case UnmarshalJSON is called with [110 117 108 108]
// in this case the value contained in the JSON should not exist
// and it should be set and !valid.
//
// This is so {"val": null} unmarshalling can turn into an sql null value.
t2 := testStruct{}
err = json.Unmarshal([]byte(`{"val": null}`), &t2)
if err != nil {
t.Error(err)
}
if !t2.Val.Set {
t.Error("should be set")
}
if t2.Val.Valid {
t.Error("should not be valid")
}
}
func TestTextUnmarshalJSON(t *testing.T) {
t.Parallel()
var i JSON
err := i.UnmarshalText([]byte(`"hello"`))
maybePanic(err)
assertJSON(t, i, "UnmarshalText() []byte")
var blank JSON
err = blank.UnmarshalText([]byte(""))
maybePanic(err)
assertNullJSON(t, blank, "UnmarshalText() empty []byte")
}
func TestMarshalJSON(t *testing.T) {
t.Parallel()
i := JSONFrom([]byte(`"hello"`))
data, err := json.Marshal(i)
maybePanic(err)
assertJSONEquals(t, data, `"hello"`, "non-empty json marshal")
// invalid values should be encoded as null
null := NewJSON(nil, false)
data, err = json.Marshal(null)
maybePanic(err)
assertJSONEquals(t, data, "null", "null json marshal")
}
func TestMarshalJSONText(t *testing.T) {
t.Parallel()
i := JSONFrom([]byte(`"hello"`))
data, err := i.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, `"hello"`, "non-empty text marshal")
// invalid values should be encoded as null
null := NewJSON(nil, false)
data, err = null.MarshalText()
maybePanic(err)
assertJSONEquals(t, data, "", "null text marshal")
}
func TestJSONPointer(t *testing.T) {
t.Parallel()
i := JSONFrom([]byte(`"hello"`))
ptr := i.Ptr()
if !bytes.Equal(*ptr, []byte(`"hello"`)) {
t.Errorf("bad %s []byte: %#v ≠ %s\n", "pointer", ptr, `"hello"`)
}
null := NewJSON(nil, false)
ptr = null.Ptr()
if ptr != nil {
t.Errorf("bad %s []byte: %#v ≠ %s\n", "nil pointer", ptr, "nil")
}
}
func TestJSONIsZero(t *testing.T) {
t.Parallel()
i := JSONFrom([]byte(`"hello"`))
if i.IsZero() {
t.Errorf("IsZero() should be false")
}
null := NewJSON(nil, false)
if !null.IsZero() {
t.Errorf("IsZero() should be true")
}
zero := NewJSON(nil, true)
if zero.IsZero() {
t.Errorf("IsZero() should be false")
}
}
func TestJSONSetValid(t *testing.T) {
t.Parallel()
change := NewJSON(nil, false)
assertNullJSON(t, change, "SetValid()")
change.SetValid([]byte(`"hello"`))
assertJSON(t, change, "SetValid()")
}
func TestJSONScan(t *testing.T) {
t.Parallel()
var i JSON
err := i.Scan(`"hello"`)
maybePanic(err)
assertJSON(t, i, "scanned []byte")
var null JSON
err = null.Scan(nil)
maybePanic(err)
assertNullJSON(t, null, "scanned null")
}
func assertJSON(t *testing.T, i JSON, from string) {
t.Helper()
if !bytes.Equal(i.JSON, []byte(`"hello"`)) {
t.Errorf("bad %s []byte: %#v ≠ %#v\n", from, string(i.JSON), string([]byte(`"hello"`)))
}
if !i.Valid {
t.Error(from, "is invalid, but should be valid")
}
}
func assertNullJSON(t *testing.T, i JSON, from string) {
t.Helper()
if i.Valid {
t.Error(from, "is valid, but should be invalid")
}
}