-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathcontainerUnscopedGetter_test.go
185 lines (144 loc) · 3.89 KB
/
containerUnscopedGetter_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
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
184
185
package di
import (
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestUnscopedSafeGet(t *testing.T) {
b, _ := NewEnhancedBuilder()
var defReq, defSub *Def
defReq = &Def{
Name: "request-object",
Scope: Request,
Build: func(ctn Container) (interface{}, error) {
return &mockD{Closed: true}, nil
},
Is: NewIs(mockD{}),
}
b.Add(defReq)
defSub = &Def{
Name: "subrequest-object",
Scope: SubRequest,
Build: func(ctn Container) (interface{}, error) {
return &mockE{
D: ctn.Get("request-object").(*mockD),
}, nil
},
Is: NewIs(mockE{}),
}
b.Add(defSub)
app, _ := b.Build()
var obj, objBis interface{}
var err error
// success subrequest
obj, err = app.UnscopedSafeGet("subrequest-object")
require.Nil(t, err)
require.True(t, obj.(*mockE).D.Closed)
obj, err = app.UnscopedSafeGet(defSub)
require.Nil(t, err)
require.True(t, obj.(*mockE).D.Closed)
obj, err = app.UnscopedSafeGet(*defSub)
require.Nil(t, err)
require.True(t, obj.(*mockE).D.Closed)
obj, err = app.UnscopedSafeGet(defSub.Index())
require.Nil(t, err)
require.True(t, obj.(*mockE).D.Closed)
obj, err = app.UnscopedSafeGet(reflect.TypeOf(mockE{}))
require.Nil(t, err)
require.True(t, obj.(*mockE).D.Closed)
// success request
objBis, err = app.UnscopedSafeGet("request-object")
require.Nil(t, err)
require.True(t, objBis.(*mockD).Closed)
// check link between objects
obj.(*mockE).D.Closed = false
require.False(t, objBis.(*mockD).Closed)
// errors
_, err = app.UnscopedSafeGet("unknown")
require.NotNil(t, err, "should not be able to get an undefined object")
_, err = app.UnscopedSafeGet(-1)
require.NotNil(t, err)
_, err = app.UnscopedSafeGet(1000)
require.NotNil(t, err)
_, err = app.UnscopedSafeGet(Def{})
require.NotNil(t, err)
_, err = app.UnscopedSafeGet(&Def{})
require.NotNil(t, err)
_, err = app.UnscopedSafeGet(reflect.TypeOf(mockA{}))
require.NotNil(t, err)
_, err = app.SafeGet("subrequest-object")
require.NotNil(t, err, "should use UnscopedSafeGet instead of SafeGet")
// can call UnscopedSafeGet on sub-request too
req, _ := app.SubContainer()
subReq, _ := req.SubContainer()
_, err = subReq.UnscopedSafeGet("subrequest-object")
require.Nil(t, err)
// error if the container has been deleted
err = req.DeleteWithSubContainers()
require.Nil(t, err)
_, err = req.UnscopedSafeGet("request-object")
require.NotNil(t, err)
_, err = req.UnscopedSafeGet("subrequest-object")
require.NotNil(t, err)
}
func TestUnscoppedCreateChild(t *testing.T) {
b, _ := NewEnhancedBuilder()
app, _ := b.Build()
req, err := app.addUnscopedChild()
require.Nil(t, err)
subReq, err := req.addUnscopedChild()
require.Nil(t, err)
_, err = subReq.addUnscopedChild()
require.NotNil(t, err)
}
func TestUnscopedGet(t *testing.T) {
b, _ := NewEnhancedBuilder()
b.Add(&Def{
Name: "object",
Scope: Request,
Build: func(ctn Container) (interface{}, error) {
return 10, nil
},
})
b.Add(&Def{
Name: "object-close-err",
Scope: Request,
Build: func(ctn Container) (interface{}, error) {
return 10, errors.New("close error")
},
})
app, _ := b.Build()
object := app.UnscopedGet("object").(int)
require.Equal(t, 10, object)
require.Panics(t, func() {
app.UnscopedGet("object-close-err")
})
}
func TestUnscopedFill(t *testing.T) {
b, _ := NewEnhancedBuilder()
b.Add(&Def{
Name: "object",
Scope: Request,
Build: func(ctn Container) (interface{}, error) {
return 10, nil
},
})
b.Add(&Def{
Name: "object-close-err",
Scope: Request,
Build: func(ctn Container) (interface{}, error) {
return 10, errors.New("close error")
},
})
app, _ := b.Build()
var object int
var wrongType string
err := app.UnscopedFill("object", &object)
require.Nil(t, err)
require.Equal(t, 10, object)
err = app.UnscopedFill("object", &wrongType)
require.NotNil(t, err)
err = app.UnscopedFill("object-close-err", &object)
require.NotNil(t, err)
}