This repository was archived by the owner on Feb 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitializer_test.go
143 lines (129 loc) · 2.9 KB
/
initializer_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
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
package kinitx
import (
"reflect"
"testing"
"github.com/go-kata/kdone"
"github.com/go-kata/kerror"
)
type testInitializerT1 struct{}
type testInitializerT2 struct{}
type testInitializerT3 struct {
Obj1 *testInitializerT1
Obj2 *testInitializerT2
}
func TestInitializer__Struct(t *testing.T) {
ctor := MustNewInitializer(testInitializerT3{})
t.Logf("%+v %+v", ctor.Type(), ctor.Parameters())
obj1 := &testInitializerT1{}
obj2 := &testInitializerT2{}
o3, dtor, err := ctor.Create(reflect.ValueOf(obj1), reflect.ValueOf(obj2))
if err != nil {
t.Logf("%+v", err)
t.Fail()
return
}
defer dtor.MustDestroy()
obj3, ok := o3.Interface().(testInitializerT3)
if !ok {
t.Logf("%+v", o3)
t.Fail()
return
}
if obj3.Obj1 != obj1 || obj3.Obj2 != obj2 {
t.Fail()
return
}
}
func TestInitializer__StructPointer(t *testing.T) {
ctor := MustNewInitializer((*testInitializerT3)(nil))
t.Logf("%+v %+v", ctor.Type(), ctor.Parameters())
obj1 := &testInitializerT1{}
obj2 := &testInitializerT2{}
o3, dtor, err := ctor.Create(reflect.ValueOf(obj1), reflect.ValueOf(obj2))
if err != nil {
t.Logf("%+v", err)
t.Fail()
return
}
defer dtor.MustDestroy()
obj3, ok := o3.Interface().(*testInitializerT3)
if !ok {
t.Logf("%+v", o3)
t.Fail()
return
}
if obj3.Obj1 != obj1 || obj3.Obj2 != obj2 {
t.Fail()
return
}
}
func TestNewInitializer__Nil(t *testing.T) {
_, err := NewInitializer(nil)
t.Logf("%+v", err)
if kerror.ClassOf(err) != kerror.EViolation {
t.Fail()
return
}
}
func TestNewInitializer__WrongType(t *testing.T) {
_, err := NewInitializer(0)
t.Logf("%+v", err)
if kerror.ClassOf(err) != kerror.EViolation {
t.Fail()
return
}
}
func TestInitializer_Create__WrongArgumentNumber(t *testing.T) {
ctor := MustNewInitializer((*testInitializerT3)(nil))
t.Logf("%+v %+v", ctor.Type(), ctor.Parameters())
_, _, err := ctor.Create(reflect.ValueOf(&testInitializerT1{}))
t.Logf("%+v", err)
if kerror.ClassOf(err) != kerror.EViolation {
t.Fail()
return
}
}
func TestInitializer_Create__WrongArgumentType(t *testing.T) {
ctor := MustNewInitializer((*testInitializerT3)(nil))
t.Logf("%+v %+v", ctor.Type(), ctor.Parameters())
_, _, err := ctor.Create(reflect.ValueOf(&testInitializerT1{}), reflect.ValueOf(0))
t.Logf("%+v", err)
if kerror.ClassOf(err) != kerror.EViolation {
t.Fail()
return
}
}
func TestNilInitializer_Type(t *testing.T) {
if (*Initializer)(nil).Type() != nil {
t.Fail()
return
}
}
func TestNilInitializer_Parameters(t *testing.T) {
if (*Initializer)(nil).Parameters() != nil {
t.Fail()
return
}
}
func TestNilInitializer_Create(t *testing.T) {
obj, dtor, err := (*Initializer)(nil).Create()
if obj != reflect.ValueOf(nil) {
t.Fail()
return
}
f, ok := dtor.(kdone.DestructorFunc)
if !ok {
t.Fail()
return
}
if err := f.Destroy(); err != nil {
t.Logf("%+v", err)
t.Fail()
return
}
if err != nil {
t.Logf("%+v", err)
t.Fail()
return
}
}