-
Notifications
You must be signed in to change notification settings - Fork 1
/
constructor_test.go
93 lines (73 loc) · 2.03 KB
/
constructor_test.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
package kinit
import (
"reflect"
"github.com/go-kata/kdone"
)
// func(...) (T, kdone.Destructor, error)
type testConstructor struct {
t reflect.Type
f reflect.Value
in []reflect.Type
}
func newTestConstructor(x interface{}) *testConstructor {
ft := reflect.TypeOf(x)
c := &testConstructor{
t: ft.Out(0),
f: reflect.ValueOf(x),
}
numIn := ft.NumIn()
c.in = make([]reflect.Type, numIn)
for i := 0; i < numIn; i++ {
c.in[i] = ft.In(i)
}
return c
}
func (c *testConstructor) Type() reflect.Type {
return c.t
}
func (c *testConstructor) Parameters() []reflect.Type {
return c.in
}
func (c *testConstructor) Create(a ...reflect.Value) (reflect.Value, kdone.Destructor, error) {
out := c.f.Call(a)
obj := out[0]
var dtor kdone.Destructor = kdone.Noop
if v := out[1].Interface(); v != nil {
dtor = v.(kdone.Destructor)
}
var err error
if v := out[2].Interface(); v != nil {
err = v.(error)
}
return obj, dtor, err
}
type testConstructorWithBrokenType struct{}
func (testConstructorWithBrokenType) Type() reflect.Type {
return nil
}
func (testConstructorWithBrokenType) Parameters() []reflect.Type {
return nil
}
func (testConstructorWithBrokenType) Create(a ...reflect.Value) (reflect.Value, kdone.Destructor, error) {
return reflect.Value{}, nil, nil
}
type testConstructorWithBrokenParameters struct{}
func (testConstructorWithBrokenParameters) Type() reflect.Type {
return reflect.TypeOf(1)
}
func (testConstructorWithBrokenParameters) Parameters() []reflect.Type {
return []reflect.Type{nil}
}
func (testConstructorWithBrokenParameters) Create(a ...reflect.Value) (reflect.Value, kdone.Destructor, error) {
return reflect.Value{}, nil, nil
}
type testConstructorWithBrokenDestructor struct{}
func (testConstructorWithBrokenDestructor) Type() reflect.Type {
return reflect.TypeOf(1)
}
func (testConstructorWithBrokenDestructor) Parameters() []reflect.Type {
return nil
}
func (testConstructorWithBrokenDestructor) Create(a ...reflect.Value) (reflect.Value, kdone.Destructor, error) {
return reflect.Value{}, nil, nil
}