Debian Codegraph @

td/td_json_pointer_test.go

raw
1 // Copyright (c) 2020-2022, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package td_test
8
9 import (
10 "encoding/json"
11 "errors"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/internal/test"
15 "github.com/maxatome/go-testdeep/td"
16 )
17
18 type jsonPtrTest int
19
20 func (j jsonPtrTest) UnmarshalJSON(b []byte) error {
21 return errors.New("jsonPtrTest unmarshal custom error")
22 }
23
24 type jsonPtrMap map[string]any
25
26 func (j *jsonPtrMap) UnmarshalJSON(b []byte) error {
27 return json.Unmarshal(b, (*map[string]any)(j))
28 }
29
30 var _ = []json.Unmarshaler{jsonPtrTest(0), &jsonPtrMap{}}
31
32 func TestJSONPointer(t *testing.T) {
33 //
34 // nil
35 t.Run("nil", func(t *testing.T) {
36 checkOK(t, nil, td.JSONPointer("", nil))
37 checkOK(t, (*int)(nil), td.JSONPointer("", nil))
38
39 // Yes encoding/json succeeds to unmarshal nil into an int
40 checkOK(t, nil, td.JSONPointer("", 0))
41 checkOK(t, (*int)(nil), td.JSONPointer("", 0))
42
43 checkError(t, map[string]int{"foo": 42}, td.JSONPointer("/foo", nil),
44 expectedError{
45 Message: mustBe("values differ"),
46 Path: mustBe("DATA.JSONPointer</foo>"),
47 Got: mustBe(`42.0`),
48 Expected: mustBe(`nil`),
49 })
50
51 // As encoding/json succeeds to unmarshal nil into an int
52 checkError(t, map[string]any{"foo": nil}, td.JSONPointer("/foo", 1),
53 expectedError{
54 Message: mustBe("values differ"),
55 Path: mustBe("DATA.JSONPointer</foo>"),
56 Got: mustBe(`0`), // as an int is expected, nil becomes 0
57 Expected: mustBe(`1`),
58 })
59 })
60
61 //
62 // Basic types
63 t.Run("basic types", func(t *testing.T) {
64 checkOK(t, 123, td.JSONPointer("", 123))
65 checkOK(t, 123, td.JSONPointer("", td.Between(120, 130)))
66 checkOK(t, true, td.JSONPointer("", true))
67 })
68
69 //
70 // More complex type with encoding/json tags
71 t.Run("complex type with json tags", func(t *testing.T) {
72 type jpStruct struct {
73 Slice []string `json:"slice,omitempty"`
74 Map map[string]*jpStruct `json:"map,omitempty"`
75 Num int `json:"num"`
76 Bool bool `json:"bool"`
77 Str string `json:"str,omitempty"`
78 }
79
80 got := jpStruct{
81 Slice: []string{"bar", "baz"},
82 Map: map[string]*jpStruct{
83 "test": {
84 Num: 2,
85 Str: "level2",
86 },
87 },
88 Num: 1,
89 Bool: true,
90 Str: "level1",
91 }
92
93 // No filter, should match got or its map representation
94 checkOK(t, got, td.JSONPointer("",
95 map[string]any{
96 "slice": []any{"bar", "baz"},
97 "map": map[string]any{
98 "test": map[string]any{
99 "num": 2,
100 "str": "level2",
101 "bool": false,
102 },
103 },
104 "num": int64(1), // should be OK as Lax is enabled
105 "bool": true,
106 "str": "level1",
107 }))
108 checkOK(t, got, td.JSONPointer("", got))
109 checkOK(t, got, td.JSONPointer("", &got))
110
111 // A specific field
112 checkOK(t, got, td.JSONPointer("/num", int64(1))) // Lax enabled
113 checkOK(t, got, td.JSONPointer("/slice/1", "baz"))
114 checkOK(t, got, td.JSONPointer("/map/test/num", 2))
115 checkOK(t, got, td.JSONPointer("/map/test/str", td.Contains("vel2")))
116
117 checkOK(t, got,
118 td.JSONPointer("/map", td.JSONPointer("/test", td.JSONPointer("/num", 2))))
119
120 checkError(t, got, td.JSONPointer("/zzz/pipo", 666),
121 expectedError{
122 Message: mustBe("cannot retrieve value via JSON pointer"),
123 Path: mustBe("DATA.JSONPointer</zzz>"),
124 Summary: mustBe("key not found"),
125 })
126
127 checkError(t, got, td.JSONPointer("/num/pipo", 666),
128 expectedError{
129 Message: mustBe("cannot retrieve value via JSON pointer"),
130 Path: mustBe("DATA.JSONPointer</num/pipo>"),
131 Summary: mustBe("not a map nor an array"),
132 })
133
134 checkError(t, got, td.JSONPointer("/slice/2", "zip"),
135 expectedError{
136 Message: mustBe("cannot retrieve value via JSON pointer"),
137 Path: mustBe("DATA.JSONPointer</slice/2>"),
138 Summary: mustBe("out of array range"),
139 })
140
141 checkError(t, got, td.JSONPointer("/slice/xxx", "zip"),
142 expectedError{
143 Message: mustBe("cannot retrieve value via JSON pointer"),
144 Path: mustBe("DATA.JSONPointer</slice/xxx>"),
145 Summary: mustBe("array but not an index in JSON pointer"),
146 })
147
148 checkError(t, got, td.JSONPointer("/slice/1", "zip"),
149 expectedError{
150 Message: mustBe("values differ"),
151 Path: mustBe("DATA.JSONPointer</slice/1>"),
152 Got: mustBe(`"baz"`),
153 Expected: mustBe(`"zip"`),
154 })
155
156 // A struct behind a specific field
157 checkOK(t, got, td.JSONPointer("/map/test", map[string]any{
158 "num": 2,
159 "str": "level2",
160 "bool": false,
161 }))
162 checkOK(t, got, td.JSONPointer("/map/test", jpStruct{
163 Num: 2,
164 Str: "level2",
165 }))
166 checkOK(t, got, td.JSONPointer("/map/test", &jpStruct{
167 Num: 2,
168 Str: "level2",
169 }))
170 checkOK(t, got, td.JSONPointer("/map/test", td.Struct(&jpStruct{
171 Num: 2,
172 Str: "level2",
173 }, nil)))
174 })
175
176 //
177 // Complex type without encoding/json tags
178 t.Run("complex type without json tags", func(t *testing.T) {
179 type jpStruct struct {
180 Slice []string
181 Map map[string]*jpStruct
182 Num int
183 Bool bool
184 Str string
185 }
186
187 got := jpStruct{
188 Slice: []string{"bar", "baz"},
189 Map: map[string]*jpStruct{
190 "test": {
191 Num: 2,
192 Str: "level2",
193 },
194 },
195 Num: 1,
196 Bool: true,
197 Str: "level1",
198 }
199
200 checkOK(t, got, td.JSONPointer("/Num", 1))
201 checkOK(t, got, td.JSONPointer("/Slice/1", "baz"))
202 checkOK(t, got, td.JSONPointer("/Map/test/Num", 2))
203 checkOK(t, got, td.JSONPointer("/Map/test/Str", td.Contains("vel2")))
204 })
205
206 //
207 // Chained list
208 t.Run("Chained list", func(t *testing.T) {
209 type Item struct {
210 Val int `json:"val"`
211 Next *Item `json:"next"`
212 }
213 got := Item{Val: 1, Next: &Item{Val: 2, Next: &Item{Val: 3}}}
214 checkOK(t, got, td.JSONPointer("/next/next", Item{Val: 3}))
215 checkOK(t, got, td.JSONPointer("/next/next", &Item{Val: 3}))
216 checkOK(t, got,
217 td.JSONPointer("/next/next",
218 td.Struct(Item{}, td.StructFields{"Val": td.Gte(3)})))
219
220 checkOK(t, json.RawMessage(`{"foo":{"bar": {"zip": true}}}`),
221 td.JSONPointer("/foo/bar", td.JSON(`{"zip": true}`)))
222 })
223
224 //
225 // Lax cases
226 t.Run("Lax", func(t *testing.T) {
227 t.Run("json.Unmarshaler", func(t *testing.T) {
228 got := jsonPtrMap{"x": 123}
229 checkOK(t, got, td.JSONPointer("", jsonPtrMap{"x": float64(123)}))
230 checkOK(t, got, td.JSONPointer("", &jsonPtrMap{"x": float64(123)}))
231 checkOK(t, got, td.JSONPointer("", got))
232 checkOK(t, got, td.JSONPointer("", &got))
233 })
234
235 t.Run("struct", func(t *testing.T) {
236 type jpStruct struct {
237 Num any
238 }
239 got := jpStruct{Num: 123}
240 checkOK(t, got, td.JSONPointer("", jpStruct{Num: float64(123)}))
241 checkOK(t, jpStruct{Num: got}, td.JSONPointer("/Num", jpStruct{Num: float64(123)}))
242 checkOK(t, got, td.JSONPointer("", got))
243 checkOK(t, got, td.JSONPointer("", &got))
244
245 expected := int8(123)
246 checkOK(t, got, td.JSONPointer("/Num", expected))
247 checkOK(t, got, td.JSONPointer("/Num", &expected))
248 })
249 })
250
251 //
252 // Errors
253 t.Run("errors", func(t *testing.T) {
254 checkError(t, func() {}, td.JSONPointer("", td.NotNil()),
255 expectedError{
256 Message: mustBe("json.Marshal failed"),
257 Path: mustBe("DATA"),
258 Summary: mustContain("json: unsupported type"),
259 })
260
261 checkError(t,
262 map[string]int{"zzz": 42},
263 td.JSONPointer("/zzz", jsonPtrTest(56)),
264 expectedError{
265 Message: mustBe("an error occurred while unmarshaling JSON into td_test.jsonPtrTest"),
266 Path: mustBe("DATA.JSONPointer</zzz>"),
267 Summary: mustBe("jsonPtrTest unmarshal custom error"),
268 })
269 })
270
271 //
272 // Bad usage
273 checkError(t, "never tested",
274 td.JSONPointer("x", 1234),
275 expectedError{
276 Message: mustBe("bad usage of JSONPointer operator"),
277 Path: mustBe("DATA"),
278 Summary: mustBe(`bad JSON pointer "x"`),
279 })
280
281 //
282 // String
283 test.EqualStr(t, td.JSONPointer("/x", td.Gt(2)).String(),
284 "JSONPointer(/x, > 2)")
285 test.EqualStr(t, td.JSONPointer("/x", 2).String(),
286 "JSONPointer(/x, 2)")
287 test.EqualStr(t, td.JSONPointer("/x", nil).String(),
288 "JSONPointer(/x, nil)")
289
290 // Erroneous op
291 test.EqualStr(t, td.JSONPointer("x", 1234).String(), "JSONPointer(<ERROR>)")
292 }
293
294 func TestJSONPointerTypeBehind(t *testing.T) {
295 equalTypes(t, td.JSONPointer("", 42), nil)
296
297 // Erroneous op
298 equalTypes(t, td.JSONPointer("x", 1234), nil)
299 }