-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconigo_test.go
119 lines (96 loc) · 2.55 KB
/
conigo_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
package conigo_test
import (
"errors"
"testing"
"github.com/KoharaKazuya/conigo"
)
type a string
type b string
func TestProvideValidation(t *testing.T) {
cases := []struct {
input interface{}
success bool
}{
{func() int { return 0 }, true},
{func() (int, error) { return 0, nil }, true},
{func(int) int { return 0 }, true},
{func(int, string) int { return 0 }, true},
{1, false},
{func() {}, false},
{func() (int, string, error) { return 0, "", nil }, false},
{func() (int, string) { return 0, "" }, false},
}
for i, c := range cases {
container := conigo.New()
err := container.Provide(c.input)
if (err == nil) != c.success {
t.Errorf("[case%03d] got: %v\ncase: %#v", i, err, c)
}
}
}
func TestResolveValidation(t *testing.T) {
cases := []struct {
input interface{}
success bool
}{
{func() {}, true},
{func() error { return nil }, true},
{func(int, string) {}, true},
{func(int, string) error { return nil }, true},
{1, false},
{func() int { return 0 }, false},
{func() (int, error) { return 0, nil }, false},
{func() (error, int) { return nil, 0 }, false},
}
for i, c := range cases {
container := conigo.New()
container.Provide(func() int { return 0 })
container.Provide(func() string { return "" })
err := container.Resolve(c.input)
if (err == nil) != c.success {
t.Errorf("[case%03d] got: %v\ncase: %#v", i, err, c)
}
}
}
func TestNoProviderError(t *testing.T) {
container := conigo.New()
container.Provide(func() a { return "a" })
err := container.Resolve(func(_ b) {})
if err == nil {
t.Error("no provider error pass through")
}
}
func TestResolverError(t *testing.T) {
container := conigo.New()
err := container.Resolve(func() error {
return errors.New("Test Error")
})
if err == nil {
t.Error("resolver error ignored")
}
}
func TestDoubleRegistrationError(t *testing.T) {
container := conigo.New()
container.Provide(func() int { return 0 })
err := container.Provide(func() int { return 1 })
if err == nil {
t.Error("double registration error pass through")
}
}
func TestCyclicDependencyDetection(t *testing.T) {
container := conigo.New()
container.Provide(func(_ b) a { return "a" })
container.Provide(func(_ a) b { return "b" })
err := container.Resolve(func(_ b) {})
if err == nil {
t.Error("cyclic dependency detection pass through")
}
}
func TestNilError(t *testing.T) {
container := conigo.New()
container.Provide(func() (a, error) { return "a", nil })
err := container.Resolve(func(_ a) {})
if err != nil {
t.Error("resolve a without error failed")
}
}