Debian Codegraph @

td/flatten_test.go

raw
1 // Copyright (c) 2020-2025, 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 "reflect"
11 "strconv"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/internal/flat"
15 "github.com/maxatome/go-testdeep/internal/test"
16 "github.com/maxatome/go-testdeep/td"
17 )
18
19 func TestFlatten(t *testing.T) {
20 t.Run("ok", func(t *testing.T) {
21 testCases := []struct {
22 name string
23 sliceOrMapOrInt any
24 fn []any
25 expectedType reflect.Type
26 expectedLen int
27 }{
28 {
29 name: "slice",
30 sliceOrMapOrInt: []int{1, 2, 3},
31 expectedType: reflect.TypeOf([]int{}),
32 expectedLen: 3,
33 },
34 {
35 name: "array",
36 sliceOrMapOrInt: [3]int{1, 2, 3},
37 expectedType: reflect.TypeOf([3]int{}),
38 expectedLen: 3,
39 },
40 {
41 name: "map",
42 sliceOrMapOrInt: map[int]int{1: 2, 3: 4},
43 expectedType: reflect.TypeOf(map[int]int{}),
44 expectedLen: 4,
45 },
46 {
47 name: "slice+untyped nil fn",
48 sliceOrMapOrInt: []int{1, 2, 3},
49 fn: []any{nil},
50 expectedType: reflect.TypeOf([]int{}),
51 expectedLen: 3,
52 },
53 {
54 name: "int",
55 sliceOrMapOrInt: 5,
56 expectedType: reflect.TypeOf(42),
57 expectedLen: 5,
58 },
59 }
60 for _, tc := range testCases {
61 t.Run(tc.name, func(t *testing.T) {
62 s := td.Flatten(tc.sliceOrMapOrInt, tc.fn...)
63 if reflect.TypeOf(s.Slice) != tc.expectedType {
64 t.Errorf("types differ: got=%s, expected=%s",
65 reflect.TypeOf(s.Slice), tc.expectedType)
66 return
67 }
68 l, _ := flat.Len([]any{s})
69 test.EqualInt(t, l, tc.expectedLen)
70 })
71 }
72 })
73
74 cmp := func(t *testing.T, got, expected []any) {
75 t.Helper()
76
77 if (got == nil) != (expected == nil) {
78 t.Errorf("nil mismatch: got=%#v, expected=%#v", got, expected)
79 return
80 }
81
82 lg, le := len(got), len(expected)
83 l := lg
84 if l > le {
85 l = le
86 }
87 i := 0
88 for ; i < l; i++ {
89 if got[i] != expected[i] {
90 t.Errorf("#%d item differ, got=%v, expected=%v", i, got[i], expected[i])
91 }
92 }
93 for ; i < lg; i++ {
94 t.Errorf("#%d item is extra, got=%v", i, got[i])
95 }
96 for ; i < le; i++ {
97 t.Errorf("#%d item is missing, expected=%v", i, expected[i])
98 }
99 }
100
101 t.Run("ok+func", func(t *testing.T) {
102 testCases := []struct {
103 name string
104 fn any
105 expected []any
106 }{
107 {
108 name: "func never called",
109 fn: func(s bool) bool { return true },
110 expected: nil,
111 },
112 {
113 name: "double",
114 fn: func(a int) int { return a * 2 },
115 expected: []any{0, 2, 4, 6, 8, 10, 12, 14, 16, 18},
116 },
117 {
118 name: "even",
119 fn: func(a int) (int, bool) { return a, a%2 == 0 },
120 expected: []any{0, 2, 4, 6, 8},
121 },
122 {
123 name: "transform",
124 fn: func(a int) (string, bool) { return strconv.Itoa(a), a%2 == 0 },
125 expected: []any{"0", "2", "4", "6", "8"},
126 },
127 {
128 name: "nil",
129 fn: func(a int) any { return nil },
130 expected: []any{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
131 },
132 {
133 name: "convertible",
134 fn: func(a int8) int8 { return a * 3 },
135 expected: []any{
136 int8(0), int8(3), int8(6), int8(9), int8(12),
137 int8(15), int8(18), int8(21), int8(24), int8(27),
138 },
139 },
140 {
141 name: "any+variadic",
142 fn: func(a any, opts ...bool) int {
143 x, _ := a.(int)
144 return x
145 },
146 expected: []any{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
147 },
148 }
149 for _, tc := range testCases {
150 t.Run(tc.name, func(t *testing.T) {
151 s := td.Flatten([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, tc.fn)
152 if sa, ok := s.Slice.([]any); test.IsTrue(t, ok) {
153 cmp(t, sa, tc.expected)
154 }
155 })
156 }
157 })
158
159 t.Run("int", func(t *testing.T) {
160 testCases := []struct {
161 name string
162 input int
163 fn any
164 expected []any
165 }{
166 {
167 name: "classic",
168 input: 5,
169 fn: func(a int) int { return -a },
170 expected: []any{0, -1, -2, -3, -4},
171 },
172 {
173 name: "convertible to int64",
174 input: 5,
175 fn: func(a int64) int64 { return a * 1000 },
176 expected: []any{int64(0), int64(1000), int64(2000), int64(3000), int64(4000)},
177 },
178 {
179 name: "empty",
180 input: 0,
181 fn: func(a int) int { return a },
182 },
183 {
184 name: "empty coz neg",
185 input: -42,
186 fn: func(a int) int { return a },
187 },
188 }
189 for _, tc := range testCases {
190 t.Run(tc.name, func(t *testing.T) {
191 s := td.Flatten(tc.input, tc.fn)
192 if sa, ok := s.Slice.([]any); test.IsTrue(t, ok) {
193 cmp(t, sa, tc.expected)
194 }
195 })
196 }
197 })
198
199 t.Run("complex", func(t *testing.T) {
200 type person struct {
201 Name string `json:"name"`
202 Age int `json:"age"`
203 }
204
205 got := []person{{"alice", 22}, {"bob", 18}, {"brian", 34}, {"britt", 32}}
206
207 td.Cmp(t, got,
208 td.Bag(td.Flatten(
209 []string{"alice", "britt", "brian", "bob"},
210 func(name string) any { return td.Smuggle("Name", name) })))
211
212 td.Cmp(t, got,
213 td.Bag(td.Flatten(
214 []string{"alice", "britt", "brian", "bob"}, "Smuggle:Name")))
215
216 td.Cmp(t, got,
217 td.Bag(td.Flatten(
218 []string{"alice", "britt", "brian", "bob"},
219 func(name string) any { return td.JSONPointer("/name", name) })))
220
221 td.Cmp(t, got,
222 td.Bag(td.Flatten(
223 []string{"alice", "britt", "brian", "bob"}, "JSONPointer:/name")))
224
225 td.Cmp(t, got,
226 td.Bag(td.Flatten(
227 []string{"alice", "britt", "brian", "bob"},
228 func(name string) any { return td.SuperJSONOf(`{"name":$1}`, name) })))
229
230 td.Cmp(t, got,
231 td.Bag(td.Flatten(
232 []string{"alice", "britt", "brian", "bob"},
233 func(name string) any { return td.Struct(person{Name: name}) })))
234 })
235
236 t.Run("errors", func(t *testing.T) {
237 const (
238 usage = `usage: Flatten(SLICE|ARRAY|MAP|int[, FUNC])`
239 usageFunc = usage + `, FUNC should be non-nil func(T) V or func(T) (V, bool) or a string "Smuggle:…" or "JSONPointer:…"`
240 )
241 testCases := []struct {
242 name string
243 fn []any
244 sliceOrMapOrInt any
245 expected string
246 }{
247 {
248 name: "too many params",
249 sliceOrMapOrInt: []int{},
250 fn: []any{1, 2},
251 expected: usage + ", too many parameters",
252 },
253 {
254 name: "nil sliceOrMapOrInt",
255 expected: usage + ", but received nil as 1st parameter",
256 },
257 {
258 name: "bad sliceOrMapOrInt type",
259 sliceOrMapOrInt: "42",
260 expected: usage + ", but received string as 1st parameter",
261 },
262 {
263 name: "not func",
264 sliceOrMapOrInt: []int{},
265 fn: []any{42},
266 expected: usageFunc + ", but received int as 2nd parameter",
267 },
268 {
269 name: "func w/0 inputs",
270 sliceOrMapOrInt: []int{},
271 fn: []any{func() int { return 0 }},
272 expected: usageFunc + ", but received func() int as 2nd parameter",
273 },
274 {
275 name: "func w/2 inputs",
276 sliceOrMapOrInt: []int{},
277 fn: []any{func(a, b int) int { return 0 }},
278 expected: usageFunc + ", but received func(int, int) int as 2nd parameter",
279 },
280 {
281 name: "variadic func",
282 sliceOrMapOrInt: []int{},
283 fn: []any{func(a ...int) int { return 0 }},
284 expected: usageFunc + ", but received func(...int) int as 2nd parameter",
285 },
286 {
287 name: "func w/0 output",
288 sliceOrMapOrInt: []int{},
289 fn: []any{func(a int) {}},
290 expected: usageFunc + ", but received func(int) as 2nd parameter",
291 },
292 {
293 name: "func w/2 out without bool",
294 sliceOrMapOrInt: []int{},
295 fn: []any{func(a int) (int, int) { return 0, 0 }},
296 expected: usageFunc + ", but received func(int) (int, int) as 2nd parameter",
297 },
298 {
299 name: "bad shortcut",
300 sliceOrMapOrInt: []int{},
301 fn: []any{"Pipo"},
302 expected: usageFunc + `, but received "Pipo" as 2nd parameter`,
303 },
304 {
305 name: "typed nil func",
306 sliceOrMapOrInt: []int{},
307 fn: []any{(func(a int) int)(nil)},
308 expected: usageFunc,
309 },
310 }
311 for _, tc := range testCases {
312 t.Run(tc.name, func(t *testing.T) {
313 test.CheckPanic(t, func() { td.Flatten(tc.sliceOrMapOrInt, tc.fn...) }, tc.expected)
314 })
315 }
316 })
317 }