-
Notifications
You must be signed in to change notification settings - Fork 0
/
injector_test.go
131 lines (113 loc) · 2.6 KB
/
injector_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
package injector
import (
"fmt"
"strings"
"testing"
)
type A struct {
num int
s string
B *B
}
type B struct {
names []string
}
type Incrementor interface {
Inc(int) int
}
type MyImplementation struct {
}
func (m *MyImplementation) Inc(x int) int {
return x + 1
}
type MyStruct struct {
MyIncrementor Incrementor `inject:"injector.MyImplementation"`
}
type Foo struct {
Bar *Bar `inject:"injector.Bar"`
}
type Bar struct {
Foo *Foo `inject:"injector.Foo"`
}
func TestInjectStructPointer(t *testing.T) {
injector := NewEngine()
b := &B{names: []string{"a", "b", "c"}}
a := &A{num: 1, s: "s"}
if err := injector.Register(b, a); err != nil {
t.Errorf("failed to register: %v", err)
}
if err := injector.Inject(); err != nil {
t.Errorf("failed to inject: %v", err)
}
if a.B == nil {
t.Errorf("injection failed - property is nil")
}
fmt.Printf("%v", a)
}
func TestInjectInterfaceImplementation(t *testing.T) {
injector := NewEngine()
incrementor := &MyImplementation{}
toInject := &MyStruct{}
if err := injector.Register(incrementor, toInject); err != nil {
t.Errorf("failed to register: %v", err)
}
if err := injector.Inject(); err != nil {
t.Errorf("failed to inject: %v", err)
}
if toInject.MyIncrementor == nil {
t.Errorf("injection failed - property is nil")
}
actual := toInject.MyIncrementor.Inc(5)
if actual != 6 {
t.Errorf("Unexpected result: %d", actual)
}
fmt.Printf("%v", toInject)
}
func TestCyclicInjection(t *testing.T) {
injector := NewEngine()
foo := &Foo{}
bar := &Bar{}
if err := injector.Register(foo, bar); err != nil {
t.Errorf("failed to register: %v", err)
}
if err := injector.Inject(); err != nil {
t.Errorf("failed to inject: %v", err)
}
if foo.Bar == nil {
t.Errorf("injection failed - property is nil")
}
if bar.Foo == nil {
t.Errorf("injection failed - property is nil")
}
if bar.Foo.Bar != bar || foo.Bar.Foo != foo {
t.Errorf("injection failed - wrong reference")
}
fmt.Printf("%v", foo)
fmt.Printf("%v", bar)
}
func TestDoubleRegister(t *testing.T) {
injector := NewEngine()
a := &A{}
err := injector.Register(a, a)
if err == nil {
t.Errorf("expected to fail")
}
if !strings.Contains(err.Error(), "already a registered") {
t.Errorf("wrong error message")
}
}
func TestGetFields(t *testing.T) {
injector := NewEngine()
fields, err := injector.getFields(&A{})
if err != nil {
t.Fatalf("failed to get fields: %v", err)
}
if len(fields) != 3 {
t.Errorf("expected 3 fields")
}
for _, field := range fields {
fieldName := field.Name
fieldType := field.Type
fmt.Printf("Name: %s, Type: %s\n", fieldName, fieldType)
}
}