| 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 |
| 8 |
|
| 9 |
import ( |
| 10 |
"github.com/maxatome/go-testdeep/internal/color" |
| 11 |
) |
| 12 |
|
| 13 |
// WithCmpHooks returns a new [*T] instance with new Cmp hooks recorded |
| 14 |
// using functions passed in fns. |
| 15 |
// |
| 16 |
// Each function in fns has to be a function with the following |
| 17 |
// possible signatures: |
| 18 |
// |
| 19 |
// func (got, expected A) bool |
| 20 |
// func (got, expected A) error |
| 21 |
// |
| 22 |
// First arg is always got, and second is always expected. |
| 23 |
// |
| 24 |
// A cannot be an interface. This restriction can be removed in the |
| 25 |
// future, if really needed. |
| 26 |
// |
| 27 |
// This function is called as soon as possible each time the type A is |
| 28 |
// encountered for got while expected type is assignable to A. |
| 29 |
// |
| 30 |
// When it returns a bool, false means A is not equal to B. |
| 31 |
// |
| 32 |
// When it returns a non-nil error (meaning got ≠ expected), its |
| 33 |
// content is used to tell the reason of the failure. |
| 34 |
// |
| 35 |
// Cmp hooks are checked before [UseEqual] feature. |
| 36 |
// |
| 37 |
// Cmp hooks are run just after [Smuggle] hooks. |
| 38 |
// |
| 39 |
// func TestCmpHook(tt *testing.T) { |
| 40 |
// t := td.NewT(tt) |
| 41 |
// |
| 42 |
// // Test reflect.Value contents instead of default field/field |
| 43 |
// t = t.WithCmpHooks(func (got, expected reflect.Value) bool { |
| 44 |
// return td.EqDeeply(got.Interface(), expected.Interface()) |
| 45 |
// }) |
| 46 |
// a, b := 1, 1 |
| 47 |
// t.Cmp(reflect.ValueOf(&a), reflect.ValueOf(&b)) // succeeds |
| 48 |
// |
| 49 |
// // Test reflect.Type correctly instead of default field/field |
| 50 |
// t = t.WithCmpHooks(func (got, expected reflect.Type) bool { |
| 51 |
// return got == expected |
| 52 |
// }) |
| 53 |
// |
| 54 |
// // Test time.Time via its Equal() method instead of default |
| 55 |
// // field/field (note it bypasses the UseEqual flag) |
| 56 |
// t = t.WithCmpHooks((time.Time).Equal) |
| 57 |
// date, _ := time.Parse(time.RFC3339, "2020-09-08T22:13:54+02:00") |
| 58 |
// t.Cmp(date, date.UTC()) // succeeds |
| 59 |
// |
| 60 |
// // Several hooks can be declared at once |
| 61 |
// t = t.WithCmpHooks( |
| 62 |
// func (got, expected reflect.Value) bool { |
| 63 |
// return td.EqDeeply(got.Interface(), expected.Interface()) |
| 64 |
// }, |
| 65 |
// func (got, expected reflect.Type) bool { |
| 66 |
// return got == expected |
| 67 |
// }, |
| 68 |
// (time.Time).Equal, |
| 69 |
// ) |
| 70 |
// } |
| 71 |
// |
| 72 |
// There is no way to add or remove hooks of an existing [*T] |
| 73 |
// instance, only to create a new [*T] instance with this method or |
| 74 |
// [T.WithSmuggleHooks] to add some. |
| 75 |
// |
| 76 |
// WithCmpHooks calls t.Fatal if an item of fns is not a function or |
| 77 |
// if its signature does not match the expected ones. |
| 78 |
// |
| 79 |
// See also [T.WithSmuggleHooks]. |
| 80 |
// |
| 81 |
// [UseEqual]: https://pkg.go.dev/github.com/maxatome/go-testdeep/td#ContextConfig.UseEqual |
| 82 |
func (t *T) WithCmpHooks(fns ...any) *T { |
| 83 |
t = t.copyWithHooks() |
| 84 |
|
| 85 |
err := t.Config.hooks.AddCmpHooks(fns) |
| 86 |
if err != nil { |
| 87 |
t.Helper() |
| 88 |
t.Fatal(color.Bad("WithCmpHooks " + err.Error())) |
| 89 |
} |
| 90 |
|
| 91 |
return t |
| 92 |
} |
| 93 |
|
| 94 |
// WithSmuggleHooks returns a new [*T] instance with new Smuggle hooks |
| 95 |
// recorded using functions passed in fns. |
| 96 |
// |
| 97 |
// Each function in fns has to be a function with the following |
| 98 |
// possible signatures: |
| 99 |
// |
| 100 |
// func (got A) B |
| 101 |
// func (got A) (B, error) |
| 102 |
// |
| 103 |
// A cannot be an interface. This restriction can be removed in the |
| 104 |
// future, if really needed. |
| 105 |
// |
| 106 |
// B cannot be an interface. If you have a use case, we can talk about it. |
| 107 |
// |
| 108 |
// This function is called as soon as possible each time the type A is |
| 109 |
// encountered for got. |
| 110 |
// |
| 111 |
// The B value returned replaces the got value for subsequent tests. |
| 112 |
// Smuggle hooks are NOT run again for this returned value to avoid |
| 113 |
// easy infinite loop recursion. |
| 114 |
// |
| 115 |
// When it returns non-nil error (meaning something wrong happened |
| 116 |
// during the conversion of A to B), it raises a global error and its |
| 117 |
// content is used to tell the reason of the failure. |
| 118 |
// |
| 119 |
// Smuggle hooks are run just before Cmp hooks. |
| 120 |
// |
| 121 |
// func TestSmuggleHook(tt *testing.T) { |
| 122 |
// t := td.NewT(tt) |
| 123 |
// |
| 124 |
// // Each encountered int is changed to a bool |
| 125 |
// t = t.WithSmuggleHooks(func (got int) bool { |
| 126 |
// return got != 0 |
| 127 |
// }) |
| 128 |
// t.Cmp(map[string]int{"ok": 1, "no": 0}, |
| 129 |
// map[string]bool{"ok", true, "no", false}) // succeeds |
| 130 |
// |
| 131 |
// // Each encountered string is converted to int |
| 132 |
// t = t.WithSmuggleHooks(strconv.Atoi) |
| 133 |
// t.Cmp("123", 123) // succeeds |
| 134 |
// |
| 135 |
// // Several hooks can be declared at once |
| 136 |
// t = t.WithSmuggleHooks( |
| 137 |
// func (got int) bool { return got != 0 }, |
| 138 |
// strconv.Atoi, |
| 139 |
// ) |
| 140 |
// } |
| 141 |
// |
| 142 |
// There is no way to add or remove hooks of an existing [*T] |
| 143 |
// instance, only create a new [*T] instance with this method or |
| 144 |
// [T.WithCmpHooks] to add some. |
| 145 |
// |
| 146 |
// WithSmuggleHooks calls t.Fatal if an item of fns is not a |
| 147 |
// function or if its signature does not match the expected ones. |
| 148 |
// |
| 149 |
// See also [T.WithCmpHooks]. |
| 150 |
func (t *T) WithSmuggleHooks(fns ...any) *T { |
| 151 |
t = t.copyWithHooks() |
| 152 |
|
| 153 |
err := t.Config.hooks.AddSmuggleHooks(fns) |
| 154 |
if err != nil { |
| 155 |
t.Helper() |
| 156 |
t.Fatal(color.Bad("WithSmuggleHooks " + err.Error())) |
| 157 |
} |
| 158 |
|
| 159 |
return t |
| 160 |
} |
| 161 |
|
| 162 |
func (t *T) copyWithHooks() *T { |
| 163 |
nt := NewT(t) |
| 164 |
nt.Config.hooks = t.Config.hooks.Copy() |
| 165 |
return nt |
| 166 |
} |