Debian Codegraph @

td/td_bag_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/internal/test"
14 "github.com/maxatome/go-testdeep/td"
15 )
16
17 func TestBag(t *testing.T) {
18 type MyArray [5]int
19 type MySlice []int
20
21 for idx, got := range []any{
22 []int{1, 3, 4, 4, 5},
23 [...]int{1, 3, 4, 4, 5},
24 MySlice{1, 3, 4, 4, 5},
25 MyArray{1, 3, 4, 4, 5},
26 &MySlice{1, 3, 4, 4, 5},
27 &MyArray{1, 3, 4, 4, 5},
28 } {
29 testName := fmt.Sprintf("Test #%d → %v", idx, got)
30
31 //
32 // Bag
33 checkOK(t, got, td.Bag(5, 4, 1, 4, 3), testName)
34
35 checkError(t, got, td.Bag(5, 4, 1, 3),
36 expectedError{
37 Message: mustBe("comparing %% as a Bag"),
38 Path: mustBe("DATA"),
39 Summary: mustBe("Extra item: (4)"),
40 },
41 testName)
42
43 checkError(t, got, td.Bag(5, 4, 1, 4, 3, 66, 42),
44 expectedError{
45 Message: mustBe("comparing %% as a Bag"),
46 Path: mustBe("DATA"),
47 // items are sorted
48 Summary: mustBe(`Missing 2 items: (42,
49 66)`),
50 },
51 testName)
52
53 checkError(t, got, td.Bag(5, 66, 4, 1, 4, 3),
54 expectedError{
55 Message: mustBe("comparing %% as a Bag"),
56 Path: mustBe("DATA"),
57 Summary: mustBe("Missing item: (66)"),
58 },
59 testName)
60
61 checkError(t, got, td.Bag(5, 66, 4, 1, 4, 3, 66),
62 expectedError{
63 Message: mustBe("comparing %% as a Bag"),
64 Path: mustBe("DATA"),
65 Summary: mustBe("Missing 2 items: (66,\n 66)"),
66 },
67 testName)
68
69 checkError(t, got, td.Bag(5, 66, 4, 1, 3),
70 expectedError{
71 Message: mustBe("comparing %% as a Bag"),
72 Path: mustBe("DATA"),
73 Summary: mustBe("Missing item: (66)\n Extra item: (4)"),
74 },
75 testName)
76
77 checkError(t, got, td.Bag(1, td.JSON("{")),
78 expectedError{
79 Message: mustBe("bad usage of JSON operator"),
80 Path: mustBe(`DATA`),
81 Summary: mustContain("JSON unmarshal error"),
82 Under: mustContain("under operator JSON at "),
83 },
84 testName+": erroneous operator expected")
85
86 // Lax
87 checkOK(t, got, td.Lax(td.Bag(float64(5), 4, 1, 4, 3)), testName)
88
89 //
90 // SubBagOf
91 checkOK(t, got, td.SubBagOf(5, 4, 1, 4, 3), testName)
92 checkOK(t, got, td.SubBagOf(5, 66, 4, 1, 4, 3), testName)
93
94 checkError(t, got, td.SubBagOf(5, 66, 4, 1, 3),
95 expectedError{
96 Message: mustBe("comparing %% as a SubBagOf"),
97 Path: mustBe("DATA"),
98 Summary: mustBe("Extra item: (4)"),
99 },
100 testName)
101
102 checkError(t, got, td.SubBagOf(1, td.JSON("{")),
103 expectedError{
104 Message: mustBe("bad usage of JSON operator"),
105 Path: mustBe(`DATA`),
106 Summary: mustContain("JSON unmarshal error"),
107 Under: mustContain("under operator JSON at "),
108 },
109 testName+": erroneous operator expected")
110
111 // Lax
112 checkOK(t, got, td.Lax(td.SubBagOf(float64(5), 4, 1, 4, 3)), testName)
113
114 //
115 // SuperBagOf
116 checkOK(t, got, td.SuperBagOf(5, 4, 1, 4, 3), testName)
117 checkOK(t, got, td.SuperBagOf(5, 4, 3), testName)
118
119 checkError(t, got, td.SuperBagOf(5, 66, 4, 1, 3),
120 expectedError{
121 Message: mustBe("comparing %% as a SuperBagOf"),
122 Path: mustBe("DATA"),
123 Summary: mustBe("Missing item: (66)"),
124 },
125 testName)
126
127 checkError(t, got, td.SuperBagOf(1, td.JSON("{")),
128 expectedError{
129 Message: mustBe("bad usage of JSON operator"),
130 Path: mustBe(`DATA`),
131 Summary: mustContain("JSON unmarshal error"),
132 Under: mustContain("under operator JSON at "),
133 },
134 testName+": erroneous operator expected")
135
136 // Lax
137 checkOK(t, got, td.Lax(td.SuperBagOf(float64(5), 4, 1, 4, 3)), testName)
138 }
139
140 checkOK(t, []any{123, "foo", nil, "bar", nil},
141 td.Bag("foo", "bar", 123, nil, nil))
142
143 var nilSlice MySlice
144 for idx, got := range []any{([]int)(nil), &nilSlice} {
145 testName := fmt.Sprintf("Test #%d", idx)
146
147 checkOK(t, got, td.Bag(), testName)
148 checkOK(t, got, td.SubBagOf(), testName)
149 checkOK(t, got, td.SubBagOf(1, 2), testName)
150 checkOK(t, got, td.SuperBagOf(), testName)
151 }
152
153 for idx, bag := range []td.TestDeep{
154 td.Bag(123),
155 td.SubBagOf(123),
156 td.SuperBagOf(123),
157 } {
158 testName := fmt.Sprintf("Test #%d → %s", idx, bag)
159
160 checkError(t, 123, bag,
161 expectedError{
162 Message: mustBe("bad kind"),
163 Path: mustBe("DATA"),
164 Got: mustBe("int"),
165 Expected: mustBe("slice OR array OR *slice OR *array"),
166 },
167 testName)
168
169 num := 123
170 checkError(t, &num, bag,
171 expectedError{
172 Message: mustBe("bad kind"),
173 Path: mustBe("DATA"),
174 Got: mustBe("*int"),
175 Expected: mustBe("slice OR array OR *slice OR *array"),
176 },
177 testName)
178
179 var list *MySlice
180 checkError(t, list, bag,
181 expectedError{
182 Message: mustBe("nil pointer"),
183 Path: mustBe("DATA"),
184 Got: mustBe("nil *slice (*td_test.MySlice type)"),
185 Expected: mustBe("non-nil *slice OR *array"),
186 },
187 testName)
188
189 checkError(t, nil, bag,
190 expectedError{
191 Message: mustBe("bad kind"),
192 Path: mustBe("DATA"),
193 Got: mustBe("nil"),
194 Expected: mustBe("slice OR array OR *slice OR *array"),
195 },
196 testName)
197 }
198
199 //
200 // String
201 test.EqualStr(t, td.Bag(1).String(), "Bag(1)")
202 test.EqualStr(t, td.Bag(1, 2).String(), "Bag(1,\n 2)")
203
204 test.EqualStr(t, td.SubBagOf(1).String(), "SubBagOf(1)")
205 test.EqualStr(t, td.SubBagOf(1, 2).String(), "SubBagOf(1,\n 2)")
206
207 test.EqualStr(t, td.SuperBagOf(1).String(), "SuperBagOf(1)")
208 test.EqualStr(t, td.SuperBagOf(1, 2).String(),
209 "SuperBagOf(1,\n 2)")
210 }
211
212 func TestBagTypeBehind(t *testing.T) {
213 equalTypes(t, td.Bag(6, 5), ([]int)(nil))
214 equalTypes(t, td.Bag(6, "foo"), nil)
215
216 equalTypes(t, td.SubBagOf(6, 5), ([]int)(nil))
217 equalTypes(t, td.SubBagOf(6, "foo"), nil)
218
219 equalTypes(t, td.SuperBagOf(6, 5), ([]int)(nil))
220 equalTypes(t, td.SuperBagOf(6, "foo"), nil)
221
222 // Always the same non-interface type (even if we encounter several
223 // interface types)
224 equalTypes(t,
225 td.Bag(
226 td.Empty(),
227 5,
228 td.Isa((*error)(nil)), // interface type (in fact pointer to ...)
229 td.All(6, 7),
230 td.Isa((*fmt.Stringer)(nil)), // interface type
231 8),
232 ([]int)(nil))
233
234 // Only one interface type
235 equalTypes(t,
236 td.Bag(
237 td.Isa((*error)(nil)),
238 td.Isa((*error)(nil)),
239 td.Isa((*error)(nil)),
240 ),
241 ([]*error)(nil))
242
243 // Several interface types, cannot be sure
244 equalTypes(t,
245 td.Bag(
246 td.Isa((*error)(nil)),
247 td.Isa((*fmt.Stringer)(nil)),
248 ),
249 nil)
250 }