File: hooks_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (413 lines) | stat: -rw-r--r-- 10,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) 2020, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package hooks_test

import (
	"errors"
	"net"
	"reflect"
	"strconv"
	"strings"
	"testing"
	"time"

	"github.com/maxatome/go-testdeep/internal/hooks"
	"github.com/maxatome/go-testdeep/internal/test"
)

func TestAddCmpHooks(t *testing.T) {
	for _, tst := range []struct {
		name string
		cmp  any
		err  string
	}{
		{
			name: "not a function",
			cmp:  "zip",
			err:  "expects a function, not a string (@1)",
		},
		{
			name: "no variadic",
			cmp:  func(a []byte, b ...byte) bool { return true },
			err:  "expects: func (T, T) bool|error not func([]uint8, ...uint8) bool (@1)",
		},
		{
			name: "in",
			cmp:  func(a, b, c int) bool { return true },
			err:  "expects: func (T, T) bool|error not func(int, int, int) bool (@1)",
		},
		{
			name: "out",
			cmp:  func(a, b int) {},
			err:  "expects: func (T, T) bool|error not func(int, int) (@1)",
		},
		{
			name: "type mismatch",
			cmp:  func(a int, b bool) bool { return true },
			err:  "expects: func (T, T) bool|error not func(int, bool) bool (@1)",
		},
		{
			name: "interface",
			cmp:  func(a, b any) bool { return true },
			err:  "expects: func (T, T) bool|error not func(interface {}, interface {}) bool (@1)",
		},
		{
			name: "bad return",
			cmp:  func(a, b int) int { return 0 },
			err:  "expects: func (T, T) bool|error not func(int, int) int (@1)",
		},
	} {
		i := hooks.NewInfo()

		err := i.AddCmpHooks([]any{
			func(a, b bool) bool { return true },
			tst.cmp,
		})
		if test.Error(t, err, tst.name) {
			if !strings.Contains(err.Error(), tst.err) {
				t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name)
			}
		}
	}
}

func TestCmp(t *testing.T) {
	t.Run("bool", func(t *testing.T) {
		var i *hooks.Info

		handled, err := i.Cmp(reflect.ValueOf(12), reflect.ValueOf(12))
		test.NoError(t, err)
		test.IsFalse(t, handled)

		i = hooks.NewInfo()

		err = i.AddCmpHooks([]any{func(a, b int) bool { return a == b }})
		test.NoError(t, err)

		handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf(12))
		test.NoError(t, err)
		test.IsTrue(t, handled)

		handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf(34))
		if err != hooks.ErrBoolean {
			test.EqualErrorMessage(t, err, hooks.ErrBoolean)
		}
		test.IsTrue(t, handled)

		handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf("twelve"))
		test.NoError(t, err)
		test.IsFalse(t, handled)

		handled, err = i.Cmp(reflect.ValueOf("twelve"), reflect.ValueOf("twelve"))
		test.NoError(t, err)
		test.IsFalse(t, handled)

		handled, err = (*hooks.Info)(nil).Cmp(reflect.ValueOf(1), reflect.ValueOf(2))
		test.NoError(t, err)
		test.IsFalse(t, handled)
	})

	t.Run("error", func(t *testing.T) {
		i := hooks.NewInfo()

		diffErr := errors.New("a≠b")

		err := i.AddCmpHooks([]any{
			func(a, b int) error {
				if a == b {
					return nil
				}
				return diffErr
			},
		})
		test.NoError(t, err)

		handled, err := i.Cmp(reflect.ValueOf(12), reflect.ValueOf(12))
		test.NoError(t, err)
		test.IsTrue(t, handled)

		handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf(34))
		if err != diffErr {
			test.EqualErrorMessage(t, err, diffErr)
		}
		test.IsTrue(t, handled)
	})
}

