-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheasyjson_functional_test.go
More file actions
400 lines (358 loc) · 9.7 KB
/
easyjson_functional_test.go
File metadata and controls
400 lines (358 loc) · 9.7 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package easyjson
import (
"encoding/hex"
"reflect"
"strconv"
"testing"
)
func TestNewJSON_Basics(t *testing.T) {
j := NewJSONObject()
if !j.IsObject() {
t.Fatalf("expected object")
}
arr := NewJSONArray()
if !arr.IsArray() {
t.Fatalf("expected array")
}
null := NewJSONNull()
if !null.IsNull() {
t.Fatalf("expected null")
}
jArr := NewJSON([]int{1, 2, 3})
if !jArr.IsArray() {
t.Fatalf("expected array from []int")
}
if jArr.ArraySize() != 3 {
t.Fatalf("expected size 3, got %d", jArr.ArraySize())
}
}
func TestPathExistsAndGetByPath_Object(t *testing.T) {
j := NewJSONObject()
ok := j.SetByPath("a.b.c", NewJSON(123))
if !ok {
t.Fatalf("SetByPath failed")
}
if !j.PathExists("a.b.c") {
t.Fatalf("PathExists a.b.c should be true")
}
if j.PathExists("a.b.x") {
t.Fatalf("PathExists a.b.x should be false")
}
got := j.GetByPath("a.b.c")
num, ok := got.AsNumeric()
if !ok || num != 123 {
t.Fatalf("expected 123, got %v (ok=%v)", got.Value, ok)
}
}
func TestPath_CustomDelimiter(t *testing.T) {
j := NewJSONObject()
if !j.SetByPathCustomDelimiter("foo/bar/baz", NewJSON("ok"), "/") {
t.Fatalf("SetByPathCustomDelimiter failed")
}
if !j.PathExists("foo/bar/baz", "/") {
t.Fatalf("PathExists with custom delimiter failed")
}
if s, ok := j.GetByPath("foo/bar/baz", "/").AsString(); !ok || s != "ok" {
t.Fatalf("expected ok at foo/bar/baz, got %q", s)
}
}
func TestSetByPath_ArrayIndexingAndPush(t *testing.T) {
j := NewJSONObject()
if !j.SetByPath("arr.2", NewJSON("bar")) {
t.Fatalf("SetByPath arr.2 failed")
}
if sz := j.GetByPath("arr").ArraySize(); sz != 3 {
t.Fatalf("expected size 3 after setting index 2, got %d", sz)
}
if s, ok := j.GetByPath("arr.2").AsString(); !ok || s != "bar" {
t.Fatalf("expected bar at arr.2, got %q", s)
}
if !j.SetByPath("arr.-1", NewJSON("tail")) {
t.Fatalf("SetByPath arr.-1 failed")
}
if sz := j.GetByPath("arr").ArraySize(); sz != 4 {
t.Fatalf("expected size 4 after push, got %d", sz)
}
if s, ok := j.GetByPath("arr.3").AsString(); !ok || s != "tail" {
t.Fatalf("expected tail at arr.3, got %q", s)
}
if !j.SetByPath("arr.0", NewJSONObject()) {
t.Fatalf("SetByPath arr.0 = {} failed")
}
if !j.SetByPath("arr.0.x", NewJSON("y")) {
t.Fatalf("SetByPath arr.0.x failed")
}
if s, ok := j.GetByPath("arr.0.x").AsString(); !ok || s != "y" {
t.Fatalf("expected y at arr.0.x, got %q", s)
}
}
func TestRemoveByPath_ObjectAndArray(t *testing.T) {
j := NewJSONObject()
j.SetByPath("a.b.c", NewJSON(1))
j.SetByPath("a.b.d", NewJSON(2))
j.SetByPath("arr.0", NewJSON("x"))
j.SetByPath("arr.1", NewJSON("y"))
if !j.RemoveByPath("a.b.c") {
t.Fatalf("RemoveByPath a.b.c failed")
}
if j.PathExists("a.b.c") {
t.Fatalf("a.b.c should be removed")
}
if !j.RemoveByPath("arr.0") {
t.Fatalf("RemoveByPath arr.0 failed")
}
if v := j.GetByPath("arr.0"); !v.IsNull() {
t.Fatalf("expected nil at arr.0 after remove, got %v", v.Value)
}
if s, ok := j.GetByPath("arr.1").AsString(); !ok || s != "y" {
t.Fatalf("expected y at arr.1, got %q", s)
}
}
func TestDeepMerge(t *testing.T) {
a := NewJSONObject()
a.SetByPath("obj.x", NewJSON(1))
a.SetByPath("arr.0", NewJSON(1))
a.SetByPath("arr.1", NewJSON(2))
b := NewJSONObject()
b.SetByPath("obj.y", NewJSON(2))
b.SetByPath("arr.0", NewJSON(2))
b.SetByPath("arr.2", NewJSON(3))
a.DeepMerge(b)
if n := a.GetByPath("obj.x").AsNumericDefault(-1); n != 1 {
t.Fatalf("obj.x expected 1, got %v", n)
}
if n := a.GetByPath("obj.y").AsNumericDefault(-1); n != 2 {
t.Fatalf("obj.y expected 2, got %v", n)
}
arr := a.GetByPath("arr")
if !arr.IsArray() {
t.Fatalf("arr should be array, got %T", arr.Value)
}
if arr.ArraySize() != 3 {
t.Fatalf("merged array length mismatch: want 3, got %d", arr.ArraySize())
}
got := []float64{
arr.GetByPath("0").AsNumericDefault(-1),
arr.GetByPath("1").AsNumericDefault(-1),
arr.GetByPath("2").AsNumericDefault(-1),
}
want := []float64{1, 2, 3}
if !reflect.DeepEqual(got, want) {
t.Fatalf("merged array mismatch\nwant: %#v\ngot : %#v", want, got)
}
}
func TestEqualsAndClone(t *testing.T) {
j := NewJSONObject()
j.SetByPath("a.b", NewJSON("c"))
j.SetByPath("n", NewJSON(10))
cl := j.Clone()
if !j.Equals(cl) {
t.Fatalf("clone should be equal initially")
}
cl.SetByPath("a.b", NewJSON("changed"))
if j.Equals(cl) {
t.Fatalf("after change, clone must differ from original")
}
if s, _ := j.GetByPath("a.b").AsString(); s != "c" {
t.Fatalf("original should keep 'c', got %q", s)
}
}
func TestJSONFromStringAndBytes(t *testing.T) {
src := `{"a": {"b": 1}, "arr":[1,2,3]}`
j, ok := JSONFromString(src)
if !ok {
t.Fatalf("JSONFromString failed")
}
if j.GetByPath("a.b").AsNumericDefault(-1) != 1 {
t.Fatalf("expected a.b == 1")
}
if j.ArraySize() != -1 {
t.Fatalf("root not an array")
}
b := j.ToBytes()
j2, ok := JSONFromBytes(b)
if !ok || !j.Equals(j2) {
t.Fatalf("JSONFromBytes/ToBytes roundtrip failed")
}
}
func TestNewJSONBytes_HexRoundtrip(t *testing.T) {
raw := []byte{0xde, 0xad, 0xbe, 0xef}
j := NewJSONBytes(raw)
got, ok := j.AsBytes()
if !ok {
t.Fatalf("AsBytes failed")
}
if !reflect.DeepEqual(got, raw) {
t.Fatalf("hex roundtrip mismatch: %s vs %s", hex.EncodeToString(got), hex.EncodeToString(raw))
}
}
func TestAsConversions(t *testing.T) {
jNum := NewJSON(3.14)
if v, ok := jNum.AsNumeric(); !ok || v != 3.14 {
t.Fatalf("AsNumeric failed: %v, %v", v, ok)
}
if jNum.AsStringDefault("x") != "x" {
t.Fatalf("AsStringDefault should return default for non-string")
}
jStr := NewJSON("hi")
if s, ok := jStr.AsString(); !ok || s != "hi" {
t.Fatalf("AsString failed")
}
if !NewJSON(true).AsBoolDefault(false) {
t.Fatalf("AsBoolDefault failed")
}
}
func TestObjectKeysAndCounts(t *testing.T) {
j := NewJSONObject()
j.SetByPath("x", NewJSON(1))
j.SetByPath("y", NewJSON(2))
keys := j.ObjectKeys()
if len(keys) != 2 {
t.Fatalf("expected 2 keys, got %d: %v", len(keys), keys)
}
if j.KeysCount() != 2 || !j.IsNonEmptyObject() {
t.Fatalf("KeysCount / IsNonEmptyObject mismatch")
}
}
func TestArrayHelpers(t *testing.T) {
arr := NewJSONArray()
arr.AddToArray(NewJSON("a"))
arr.AddToArray(NewJSON("b"))
if arr.ArraySize() != 2 {
t.Fatalf("ArraySize expected 2, got %d", arr.ArraySize())
}
if arr.ArrayElement(1).AsStringDefault("") != "b" {
t.Fatalf("ArrayElement(1) expected 'b'")
}
}
func TestSetByPaths(t *testing.T) {
j := NewJSONObject()
j.SetByPaths(map[string]interface{}{
"a.b": 1,
"x.y": "z",
})
if j.GetByPath("a.b").AsNumericDefault(-1) != 1 {
t.Fatalf("a.b expected 1")
}
if j.GetByPath("x.y").AsStringDefault("") != "z" {
t.Fatalf("x.y expected z")
}
}
func TestNewJSONObjectFromMapAndBuildFromTemplate(t *testing.T) {
data := map[string]interface{}{
"a.b": 1,
"x.y": "z",
}
j := NewJSONObjectFromMap(data)
if j.GetByPath("a.b").AsNumericDefault(-1) != 1 ||
j.GetByPath("x.y").AsStringDefault("") != "z" {
t.Fatalf("NewJSONObjectFromMap failed")
}
tpl := map[string]interface{}{
"k.v": 42,
}
j2 := BuildFromTemplate(tpl)
if j2.GetByPath("k.v").AsNumericDefault(0) != 42 {
t.Fatalf("BuildFromTemplate failed")
}
}
func TestJSONBuilder(t *testing.T) {
b := NewJSONBuilder().
Set("a.b", 1).
SetIfNotEmpty("skip.empty", "").
AddToArray("arr", "x").
AddToArray("arr", "y")
j := b.Build()
if j.PathExists("skip.empty") {
t.Fatalf("SetIfNotEmpty should skip empty values")
}
if j.GetByPath("a.b").AsNumericDefault(-1) != 1 {
t.Fatalf("a.b expected 1")
}
if j.GetByPath("arr").ArraySize() != 2 {
t.Fatalf("arr size expected 2")
}
}
func TestBuildArrayFromSlice(t *testing.T) {
type item struct{ ID int }
js := BuildArrayFromSlice([]item{{1}, {2}, {3}}, func(it item) map[string]interface{} {
return map[string]interface{}{"id": it.ID}
})
if js.ArraySize() != 3 {
t.Fatalf("expected array of 3")
}
for i := 0; i < 3; i++ {
got := js.GetByPath(strconv.Itoa(i) + ".id").AsNumericDefault(-1)
if int(got) != i+1 {
t.Fatalf("item %d id expected %d, got %v", i, i+1, got)
}
}
}
func TestAsInt64(t *testing.T) {
// int64 value that loses precision through float64
big := int64(1<<53 + 1) // 9007199254740993
j := NewJSON(big)
got, ok := j.AsInt64()
if !ok {
t.Fatalf("AsInt64 returned ok=false for int64 value")
}
if got != big {
t.Fatalf("AsInt64 precision loss: expected %d, got %d", big, got)
}
// AsNumeric loses precision for this value
f, _ := j.AsNumeric()
if int64(f) == big {
t.Fatalf("expected float64 to lose precision for 2^53+1, but it didn't")
}
// Test with other numeric types
tests := []struct {
name string
val interface{}
want int64
}{
{"int", int(42), 42},
{"uint", uint(100), 100},
{"uint32", uint32(200), 200},
{"uint64", uint64(300), 300},
{"byte", byte(7), 7},
{"float64", float64(99), 99},
{"float32", float32(50), 50},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
j := NewJSON(tt.val)
got, ok := j.AsInt64()
if !ok {
t.Fatalf("AsInt64 returned ok=false for %T", tt.val)
}
if got != tt.want {
t.Fatalf("expected %d, got %d", tt.want, got)
}
})
}
// Non-numeric should return false
if _, ok := NewJSON("hello").AsInt64(); ok {
t.Fatalf("AsInt64 should return false for string")
}
}
func TestAsInt64Default(t *testing.T) {
j := NewJSON(int64(42))
if got := j.AsInt64Default(0); got != 42 {
t.Fatalf("expected 42, got %d", got)
}
if got := NewJSON("x").AsInt64Default(-1); got != -1 {
t.Fatalf("expected default -1, got %d", got)
}
}
func TestAsInt64_SetByPath(t *testing.T) {
big := int64(1<<53 + 1)
j := NewJSONObject()
j.SetByPath("op.time", NewJSON(big))
got, ok := j.GetByPath("op.time").AsInt64()
if !ok {
t.Fatalf("AsInt64 returned false after SetByPath")
}
if got != big {
t.Fatalf("precision loss after SetByPath: expected %d, got %d", big, got)
}
}