-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
218 lines (204 loc) Β· 5.06 KB
/
Copy pathjson_test.go
File metadata and controls
218 lines (204 loc) Β· 5.06 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
package flexjson
import (
"reflect"
"testing"
)
func TestParsePartialJSONObject(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]any
wantErr bool
}{
// Complete JSON objects
{
name: "Empty object",
input: "{}",
expected: map[string]any{},
wantErr: false,
},
{
name: "Complete simple key-value",
input: `{"key": 123}`,
expected: map[string]any{"key": int64(123)},
wantErr: false,
},
{
name: "Complete multiple key-values",
input: `{"key1": 123, "key2": "value", "key3": true}`,
expected: map[string]any{
"key1": int64(123),
"key2": "value",
"key3": true,
},
wantErr: false,
},
// Partial JSON cases
{
name: "Partial simple key-value without closing brace",
input: `{"key": 123`,
expected: map[string]any{"key": int64(123)},
wantErr: false,
},
{
name: "Partial key-value with incomplete second key",
input: `{"key1": 1234, "key2":`,
expected: map[string]any{"key1": int64(1234), "key2": nil},
wantErr: false,
},
{
name: "Partial with trailing comma",
input: `{"key1": "value", "key2": false,`,
expected: map[string]any{"key1": "value", "key2": false},
wantErr: false,
},
{
name: "Partial with incomplete key (no colon)",
input: `{"key1": true, "key2`,
expected: map[string]any{"key1": true, "key2": nil},
wantErr: false,
},
// Nested objects and arrays
{
name: "Complete nested object",
input: `{"key1": {"nested": 42}}`,
expected: map[string]any{"key1": map[string]any{"nested": int64(42)}},
wantErr: false,
},
{
name: "Partial nested object",
input: `{"key1": {"nested": 42`,
expected: map[string]any{"key1": map[string]any{"nested": int64(42)}},
wantErr: false,
},
{
name: "Complete with array",
input: `{"key1": [1, 2, 3]}`,
expected: map[string]any{"key1": []interface{}{int64(1), int64(2), int64(3)}},
wantErr: false,
},
{
name: "Partial array",
input: `{"key1": [1, 2,`,
expected: map[string]any{"key1": []interface{}{int64(1), int64(2)}},
wantErr: false,
},
// Error cases
{
name: "Not an object",
input: `[1, 2, 3]`,
expected: nil,
wantErr: true,
},
{
name: "Empty string",
input: ``,
expected: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Parse(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParsePartialJSONObject() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ParsePartialJSONObject() = %v, want %v", result, tt.expected)
}
})
}
}
// Test the exact examples from the requirements
func TestRequirementExamples(t *testing.T) {
// Example 1: {"key": 123 should parse into map[string]any{"key": 123}
example1, err := Parse(`{"key": 123`)
if err != nil {
t.Errorf("Failed on example 1: %v", err)
}
expected1 := map[string]any{"key": int64(123)}
if !reflect.DeepEqual(example1, expected1) {
t.Errorf("Example 1 result = %v, want %v", example1, expected1)
}
// Example 2: {"key": 1234, "key2": should parse into map[string]any{"key": 1234, "key2": nil}
example2, err := Parse(`{"key": 1234, "key2":`)
if err != nil {
t.Errorf("Failed on example 2: %v", err)
}
expected2 := map[string]any{"key": int64(1234), "key2": nil}
if !reflect.DeepEqual(example2, expected2) {
t.Errorf("Example 2 result = %v, want %v", example2, expected2)
}
}
func TestEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
expected map[string]any
wantErr bool
}{
{
name: "Nested incomplete objects",
input: `{"obj1": {"key1": 1, "obj2": {"key2":`,
expected: map[string]any{
"obj1": map[string]any{
"key1": int64(1),
"obj2": map[string]any{
"key2": nil,
},
},
},
wantErr: false,
},
{
name: "Mixed types",
input: `{"str": "hello", "num": 42, "bool": true, "null": null, "arr": [1,`,
expected: map[string]any{
"str": "hello",
"num": int64(42),
"bool": true,
"null": nil,
"arr": []interface{}{int64(1)},
},
wantErr: false,
},
{
name: "Unicode characters",
input: `{"unicode": "πππ", "key2":`,
expected: map[string]any{
"unicode": "πππ",
"key2": nil,
},
wantErr: false,
},
{
name: "Deeply nested structure",
input: `{"l1": {"l2": {"l3": {"l4": {"l5":`,
expected: map[string]any{
"l1": map[string]any{
"l2": map[string]any{
"l3": map[string]any{
"l4": map[string]any{
"l5": nil,
},
},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Parse(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParsePartialJSONObject() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ParsePartialJSONObject() = %v, want %v", result, tt.expected)
}
})
}
}