Debian Codegraph @

internal/anchors/anchor.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 anchors
8
9 import (
10 "errors"
11 "fmt"
12 "math"
13 "reflect"
14 "sync"
15 )
16
17 type anchor struct {
18 Anchor reflect.Value // Anchor is the generated value used as anchor
19 Operator reflect.Value // Operator is a td.TestDeep behind
20 }
21
22 // Info gathers all anchors information.
23 type Info struct {
24 sync.Mutex
25 index int
26 persist bool
27 anchors map[any]anchor
28 }
29
30 // NewInfo returns a new instance of [*Info].
31 func NewInfo() *Info {
32 return &Info{
33 anchors: map[any]anchor{},
34 }
35 }
36
37 // AddAnchor anchors a new operator op, with type typ then returns the
38 // anchor value.
39 func (i *Info) AddAnchor(typ reflect.Type, op reflect.Value) (reflect.Value, error) {
40 i.Lock()
41 defer i.Unlock()
42
43 anc, key, err := i.build(typ)
44 if err != nil {
45 return reflect.Value{}, err
46 }
47
48 if i.anchors == nil {
49 i.anchors = map[any]anchor{}
50 }
51
52 i.anchors[key] = anchor{
53 Anchor: anc,
54 Operator: op,
55 }
56
57 return anc, nil
58 }
59
60 // DoAnchorsPersist returns true if anchors are persistent across tests.
61 func (i *Info) DoAnchorsPersist() bool {
62 i.Lock()
63 defer i.Unlock()
64 return i.persist
65 }
66
67 // SetAnchorsPersist enables or disables anchors persistence.
68 func (i *Info) SetAnchorsPersist(persist bool) {
69 i.Lock()
70 defer i.Unlock()
71 i.persist = persist
72 }
73
74 // ResetAnchors removes all anchors if persistence is disabled or
75 // force is true.
76 func (i *Info) ResetAnchors(force bool) {
77 i.Lock()
78 defer i.Unlock()
79
80 if !i.persist || force {
81 for k := range i.anchors {
82 delete(i.anchors, k)
83 }
84 i.index = 0
85 }
86 }
87
88 func (i *Info) nextIndex() (n int) {
89 n = i.index
90 i.index++
91 return
92 }
93
94 // ResolveAnchor checks whether the passed value matches an anchored
95 // operator or not. If yes, this operator is returned with true. If
96 // no, the value is returned as is with false.
97 func (i *Info) ResolveAnchor(v reflect.Value) (reflect.Value, bool) {
98 if i == nil || !v.CanInterface() {
99 return v, false
100 }
101 // Shortcut
102 i.Lock()
103 la := len(i.anchors)
104 i.Unlock()
105 if la == 0 {
106 return v, false
107 }
108
109 var key any
110 sw:
111 switch v.Kind() {
112 case reflect.Int,
113 reflect.Int8,
114 reflect.Int16,
115 reflect.Int32,
116 reflect.Int64,
117 reflect.Uint,
118 reflect.Uint8,
119 reflect.Uint16,
120 reflect.Uint32,
121 reflect.Uint64,
122 reflect.Uintptr,
123 reflect.Float32,
124 reflect.Float64,
125 reflect.Complex64,
126 reflect.Complex128,
127 reflect.String:
128 key = v.Interface()
129
130 case reflect.Chan,
131 reflect.Map,
132 reflect.Slice,
133 reflect.Ptr:
134 key = v.Pointer()
135
136 case reflect.Struct:
137 typ := v.Type()
138 if typ.Comparable() {
139 // Check for anchorable types. No need of 2 passes here.
140 for _, at := range AnchorableTypes {
141 if typ == at.typ || at.typ.ConvertibleTo(typ) { // 1.17 ok as struct here
142 key = v.Interface()
143 break sw
144 }
145 }
146 }
147 fallthrough
148
149 default:
150 return v, false
151 }
152
153 i.Lock()
154 defer i.Unlock()
155 if anchor, ok := i.anchors[key]; ok {
156 return anchor.Operator, true
157 }
158 return v, false
159 }
160
161 func (i *Info) setInt(typ reflect.Type, min int64) (reflect.Value, any) {
162 nvm := reflect.New(typ).Elem()
163 nvm.SetInt(min + int64(i.nextIndex()))
164 return nvm, nvm.Interface()
165 }
166
167 func (i *Info) setUint(typ reflect.Type, max uint64) (reflect.Value, any) {
168 nvm := reflect.New(typ).Elem()
169 nvm.SetUint(max - uint64(i.nextIndex()))
170 return nvm, nvm.Interface()
171 }
172
173 func (i *Info) setFloat(typ reflect.Type, min float64) (reflect.Value, any) {
174 nvm := reflect.New(typ).Elem()
175 nvm.SetFloat(min + float64(i.nextIndex()))
176 return nvm, nvm.Interface()
177 }
178
179 func (i *Info) setComplex(typ reflect.Type, min float64) (reflect.Value, any) {
180 nvm := reflect.New(typ).Elem()
181 min += float64(i.nextIndex())
182 nvm.SetComplex(complex(min, min))
183 return nvm, nvm.Interface()
184 }
185
186 // build builds a new value of type "typ" and returns it under two
187 // forms:
188 // - the new value itself as a reflect.Value;
189 // - an any usable as a key in an AnchorsSet map.
190 //
191 // It returns an error if "typ" kind is not recognized or if it is a
192 // non-anchorable struct.
193 func (i *Info) build(typ reflect.Type) (reflect.Value, any, error) {
194 // For each numeric type, anchor the operator on a number close to
195 // the limit of this type, but not at the extreme limit to avoid
196 // edge cases where these limits are used in real world and so avoid
197 // collisions
198 switch typ.Kind() {
199 case reflect.Int:
200 nvm, iface := i.setInt(typ, int64(^int(^uint(0)>>1))+1004293)
201 return nvm, iface, nil
202 case reflect.Int8:
203 nvm, iface := i.setInt(typ, math.MinInt8+13)
204 return nvm, iface, nil
205 case reflect.Int16:
206 nvm, iface := i.setInt(typ, math.MinInt16+1049)
207 return nvm, iface, nil
208 case reflect.Int32:
209 nvm, iface := i.setInt(typ, math.MinInt32+1004293)
210 return nvm, iface, nil
211 case reflect.Int64:
212 nvm, iface := i.setInt(typ, math.MinInt64+1000424443)
213 return nvm, iface, nil
214
215 case reflect.Uint:
216 nvm, iface := i.setUint(typ, uint64(^uint(0))-1004293)
217 return nvm, iface, nil
218 case reflect.Uint8:
219 nvm, iface := i.setUint(typ, math.MaxUint8-29)
220 return nvm, iface, nil
221 case reflect.Uint16:
222 nvm, iface := i.setUint(typ, math.MaxUint16-2099)
223 return nvm, iface, nil
224 case reflect.Uint32:
225 nvm, iface := i.setUint(typ, math.MaxUint32-2008571)
226 return nvm, iface, nil
227 case reflect.Uint64:
228 nvm, iface := i.setUint(typ, math.MaxUint64-2000848901)
229 return nvm, iface, nil
230 case reflect.Uintptr:
231 nvm, iface := i.setUint(typ, uint64(^uintptr(0))-2000848901)
232 return nvm, iface, nil
233
234 case reflect.Float32:
235 nvm, iface := i.setFloat(typ, -(1<<24)+104243)
236 return nvm, iface, nil
237 case reflect.Float64:
238 nvm, iface := i.setFloat(typ, -(1<<53)+100004243)
239 return nvm, iface, nil
240
241 case reflect.Complex64:
242 nvm, iface := i.setComplex(typ, -(1<<24)+104243)
243 return nvm, iface, nil
244 case reflect.Complex128:
245 nvm, iface := i.setComplex(typ, -(1<<53)+100004243)
246 return nvm, iface, nil
247
248 case reflect.String:
249 nvm := reflect.New(typ).Elem()
250 nvm.SetString(fmt.Sprintf("<testdeep@anchor#%d>", i.nextIndex()))
251 return nvm, nvm.Interface(), nil
252
253 case reflect.Chan:
254 nvm := reflect.MakeChan(typ, 0)
255 return nvm, nvm.Pointer(), nil
256
257 case reflect.Map:
258 nvm := reflect.MakeMap(typ)
259 return nvm, nvm.Pointer(), nil
260
261 case reflect.Slice:
262 nvm := reflect.MakeSlice(typ, 0, 1) // cap=1 to avoid same ptr below
263 return nvm, nvm.Pointer(), nil
264
265 case reflect.Ptr:
266 nvm := reflect.New(typ.Elem())
267 return nvm, nvm.Pointer(), nil
268
269 case reflect.Struct:
270 // First pass for the exact type
271 for _, at := range AnchorableTypes {
272 if typ == at.typ {
273 nvm := at.builder.Call([]reflect.Value{reflect.ValueOf(i.nextIndex())})[0]
274 return nvm, nvm.Interface(), nil
275 }
276 }
277 // Second pass for convertible type
278 for _, at := range AnchorableTypes {
279 if at.typ.ConvertibleTo(typ) {
280 nvm := at.builder.Call([]reflect.Value{reflect.ValueOf(i.nextIndex())})[0].
281 Convert(typ)
282 return nvm, nvm.Interface(), nil
283 }
284 }
285 return reflect.Value{}, nil,
286 errors.New(typ.String() + " struct type is not supported as an anchor. Try AddAnchorableStructType")
287
288 default:
289 return reflect.Value{}, nil,
290 errors.New(typ.Kind().String() + " kind is not supported as an anchor")
291 }
292 }