Debian Codegraph @

td/td_contains_key_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 "fmt"
11 "testing"
12
13 "github.com/maxatome/go-testdeep/td"
14 )
15
16 func TestContainsKey(t *testing.T) {
17 type MyMap map[int]string
18
19 for idx, got := range []any{
20 map[int]string{12: "foo", 34: "bar", 28: "zip"},
21 MyMap{12: "foo", 34: "bar", 28: "zip"},
22 } {
23 testName := fmt.Sprintf("#%d: got=%v", idx, got)
24
25 checkOK(t, got, td.ContainsKey(34), testName)
26 checkOK(t, got, td.ContainsKey(td.Between(30, 35)),
27 testName)
28
29 checkError(t, got, td.ContainsKey(35),
30 expectedError{
31 Message: mustBe("does not contain key"),
32 Path: mustBe("DATA"),
33 Summary: mustMatch(`expected key: 35
34 not in keys: \((12|28|34),
35 (12|28|34),
36 (12|28|34)\)`),
37 }, testName)
38
39 // Lax
40 checkOK(t, got, td.Lax(td.ContainsKey(float64(34))), testName)
41 }
42
43 checkError(t, map[int]bool{12: false}, td.ContainsKey(td.JSON("{")),
44 expectedError{
45 Message: mustBe("bad usage of JSON operator"),
46 Path: mustBe(`DATA`),
47 Summary: mustContain("JSON unmarshal error"),
48 Under: mustContain("under operator JSON at "),
49 },
50 "erroneous operator expected")
51 }
52
53 // nil case.
54 func TestContainsKeyNil(t *testing.T) {
55 type MyPtrMap map[*int]int
56
57 num := 12345642
58 for idx, got := range []any{
59 map[*int]int{&num: 42, nil: 666},
60 MyPtrMap{&num: 42, nil: 666},
61 } {
62 testName := fmt.Sprintf("#%d: got=%v", idx, got)
63
64 checkOK(t, got, td.ContainsKey(nil), testName)
65 checkOK(t, got, td.ContainsKey((*int)(nil)), testName)
66 checkOK(t, got, td.ContainsKey(td.Nil()), testName)
67 checkOK(t, got, td.ContainsKey(td.NotNil()), testName)
68
69 checkError(t, got, td.ContainsKey((*uint8)(nil)),
70 expectedError{
71 Message: mustBe("does not contain key"),
72 Path: mustBe("DATA"),
73 Summary: mustMatch(`expected key: \(\*uint8\)\(<nil>\)
74 not in keys: \(\(\*int\)\((<nil>|.*12345642.*)\),
75 \(\*int\)\((<nil>|.*12345642.*)\)\)`),
76 }, testName)
77 }
78
79 checkError(t,
80 map[string]int{"foo": 12, "bar": 34, "zip": 28}, // got
81 td.ContainsKey(nil),
82 expectedError{
83 Message: mustBe("does not contain key"),
84 Path: mustBe("DATA"),
85 Summary: mustMatch(`expected key: nil
86 not in keys: \("(foo|bar|zip)",
87 "(foo|bar|zip)",
88 "(foo|bar|zip)"\)`),
89 })
90
91 checkError(t, "foobar", td.ContainsKey(nil),
92 expectedError{
93 Message: mustBe("cannot check contains key"),
94 Path: mustBe("DATA"),
95 Got: mustBe("string"),
96 Expected: mustBe("ContainsKey(nil)"),
97 })
98
99 checkError(t, "foobar", td.ContainsKey(123),
100 expectedError{
101 Message: mustBe("cannot check contains key"),
102 Path: mustBe("DATA"),
103 Got: mustBe("string"),
104 Expected: mustBe("int"),
105 })
106
107 // Caught by deepValueEqual, before Match() call
108 checkError(t, nil, td.ContainsKey(nil),
109 expectedError{
110 Message: mustBe("values differ"),
111 Path: mustBe("DATA"),
112 Got: mustBe("nil"),
113 Expected: mustBe("ContainsKey(nil)"),
114 })
115 }
116
117 func TestContainsKeyTypeBehind(t *testing.T) {
118 equalTypes(t, td.ContainsKey("x"), nil)
119 }