-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalias_test.go
176 lines (137 loc) · 6.01 KB
/
alias_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
package ore
import (
"context"
"github.com/firasdarwish/ore/internal/interfaces"
"testing"
m "github.com/firasdarwish/ore/internal/models"
"github.com/stretchr/testify/assert"
)
func TestAliasResolverConflict(t *testing.T) {
clearAll()
RegisterFunc(Singleton, func(ctx context.Context) (m.IPerson, context.Context) {
return &m.Trader{Name: "Peter Singleton"}, ctx
})
RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Mary Transient"}, ctx
})
RegisterAlias[m.IPerson, *m.Trader]()
RegisterAlias[m.IPerson, *m.Broker]()
ctx := context.Background()
//The last registered IPerson is "Mary Transient", it would normally takes precedence.
//However, we registered a direct resolver for IPerson which is "Peter Singleton".
//So Ore won't treat IPerson as an alias and will resolve IPerson directly as "Peter Singleton"
person, ctx := Get[m.IPerson](ctx)
assert.Equal(t, person.(*m.Trader).Name, "Peter Singleton")
//GetList will return all possible IPerson whatever alias or from direct resolver.
personList, _ := GetList[m.IPerson](ctx)
assert.Equal(t, len(personList), 2)
}
func TestAliasOfAliasIsNotAllow(t *testing.T) {
clearAll()
RegisterFunc(Singleton, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Peter Singleton"}, ctx
})
RegisterFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Mary Transient"}, ctx
})
RegisterAlias[m.IPerson, *m.Trader]()
RegisterAlias[m.IPerson, *m.Broker]()
RegisterAlias[m.IHuman, m.IPerson]() //alias of alias
assert.Panics(t, func() {
_, _ = Get[m.IHuman](context.Background())
}, "implementation not found for type: IHuman")
humans, _ := GetList[m.IHuman](context.Background())
assert.Empty(t, humans)
}
func TestAliasWithDifferentScope(t *testing.T) {
clearAll()
module := "TestGetInterfaceAliasWithDifferentScope"
RegisterKeyedFunc(Transient, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Transient"}, ctx
}, module)
RegisterKeyedFunc(Singleton, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Singleton"}, ctx
}, module)
RegisterKeyedFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Scoped"}, ctx
}, module)
RegisterAlias[m.IPerson, *m.Broker]() //link m.IPerson to *m.Broker
ctx := context.Background()
person, ctx := GetKeyed[m.IPerson](ctx, module)
assert.Equal(t, person.(*m.Broker).Name, "Scoped")
personList, _ := GetKeyedList[m.IPerson](ctx, module)
assert.Equal(t, len(personList), 3)
}
func TestAliasIsScopedByKeys(t *testing.T) {
clearAll()
RegisterKeyedFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "Peter1"}, ctx
}, "module1")
RegisterKeyedFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "John1"}, ctx
}, "module1")
RegisterKeyedFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Mary1"}, ctx
}, "module1")
RegisterKeyedFunc(Scoped, func(ctx context.Context) (*m.Broker, context.Context) {
return &m.Broker{Name: "John2"}, ctx
}, "module2")
RegisterKeyedFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Mary2"}, ctx
}, "module2")
RegisterKeyedFunc(Scoped, func(ctx context.Context) (*m.Trader, context.Context) {
return &m.Trader{Name: "Mary3"}, ctx
}, "module3")
RegisterAlias[m.IPerson, *m.Trader]() //link m.IPerson to *m.Trader
RegisterAlias[m.IPerson, *m.Broker]() //link m.IPerson to *m.Broker
ctx := context.Background()
person1, ctx := GetKeyed[m.IPerson](ctx, "module1") // will return the m.Broker John
assert.Equal(t, person1.(*m.Broker).Name, "John1")
personList1, ctx := GetKeyedList[m.IPerson](ctx, "module1") // will return all registered m.Broker and m.Trader
assert.Equal(t, len(personList1), 3)
person2, ctx := GetKeyed[m.IPerson](ctx, "module2") // will return the m.Broker John
assert.Equal(t, person2.(*m.Broker).Name, "John2")
personList2, ctx := GetKeyedList[m.IPerson](ctx, "module2") // will return all registered m.Broker and m.Trader
assert.Equal(t, len(personList2), 2)
person3, ctx := GetKeyed[m.IPerson](ctx, "module3") // will return the m.Trader Mary
assert.Equal(t, person3.(*m.Trader).Name, "Mary3")
personList3, ctx := GetKeyedList[m.IPerson](ctx, "module3") // will return all registered m.Broker and m.Trader
assert.Equal(t, len(personList3), 1)
personListNoModule, _ := GetList[m.IPerson](ctx) // will return all registered m.Broker and m.Trader without keys
assert.Empty(t, personListNoModule)
}
func TestInvalidAlias(t *testing.T) {
assert.Panics(t, func() {
RegisterAlias[error, *m.Broker]() //register a struct (Broker) that does not implement interface (error)
}, "Broker does not implements error")
}
func TestGetGenericAlias(t *testing.T) {
for _, registrationType := range types {
container := NewContainer()
RegisterFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
return &m.SimpleCounterUint{}, ctx
})
RegisterAliasToContainer[interfaces.SomeCounterGeneric[uint], *m.SimpleCounterUint](container)
c, _ := GetFromContainer[interfaces.SomeCounterGeneric[uint]](container, context.Background())
c.Add(1)
c.Add(1)
assert.Equal(t, uint(2), c.GetCount())
}
}
func TestGetListGenericAlias(t *testing.T) {
for _, registrationType := range types {
container := NewContainer()
for i := 0; i < 3; i++ {
RegisterFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
return &m.SimpleCounterUint{}, ctx
})
}
RegisterAliasToContainer[interfaces.SomeCounterGeneric[uint], *m.SimpleCounterUint](container)
counters, _ := GetListFromContainer[interfaces.SomeCounterGeneric[uint]](container, context.Background())
assert.Equal(t, len(counters), 3)
c := counters[1]
c.Add(1)
c.Add(1)
assert.Equal(t, uint(2), c.GetCount())
}
}