File: anchor.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 (292 lines) | stat: -rw-r--r-- 7,214 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
// 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 anchors

import (
	"errors"
	"fmt"
	"math"
	"reflect"
	"sync"
)

type anchor struct {
	Anchor   reflect.Value // Anchor is the generated value used as anchor
	Operator reflect.Value // Operator is a td.TestDeep behind
}

// Info gathers all anchors information.
type Info struct {
	sync.Mutex
	index   int
	persist bool
	anchors map[any]anchor
}

// NewInfo returns a new instance of [*Info].
func NewInfo() *Info {
	return &Info{
		anchors: map[any]anchor{},
	}
}

// AddAnchor anchors a new operator op, with type typ then returns the
// anchor value.
func (i *Info) AddAnchor(typ reflect.Type, op reflect.Value) (reflect.Value, error) {
	i.Lock()
	defer i.Unlock()

	anc, key, err := i.build(typ)
	if err != nil {
		return reflect.Value{}, err
	}

	if i.anchors == nil {
		i.anchors = map[any]anchor{}
	}

	i.anchors[key] = anchor{
		Anchor:   anc,
		Operator: op,
	}

	return anc, nil
}

// DoAnchorsPersist returns true if anchors are persistent across tests.
func (i *Info) DoAnchorsPersist() bool {
	i.Lock()
	defer i.Unlock()
	return i.persist
}

// SetAnchorsPersist enables or disables anchors persistence.
func (i *Info) SetAnchorsPersist(persist bool) {
	i.Lock()
	defer i.Unlock()
	i.persist = persist
}

// ResetAnchors removes all anchors if persistence is disabled or
// force is true.
func (i *Info) ResetAnchors(force bool) {
	i.Lock()
	defer i.Unlock()

	if !i.persist || force {
		for k := range i.anchors {
			delete(i.anchors, k)
		}
		i.index = 0
	}
}

func (i *Info) nextIndex() (n int) {
	n = i.index
	i.index++
	return
}

// ResolveAnchor checks whether the passed value matches an anchored
// operator or not. If yes, this operator is returned with true. If
// no, the value is returned as is with false.
func (i *Info) ResolveAnchor(v reflect.Value) (reflect.Value, bool) {
	if i == nil || !v.CanInterface() {
		return v, false
	}
	// Shortcut
	i.Lock()
	la := len(i.anchors)
	i.Unlock()
	if la == 0 {
		return v, false
	}

	var key any
sw:
	switch v.Kind() {
	case reflect.Int,
		reflect.Int8,
		reflect.Int16,
		reflect.Int32,
		reflect.Int64,
		reflect.Uint,
		reflect.Uint8,
		reflect.Uint16,
		reflect.Uint32,
		reflect.Uint64,
		reflect.Uintptr,
		reflect.Float32,
		reflect.Float64,
		reflect.Complex64,
		reflect.Complex128,
		reflect.String:
		key = v.Interface()

	case reflect.Chan,
		reflect.Map,
		reflect.Slice,
		reflect.Ptr:
		key = v.Pointer()

	case reflect.Struct:
		typ := v.Type()
		if typ.Comparable() {
			// Check for anchorable types. No need of 2 passes here.
			for _, at := range AnchorableTypes {
				if typ == at.typ || at.typ.ConvertibleTo(typ) { // 1.17 ok as struct here
					key = v.Interface()
					break sw
				}
			}
		}
		fallthrough

	default:
		return v, false
	}

	i.Lock()
	defer i.Unlock()
	if anchor, ok := i.anchors[key]; ok {
		return anchor.Operator, true
	}
	return v, false
}

func (i *Info) setInt(typ reflect.Type, min int64) (reflect.Value, any) {
	nvm := reflect.New(typ).Elem()
	nvm.SetInt(min + int64(i.nextIndex()))
	return nvm, nvm.Interface()
}

func (i *Info) setUint(typ reflect.Type, max uint64) (reflect.Value, any) {
	nvm := reflect.New(typ).Elem()
	nvm.SetUint(max - uint64(i.nextIndex()))
	return nvm, nvm.Interface()
}

func (i *Info) setFloat(typ reflect.Type, min float64) (reflect.Value, any) {
	nvm := reflect.New(typ).Elem()
	nvm.SetFloat(min + float64(i.nextIndex()))
	return nvm, nvm.Interface()
}

