-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_extractor_base.go
183 lines (160 loc) · 4.16 KB
/
value_extractor_base.go
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
package justest
import (
"reflect"
. "github.com/arikkfir/justest/internal"
)
var (
tTypePkgPath string
tTypeName string
)
//go:noinline
func init() {
rt := reflect.TypeOf((*T)(nil)).Elem()
tTypePkgPath = rt.PkgPath()
tTypeName = rt.Name()
}
type Extractor func(T, any) (any, bool)
type ValueExtractor map[reflect.Kind]Extractor
//go:noinline
func NewValueExtractor(defaultExtractor Extractor) ValueExtractor {
ve := make(map[reflect.Kind]Extractor)
ve[reflect.Invalid] = defaultExtractor
return ve
}
//go:noinline
func NewValueExtractorWithMap(defaultExtractor Extractor, extractors map[reflect.Kind]Extractor) ValueExtractor {
ve := NewValueExtractor(defaultExtractor)
for k, v := range extractors {
ve[k] = v
}
return ve
}
//go:noinline
func (ve ValueExtractor) ExtractValue(t T, actual any) (any, bool) {
GetHelper(t).Helper()
if actual == nil {
return nil, true
}
v := reflect.ValueOf(actual)
extractor, ok := ve[v.Kind()]
if !ok {
extractor, ok = ve[reflect.Invalid]
if !ok {
t.Fatalf("Default extractor is missing")
}
}
return extractor(t, actual)
}
//go:noinline
func (ve ValueExtractor) MustExtractValue(t T, actual any) any {
GetHelper(t).Helper()
value, found := ve.ExtractValue(t, actual)
if !found {
t.Fatalf("Value could not be extracted from an actual of type '%T': %+v", actual, actual)
}
return value
}
//go:noinline
func ExtractSameValue(t T, v any) (any, bool) {
GetHelper(t).Helper()
return v, true
}
//go:noinline
func ExtractorUnsupported(t T, v any) (any, bool) {
GetHelper(t).Helper()
t.Fatalf("Unsupported actual value: %+v", v)
panic("unreachable")
}
//go:noinline
func NewChannelExtractor(ve ValueExtractor, recurse bool) Extractor {
return func(t T, v any) (any, bool) {
GetHelper(t).Helper()
msg, ok := reflect.ValueOf(v).TryRecv()
if ok {
if recurse {
return ve.ExtractValue(t, msg.Interface())
} else {
return msg.Interface(), true
}
} else {
return nil, false
}
}
}
//go:noinline
func NewPointerExtractor(ve ValueExtractor, recurse bool) Extractor {
return func(t T, v any) (any, bool) {
GetHelper(t).Helper()
underlyingValue := reflect.ValueOf(v).Elem()
if recurse {
return ve.ExtractValue(t, underlyingValue.Interface())
} else {
return underlyingValue.Interface(), true
}
}
}
//go:noinline
func NewFuncExtractor(ve ValueExtractor, recurse bool) Extractor {
return func(t T, v any) (any, bool) {
GetHelper(t).Helper()
funcValue := reflect.ValueOf(v)
funcType := funcValue.Type()
// Prepare input parameters
var in []reflect.Value
switch funcType.NumIn() {
case 0:
in = nil
case 1:
arg0Type := funcType.In(0)
if arg0Type.PkgPath() == tTypePkgPath && arg0Type.Name() == tTypeName {
in = append(in, reflect.ValueOf(t))
} else {
t.Fatalf("Argument of functions with one argument must be of type TT, found: %+v", arg0Type.Name())
panic("unreachable")
}
default:
t.Fatalf("Functions with more than 1 input parameter are not supported in this context: %+v", v)
panic("unreachable")
}
// Prepare call & output extraction
switch funcType.NumOut() {
case 0:
funcValue.Call(in)
return nil, false
case 1:
returnValues := funcValue.Call(in)
if IsErrorType(funcType.Out(0)) {
if returnValues[0].IsNil() {
return nil, true
} else {
t.Fatalf("Function failed: %+v", returnValues[0].Interface())
panic("unreachable")
}
} else if recurse {
return ve.ExtractValue(t, returnValues[0].Interface())
} else {
return returnValues[0].Interface(), true
}
case 2:
if IsErrorType(funcType.Out(1)) {
returnValues := funcValue.Call(in)
if returnValues[1].IsNil() {
if recurse {
return ve.ExtractValue(t, returnValues[0].Interface())
} else {
return returnValues[0].Interface(), true
}
} else {
t.Fatalf("Function failed: %+v", returnValues[1].Interface())
panic("unreachable")
}
} else {
t.Fatalf("Functions with 2 return values must return 'error' as the 2nd return value: %+v", v)
panic("unreachable")
}
default:
t.Fatalf("Functions with %d return values are not supported: %+v", funcType.NumOut(), v)
panic("unreachable")
}
}
}