-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_test.go
214 lines (173 loc) · 4.75 KB
/
update_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
package astjson
import (
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestObjectDelSet(t *testing.T) {
var p Parser
var o *Object
o.Del("xx")
v, err := p.Parse(`{"fo\no": "bar", "x": [1,2,3]}`)
if err != nil {
t.Fatalf("unexpected error during parse: %s", err)
}
o, err = v.Object()
if err != nil {
t.Fatalf("cannot obtain object: %s", err)
}
// Delete x
o.Del("x")
if o.Len() != 1 {
t.Fatalf("unexpected number of items left; got %d; want %d", o.Len(), 1)
}
// Try deleting non-existing value
o.Del("xxx")
if o.Len() != 1 {
t.Fatalf("unexpected number of items left; got %d; want %d", o.Len(), 1)
}
// Set new value
vNew := MustParse(`{"foo":[1,2,3]}`)
o.Set("new_key", vNew)
// Delete item with escaped key
o.Del("fo\no")
if o.Len() != 1 {
t.Fatalf("unexpected number of items left; got %d; want %d", o.Len(), 1)
}
str := o.String()
strExpected := `{"new_key":{"foo":[1,2,3]}}`
if str != strExpected {
t.Fatalf("unexpected string representation for o: got %q; want %q", str, strExpected)
}
// Set and Del function as no-op on nil value
o = nil
o.Del("x")
o.Set("x", MustParse(`[3]`))
}
func TestValueDelSet(t *testing.T) {
var p Parser
v, err := p.Parse(`{"xx": 123, "x": [1,2,3]}`)
if err != nil {
t.Fatalf("unexpected error during parse: %s", err)
}
// Delete xx
v.Del("xx")
n := v.GetObject().Len()
if n != 1 {
t.Fatalf("unexpected number of items left; got %d; want %d", n, 1)
}
// Try deleting non-existing value in the array
va := v.Get("x")
va.Del("foobar")
// Delete middle element in the array
va.Del("1")
a := v.GetArray("x")
if len(a) != 2 {
t.Fatalf("unexpected number of items left in the array; got %d; want %d", len(a), 2)
}
// Update the first element in the array
vNew := MustParse(`"foobar"`)
va.Set("0", vNew)
// Add third element to the array
vNew = MustParse(`[3]`)
va.Set("3", vNew)
// Add invalid array index to the array
va.Set("invalid", MustParse(`"nonsense"`))
str := v.String()
strExpected := `{"x":["foobar",3,null,[3]]}`
if str != strExpected {
t.Fatalf("unexpected string representation for o: got %q; want %q", str, strExpected)
}
// Set and Del function as no-op on nil value
v = nil
v.Del("x")
v.Set("x", MustParse(`[]`))
v.SetArrayItem(1, MustParse(`[]`))
}
func TestValue_AppendArrayItems(t *testing.T) {
left := MustParse(`[1,2,3]`)
right := MustParse(`[4,5,6]`)
left.AppendArrayItems(right)
if len(left.GetArray()) != 6 {
t.Fatalf("unexpected length; got %d; want %d", len(left.GetArray()), 6)
}
out := left.MarshalTo(nil)
if string(out) != `[1,2,3,4,5,6]` {
t.Fatalf("unexpected output; got %q; want %q", out, `[1,2,3,4,5,6]`)
}
}
func TestMergeWithSwap(t *testing.T) {
left := MustParse(`{"a":{"b":1,"c":2,"e":[],"f":[1],"h":[1,2,3]}}`)
right := MustParse(`{"a":{"b":2,"d":3,"e":[1,2,3],"g":[1],"h":[4,5,6]}}`)
out, _ := MergeValues(left, right)
assert.Equal(t, `{"a":{"b":2,"c":2,"e":[1,2,3],"f":[1],"h":[4,5,6],"d":3,"g":[1]}}`, out.String())
}
type RootObject struct {
Child *ChildObject `json:"child"`
}
type ChildObject struct {
GrandChild *GrandChildObject `json:"grand_child"`
}
type GrandChildObject struct {
Items []string `json:"items"`
}
func BenchmarkValue_SetArrayItem(b *testing.B) {
root := &RootObject{
Child: &ChildObject{
GrandChild: &GrandChildObject{
Items: make([]string, 0),
},
},
}
l, err := json.Marshal(root)
assert.NoError(b, err)
root.Child.GrandChild.Items = make([]string, 1024*1024)
for i := 0; i < 1024*1024; i++ {
root.Child.GrandChild.Items[i] = strings.Repeat("a", 1024)
}
r, err := json.Marshal(root)
assert.NoError(b, err)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
l, _ := ParseBytesWithoutCache(l)
r, _ := ParseBytesWithoutCache(r)
out, _ := MergeValues(l, r)
arr := out.GetArray("child", "grand_child", "items")
assert.Len(b, arr, 1024*1024)
}
}
func BenchmarkMergeValuesWithATonOfRecursion(b *testing.B) {
b.ReportAllocs()
left := MustParse(`{"a":{}}`)
str := fmt.Sprintf(
`{"ba":{"bb":{"bc":{"bd":[%s, %s, %s], "be": {"bf": %s}}}}}`,
objectWithRecursion(10),
objectWithRecursion(20),
objectWithRecursion(2),
objectWithRecursion(3))
expected := fmt.Sprintf(
`{"a":{},"ba":{"bb":{"bc":{"bd":[%s,%s,%s],"be":{"bf":%s}}}}}`,
objectWithRecursion(10),
objectWithRecursion(20),
objectWithRecursion(2),
objectWithRecursion(3))
_ = expected
right := MustParse(str)
for i := 0; i < b.N; i++ {
_, _ = MergeValues(left, right)
/*v, _ := MergeValues(left, right)
if v.String() != expected {
assert.Equal(b, expected, v.String())
}*/
}
fmt.Printf("left: %s\n", left.String())
}
func objectWithRecursion(depth int) string {
if depth == 0 {
return `{}`
}
return `{"a":` + objectWithRecursion(depth-1) + `}`
}