func (i *Info) setComplex(typ reflect.Type, min float64) (reflect.Value, any) {
	nvm := reflect.New(typ).Elem()
	min += float64(i.nextIndex())
	nvm.SetComplex(complex(min, min))
	return nvm, nvm.Interface()
}

// build builds a new value of type "typ" and returns it under two
// forms:
//   - the new value itself as a reflect.Value;
//   - an any usable as a key in an AnchorsSet map.
//
// It returns an error if "typ" kind is not recognized or if it is a
// non-anchorable struct.
func (i *Info) build(typ reflect.Type) (reflect.Value, any, error) {
	// For each numeric type, anchor the operator on a number close to
	// the limit of this type, but not at the extreme limit to avoid
	// edge cases where these limits are used in real world and so avoid
	// collisions
	switch typ.Kind() {
	case reflect.Int:
		nvm, iface := i.setInt(typ, int64(^int(^uint(0)>>1))+1004293)
		return nvm, iface, nil
	case reflect.Int8:
		nvm, iface := i.setInt(typ, math.MinInt8+13)
		return nvm, iface, nil
	case reflect.Int16:
		nvm, iface := i.setInt(typ, math.MinInt16+1049)
		return nvm, iface, nil
	case reflect.Int32:
		nvm, iface := i.setInt(typ, math.MinInt32+1004293)
		return nvm, iface, nil
	case reflect.Int64:
		nvm, iface := i.setInt(typ, math.MinInt64+1000424443)
		return nvm, iface, nil

	case reflect.Uint:
		nvm, iface := i.setUint(typ, uint64(^uint(0))-1004293)
		return nvm, iface, nil
	case reflect.Uint8:
		nvm, iface := i.setUint(typ, math.MaxUint8-29)
		return nvm, iface, nil
	case reflect.Uint16:
		nvm, iface := i.setUint(typ, math.MaxUint16-2099)
		return nvm, iface, nil
	case reflect.Uint32:
		nvm, iface := i.setUint(typ, math.MaxUint32-2008571)
		return nvm, iface, nil
	case reflect.Uint64:
		nvm, iface := i.setUint(typ, math.MaxUint64-2000848901)
		return nvm, iface, nil
	case reflect.Uintptr:
		nvm, iface := i.setUint(typ, uint64(^uintptr(0))-2000848901)
		return nvm, iface, nil

	case reflect.Float32:
		nvm, iface := i.setFloat(typ, -(1<<24)+104243)
		return nvm, iface, nil
	case reflect.Float64:
		nvm, iface := i.setFloat(typ, -(1<<53)+100004243)
		return nvm, iface, nil

	case reflect.Complex64:
		nvm, iface := i.setComplex(typ, -(1<<24)+104243)
		return nvm, iface, nil
	case reflect.Complex128:
		nvm, iface := i.setComplex(typ, -(1<<53)+100004243)
		return nvm, iface, nil

	case reflect.String:
		nvm := reflect.New(typ).Elem()
		nvm.SetString(fmt.Sprintf("<testdeep@anchor#%d>", i.nextIndex()))
		return nvm, nvm.Interface(), nil

	case reflect.Chan:
		nvm := reflect.MakeChan(typ, 0)
		return nvm, nvm.Pointer(), nil

	case reflect.Map:
		nvm := reflect.MakeMap(typ)
		return nvm, nvm.Pointer(), nil

	case reflect.Slice:
		nvm := reflect.MakeSlice(typ, 0, 1) // cap=1 to avoid same ptr below
		return nvm, nvm.Pointer(), nil

	case reflect.Ptr:
		nvm := reflect.New(typ.Elem())
		return nvm, nvm.Pointer(), nil

	case reflect.Struct:
		// First pass for the exact type
		for _, at := range AnchorableTypes {
			if typ == at.typ {
				nvm := at.builder.Call([]reflect.Value{reflect.ValueOf(i.nextIndex())})[0]
				return nvm, nvm.Interface(), nil
			}
		}
		// Second pass for convertible type
		for _, at := range AnchorableTypes {
			if at.typ.ConvertibleTo(typ) {
				nvm := at.builder.Call([]reflect.Value{reflect.ValueOf(i.nextIndex())})[0].
					Convert(typ)
				return nvm, nvm.Interface(), nil
			}
		}
		return reflect.Value{}, nil,
			errors.New(typ.String() + " struct type is not supported as an anchor. Try AddAnchorableStructType")

	default:
		return reflect.Value{}, nil,
			errors.New(typ.Kind().String() + " kind is not supported as an anchor")
	}
}