-
Notifications
You must be signed in to change notification settings - Fork 0
/
binder_test.go
132 lines (100 loc) · 2.91 KB
/
binder_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
package inversify
import (
"errors"
"sync"
"testing"
"github.com/stretchr/testify/suite"
)
const resolvedValue = "resolved value"
type BindingTestSuite struct {
suite.Suite
binding *Binding
}
func defaultDepVal(val Any) FactoryFunc { return func() (Any, error) { return val, nil } }
func defaultDepError(err error) FactoryFunc { return func() (Any, error) { return nil, err } }
func (t *BindingTestSuite) BeforeTest(suiteName, testName string) {
t.binding = &Binding{sync.Once{}, nil, NAny{}, NAny{}}
t.binding.resolves = NAny{defaultDepVal("resolved"), defaultDepVal(" value")}
}
func (t *BindingTestSuite) TestBindValue() {
binding := t.binding
binding.To(resolvedValue)
val, err := binding.factory()
t.Equal(resolvedValue, val)
t.NoError(err)
}
func (t *BindingTestSuite) TestBindAbstractFactory() {
binding := t.binding
binding.ToFactory(func(a, b Any) (Any, error) {
return a.(string) + b.(string), nil
}, testDep1, testDep2)
val, err := binding.factory()
t.Equal(resolvedValue, val)
t.NoError(err)
}
func (t *BindingTestSuite) TestBindAbstractFactoryError() {
binding := t.binding
binding.ToFactory(func(a, b Any) (Any, error) {
return a.(string) + b.(string), nil
}, testDep1, testDep2)
binding.resolves = NAny{defaultDepVal("resolved"), defaultDepError(errors.New("error"))}
val, err := binding.factory()
t.Nil(val)
t.Error(err)
}
func (t *BindingTestSuite) TestBindTypedFactorySimple() {
binding := t.binding
counter := 0
binding.ToTypedFactory(func(a, b string) (string, error) {
counter++
return a + b, nil
}, 1, 2)
val, err := binding.factory()
val, err = binding.factory()
t.Equal(resolvedValue, val)
t.NoError(err)
t.Equal(2, counter)
}
type interfaceValue interface {
get() string
}
type structValue1 struct{}
func (v structValue1) get() string {
return "resolved"
}
func (t *BindingTestSuite) TestBindTypedFactoryAndPointers() {
binding := t.binding
binding.ToTypedFactory(func(a interfaceValue, b *string) (string, error) {
return a.get() + *b, nil
}, testDep1, testDep2)
valueString := " value"
binding.resolves = NAny{defaultDepVal(&structValue1{}), defaultDepVal(&valueString)}
val, err := binding.factory()
t.Equal(resolvedValue, val)
t.NoError(err)
}
func (t *BindingTestSuite) TestBindTypedFactorySingleton() {
binding := t.binding
counter := 0
binding.ToTypedFactory(func(a, b string) (string, error) {
counter++
return a + b, nil
}, testDep1, testDep2).InSingletonScope()
val, err := binding.factory()
val, err = binding.factory()
t.Equal(resolvedValue, val)
t.NoError(err)
t.Equal(1, counter)
}
func (t *BindingTestSuite) TestBindTypedFactoryNoDeps() {
binding := &Binding{sync.Once{}, nil, NAny{}, NAny{}}
binding.ToTypedFactory(func() (string, error) {
return "no dependencies", nil
})
val, err := binding.factory()
t.Equal("no dependencies", val)
t.NoError(err)
}
func TestBindingSuite(t *testing.T) {
suite.Run(t, new(BindingTestSuite))
}