Debian Codegraph @

td/t_hooks_test.go

raw
1 // Copyright (c) 2020, 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 "errors"
11 "fmt"
12 "reflect"
13 "strconv"
14 "strings"
15 "testing"
16 "time"
17
18 "github.com/maxatome/go-testdeep/internal/test"
19 "github.com/maxatome/go-testdeep/td"
20 )
21
22 func TestWithCmpHooks(tt *testing.T) {
23 na, nb := 1234, 1234
24 date, _ := time.Parse(time.RFC3339, "2020-09-08T22:13:54+02:00")
25
26 for _, tst := range []struct {
27 name string
28 cmp any
29 got, expected any
30 }{
31 {
32 name: "reflect.Value",
33 cmp: func(got, expected reflect.Value) bool {
34 return td.EqDeeply(got.Interface(), expected.Interface())
35 },
36 got: reflect.ValueOf(&na),
37 expected: reflect.ValueOf(&nb),
38 },
39 {
40 name: "time.Time",
41 cmp: (time.Time).Equal,
42 got: date,
43 expected: date.UTC(),
44 },
45 {
46 name: "numify",
47 cmp: func(got, expected string) error {
48 ngot, err := strconv.Atoi(got)
49 if err != nil {
50 return fmt.Errorf("strconv.Atoi(got) failed: %s", err)
51 }
52 nexpected, err := strconv.Atoi(expected)
53 if err != nil {
54 return fmt.Errorf("strconv.Atoi(expected) failed: %s", err)
55 }
56 if ngot != nexpected {
57 return errors.New("values differ")
58 }
59 return nil
60 },
61 got: "0000001234",
62 expected: "1234",
63 },
64 {
65 name: "false test :)",
66 cmp: func(got, expected int) bool {
67 return got == -expected
68 },
69 got: 1,
70 expected: -1,
71 },
72 } {
73 tt.Run(tst.name, func(tt *testing.T) {
74 ttt := test.NewTestingTB(tt.Name())
75
76 t := td.NewT(ttt)
77
78 td.CmpFalse(tt, func() bool {
79 // A panic can occur when -tags safe:
80 // dark.GetInterface() does not handle private unsafe.Pointer kind
81 defer func() { recover() }() //nolint: errcheck
82 return t.Cmp(tst.got, tst.expected)
83 }())
84
85 t = t.WithCmpHooks(tst.cmp)
86
87 td.CmpTrue(tt, t.Cmp(tst.got, tst.expected))
88 })
89 }
90
91 tt.Run("Error", func(tt *testing.T) {
92 ttt := test.NewTestingTB(tt.Name())
93
94 t := td.NewT(ttt).
95 WithCmpHooks(func(got, expected int) error {
96 return errors.New("never equal")
97 })
98
99 td.CmpFalse(tt, t.Cmp(1, 1))
100
101 if !strings.Contains(ttt.LastMessage(), "DATA: never equal\n") {
102 tt.Errorf(`<%s> does not contain "DATA: never equal\n"`, ttt.LastMessage())
103 }
104 })
105
106 for _, tst := range []struct {
107 name string
108 cmp any
109 fatal string
110 }{
111 {
112 name: "not a function",
113 cmp: "Booh",
114 fatal: "WithCmpHooks expects a function, not a string",
115 },
116 {
117 name: "wrong signature",
118 cmp: func(a []int, b ...int) bool { return false },
119 fatal: "WithCmpHooks expects: func (T, T) bool|error not ",
120 },
121 } {
122 tt.Run("panic: "+tst.name, func(tt *testing.T) {
123 ttt := test.NewTestingTB(tt.Name())
124
125 t := td.NewT(ttt)
126
127 fatalMesg := ttt.CatchFatal(func() { t.WithCmpHooks(tst.cmp) })
128 test.IsTrue(tt, ttt.IsFatal)
129 if !strings.Contains(fatalMesg, tst.fatal) {
130 tt.Errorf(`<%s> does not contain %q`, fatalMesg, tst.fatal)
131 }
132 })
133 }
134 }
135
136 func TestWithSmuggleHooks(tt *testing.T) {
137 for _, tst := range []struct {
138 name string
139 cmp any
140 got, expected any
141 }{
142 {
143 name: "abs",
144 cmp: func(got int) int {
145 if got < 0 {
146 return -got
147 }
148 return got
149 },
150 got: -1234,
151 expected: 1234,
152 },
153 {
154 name: "int2bool",
155 cmp: func(got int) bool { return got != 0 },
156 got: 1,
157 expected: true,
158 },
159 {
160 name: "Atoi",
161 cmp: strconv.Atoi,
162 got: "1234",
163 expected: 1234,
164 },
165 {
166 name: "reflect2any",
167 cmp: func(v reflect.Value) any {
168 return v.Interface()
169 },
170 got: reflect.ValueOf(123),
171 expected: 123,
172 },
173 } {
174 tt.Run(tst.name, func(tt *testing.T) {
175 ttt := test.NewTestingTB(tt.Name())
176
177 t := td.NewT(ttt)
178
179 td.CmpFalse(tt, t.Cmp(tst.got, tst.expected))
180
181 t = t.WithSmuggleHooks(tst.cmp)
182
183 td.CmpTrue(tt, t.Cmp(tst.got, tst.expected))
184 })
185 }
186
187 tt.Run("Array", func(tt *testing.T) {
188 ttt := test.NewTestingTB(tt.Name())
189
190 t := td.NewT(ttt).WithSmuggleHooks(func(got reflect.Value) any {
191 if got.IsValid() {
192 return got.Interface()
193 }
194 return nil
195 })
196
197 got := [3]reflect.Value{1: reflect.ValueOf(42)}
198
199 td.CmpTrue(tt, td.Cmp(t, got, td.Array([3]reflect.Value{}, td.ArrayEntries{
200 0: nil, // if omitted, td.Array sets it to reflect.Value{}
201 1: 42,
202 2: nil, // if omitted, td.Array sets it to reflect.Value{}
203 })))
204 test.EqualStr(tt, ttt.LastMessage(), "")
205 ttt.ResetMessages()
206
207 td.CmpTrue(tt, td.Cmp(t, got[:], td.Slice([]reflect.Value{}, td.ArrayEntries{
208 0: nil, // if omitted, td.Array sets it to reflect.Value{}
209 1: 42,
210 2: nil, // if omitted, td.Array sets it to reflect.Value{}
211 })))
212 test.EqualStr(tt, ttt.LastMessage(), "")
213 ttt.ResetMessages()
214
215 td.CmpTrue(tt, td.Cmp(t, got[:], td.SuperSliceOf([]reflect.Value{}, td.ArrayEntries{
216 1: 42,
217 })))
218 test.EqualStr(tt, ttt.LastMessage(), "")
219 ttt.ResetMessages()
220 })
221
222 tt.Run("Map", func(tt *testing.T) {
223 ttt := test.NewTestingTB(tt.Name())
224
225 t := td.NewT(ttt).WithSmuggleHooks(func(got reflect.Value) any {
226 if got.IsValid() {
227 return got.Interface()
228 }
229 return nil
230 })
231
232 got := map[string]reflect.Value{
233 "pipo": reflect.ValueOf(42),
234 "bingo": reflect.ValueOf(666),
235 "zip": {},
236 }
237
238 td.CmpTrue(tt, td.Cmp(t, got, td.Map(map[string]reflect.Value{}, td.MapEntries{
239 "pipo": 42,
240 "bingo": 666,
241 "zip": nil,
242 })))
243 test.EqualStr(tt, ttt.LastMessage(), "")
244 ttt.ResetMessages()
245
246 td.CmpTrue(tt, td.Cmp(t, got, td.SubMapOf(map[string]reflect.Value{}, td.MapEntries{
247 "pipo": 42,
248 "bingo": 666,
249 "zip": nil,
250 "test": "more",
251 })))
252 test.EqualStr(tt, ttt.LastMessage(), "")
253 ttt.ResetMessages()
254
255 td.CmpTrue(tt, td.Cmp(t, got, td.SuperMapOf(map[string]reflect.Value{}, td.MapEntries{
256 "pipo": 42,
257 "zip": nil,
258 })))
259 test.EqualStr(tt, ttt.LastMessage(), "")
260 ttt.ResetMessages()
261 })
262
263 tt.Run("Struct", func(tt *testing.T) {
264 ttt := test.NewTestingTB(tt.Name())
265
266 t := td.NewT(ttt).WithSmuggleHooks(func(got reflect.Value) any {
267 if got.IsValid() {
268 return got.Interface()
269 }
270 return nil
271 })
272
273 type Rstruct struct {
274 V1 reflect.Value
275 V2 reflect.Value
276 }
277
278 got := Rstruct{V1: reflect.ValueOf(42)}
279
280 td.CmpTrue(tt, td.Cmp(t, got, td.Struct(Rstruct{}, td.StructFields{
281 "V1": 42,
282 "V2": nil,
283 })))
284 test.EqualStr(tt, ttt.LastMessage(), "")
285 ttt.ResetMessages()
286
287 td.CmpTrue(tt, td.Cmp(t, got, td.SStruct(Rstruct{}, td.StructFields{
288 "V1": 42,
289 "V2": nil,
290 })))
291 test.EqualStr(tt, ttt.LastMessage(), "")
292 ttt.ResetMessages()
293
294 td.CmpTrue(tt, td.Cmp(t, got, td.Struct(nil, td.StructFields{
295 "V1": 42,
296 "V2": nil,
297 })))
298 test.EqualStr(tt, ttt.LastMessage(), "")
299 ttt.ResetMessages()
300 })
301
302 tt.Run("Error", func(tt *testing.T) {
303 ttt := test.NewTestingTB(tt.Name())
304
305 t := td.NewT(ttt).WithSmuggleHooks(func(got int) (int, error) {
306 return 0, errors.New("never equal")
307 })
308
309 td.CmpFalse(tt, t.Cmp(1, 1))
310
311 if !strings.Contains(ttt.LastMessage(), "DATA: never equal\n") {
312 tt.Errorf(`<%s> does not contain "DATA: never equal\n"`, ttt.LastMessage())
313 }
314 })
315
316 for _, tst := range []struct {
317 name string
318 cmp any
319 fatal string
320 }{
321 {
322 name: "not a function",
323 cmp: "Booh",
324 fatal: "WithSmuggleHooks expects a function, not a string",
325 },
326 {
327 name: "wrong signature",
328 cmp: func(a []int, b ...int) bool { return false },
329 fatal: "WithSmuggleHooks expects: func (A) (B[, error]) not ",
330 },
331 } {
332 tt.Run("panic: "+tst.name, func(tt *testing.T) {
333 ttt := test.NewTestingTB(tt.Name())
334
335 t := td.NewT(ttt)
336
337 fatalMesg := ttt.CatchFatal(func() { t.WithSmuggleHooks(tst.cmp) })
338 test.IsTrue(tt, ttt.IsFatal)
339 if !strings.Contains(fatalMesg, tst.fatal) {
340 tt.Errorf(`<%s> does not contain %q`, fatalMesg, tst.fatal)
341 }
342 })
343 }
344 }