forked from zendev-sh/goai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartial_json_test.go
More file actions
508 lines (469 loc) · 10.7 KB
/
partial_json_test.go
File metadata and controls
508 lines (469 loc) · 10.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package goai
import (
"testing"
)
func TestRepairJSON(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
// Valid JSON passes through unchanged.
{
name: "complete object",
input: `{"name": "Alice", "age": 30}`,
expected: `{"name": "Alice", "age": 30}`,
},
{
name: "complete array",
input: `[1, 2, 3]`,
expected: `[1, 2, 3]`,
},
{
name: "complete nested",
input: `{"a": {"b": [1, 2]}}`,
expected: `{"a": {"b": [1, 2]}}`,
},
// Empty string → "{}".
{
name: "empty string",
input: "",
expected: "{}",
},
{
name: "whitespace only",
input: " \t\n",
expected: "{}",
},
// Truncated string values.
{
name: "truncated string value",
input: `{"name": "Pa`,
expected: `{"name": "Pa"}`,
},
{
name: "truncated string mid-word",
input: `{"greeting": "hello wor`,
expected: `{"greeting": "hello wor"}`,
},
// Truncated arrays.
{
name: "truncated array trailing comma",
input: `[1, 2,`,
expected: `[1, 2]`,
},
{
name: "truncated array no comma",
input: `[1, 2`,
expected: `[1, 2]`,
},
{
name: "truncated array after opening",
input: `[`,
expected: `[]`,
},
// Truncated objects.
{
name: "truncated object trailing colon",
input: `{"a": 1, "b":`,
expected: `{"a": 1}`,
},
{
name: "truncated object trailing comma",
input: `{"a": 1,`,
expected: `{"a": 1}`,
},
{
name: "truncated object after opening",
input: `{`,
expected: `{}`,
},
{
name: "truncated object key only",
input: `{"a": 1, "b"`,
expected: `{"a": 1, "b"}`,
},
// Nested truncation.
{
name: "nested object truncation",
input: `{"a": {"b": 1`,
expected: `{"a": {"b": 1}}`,
},
{
name: "deeply nested truncation",
input: `{"a": {"b": {"c": 2`,
expected: `{"a": {"b": {"c": 2}}}`,
},
// Array in object.
{
name: "array in object truncated",
input: `{"a": [1, 2`,
expected: `{"a": [1, 2]}`,
},
{
name: "array in object trailing comma",
input: `{"a": [1, 2,`,
expected: `{"a": [1, 2]}`,
},
// Truncated keywords.
{
name: "truncated true",
input: `{"valid": tru`,
expected: `{"valid": true}`,
},
{
name: "truncated false",
input: `{"valid": fal`,
expected: `{"valid": false}`,
},
{
name: "truncated null",
input: `{"val": nul`,
expected: `{"val": null}`,
},
{
name: "truncated true single char",
input: `{"ok": t`,
expected: `{"ok": true}`,
},
// Truncated numbers.
{
name: "truncated decimal",
input: `{"val": 1.`,
expected: `{"val": 1.0}`,
},
{
name: "truncated negative sign",
input: `{"val": -`,
expected: `{"val": -0}`,
},
{
name: "truncated exponent",
input: `{"val": 1e`,
expected: `{"val": 1e0}`,
},
{
name: "truncated exponent uppercase",
input: `{"val": 1E`,
expected: `{"val": 1E0}`,
},
// Escaped quotes in strings.
{
name: "escaped quote truncated",
input: `{"a": "he said \"hi`,
expected: `{"a": "he said \"hi"}`,
},
{
name: "escaped quote complete",
input: `{"a": "he said \"hi\""}`,
expected: `{"a": "he said \"hi\""}`,
},
{
name: "truncated after escape backslash",
input: `{"a": "path\\`,
expected: `{"a": "path\\"}`,
},
// Object in array.
{
name: "object in array truncated",
input: `[{"a": 1}, {"b":`,
expected: `[{"a": 1}, {}]`,
},
// Mixed nesting.
{
name: "array of arrays truncated",
input: `[[1, 2], [3`,
expected: `[[1, 2], [3]]`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := repairJSON(tt.input)
if got != tt.expected {
t.Errorf("repairJSON(%q)\n got: %q\n want: %q", tt.input, got, tt.expected)
}
})
}
}
func TestCompleteTrailing(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "complete true keyword",
input: `{"a": tr`,
expected: `{"a": true`,
},
{
name: "complete false keyword",
input: `{"a": fa`,
expected: `{"a": false`,
},
{
name: "complete null keyword",
input: `{"a": nu`,
expected: `{"a": null`,
},
{
name: "trailing dot",
input: `{"a": 3.`,
expected: `{"a": 3.0`,
},
{
name: "trailing minus",
input: `{"a": -`,
expected: `{"a": -0`,
},
{
name: "trailing plus",
input: `{"a": +`,
expected: `{"a": +0`,
},
{
name: "trailing e",
input: `{"a": 5e`,
expected: `{"a": 5e0`,
},
{
name: "no trailing issue",
input: `{"a": 5`,
expected: `{"a": 5`,
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "keyword not at word boundary -- e treated as exponent",
input: `{"nature`,
expected: `{"nature0`,
},
{
name: "keyword at start of string",
input: `tru`,
expected: `true`,
},
{
name: "keyword prefix not at word boundary -- continue branch",
input: `{"count`,
expected: `{"count`,
},
{
name: "trailing uppercase E",
input: `{"a": 9E`,
expected: `{"a": 9E0`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := completeTrailing(tt.input)
if got != tt.expected {
t.Errorf("completeTrailing(%q)\n got: %q\n want: %q", tt.input, got, tt.expected)
}
})
}
}
func TestTrimTrailingIncomplete(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "trailing comma",
input: `{"a": 1,`,
expected: `{"a": 1`,
},
{
name: "trailing colon with key",
input: `{"a": 1, "b":`,
expected: `{"a": 1`,
},
{
name: "trailing colon with key and spaces",
input: `{"a": 1, "b" :`,
expected: `{"a": 1`,
},
{
name: "no trailing issue",
input: `{"a": 1`,
expected: `{"a": 1`,
},
{
name: "empty string",
input: "",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := trimTrailingIncomplete(tt.input)
if got != tt.expected {
t.Errorf("trimTrailingIncomplete(%q)\n got: %q\n want: %q", tt.input, got, tt.expected)
}
})
}
}
// --- parsePartialJSON tests with typed structs ---
type simpleObj struct {
Name string `json:"name"`
Age int `json:"age"`
}
type nestedObj struct {
A innerObj `json:"a"`
}
type innerObj struct {
B int `json:"b"`
}
type objWithArray struct {
Items []int `json:"items"`
}
type objWithBool struct {
Valid bool `json:"valid"`
}
type objWithFloat struct {
Val float64 `json:"val"`
}
type objWithNull struct {
Val *string `json:"val"`
}
func TestParsePartialJSON_CompleteJSON(t *testing.T) {
input := `{"name": "Alice", "age": 30}`
got, err := parsePartialJSON[simpleObj](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Name != "Alice" || got.Age != 30 {
t.Errorf("got %+v, want {Name:Alice Age:30}", got)
}
}
func TestParsePartialJSON_TruncatedString(t *testing.T) {
input := `{"name": "Ali`
got, err := parsePartialJSON[simpleObj](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Name != "Ali" {
t.Errorf("got Name=%q, want %q", got.Name, "Ali")
}
}
func TestParsePartialJSON_NestedObject(t *testing.T) {
input := `{"a": {"b": 42`
got, err := parsePartialJSON[nestedObj](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.A.B != 42 {
t.Errorf("got A.B=%d, want 42", got.A.B)
}
}
func TestParsePartialJSON_ArrayInObject(t *testing.T) {
input := `{"items": [1, 2, 3`
got, err := parsePartialJSON[objWithArray](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(got.Items) != 3 || got.Items[0] != 1 || got.Items[1] != 2 || got.Items[2] != 3 {
t.Errorf("got Items=%v, want [1 2 3]", got.Items)
}
}
func TestParsePartialJSON_TruncatedBool(t *testing.T) {
input := `{"valid": tru`
got, err := parsePartialJSON[objWithBool](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !got.Valid {
t.Errorf("got Valid=%v, want true", got.Valid)
}
}
func TestParsePartialJSON_TruncatedFloat(t *testing.T) {
input := `{"val": 3.`
got, err := parsePartialJSON[objWithFloat](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Val != 3.0 {
t.Errorf("got Val=%f, want 3.0", got.Val)
}
}
func TestParsePartialJSON_NullValue(t *testing.T) {
input := `{"val": nul`
got, err := parsePartialJSON[objWithNull](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Val != nil {
t.Errorf("got Val=%v, want nil", got.Val)
}
}
func TestParsePartialJSON_EmptyString(t *testing.T) {
// Empty string repairs to "{}", which unmarshals to zero-value struct.
got, err := parsePartialJSON[simpleObj]("")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Name != "" || got.Age != 0 {
t.Errorf("got %+v, want zero-value simpleObj", got)
}
}
func TestParsePartialJSON_Array(t *testing.T) {
input := `[1, 2, 3`
got, err := parsePartialJSON[[]int](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(*got) != 3 {
t.Errorf("got len=%d, want 3", len(*got))
}
}
func TestParsePartialJSON_Map(t *testing.T) {
input := `{"key": "val`
got, err := parsePartialJSON[map[string]string](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if (*got)["key"] != "val" {
t.Errorf("got key=%q, want %q", (*got)["key"], "val")
}
}
func TestParsePartialJSON_EscapedQuotes(t *testing.T) {
input := `{"name": "he said \"hi`
got, err := parsePartialJSON[simpleObj](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
expected := `he said "hi`
if got.Name != expected {
t.Errorf("got Name=%q, want %q", got.Name, expected)
}
}
func TestParsePartialJSON_AlreadyValid(t *testing.T) {
// Ensure valid JSON does NOT go through repair (no mutation).
input := `{"name":"Bob","age":25}`
got, err := parsePartialJSON[simpleObj](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Name != "Bob" || got.Age != 25 {
t.Errorf("got %+v, want {Name:Bob Age:25}", got)
}
}
func TestParsePartialJSON_TruncatedObjectKey(t *testing.T) {
// Truncated after key + colon → should drop the incomplete pair.
input := `{"name": "Alice", "age":`
got, err := parsePartialJSON[simpleObj](input)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.Name != "Alice" {
t.Errorf("got Name=%q, want %q", got.Name, "Alice")
}
// age should be zero since the value was truncated.
if got.Age != 0 {
t.Errorf("got Age=%d, want 0", got.Age)
}
}