-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_test.go
275 lines (222 loc) · 6.18 KB
/
stream_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
package flexjson
import (
"reflect"
"testing"
)
func TestStreamingParser_SimpleObject(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
// Process a simple JSON object character by character
json := `{"name":"John","age":30}`
for _, char := range json {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Check the result
expected := map[string]any{
"name": "John",
"age": int64(30),
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Unexpected result. Got %v, expected %v", output, expected)
}
}
func TestStreamingParser_NestedObject(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
sp.SetDebug(true)
// Process a nested JSON object character by character
json := `{"person":{"name":"John","age":30},"active":true}`
for i, char := range json {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing %dth character '%c': %v", i, char, err)
}
}
// Check the result
expected := map[string]any{
"person": map[string]any{
"name": "John",
"age": int64(30),
},
"active": true,
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Unexpected result. Got %v, expected %v", output, expected)
}
}
func TestStreamingParser_Array(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
// Process a JSON array character by character
json := `{"numbers":[1,2,3],"names":["John","Jane"]}`
for _, char := range json {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Check the result
expected := map[string]any{
"numbers": &[]interface{}{int64(1), int64(2), int64(3)},
"names": &[]interface{}{"John", "Jane"},
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Unexpected result. Got %v, expected %v", output, expected)
}
}
func TestStreamingParser_ComplexTypes(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
// Process JSON with various types character by character
json := `{"string":"hello","number":42,"float":3.14,"bool":true,"null":null}`
for _, char := range json {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Check the result
expected := map[string]any{
"string": "hello",
"number": int64(42),
"float": 3.14,
"bool": true,
"null": nil,
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Unexpected result. Got %v, expected %v", output, expected)
}
}
func TestStreamingParser_PartialJSON(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
// Process the first part of the JSON
json1 := `{"name":"John"`
for _, char := range json1 {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Check intermediate result
expected1 := map[string]any{
"name": "John",
}
if !reflect.DeepEqual(output, expected1) {
t.Errorf("Unexpected intermediate result. Got %v, expected %v", output, expected1)
}
// Process the second part
json2 := `,"age":30}`
for _, char := range json2 {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Check final result
expected2 := map[string]any{
"name": "John",
"age": int64(30),
}
if !reflect.DeepEqual(output, expected2) {
t.Errorf("Unexpected final result. Got %v, expected %v", output, expected2)
}
}
func TestStreamingParser_Reset(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
// Process a JSON object
json1 := `{"name":"John"}`
for _, char := range json1 {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Reset the parser
sp.Reset()
// Process another JSON object
json2 := `{"age":30}`
for _, char := range json2 {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Check the result after reset
expected := map[string]any{
"age": int64(30),
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Unexpected result after reset. Got %v, expected %v", output, expected)
}
}
func TestStreamingParser_Append(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
// Test the Append method with chunks
chunks := []string{
`{"name":`,
`"John",`,
`"age":`,
`30}`,
}
for _, chunk := range chunks {
err := sp.ProcessString(chunk)
if err != nil {
t.Fatalf("Error appending chunk '%s': %v", chunk, err)
}
}
// Check the result
expected := map[string]any{
"name": "John",
"age": int64(30),
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Unexpected result. Got %v, expected %v", output, expected)
}
}
func TestStreamingParser_StringEscapes(t *testing.T) {
output := make(map[string]any)
sp := NewStreamingParser(&output)
// Process JSON with escaped characters
json := `{"escaped":"hello\\world\\\"with quotes\\\""}`
for _, char := range json {
err := sp.ProcessChar(string(char))
if err != nil {
t.Fatalf("Error processing character '%c': %v", char, err)
}
}
// Check the result
expected := map[string]any{
"escaped": `hello\world\"with quotes\"`,
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Unexpected result. Got %v, expected %v", output, expected)
}
}
func TestStreamingParser_RequirementExample(t *testing.T) {
// Test the exact example from the requirements
output := make(map[string]any)
sp := NewStreamingParser(&output)
jsonStr := `{"name":"John Doe","age":30,"email":"[email protected]"}`
// Process character by character with the Append method
for _, char := range jsonStr {
err := sp.ProcessString(string(char))
if err != nil {
t.Fatalf("Error in example: %v", err)
}
}
// Check the result
expected := map[string]any{
"name": "John Doe",
"age": int64(30),
"email": "[email protected]",
}
if !reflect.DeepEqual(output, expected) {
t.Errorf("Example failed. Got %v, expected %v", output, expected)
}
}