-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocator_test.go
92 lines (72 loc) · 1.52 KB
/
locator_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
package locator
import "testing"
type TestServiceInterface interface {
Call() string
}
type TestServiceInterface2 interface {
Me() string
}
type TestService2 struct {
}
func (t *TestService2) Me() string {
return "test2"
}
type TestService struct {
}
func (t *TestService) Call() string {
return "test"
}
func newTestService2() TestServiceInterface2 {
return &TestService2{}
}
func newTestService1() TestServiceInterface {
return &TestService{}
}
func TestGetTypeName(t *testing.T) {
l := newServiceLocator()
name := l.getTypeName(newTestService1())
if name != "*locator.TestService" {
t.Errorf("expected locator.TestService, got %s", name)
}
t.Run("TestNameUniqueness", func(t *testing.T) {
s1 := struct {
Name string
Age int
}{
Name: "",
Age: 0,
}
s2 := struct {
Name2 string
Fake byte
}{
Name2: "",
Fake: 0,
}
name1 := l.getTypeName(s1)
name2 := l.getTypeName(s2)
if name1 == name2 {
t.Errorf("name not working, got %s and %s", name1, name2)
}
})
}
func TestRegisterAndResolve(t *testing.T) {
Register(newTestService1())
Register(newTestService2())
srv, err := Resolve[TestServiceInterface]()
if err != nil {
t.Errorf("expected no error, got %s", err)
}
if srv.Call() != "test" {
t.Errorf("expected test, got %s", srv.Call())
}
}
func TestListCount(t *testing.T) {
Register(newTestService1())
Register(newTestService1())
Register(newTestService2())
services := List()
if len(services) != 2 {
t.Errorf("expected 2 services, got %d", len(services))
}
}