| 1 |
// Copyright (c) 2019, 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 |
"reflect" |
| 11 |
"sync" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/anchors" |
| 14 |
"github.com/maxatome/go-testdeep/internal/color" |
| 15 |
) |
| 16 |
|
| 17 |
// Anchors are stored globally by testing.TB.Name(). |
| 18 |
var allAnchors = map[string]*anchors.Info{} |
| 19 |
var allAnchorsMu sync.Mutex |
| 20 |
|
| 21 |
// AddAnchorableStructType declares a struct type as anchorable. fn |
| 22 |
// is a function allowing to return a unique and identifiable instance |
| 23 |
// of the struct type. |
| 24 |
// |
| 25 |
// fn has to have the following signature: |
| 26 |
// |
| 27 |
// func (nextAnchor int) TYPE |
| 28 |
// |
| 29 |
// TYPE is the struct type to make anchorable and nextAnchor is an |
| 30 |
// index to allow to differentiate several instances of the same type. |
| 31 |
// |
| 32 |
// For example, the [time.Time] type which is anchorable by default, |
| 33 |
// could be declared as: |
| 34 |
// |
| 35 |
// AddAnchorableStructType(func (nextAnchor int) time.Time { |
| 36 |
// return time.Unix(int64(math.MaxInt64-1000424443-nextAnchor), 42) |
| 37 |
// }) |
| 38 |
// |
| 39 |
// Just as a note, the 1000424443 constant allows to avoid to flirt |
| 40 |
// with the math.MaxInt64 extreme limit and so avoid possible |
| 41 |
// collision with real world values. |
| 42 |
// |
| 43 |
// It panics if the provided fn is not a function or if it has not the |
| 44 |
// expected signature (see above). |
| 45 |
// |
| 46 |
// See also [T.Anchor], [T.AnchorsPersistTemporarily], |
| 47 |
// [T.DoAnchorsPersist], [T.ResetAnchors] and [T.SetAnchorsPersist]. |
| 48 |
func AddAnchorableStructType(fn any) { |
| 49 |
err := anchors.AddAnchorableStructType(fn) |
| 50 |
if err != nil { |
| 51 |
panic(color.Bad(err.Error())) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Anchor returns a typed value allowing to anchor the TestDeep |
| 56 |
// operator operator in a go classic literal like a struct, slice, |
| 57 |
// array or map value. |
| 58 |
// |
| 59 |
// Starting go1.27, you should prefer to use [T.AT]. |
| 60 |
// |
| 61 |
// If the TypeBehind method of operator returns non-nil, model can be |
| 62 |
// omitted (like with [Between] operator in the example |
| 63 |
// below). Otherwise, model should contain only one value |
| 64 |
// corresponding to the returning type. It can be: |
| 65 |
// - a go value: returning type is the type of the value, |
| 66 |
// whatever the value is; |
| 67 |
// - a [reflect.Type]. |
| 68 |
// |
| 69 |
// It returns a typed value ready to be embed in a go data structure to |
| 70 |
// be compared using [T.Cmp] or [T.CmpLax]: |
| 71 |
// |
| 72 |
// import ( |
| 73 |
// "testing" |
| 74 |
// |
| 75 |
// "github.com/maxatome/go-testdeep/td" |
| 76 |
// ) |
| 77 |
// |
| 78 |
// func TestFunc(tt *testing.T) { |
| 79 |
// got := Func() |
| 80 |
// |
| 81 |
// t := td.NewT(tt) |
| 82 |
// t.Cmp(got, &MyStruct{ |
| 83 |
// Name: "Bob", |
| 84 |
// Details: &MyDetails{ |
| 85 |
// Nick: t.Anchor(td.HasPrefix("Bobby"), "").(string), |
| 86 |
// Age: t.Anchor(td.Between(40, 50)).(int), |
| 87 |
// }, |
| 88 |
// }) |
| 89 |
// } |
| 90 |
// |
| 91 |
// In this example: |
| 92 |
// |
| 93 |
// - [HasPrefix] operates on several input types (string, |
| 94 |
// [fmt.Stringer], error, …), so its TypeBehind method returns always |
| 95 |
// nil as it can not guess in advance on which type it operates. In |
| 96 |
// this case, we must pass "" as model parameter in order to tell it |
| 97 |
// to return the string type. Note that the .(string) type assertion |
| 98 |
// is then mandatory to conform to the strict type checking. |
| 99 |
// - [Between], on its side, knows the type on which it operates, as |
| 100 |
// it is the same as the one of its parameters. So its TypeBehind |
| 101 |
// method returns the right type, and so no need to pass it as model |
| 102 |
// parameter. Note that the .(int) type assertion is still mandatory |
| 103 |
// to conform to the strict type checking. |
| 104 |
// |
| 105 |
// Without operator anchoring feature, the previous example would have |
| 106 |
// been: |
| 107 |
// |
| 108 |
// import ( |
| 109 |
// "testing" |
| 110 |
// |
| 111 |
// "github.com/maxatome/go-testdeep/td" |
| 112 |
// ) |
| 113 |
// |
| 114 |
// func TestFunc(tt *testing.T) { |
| 115 |
// got := Func() |
| 116 |
// |
| 117 |
// t := td.NewT(tt) |
| 118 |
// t.Cmp(got, td.Struct(&MyStruct{Name: "Bob"}, |
| 119 |
// td.StructFields{ |
| 120 |
// "Details": td.Struct(&MyDetails{}, |
| 121 |
// td.StructFields{ |
| 122 |
// "Nick": td.HasPrefix("Bobby"), |
| 123 |
// "Age": td.Between(40, 50), |
| 124 |
// }), |
| 125 |
// })) |
| 126 |
// } |
| 127 |
// |
| 128 |
// using two times the [Struct] operator to work around the strict type |
| 129 |
// checking of golang. |
| 130 |
// |
| 131 |
// By default, the value returned by Anchor can only be used in the |
| 132 |
// next [T.Cmp] or [T.CmpLax] call. To make it persistent across calls, |
| 133 |
// see [T.SetAnchorsPersist] and [T.AnchorsPersistTemporarily] methods. |
| 134 |
// |
| 135 |
// See [T.AT] & [T.AnchorT] methods for generic variants (go1.27 |
| 136 |
// required) and [T.A] method for a shorter synonym of Anchor. |
| 137 |
// |
| 138 |
// See also [T.AnchorsPersistTemporarily], [T.DoAnchorsPersist], |
| 139 |
// [T.ResetAnchors], [T.SetAnchorsPersist] and [AddAnchorableStructType]. |
| 140 |
func (t *T) Anchor(operator TestDeep, model ...any) any { |
| 141 |
if operator == nil { |
| 142 |
t.Helper() |
| 143 |
t.Fatal(color.Bad("Cannot anchor a nil TestDeep operator")) |
| 144 |
} |
| 145 |
|
| 146 |
var typ reflect.Type |
| 147 |
if len(model) > 0 { |
| 148 |
if len(model) != 1 { |
| 149 |
t.Helper() |
| 150 |
t.Fatal(color.TooManyParams("Anchor(OPERATOR[, MODEL])")) |
| 151 |
} |
| 152 |
var ok bool |
| 153 |
typ, ok = model[0].(reflect.Type) |
| 154 |
if !ok { |
| 155 |
typ = reflect.TypeOf(model[0]) |
| 156 |
if typ == nil { |
| 157 |
t.Helper() |
| 158 |
t.Fatal(color.Bad("Untyped nil value is not valid as model for an anchor")) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
typeBehind := operator.TypeBehind() |
| 163 |
if typeBehind != nil && typeBehind != typ { |
| 164 |
t.Helper() |
| 165 |
t.Fatal(color.Bad("Operator %s TypeBehind() returned %s which differs from model type %s. Omit model or ensure its type is %[2]s", |
| 166 |
operator.GetLocation().Func, typeBehind, typ)) |
| 167 |
} |
| 168 |
} else { |
| 169 |
typ = operator.TypeBehind() |
| 170 |
if typ == nil { |
| 171 |
t.Helper() |
| 172 |
t.Fatal(color.Bad("Cannot anchor operator %s as TypeBehind() returned nil. Use model parameter to specify the type to return", |
| 173 |
operator.GetLocation().Func)) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
nvm, err := t.Config.anchors.AddAnchor(typ, reflect.ValueOf(operator)) |
| 178 |
if err != nil { |
| 179 |
t.Helper() |
| 180 |
t.Fatal(color.Bad(err.Error())) |
| 181 |
} |
| 182 |
|
| 183 |
return nvm.Interface() |
| 184 |
} |
| 185 |
|
| 186 |
// A is a synonym for [T.Anchor]. |
| 187 |
// |
| 188 |
// Starting go1.27, you should prefer to use [T.AT]. |
| 189 |
// |
| 190 |
// import ( |
| 191 |
// "testing" |
| 192 |
// |
| 193 |
// "github.com/maxatome/go-testdeep/td" |
| 194 |
// ) |
| 195 |
// |
| 196 |
// func TestFunc(tt *testing.T) { |
| 197 |
// got := Func() |
| 198 |
// |
| 199 |
// t := td.NewT(tt) |
| 200 |
// t.Cmp(got, &MyStruct{ |
| 201 |
// Name: "Bob", |
| 202 |
// Details: &MyDetails{ |
| 203 |
// Nick: t.A(td.HasPrefix("Bobby"), "").(string), |
| 204 |
// Age: t.A(td.Between(40, 50)).(int), |
| 205 |
// }, |
| 206 |
// }) |
| 207 |
// } |
| 208 |
// |
| 209 |
// See [T.AT] & [T.AnchorT] methods for generic variants (go1.27 required). |
| 210 |
// |
| 211 |
// See also [T.AnchorsPersistTemporarily], [T.DoAnchorsPersist], |
| 212 |
// [T.ResetAnchors], [T.SetAnchorsPersist] and [AddAnchorableStructType]. |
| 213 |
func (t *T) A(operator TestDeep, model ...any) any { |
| 214 |
t.Helper() |
| 215 |
return t.Anchor(operator, model...) |
| 216 |
} |
| 217 |
|
| 218 |
func (t *T) resetNonPersistentAnchors() { |
| 219 |
t.Config.anchors.ResetAnchors(false) |
| 220 |
} |
| 221 |
|
| 222 |
// ResetAnchors frees all operators anchored with [T.Anchor] |
| 223 |
// method. Unless operators anchoring persistence has been enabled |
| 224 |
// with [T.SetAnchorsPersist], there is no need to call this |
| 225 |
// method. Anchored operators are automatically freed after each [Cmp], |
| 226 |
// [CmpDeeply] and [CmpPanic] call (or others methods calling them behind |
| 227 |
// the scene). |
| 228 |
// |
| 229 |
// See also [T.Anchor], [T.AnchorsPersistTemporarily], |
| 230 |
// [T.DoAnchorsPersist], [T.SetAnchorsPersist] and [AddAnchorableStructType]. |
| 231 |
func (t *T) ResetAnchors() { |
| 232 |
t.Config.anchors.ResetAnchors(true) |
| 233 |
} |
| 234 |
|
| 235 |
// AnchorsPersistTemporarily is used by helpers to temporarily enable |
| 236 |
// anchors persistence. See [tdhttp] package for an example of use. It |
| 237 |
// returns a function to be deferred, to restore the normal behavior |
| 238 |
// (clear anchored operators if persistence was false, do nothing |
| 239 |
// otherwise). |
| 240 |
// |
| 241 |
// Typically used as: |
| 242 |
// |
| 243 |
// defer t.AnchorsPersistTemporarily()() |
| 244 |
// // or |
| 245 |
// t.Cleanup(t.AnchorsPersistTemporarily()) |
| 246 |
// |
| 247 |
// See also [T.Anchor], [T.DoAnchorsPersist], [T.ResetAnchors], |
| 248 |
// [T.SetAnchorsPersist] and [AddAnchorableStructType]. |
| 249 |
// |
| 250 |
// [tdhttp]: https://pkg.go.dev/github.com/maxatome/go-testdeep/helpers/tdhttp |
| 251 |
func (t *T) AnchorsPersistTemporarily() func() { |
| 252 |
// If already persistent, do nothing on defer |
| 253 |
if t.DoAnchorsPersist() { |
| 254 |
return func() {} |
| 255 |
} |
| 256 |
|
| 257 |
t.SetAnchorsPersist(true) |
| 258 |
return func() { |
| 259 |
t.SetAnchorsPersist(false) |
| 260 |
t.Config.anchors.ResetAnchors(true) |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// DoAnchorsPersist returns true if anchors persistence is enabled, |
| 265 |
// false otherwise. |
| 266 |
// |
| 267 |
// See also [T.Anchor], [T.AnchorsPersistTemporarily], |
| 268 |
// [T.ResetAnchors], [T.SetAnchorsPersist] and [AddAnchorableStructType]. |
| 269 |
func (t *T) DoAnchorsPersist() bool { |
| 270 |
return t.Config.anchors.DoAnchorsPersist() |
| 271 |
} |
| 272 |
|
| 273 |
// SetAnchorsPersist allows to enable or disable anchors persistence. |
| 274 |
// |
| 275 |
// See also [T.Anchor], [T.AnchorsPersistTemporarily], |
| 276 |
// [T.DoAnchorsPersist], [T.ResetAnchors] and [AddAnchorableStructType]. |
| 277 |
func (t *T) SetAnchorsPersist(persist bool) { |
| 278 |
t.Config.anchors.SetAnchorsPersist(persist) |
| 279 |
} |
| 280 |
|
| 281 |
func (t *T) initAnchors() { |
| 282 |
if t.Config.anchors != nil { |
| 283 |
return |
| 284 |
} |
| 285 |
|
| 286 |
name := t.Name() |
| 287 |
|
| 288 |
allAnchorsMu.Lock() |
| 289 |
defer allAnchorsMu.Unlock() |
| 290 |
|
| 291 |
t.Config.anchors = allAnchors[name] |
| 292 |
if t.Config.anchors == nil { |
| 293 |
t.Config.anchors = anchors.NewInfo() |
| 294 |
allAnchors[name] = t.Config.anchors |
| 295 |
|
| 296 |
// Do not record a finalizer if no name (should not happen |
| 297 |
// except perhaps in tests) |
| 298 |
if name != "" { |
| 299 |
t.Cleanup(func() { |
| 300 |
allAnchorsMu.Lock() |
| 301 |
defer allAnchorsMu.Unlock() |
| 302 |
delete(allAnchors, name) |
| 303 |
}) |
| 304 |
} |
| 305 |
} |
| 306 |
} |