-
Notifications
You must be signed in to change notification settings - Fork 9
/
container_error_test.go
97 lines (90 loc) · 2.13 KB
/
container_error_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
package iocgo
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContainer_DependenceNotFound(t *testing.T) {
defer Reset()
err := Register(NewFoobar)
assert.Nil(t, err)
//Register(func() Fooer { return &Foo{} })
err = Register(func() Barer { return &Bar{} })
assert.Nil(t, err)
var fb Foobarer
err = Resolve(&fb)
assert.NotNil(t, err)
t.Log(err)
}
func TestContainer_RegisterNotAFunctionError(t *testing.T) {
defer Reset()
err := Register(&Foo{})
assert.NotNil(t, err)
t.Log(err)
}
func TestContainer_RegisterInterfaceError(t *testing.T) {
defer Reset()
err := Register(NewFoobar)
assert.Nil(t, err)
var f Fooer
err = Register(NewFoo, Interface(f))
assert.NotNil(t, err)
t.Log(err)
//var b Barer
err = Register(NewBar, Interface(&f))
assert.NotNil(t, err)
t.Log(err)
var fb Foobarer
err = Resolve(&fb)
assert.NotNil(t, err)
t.Log(err)
}
func TestContainer_RegisterInstanceError(t *testing.T) {
defer Reset()
Register(NewFoobar)
Register(func() Fooer { return &Foo{} })
b := NewBar()
var bar Barer
err := RegisterInstance(bar, b)
assert.NotNil(t, err)
t.Log(err)
}
type Foobarer2 interface {
Say2(int, string)
}
func TestContainer_ResolveError(t *testing.T) {
defer Reset()
Register(NewFoobarWithMsg, Parameters(map[int]interface{}{2: "studyzy"}))
Register(func() Fooer { return &Foo{} })
Register(func() Barer { return &Bar{} })
var fb Foobarer
err := Resolve(fb, Arguments(map[int]interface{}{2: "arg2"})) //resolve use new argument to replace register parameters
assert.NotNil(t, err)
t.Log(err)
var fb2 Foobarer2
err = Resolve(&fb2)
assert.NotNil(t, err)
t.Log(err)
}
func NewFoobarError(f Fooer, b Barer) (Foobarer, error) {
if f == nil || b == nil {
return nil, errors.New("input nil")
}
return &Foobar{
foo: f,
bar: b,
}, nil
}
func TestContainer_ResolveReturnError(t *testing.T) {
defer Reset()
Register(NewFoobarError, Optional(0))
Register(func() Barer { return &Bar{} })
var fb Foobarer
err := Resolve(&fb)
assert.NotNil(t, err)
assert.Equal(t, "input nil", err.Error())
t.Log(err)
Register(func() Fooer { return &Foo{} })
err = Resolve(&fb)
assert.Nil(t, err)
}