func TestSmuggle(t *testing.T) {
	var i *hooks.Info

	got := reflect.ValueOf(123)
	handled, err := i.Smuggle(&got)
	test.NoError(t, err)
	test.IsFalse(t, handled)

	i = hooks.NewInfo()

	err = i.AddSmuggleHooks([]any{func(a int) bool { return a != 0 }})
	test.NoError(t, err)

	got = reflect.ValueOf(123)
	handled, err = i.Smuggle(&got)
	test.NoError(t, err)
	test.IsTrue(t, handled)
	if test.EqualInt(t, int(got.Kind()), int(reflect.Bool)) {
		test.IsTrue(t, got.Bool())
	}

	got = reflect.ValueOf("biz")
	handled, err = i.Smuggle(&got)
	test.NoError(t, err)
	test.IsFalse(t, handled)
	test.EqualStr(t, got.String(), "biz")

	err = i.AddSmuggleHooks([]any{strconv.Atoi})
	test.NoError(t, err)

	got = reflect.ValueOf("123")
	handled, err = i.Smuggle(&got)
	test.NoError(t, err)
	test.IsTrue(t, handled)
	if test.EqualInt(t, int(got.Kind()), int(reflect.Int)) {
		test.EqualInt(t, int(got.Int()), 123)
	}

	got = reflect.ValueOf("NotANumber")
	handled, err = i.Smuggle(&got)
	test.Error(t, err)
	test.IsTrue(t, handled)
}

func TestAddSmuggleHooks(t *testing.T) {
	for _, tst := range []struct {
		name    string
		smuggle any
		err     string
	}{
		{
			name:    "not a function",
			smuggle: "zip",
			err:     "expects a function, not a string (@1)",
		},
		{
			name:    "no variadic",
			smuggle: func(a ...byte) bool { return true },
			err:     "expects: func (A) (B[, error]) not func(...uint8) bool (@1)",
		},
		{
			name:    "in",
			smuggle: func(a, b int) bool { return true },
			err:     "expects: func (A) (B[, error]) not func(int, int) bool (@1)",
		},
		{
			name:    "interface",
			smuggle: func(a any) bool { return true },
			err:     "expects: func (A) (B[, error]) not func(interface {}) bool (@1)",
		},
		{
			name:    "out",
			smuggle: func(a int) {},
			err:     "expects: func (A) (B[, error]) not func(int) (@1)",
		},
		{
			name:    "bad return",
			smuggle: func(a int) (int, int) { return 0, 0 },
			err:     "expects: func (A) (B[, error]) not func(int) (int, int) (@1)",
		},
		{
			name:    "return interface",
			smuggle: func(a int) any { return 0 },
			err:     "expects: func (A) (B[, error]) not func(int) interface {} (@1)",
		},
		{
			name:    "return interface, error",
			smuggle: func(a int) (any, error) { return 0, nil },
			err:     "expects: func (A) (B[, error]) not func(int) (interface {}, error) (@1)",
		},
	} {
		i := hooks.NewInfo()

		err := i.AddSmuggleHooks([]any{
			func(a int) bool { return true },
			tst.smuggle,
		})
		if test.Error(t, err, tst.name) {
			if !strings.Contains(err.Error(), tst.err) {
				t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name)
			}
		}
	}
}

func TestUseEqual(t *testing.T) {
	var i *hooks.Info

	test.IsFalse(t, i.UseEqual(reflect.TypeOf(42)))

	i = hooks.NewInfo()
	test.IsFalse(t, i.UseEqual(reflect.TypeOf(42)))

	test.NoError(t, i.AddUseEqual([]any{}))

	test.NoError(t, i.AddUseEqual([]any{time.Time{}, net.IP{}}))
	test.IsTrue(t, i.UseEqual(reflect.TypeOf(time.Time{})))
	test.IsTrue(t, i.UseEqual(reflect.TypeOf(net.IP{})))
}

func TestAddUseEqual(t *testing.T) {
	for _, tst := range []struct {
		name string
		typ  any
		err  string
	}{
		{
			name: "no Equal() method",
			typ:  &testing.T{},
			err:  "expects type *testing.T owns an Equal method (@1)",
		},
		{
			name: "variadic Equal() method",
			typ:  badEqualVariadic{},
			err:  "expects type hooks_test.badEqualVariadic Equal method signature be Equal(hooks_test.badEqualVariadic) bool (@1)",
		},
		{
			name: "bad NumIn Equal() method",
			typ:  badEqualNumIn{},
			err:  "expects type hooks_test.badEqualNumIn Equal method signature be Equal(hooks_test.badEqualNumIn) bool (@1)",
		},
		{
			name: "bad NumOut Equal() method",
			typ:  badEqualNumOut{},
			err:  "expects type hooks_test.badEqualNumOut Equal method signature be Equal(hooks_test.badEqualNumOut) bool (@1)",
		},
		{
			name: "In(0) not assignable to In(1) Equal() method",
			typ:  badEqualInAssign{},
			err:  "expects type hooks_test.badEqualInAssign Equal method signature be Equal(hooks_test.badEqualInAssign) bool (@1)",
		},
		{
			name: "Equal() method don't return bool",
			typ:  badEqualOutType{},
			err:  "expects type hooks_test.badEqualOutType Equal method signature be Equal(hooks_test.badEqualOutType) bool (@1)",
		},
	} {
		i := hooks.NewInfo()

		err := i.AddUseEqual([]any{time.Time{}, tst.typ})
		if test.Error(t, err, tst.name) {
			if !strings.Contains(err.Error(), tst.err) {
				t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name)
			}
		}
	}
}

