Debian Codegraph @

td/td_map_test.go

raw
1 // Copyright (c) 2018, 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 "testing"
11
12 "github.com/maxatome/go-testdeep/internal/test"
13 "github.com/maxatome/go-testdeep/td"
14 )
15
16 func TestMap(t *testing.T) {
17 type MyMap map[string]int
18
19 //
20 // Map
21 checkOK(t, (map[string]int)(nil), td.Map(map[string]int{}, nil))
22
23 checkError(t, nil, td.Map(map[string]int{}, nil),
24 expectedError{
25 Message: mustBe("values differ"),
26 Path: mustBe(`DATA`),
27 Got: mustBe("nil"),
28 Expected: mustBe("map[string]int{}"),
29 })
30
31 gotMap := map[string]int{"foo": 1, "bar": 2}
32
33 checkOK(t, gotMap, td.Map(map[string]int{"foo": 1, "bar": 2}))
34 checkOK(t, gotMap, td.Map(map[string]int{"foo": 1, "bar": 2}, nil))
35 checkOK(t, gotMap,
36 td.Map(map[string]int{"foo": 1}, td.MapEntries{"bar": 2}))
37 checkOK(t, gotMap,
38 td.Map(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2}))
39 checkOK(t, gotMap,
40 td.Map((map[string]int)(nil), td.MapEntries{"foo": 1, "bar": 2}))
41
42 checkOK(t, gotMap,
43 td.Map((map[string]int)(nil),
44 td.MapEntries{"foo": 66, "bar": 0},
45 td.MapEntries{"foo": 42, "bar": 1},
46 td.MapEntries{"foo": 1, "bar": 2},
47 ))
48
49 one := 1
50 checkOK(t, map[string]*int{"foo": nil, "bar": &one},
51 td.Map(map[string]*int{}, td.MapEntries{"foo": nil, "bar": &one}))
52
53 checkError(t, gotMap, td.Map(map[string]int{"foo": 1, "bar": 3}, nil),
54 expectedError{
55 Message: mustBe("values differ"),
56 Path: mustBe(`DATA["bar"]`),
57 Got: mustBe("2"),
58 Expected: mustBe("3"),
59 })
60
61 checkError(t, gotMap, td.Map(map[string]int{}, nil),
62 expectedError{
63 Message: mustBe("comparing hash keys of %%"),
64 Path: mustBe("DATA"),
65 Summary: mustMatch(`^Extra 2 keys: \("bar",\s+"foo"\)\z`),
66 })
67 checkError(t, gotMap, td.Map(map[string]int{"test": 2}, nil),
68 expectedError{
69 Message: mustBe("comparing hash keys of %%"),
70 Path: mustBe("DATA"),
71 Summary: mustMatch(
72 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),
73 })
74 checkError(t, gotMap,
75 td.Map(map[string]int{}, td.MapEntries{"test": 2}),
76 expectedError{
77 Message: mustBe("comparing hash keys of %%"),
78 Path: mustBe("DATA"),
79 Summary: mustMatch(
80 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),
81 })
82 checkError(t, gotMap,
83 td.Map(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2, "test": 2}),
84 expectedError{
85 Message: mustBe("comparing hash keys of %%"),
86 Path: mustBe("DATA"),
87 Summary: mustBe(`Missing key: ("test")`),
88 })
89 checkError(t, gotMap,
90 td.Map(MyMap{}, td.MapEntries{"foo": 1, "bar": 2}),
91 expectedError{
92 Message: mustBe("type mismatch"),
93 Path: mustBe("DATA"),
94 Got: mustBe("map[string]int"),
95 Expected: mustBe("td_test.MyMap"),
96 })
97
98 //
99 // Map type
100 gotTypedMap := MyMap{"foo": 1, "bar": 2}
101
102 checkOK(t, gotTypedMap, td.Map(MyMap{"foo": 1, "bar": 2}, nil))
103 checkOK(t, gotTypedMap,
104 td.Map(MyMap{"foo": 1}, td.MapEntries{"bar": 2}))
105 checkOK(t, gotTypedMap,
106 td.Map(MyMap{}, td.MapEntries{"foo": 1, "bar": 2}))
107 checkOK(t, gotTypedMap,
108 td.Map((MyMap)(nil), td.MapEntries{"foo": 1, "bar": 2}))
109
110 checkOK(t, &gotTypedMap, td.Map(&MyMap{"foo": 1, "bar": 2}, nil))
111 checkOK(t, &gotTypedMap,
112 td.Map(&MyMap{"foo": 1}, td.MapEntries{"bar": 2}))
113 checkOK(t, &gotTypedMap,
114 td.Map(&MyMap{}, td.MapEntries{"foo": 1, "bar": 2}))
115 checkOK(t, &gotTypedMap,
116 td.Map((*MyMap)(nil), td.MapEntries{"foo": 1, "bar": 2}))
117
118 checkError(t, gotTypedMap, td.Map(MyMap{"foo": 1, "bar": 3}, nil),
119 expectedError{
120 Message: mustBe("values differ"),
121 Path: mustBe(`DATA["bar"]`),
122 Got: mustBe("2"),
123 Expected: mustBe("3"),
124 })
125
126 checkError(t, gotTypedMap, td.Map(MyMap{}, nil),
127 expectedError{
128 Message: mustBe("comparing hash keys of %%"),
129 Path: mustBe("DATA"),
130 Summary: mustMatch(`^Extra 2 keys: \("bar",\s+"foo"\)\z`),
131 })
132 checkError(t, gotTypedMap, td.Map(MyMap{"test": 2}, nil),
133 expectedError{
134 Message: mustBe("comparing hash keys of %%"),
135 Path: mustBe("DATA"),
136 Summary: mustMatch(
137 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),
138 })
139 checkError(t, gotTypedMap, td.Map(MyMap{}, td.MapEntries{"test": 2}),
140 expectedError{
141 Message: mustBe("comparing hash keys of %%"),
142 Path: mustBe("DATA"),
143 Summary: mustMatch(
144 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),
145 })
146 checkError(t, gotTypedMap,
147 td.Map(MyMap{}, td.MapEntries{"foo": 1, "bar": 2, "test": 2}),
148 expectedError{
149 Message: mustBe("comparing hash keys of %%"),
150 Path: mustBe("DATA"),
151 Summary: mustBe(`Missing key: ("test")`),
152 })
153
154 checkError(t, &gotTypedMap, td.Map(&MyMap{"foo": 1, "bar": 3}, nil),
155 expectedError{
156 Message: mustBe("values differ"),
157 Path: mustBe(`DATA["bar"]`),
158 Got: mustBe("2"),
159 Expected: mustBe("3"),
160 })
161
162 checkError(t, &gotTypedMap, td.Map(&MyMap{}, nil),
163 expectedError{
164 Message: mustBe("comparing hash keys of %%"),
165 Path: mustBe("DATA"),
166 Summary: mustMatch(`^Extra 2 keys: \("bar",\s+"foo"\)\z`),
167 })
168 checkError(t, &gotTypedMap, td.Map(&MyMap{"test": 2}, nil),
169 expectedError{
170 Message: mustBe("comparing hash keys of %%"),
171 Path: mustBe("DATA"),
172 Summary: mustMatch(
173 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),
174 })
175 checkError(t, &gotTypedMap, td.Map(&MyMap{}, td.MapEntries{"test": 2}),
176 expectedError{
177 Message: mustBe("comparing hash keys of %%"),
178 Path: mustBe("DATA"),
179 Summary: mustMatch(
180 `^ Missing key: \("test"\)\nExtra 2 keys: \("bar",\s+"foo"\)\z`),
181 })
182 checkError(t, &gotTypedMap,
183 td.Map(&MyMap{}, td.MapEntries{"foo": 1, "bar": 2, "test": 2}),
184 expectedError{
185 Message: mustBe("comparing hash keys of %%"),
186 Path: mustBe("DATA"),
187 Summary: mustBe(`Missing key: ("test")`),
188 })
189
190 checkError(t, &gotMap, td.Map(&MyMap{}, nil),
191 expectedError{
192 Message: mustBe("type mismatch"),
193 Path: mustBe("DATA"),
194 Got: mustBe("*map[string]int"),
195 Expected: mustBe("*td_test.MyMap"),
196 })
197 checkError(t, gotMap, td.Map(&MyMap{}, nil),
198 expectedError{
199 Message: mustBe("type mismatch"),
200 Path: mustBe("DATA"),
201 Got: mustBe("map[string]int"),
202 Expected: mustBe("*td_test.MyMap"),
203 })
204 checkError(t, nil, td.Map(&MyMap{}, nil),
205 expectedError{
206 Message: mustBe("values differ"),
207 Path: mustBe("DATA"),
208 Got: mustBe("nil"),
209 Expected: mustBe("*td_test.MyMap{}"),
210 })
211 checkError(t, nil, td.Map(MyMap{}, nil),
212 expectedError{
213 Message: mustBe("values differ"),
214 Path: mustBe("DATA"),
215 Got: mustBe("nil"),
216 Expected: mustBe("td_test.MyMap{}"),
217 })
218
219 //
220 // nil cases
221 var (
222 gotNilMap map[string]int
223 gotNilTypedMap MyMap
224 )
225
226 checkOK(t, gotNilMap, td.Map(map[string]int{}, nil))
227 checkOK(t, gotNilTypedMap, td.Map(MyMap{}, nil))
228 checkOK(t, &gotNilTypedMap, td.Map(&MyMap{}, nil))
229
230 // Be lax...
231 // Without Lax → error
232 checkError(t, MyMap{}, td.Map(map[string]int{}, nil),
233 expectedError{
234 Message: mustBe("type mismatch"),
235 })
236 checkError(t, map[string]int{}, td.Map(MyMap{}, nil),
237 expectedError{
238 Message: mustBe("type mismatch"),
239 })
240 // With Lax → OK
241 checkOK(t, MyMap{}, td.Lax(td.Map(map[string]int{}, nil)))
242 checkOK(t, map[string]int{}, td.Lax(td.Map(MyMap{}, nil)))
243
244 //
245 // SuperMapOf
246 checkOK(t, gotMap, td.SuperMapOf(map[string]int{"foo": 1}))
247 checkOK(t, gotMap, td.SuperMapOf(map[string]int{"foo": 1}, nil))
248 checkOK(t, gotMap,
249 td.SuperMapOf(map[string]int{"foo": 1}, td.MapEntries{"bar": 2}))
250 checkOK(t, gotMap,
251 td.SuperMapOf(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2}))
252
253 checkOK(t, gotMap,
254 td.SuperMapOf(map[string]int{"foo": 1},
255 td.MapEntries{"bar": 0},
256 td.MapEntries{"bar": 1},
257 td.MapEntries{"bar": 2},
258 ))
259
260 checkError(t, gotMap,
261 td.SuperMapOf(map[string]int{"foo": 1, "bar": 3}),
262 expectedError{
263 Message: mustBe("values differ"),
264 Path: mustBe(`DATA["bar"]`),
265 Got: mustBe("2"),
266 Expected: mustBe("3"),
267 })
268
269 checkError(t, gotMap,
270 td.SuperMapOf(map[string]int{"foo": 1, "bar": 3}, nil),
271 expectedError{
272 Message: mustBe("values differ"),
273 Path: mustBe(`DATA["bar"]`),
274 Got: mustBe("2"),
275 Expected: mustBe("3"),
276 })
277
278 checkError(t, gotMap, td.SuperMapOf(map[string]int{"test": 2}),
279 expectedError{
280 Message: mustBe("comparing hash keys of %%"),
281 Path: mustBe("DATA"),
282 Summary: mustBe(`Missing key: ("test")`),
283 })
284 checkError(t, gotMap,
285 td.SuperMapOf(map[string]int{}, td.MapEntries{"test": 2}),
286 expectedError{
287 Message: mustBe("comparing hash keys of %%"),
288 Path: mustBe("DATA"),
289 Summary: mustBe(`Missing key: ("test")`),
290 })
291
292 checkOK(t, gotNilMap, td.SuperMapOf(map[string]int{}, nil))
293 checkOK(t, gotNilTypedMap, td.SuperMapOf(MyMap{}, nil))
294 checkOK(t, &gotNilTypedMap, td.SuperMapOf(&MyMap{}, nil))
295
296 //
297 // SubMapOf
298 checkOK(t, gotMap,
299 td.SubMapOf(map[string]int{"foo": 1, "bar": 2, "tst": 3}))
300 checkOK(t, gotMap,
301 td.SubMapOf(map[string]int{"foo": 1, "bar": 2, "tst": 3}, nil))
302 checkOK(t, gotMap,
303 td.SubMapOf(map[string]int{"foo": 1, "tst": 3}, td.MapEntries{"bar": 2}))
304 checkOK(t, gotMap,
305 td.SubMapOf(map[string]int{}, td.MapEntries{"foo": 1, "bar": 2, "tst": 3}))
306
307 checkOK(t, gotMap,
308 td.SubMapOf(map[string]int{"foo": 1, "tst": 3},
309 td.MapEntries{"bar": 0},
310 td.MapEntries{"bar": 1},
311 td.MapEntries{"bar": 2},
312 ))
313
314 checkError(t, gotMap,
315 td.SubMapOf(map[string]int{"foo": 1, "bar": 3}),
316 expectedError{
317 Message: mustBe("values differ"),
318 Path: mustBe(`DATA["bar"]`),
319 Got: mustBe("2"),
320 Expected: mustBe("3"),
321 })
322
323 checkError(t, gotMap,
324 td.SubMapOf(map[string]int{"foo": 1, "bar": 3}, nil),
325 expectedError{
326 Message: mustBe("values differ"),
327 Path: mustBe(`DATA["bar"]`),
328 Got: mustBe("2"),
329 Expected: mustBe("3"),
330 })
331
332 checkError(t, gotMap, td.SubMapOf(map[string]int{"foo": 1}),
333 expectedError{
334 Message: mustBe("comparing hash keys of %%"),
335 Path: mustBe("DATA"),
336 Summary: mustBe(`Extra key: ("bar")`),
337 })
338 checkError(t, gotMap,
339 td.SubMapOf(map[string]int{}, td.MapEntries{"foo": 1, "test": 2}),
340 expectedError{
341 Message: mustBe("comparing hash keys of %%"),
342 Path: mustBe("DATA"),
343 Summary: mustBe(`Missing key: ("test")
344 Extra key: ("bar")`),
345 })
346
347 checkOK(t, gotNilMap, td.SubMapOf(map[string]int{"foo": 1}, nil))
348 checkOK(t, gotNilTypedMap, td.SubMapOf(MyMap{"foo": 1}, nil))
349 checkOK(t, &gotNilTypedMap, td.SubMapOf(&MyMap{"foo": 1}, nil))
350
351 //
352 // Bad usage
353 checkError(t, "never tested",
354 td.Map("test", nil),
355 expectedError{
356 Message: mustBe("bad usage of Map operator"),
357 Path: mustBe("DATA"),
358 Summary: mustContain("usage: Map("),
359 })
360 checkError(t, "never tested",
361 td.SuperMapOf("test", nil),
362 expectedError{
363 Message: mustBe("bad usage of SuperMapOf operator"),
364 Path: mustBe("DATA"),
365 Summary: mustContain("usage: SuperMapOf("),
366 })
367 checkError(t, "never tested",
368 td.SubMapOf("test", nil),
369 expectedError{
370 Message: mustBe("bad usage of SubMapOf operator"),
371 Path: mustBe("DATA"),
372 Summary: mustContain("usage: SubMapOf("),
373 })
374
375 num := 12
376 checkError(t, "never tested",
377 td.Map(&num, nil),
378 expectedError{
379 Message: mustBe("bad usage of Map operator"),
380 Path: mustBe("DATA"),
381 Summary: mustContain("usage: Map("),
382 })
383 checkError(t, "never tested",
384 td.SuperMapOf(&num, nil),
385 expectedError{
386 Message: mustBe("bad usage of SuperMapOf operator"),
387 Path: mustBe("DATA"),
388 Summary: mustContain("usage: SuperMapOf("),
389 })
390 checkError(t, "never tested",
391 td.SubMapOf(&num, nil),
392 expectedError{
393 Message: mustBe("bad usage of SubMapOf operator"),
394 Path: mustBe("DATA"),
395 Summary: mustContain("usage: SubMapOf("),
396 })
397
398 checkError(t, "never tested",
399 td.Map(&MyMap{}, td.MapEntries{1: 2}),
400 expectedError{
401 Message: mustBe("bad usage of Map operator"),
402 Path: mustBe("DATA"),
403 Summary: mustBe("expected key 1 type mismatch: int != model key type (string)"),
404 })
405 checkError(t, "never tested",
406 td.SuperMapOf(&MyMap{}, td.MapEntries{1: 2}),
407 expectedError{
408 Message: mustBe("bad usage of SuperMapOf operator"),
409 Path: mustBe("DATA"),
410 Summary: mustBe("expected key 1 type mismatch: int != model key type (string)"),
411 })
412 checkError(t, "never tested",
413 td.SubMapOf(&MyMap{}, td.MapEntries{1: 2}),
414 expectedError{
415 Message: mustBe("bad usage of SubMapOf operator"),
416 Path: mustBe("DATA"),
417 Summary: mustBe("expected key 1 type mismatch: int != model key type (string)"),
418 })
419
420 checkError(t, &MyMap{"foo": 42},
421 td.Map(&MyMap{}, td.MapEntries{"foo": nil}),
422 expectedError{
423 Message: mustBe("values differ"),
424 Path: mustBe(`DATA["foo"]`),
425 Got: mustBe("42"),
426 Expected: mustBe("nil"),
427 })
428
429 checkError(t, &MyMap{"foo": 12},
430 td.Map(&MyMap{}, td.MapEntries{"foo": uint16(2)}),
431 expectedError{
432 Message: mustBe("type mismatch"),
433 Path: mustBe(`DATA["foo"]`),
434 Got: mustBe("int"),
435 Expected: mustBe("uint16"),
436 })
437
438 checkError(t, "never tested",
439 td.Map(&MyMap{"foo": 1}, td.MapEntries{"foo": 1}),
440 expectedError{
441 Message: mustBe("bad usage of Map operator"),
442 Path: mustBe("DATA"),
443 Summary: mustBe(`"foo" entry exists in both model & expectedEntries`),
444 })
445
446 //
447 // String
448 test.EqualStr(t, td.Map(MyMap{}, nil).String(),
449 "td_test.MyMap{}")
450 test.EqualStr(t, td.Map(&MyMap{}, nil).String(),
451 "*td_test.MyMap{}")
452 test.EqualStr(t, td.Map(&MyMap{"foo": 2}, nil).String(),
453 `*td_test.MyMap{
454 "foo": 2,
455 }`)
456
457 test.EqualStr(t, td.SubMapOf(MyMap{}, nil).String(),
458 "SubMapOf(td_test.MyMap{})")
459 test.EqualStr(t, td.SubMapOf(&MyMap{}, nil).String(),
460 "SubMapOf(*td_test.MyMap{})")
461 test.EqualStr(t, td.SubMapOf(&MyMap{"foo": 2}, nil).String(),
462 `SubMapOf(*td_test.MyMap{
463 "foo": 2,
464 })`)
465
466 test.EqualStr(t, td.SuperMapOf(MyMap{}, nil).String(),
467 "SuperMapOf(td_test.MyMap{})")
468 test.EqualStr(t, td.SuperMapOf(&MyMap{}, nil).String(),
469 "SuperMapOf(*td_test.MyMap{})")
470 test.EqualStr(t, td.SuperMapOf(&MyMap{"foo": 2}, nil).String(),
471 `SuperMapOf(*td_test.MyMap{
472 "foo": 2,
473 })`)
474
475 // Erroneous op
476 test.EqualStr(t, td.Map(12, nil).String(), "Map(<ERROR>)")
477 test.EqualStr(t, td.SubMapOf(12, nil).String(), "SubMapOf(<ERROR>)")
478 test.EqualStr(t, td.SuperMapOf(12, nil).String(), "SuperMapOf(<ERROR>)")
479 }
480
481 func TestMapTypeBehind(t *testing.T) {
482 type MyMap map[string]int
483
484 // Map
485 equalTypes(t, td.Map(map[string]int{}, nil), map[string]int{})
486 equalTypes(t, td.Map(MyMap{}, nil), MyMap{})
487 equalTypes(t, td.Map(&MyMap{}, nil), &MyMap{})
488
489 // SubMap
490 equalTypes(t, td.SubMapOf(map[string]int{}, nil), map[string]int{})
491 equalTypes(t, td.SubMapOf(MyMap{}, nil), MyMap{})
492 equalTypes(t, td.SubMapOf(&MyMap{}, nil), &MyMap{})
493
494 // SuperMap
495 equalTypes(t, td.SuperMapOf(map[string]int{}, nil), map[string]int{})
496 equalTypes(t, td.SuperMapOf(MyMap{}, nil), MyMap{})
497 equalTypes(t, td.SuperMapOf(&MyMap{}, nil), &MyMap{})
498
499 // Erroneous op
500 equalTypes(t, td.Map(12, nil), nil)
501 equalTypes(t, td.SubMapOf(12, nil), nil)
502 equalTypes(t, td.SuperMapOf(12, nil), nil)
503 }