-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconstruct.go
More file actions
243 lines (186 loc) · 5.2 KB
/
Copy pathconstruct.go
File metadata and controls
243 lines (186 loc) · 5.2 KB
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
package testo
import (
"fmt"
"reflect"
"slices"
"github.com/ozontech/testo/internal/reflectutil"
"github.com/ozontech/testo/internal/stack"
"github.com/ozontech/testo/testoplugin"
)
//nolint:cyclop,funlen // this is the core of the whole framework
func construct[T CommonT](
t TestingT,
parent *T,
fill func(t *testoT),
options ...testoplugin.Option,
) T {
t.Helper()
seed := testoT{
common: t,
testingT: t,
levelOptions: options,
}
if parent != nil {
seed.parent = (*parent).unwrap()
}
if fill != nil {
fill(&seed)
}
// Passed T type may be an interface.
// In that case, we can not initialize it, since
// we don't know its underlying concrete type.
//
// However, parent is always a concrete value,
// so, if present, we extract that type from it.
realType := reflect.TypeFor[T]()
if parent != nil {
rv := reflect.ValueOf(*parent)
realType = rv.Type()
}
// special case when T is *testo.T
if realType == reflect.TypeFor[*testoT]() {
return any(&seed).(T)
}
value := reflect.New(realType)
if !reflectutil.Fill(value) {
panic(fmt.Sprintf(
"testo: infinite type recursion detected for %s inside %s",
reflectutil.FindRecursiveType(realType),
realType,
))
}
if parent != nil {
seed.pluginOrder = (*parent).unwrap().pluginOrder
} else {
seen := make(map[reflect.Type]struct{})
collectPlugins(realType, seen, &seed.pluginOrder)
// the T type itself is not a plugin on its own.
seed.pluginOrder = slices.DeleteFunc(seed.pluginOrder, func(typ reflect.Type) bool {
return typ == realType
})
}
plugins := make(map[reflect.Type]testoplugin.Plugin, len(seed.pluginOrder))
for _, pluginType := range seed.pluginOrder {
var child testoplugin.Plugin
if pluginType == reflect.TypeFor[*testoT]() {
child = &seed
} else {
v := reflect.New(pluginType.Elem())
reflectutil.Fill(v)
child = v.Interface().(testoplugin.Plugin)
}
plugins[pluginType] = child
}
seed.plugins = plugins
specsStack := stack.New[typedPlugin]()
for _, pluginType := range seed.pluginOrder {
specsStack.Push(typedPlugin{
Plugin: seed.plugins[pluginType],
Type: pluginType,
})
setPlugins(reflect.ValueOf(seed.plugins[pluginType]), seed.plugins, &specsStack)
}
setPlugins(value, seed.plugins, &specsStack)
specs := make(map[reflect.Type]testoplugin.Spec, len(seed.plugins))
for {
p, ok := specsStack.Pop()
if !ok {
break
}
if _, ok := specs[p.Type]; ok {
continue
}
// For top-level tests the parent is a typed-nil instance of the
// plugin's own type: the interface is non-nil, so unconditional
// parent.(*MyPlugin) assertions keep working, and the concrete
// pointer is nil. See [testoplugin.Plugin].
var parentPlugin testoplugin.Plugin
if parent != nil {
parentPlugin = (*parent).unwrap().plugins[p.Type]
} else {
parentPlugin = reflect.New(p.Type).Elem().Interface().(testoplugin.Plugin)
}
specs[p.Type] = p.Plugin.Plugin(parentPlugin, seed.options()...)
}
// merge specs in the declaration order of plugins inside T so that
// equal-priority hooks and overrides run deterministically.
orderedSpecs := make([]testoplugin.Spec, 0, len(specs))
for _, pluginType := range seed.pluginOrder {
if spec, ok := specs[pluginType]; ok {
orderedSpecs = append(orderedSpecs, spec)
}
}
seed.spec = mergeSpecs(t, orderedSpecs...)
return value.Elem().Interface().(T)
}
type typedPlugin struct {
testoplugin.Plugin
Type reflect.Type
}
func setPlugins(
v reflect.Value,
plugins map[reflect.Type]testoplugin.Plugin,
specs *stack.Stack[typedPlugin],
) {
if plugin, ok := plugins[v.Type().Elem()]; ok {
elem := v.Elem()
if !elem.IsValid() {
panic(fmt.Sprintf("testo: invalid elem for %s", v.Type()))
}
if !elem.CanSet() {
// TODO(metafates): add path to the field so that it is clear where error happens
panic(fmt.Sprintf("testo: can't set value for %s", v.Type()))
}
elem.Set(reflect.ValueOf(plugin))
specs.Push(typedPlugin{
Plugin: elem.Interface().(testoplugin.Plugin),
Type: elem.Type(),
})
}
v = reflectutil.Elem(v)
if v.Kind() != reflect.Struct {
return
}
// special case - we do not go deeper than that.
if v.Type() == reflect.TypeFor[T]() {
return
}
for i := range v.NumField() {
field := v.Field(i)
if !v.Type().Field(i).IsExported() {
continue
}
if field.Kind() != reflect.Pointer {
panic(
fmt.Sprintf(
"testo: all exported fields in T and plugins must be pointers: field %s.%s has non-pointer type %s",
v.Type(),
v.Type().Field(i).Name,
field.Type(),
),
)
}
setPlugins(field.Addr(), plugins, specs)
}
}
var pluginInterfaceType = reflect.TypeFor[testoplugin.Plugin]()
// collectPlugins gathers plugin types in their declaration order:
// a depth-first walk over the exported fields of typ.
func collectPlugins(typ reflect.Type, seen map[reflect.Type]struct{}, order *[]reflect.Type) {
if typ.Implements(pluginInterfaceType) {
if _, ok := seen[typ]; !ok {
seen[typ] = struct{}{}
*order = append(*order, typ)
}
}
typ = reflectutil.Elem(typ)
if typ.Kind() != reflect.Struct {
return
}
for i := range typ.NumField() {
field := typ.Field(i)
if field.IsExported() {
collectPlugins(field.Type, seen, order)
}
}
}