-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathbuilder_test.go
165 lines (125 loc) · 3.82 KB
/
builder_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
package di
import (
"reflect"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewBuilder(t *testing.T) {
var b *Builder
var err error
_, err = NewBuilder("app", "")
require.NotNil(t, err, "should not be able to create a Builder with an empty scope")
_, err = NewBuilder("app", "request", "app", "subrequest")
require.NotNil(t, err, "should not be able to create a Builder with two identical scopes")
b, err = NewBuilder("a", "b", "c")
require.Nil(t, err)
require.Equal(t, ScopeList{"a", "b", "c"}, b.Scopes())
b, err = NewBuilder()
require.Nil(t, err)
require.Equal(t, ScopeList{App, Request, SubRequest}, b.Scopes())
}
func TestBuilderDefinitions(t *testing.T) {
b, _ := NewBuilder()
b.Add([]Def{
{
Name: "o1",
Build: func(ctn Container) (interface{}, error) { return nil, nil },
},
{
Name: "o2",
Build: func(ctn Container) (interface{}, error) { return nil, nil },
},
}...)
defs := b.Definitions()
require.Len(t, defs, 2)
require.Equal(t, "o1", defs["o1"].Name)
require.Equal(t, "o2", defs["o2"].Name)
}
func TestBuilderIsDefined(t *testing.T) {
b, _ := NewBuilder()
b.Add(Def{
Name: "name",
Scope: App,
Build: func(ctn Container) (interface{}, error) { return nil, nil },
})
require.True(t, b.IsDefined("name"))
require.False(t, b.IsDefined("undefined"))
}
func TestBuilderServiceOverride(t *testing.T) {
b, _ := NewBuilder()
var err error
err = b.Add(Def{
Name: "name",
Build: func(ctn Container) (interface{}, error) {
return "first", nil
},
})
require.Nil(t, err)
err = b.Add(Def{
Name: "name",
Build: func(ctn Container) (interface{}, error) {
return "second", nil
},
})
require.Nil(t, err)
require.Equal(t, "second", b.Build().Get("name").(string))
}
func TestBuilderAddErrors(t *testing.T) {
b, _ := NewBuilder()
var err error
buildFunc := func(ctn Container) (interface{}, error) { return nil, nil }
err = b.Add(Def{Name: "name", Scope: App, Build: buildFunc})
require.Nil(t, err)
err = b.Add(Def{Name: "object", Scope: "undefined", Build: buildFunc})
require.NotNil(t, err, "should not be able to add a Def in an undefined scope")
err = b.Add(Def{Name: "", Scope: App, Build: buildFunc})
require.NotNil(t, err, "should not be able to add a Def if the name is empty")
err = b.Add(Def{Name: "object", Scope: App})
require.NotNil(t, err, "should not be able to add a Def if Build is empty")
err = b.Add(Def{Name: "object", Scope: App, Build: buildFunc})
require.Nil(t, err)
}
func TestBuilderSet(t *testing.T) {
b, _ := NewBuilder()
var err error
err = b.Set("", "error")
require.NotNil(t, err, "should not be able to set an object without a name")
err = b.Set("key", "value")
require.Nil(t, err)
ctn := b.Build()
require.Equal(t, "value", ctn.Get("key").(string))
}
func TestBuilderBuild(t *testing.T) {
ctn := (&Builder{}).Build()
require.True(t, ctn.core.closed, "should have at least one scope to use Build")
b, _ := NewBuilder()
b.Add([]Def{
{
Name: "o1",
Build: func(ctn Container) (interface{}, error) { return nil, nil },
},
{
Name: "o2",
Scope: Request,
Build: func(ctn Container) (interface{}, error) { return nil, nil },
},
}...)
app := b.Build()
require.Equal(t, App, app.Scope())
require.Len(t, app.Definitions(), 2)
require.Equal(t, App, app.Definitions()["o1"].Scope)
require.Equal(t, Request, app.Definitions()["o2"].Scope)
}
func TestBuilderGet(t *testing.T) {
var err error
b, _ := NewBuilder()
err = b.Add(*NewDefForType(mockA{}).SetName("nameA").SetIs(mockA{}))
require.Nil(t, err)
err = b.Add(*NewDefForType(&mockB{}).SetName("nameB").SetIs(&mockB{}))
require.Nil(t, err)
err = b.Add(*NewDefFor(mockC{SField: "ok"}).SetName("nameC").SetIs(mockC{}))
require.Nil(t, err)
app := b.Build()
a := app.Get(reflect.TypeOf(mockA{})).(mockA)
require.Equal(t, "ok", a.BField.CField.SField)
}