func TestIgnoreUnexported(t *testing.T) {
	var i *hooks.Info

	test.IsFalse(t, i.IgnoreUnexported(reflect.TypeOf(struct{}{})))

	i = hooks.NewInfo()
	test.IsFalse(t, i.IgnoreUnexported(reflect.TypeOf(struct{}{})))

	test.NoError(t, i.AddIgnoreUnexported([]any{}))

	test.NoError(t, i.AddIgnoreUnexported([]any{testing.T{}, time.Time{}}))
	test.IsTrue(t, i.IgnoreUnexported(reflect.TypeOf(time.Time{})))
	test.IsTrue(t, i.IgnoreUnexported(reflect.TypeOf(testing.T{})))
}

func TestAddIgnoreUnexported(t *testing.T) {
	i := hooks.NewInfo()

	err := i.AddIgnoreUnexported([]any{time.Time{}, 0})
	if test.Error(t, err) {
		test.EqualStr(t, err.Error(), "expects type int be a struct, not a int (@1)")
	}
}

func TestCopy(t *testing.T) {
	var orig *hooks.Info

	ni := orig.Copy()
	if ni == nil {
		t.Errorf("Copy should never return nil, even for a nil instance")
	}

	orig = hooks.NewInfo()
	copy1 := orig.Copy()
	if copy1 == nil {
		t.Errorf("Copy should never return nil")
	}
	hookedBool := false
	test.NoError(t, copy1.AddSmuggleHooks([]any{
		func(in bool) bool { hookedBool = true; return in },
	}))

	gotBool := reflect.ValueOf(true)

	// orig instance does not have any hook
	handled, _ := orig.Smuggle(&gotBool)
	test.IsFalse(t, hookedBool)
	test.IsFalse(t, handled)

	// new bool smuggle hook OK
	hookedBool = false
	handled, _ = copy1.Smuggle(&gotBool)
	test.IsTrue(t, hookedBool)
	test.IsTrue(t, handled)

	copy2 := copy1.Copy()
	if copy2 == nil {
		t.Errorf("Copy should never return nil")
	}
	hookedInt := false
	test.NoError(t, copy2.AddSmuggleHooks([]any{
		func(in int) int { hookedInt = true; return in },
	}))

	// bool smuggle hook inherited from copy1
	hookedBool = false
	handled, _ = copy2.Smuggle(&gotBool)
	test.IsTrue(t, hookedBool)
	test.IsTrue(t, handled)

	gotInt := reflect.ValueOf(123)

	// new int smuggle hook not available in copy1 instance
	hookedInt = false
	handled, _ = copy1.Smuggle(&gotInt)
	test.IsFalse(t, hookedInt)
	test.IsFalse(t, handled)

	// new int smuggle hook OK
	hookedInt = false
	handled, _ = copy2.Smuggle(&gotInt)
	test.IsTrue(t, hookedInt)
	test.IsTrue(t, handled)
	test.IsTrue(t, handled)
}

type badEqualVariadic struct{}

func (badEqualVariadic) Equal(a ...badEqualVariadic) bool { return false }

type badEqualNumIn struct{}

func (badEqualNumIn) Equal(a badEqualNumIn, b badEqualNumIn) bool { return false }

type badEqualNumOut struct{}

func (badEqualNumOut) Equal(a badEqualNumOut) {}

type badEqualInAssign struct{}

func (badEqualInAssign) Equal(a int) bool { return false }

type badEqualOutType struct{}

func (badEqualOutType) Equal(a badEqualOutType) int { return 42 }