-
Notifications
You must be signed in to change notification settings - Fork 13
/
options_test.go
79 lines (71 loc) · 1.88 KB
/
options_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
package di_test
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/require"
"github.com/defval/di"
)
func TestOptions(t *testing.T) {
t.Run("simple", func(t *testing.T) {
var loadedServer *http.Server
var resolvedServer *http.Server
server := &http.Server{}
mux := &http.ServeMux{}
c, err := di.New(
di.Options(
di.Provide(func(handler http.Handler) *http.Server {
server.Handler = handler
return server
}),
di.Provide(func() *http.ServeMux {
return mux
}, di.As(new(http.Handler))),
di.Invoke(func(server *http.Server) {
loadedServer = server
}),
di.Resolve(&resolvedServer),
),
)
require.NoError(t, err)
require.NotNil(t, c)
require.Equal(t, loadedServer, server)
require.Equal(t, loadedServer.Handler, mux)
require.Equal(t, resolvedServer, server)
})
t.Run("provide failed", func(t *testing.T) {
c, err := di.New(
di.Provide(func() {}),
)
require.Nil(t, c)
require.NotNil(t, err)
require.Contains(t, err.Error(), "options_test.go:")
require.Contains(t, err.Error(), ": invalid constructor signature, got func()")
})
t.Run("invoke failed", func(t *testing.T) {
c, err := di.New(
di.Invoke(func(string2 string) {}),
)
require.Nil(t, c)
require.Error(t, err)
require.Contains(t, err.Error(), "options_test.go:")
require.Contains(t, err.Error(), ": type string not exists in the container")
})
t.Run("invoke error return as is if not internal error", func(t *testing.T) {
var myError = errors.New("my error")
_, err := di.New(
di.Invoke(func() error {
return myError
}),
)
require.True(t, err == myError)
})
t.Run("resolve failed", func(t *testing.T) {
_, err := di.New(
di.Resolve(func() {}),
)
require.Error(t, err)
require.Contains(t, err.Error(), "options_test.go:")
require.Contains(t, err.Error(), ": target must be a pointer, got func()")
})
}