-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmapref_test.go
More file actions
283 lines (259 loc) · 6.99 KB
/
mapref_test.go
File metadata and controls
283 lines (259 loc) · 6.99 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
package jsonic
import (
"testing"
)
// expectMapRef parses input with MapRef enabled and checks the result.
func expectMapRef(t *testing.T, input string, expected any) {
t.Helper()
j := Make(Options{Info: &InfoOptions{Map: boolPtr(true)}})
got, err := j.Parse(input)
if err != nil {
t.Errorf("Parse(%q) unexpected error: %v", input, err)
return
}
if !mapRefEqual(got, expected) {
t.Errorf("Parse(%q)\n got: %#v\n expected: %#v",
input, got, expected)
}
}
// mapRefEqual compares values including MapRef structs.
func mapRefEqual(a, b any) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
switch av := a.(type) {
case MapRef:
bv, ok := b.(MapRef)
if !ok {
return false
}
if av.Implicit != bv.Implicit {
return false
}
if len(av.Val) != len(bv.Val) {
return false
}
for k, v := range av.Val {
bval, exists := bv.Val[k]
if !exists || !mapRefEqual(v, bval) {
return false
}
}
// Meta: treat nil and empty map as equal
if len(av.Meta) != 0 || len(bv.Meta) != 0 {
if len(av.Meta) != len(bv.Meta) {
return false
}
for k, v := range av.Meta {
bval, exists := bv.Meta[k]
if !exists || !mapRefEqual(v, bval) {
return false
}
}
}
return true
case map[string]any:
bv, ok := b.(map[string]any)
if !ok || len(av) != len(bv) {
return false
}
for k, v := range av {
bval, exists := bv[k]
if !exists || !mapRefEqual(v, bval) {
return false
}
}
return true
case []any:
bv, ok := b.([]any)
if !ok || len(av) != len(bv) {
return false
}
for i := range av {
if !mapRefEqual(av[i], bv[i]) {
return false
}
}
return true
case float64:
bv, ok := b.(float64)
return ok && av == bv
case bool:
bv, ok := b.(bool)
return ok && av == bv
case string:
bv, ok := b.(string)
return ok && av == bv
default:
return false
}
}
// mr is shorthand to create a MapRef value.
func mr(implicit bool, pairs ...any) MapRef {
m := make(map[string]any)
for i := 0; i+1 < len(pairs); i += 2 {
k, _ := pairs[i].(string)
m[k] = pairs[i+1]
}
return MapRef{Val: m, Implicit: implicit}
}
func TestMapRefOff(t *testing.T) {
// Default (MapRef off) - plain map[string]any in output.
j := Make()
got, err := j.Parse("{a:1}")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := got.(map[string]any); !ok {
t.Errorf("expected map[string]any, got %T: %#v", got, got)
}
}
func TestMapRefExplicitOff(t *testing.T) {
// Explicitly setting MapRef to false.
j := Make(Options{Info: &InfoOptions{Map: boolPtr(false)}})
got, err := j.Parse("{a:1}")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := got.(map[string]any); !ok {
t.Errorf("expected map[string]any, got %T: %#v", got, got)
}
}
func TestMapRefExplicitMap(t *testing.T) {
// Explicit map with braces: not implicit.
expectMapRef(t, "{a:1,b:2}", mr(false, "a", 1.0, "b", 2.0))
}
func TestMapRefExplicitEmpty(t *testing.T) {
// Empty explicit map.
expectMapRef(t, "{}", mr(false))
}
func TestMapRefImplicitMap(t *testing.T) {
// Implicit map via key:value without braces.
expectMapRef(t, "a:1", mr(true, "a", 1.0))
}
func TestMapRefImplicitMultipleKeys(t *testing.T) {
// Implicit map with multiple keys.
expectMapRef(t, "a:1,b:2", mr(true, "a", 1.0, "b", 2.0))
}
func TestMapRefImplicitSpaceSeparated(t *testing.T) {
// Implicit map with space-separated pairs.
expectMapRef(t, "a:1 b:2", mr(true, "a", 1.0, "b", 2.0))
}
func TestMapRefNestedExplicit(t *testing.T) {
// Nested explicit maps.
expectMapRef(t, "{a:{b:1}}", mr(false, "a", mr(false, "b", 1.0)))
}
func TestMapRefNestedImplicitInExplicit(t *testing.T) {
// Implicit map nested inside explicit map value (via pair dive).
expectMapRef(t, "{a:b:1}", mr(false, "a", mr(true, "b", 1.0)))
}
func TestMapRefListsUnaffected(t *testing.T) {
// Lists should not be wrapped in MapRef.
j := Make(Options{Info: &InfoOptions{Map: boolPtr(true)}})
got, err := j.Parse("[1,2]")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if _, ok := got.([]any); !ok {
t.Errorf("expected []any, got %T: %#v", got, got)
}
}
func TestMapRefScalarsUnaffected(t *testing.T) {
// Scalars should not be affected.
j := Make(Options{Info: &InfoOptions{Map: boolPtr(true)}})
got, err := j.Parse("42")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if f, ok := got.(float64); !ok || f != 42.0 {
t.Errorf("expected 42.0, got %#v", got)
}
got, err = j.Parse("true")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if b, ok := got.(bool); !ok || b != true {
t.Errorf("expected true, got %#v", got)
}
}
func TestMapRefDeepMerge(t *testing.T) {
// Extension (deep merge) with MapRef enabled.
expectMapRef(t, "a:{b:1},a:{c:2}", mr(true, "a", mr(false, "b", 1.0, "c", 2.0)))
}
func TestMapRefMapInList(t *testing.T) {
// MapRef inside a list.
j := Make(Options{Info: &InfoOptions{Map: boolPtr(true)}})
got, err := j.Parse("[{a:1},{b:2}]")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
arr, ok := got.([]any)
if !ok {
t.Fatalf("expected []any, got %T: %#v", got, got)
}
if len(arr) != 2 {
t.Fatalf("expected 2 elements, got %d", len(arr))
}
m0, ok := arr[0].(MapRef)
if !ok {
t.Errorf("expected MapRef for element 0, got %T: %#v", arr[0], arr[0])
} else if m0.Implicit || len(m0.Val) != 1 || m0.Val["a"] != 1.0 {
t.Errorf("element 0: expected {a:1} explicit, got %#v", m0)
}
m1, ok := arr[1].(MapRef)
if !ok {
t.Errorf("expected MapRef for element 1, got %T: %#v", arr[1], arr[1])
} else if m1.Implicit || len(m1.Val) != 1 || m1.Val["b"] != 2.0 {
t.Errorf("element 1: expected {b:2} explicit, got %#v", m1)
}
}
func TestMapRefWithStringValues(t *testing.T) {
expectMapRef(t, `{a:"hello",b:"world"}`, mr(false, "a", "hello", "b", "world"))
}
func TestMapRefCombinedWithListRef(t *testing.T) {
// Both MapRef and ListRef enabled.
j := Make(Options{Info: &InfoOptions{Map: boolPtr(true), List: boolPtr(true)}})
got, err := j.Parse("{a:[1,2]}")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
m, ok := got.(MapRef)
if !ok {
t.Fatalf("expected MapRef, got %T: %#v", got, got)
}
if m.Implicit {
t.Errorf("expected Implicit=false for braced map")
}
listVal, ok := m.Val["a"].(ListRef)
if !ok {
t.Fatalf("expected ListRef for key 'a', got %T: %#v", m.Val["a"], m.Val["a"])
}
if listVal.Implicit {
t.Errorf("expected Implicit=false for bracketed list")
}
if len(listVal.Val) != 2 {
t.Fatalf("expected 2 elements, got %d", len(listVal.Val))
}
}
func TestMapRefCombinedWithTextInfo(t *testing.T) {
// Both MapRef and TextInfo enabled.
j := Make(Options{Info: &InfoOptions{Map: boolPtr(true), Text: boolPtr(true)}})
got, err := j.Parse(`{"a":1}`)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
m, ok := got.(MapRef)
if !ok {
t.Fatalf("expected MapRef, got %T: %#v", got, got)
}
if m.Implicit {
t.Errorf("expected Implicit=false for braced map")
}
}
func TestMapRefSingleKey(t *testing.T) {
// Single key explicit map.
expectMapRef(t, "{a:1}", mr(false, "a", 1.0))
}