-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroot_test.go
94 lines (80 loc) · 2.54 KB
/
root_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
// Copyright 2017 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package gioc
import (
"fmt"
"github.com/vlorc/gioc/binder"
"github.com/vlorc/gioc/factory"
"github.com/vlorc/gioc/register"
"github.com/vlorc/gioc/selector"
"github.com/vlorc/gioc/types"
"reflect"
"testing"
)
func test_register(t *testing.T, r types.Register) {
if nil == r {
t.Errorf("can't allocate a Register")
}
err := r.RegisterInstance(1, "id")
if nil != err {
t.Errorf("can't register a int error : %s", err.Error())
}
iface, err := r.AsSelector().FactoryOf(reflect.TypeOf((*int)(nil)).Elem(), "id").Instance(nil)
if nil != err {
t.Errorf("can't get a int error : %s", err.Error())
}
if iface != interface{}(1) {
t.Errorf("can't matching instance,were modified")
}
}
func test_registerFactory(t *testing.T, f types.RegisterFactory) {
if nil == f {
t.Errorf("can't allocate a RegisterFactory")
}
r, err := f.Instance(selector.NewTypeNameSelector())
if nil != err {
t.Errorf("can't allocate a Register error : %s", err.Error())
}
test_register(t, r)
}
func Test_Register(t *testing.T) {
test_register(t, register.NewRegister(selector.NewTypeSelector(binder.NewBinderFactory())))
}
func Test_RegisterFactory(t *testing.T) {
test_registerFactory(t, register.NewRegisterFactory())
}
func Test_Invoker(t *testing.T) {
root := NewRootContainer()
getKey := func(id int64, name *string) (r string) {
if nil != name {
r = fmt.Sprintf("id(%d) - name(%s)", id, *name)
} else {
r = fmt.Sprintf("id(%d)", id)
}
return
}
name := "angel"
root.AsRegister().RegisterInstance(&name)
var dependFactory types.DependencyFactory
var builderFactory types.BuilderFactory
var invokerFactory types.InvokerFactory
root.AsProvider().Assign(&dependFactory)
root.AsProvider().Assign(&builderFactory)
root.AsProvider().Assign(&invokerFactory)
dep, err := dependFactory.Instance(getKey)
if nil != err {
t.Errorf("can't allocate a depend error : %s", err.Error())
}
build, err := builderFactory.Instance(factory.NewParamFactory(dep.Length()), dep)
if nil != err {
t.Errorf("can't allocate a build error : %s", err.Error())
}
invoker, err := invokerFactory.Instance(getKey, build)
if nil != err {
t.Errorf("can't allocate a invoker error : %s", err.Error())
}
results := invoker.ApplyWith(root.AsProvider(), 1)
t.Log("getKey", results[0].Interface())
results = invoker.ApplyWith(root.AsProvider(), -2, nil)
t.Log("getKey", results[0].Interface())
}