-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathregistry_test.go
110 lines (100 loc) · 3.21 KB
/
registry_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
package kod
import (
"io"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/go-kod/kod/internal/registry"
)
func TestFill(t *testing.T) {
t.Run("case 1", func(t *testing.T) {
assert.NotNil(t, fillLog(nil, nil))
})
t.Run("case 2", func(t *testing.T) {
assert.NotNil(t, fillRefs(nil, nil, nil))
})
t.Run("case 3", func(t *testing.T) {
i := 0
assert.NotNil(t, fillRefs(&i, nil, nil))
})
}
func TestValidateUnregisteredRef(t *testing.T) {
type foo interface{}
type fooImpl struct{ Ref[io.Reader] }
regs := []*registry.Registration{
{
Name: "foo",
Interface: reflect.TypeFor[foo](),
Impl: reflect.TypeFor[fooImpl](),
},
}
_, err := processRegistrations(regs)
if err == nil {
t.Fatal("unexpected validateRegistrations success")
}
const want = "component io.Reader was not registered"
if !strings.Contains(err.Error(), want) {
t.Fatalf("validateRegistrations: got %q, want %q", err, want)
}
}
// TestValidateNoRegistrations tests that validateRegistrations succeeds on an
// empty set of registrations.
func TestValidateNoRegistrations(t *testing.T) {
if _, err := processRegistrations(nil); err != nil {
t.Fatal(err)
}
}
func TestMultipleRegistrations(t *testing.T) {
type foo interface{}
type fooImpl struct{ Ref[io.Reader] }
regs := []*Registration{
{
Name: "github.com/go-kod/kod/Main",
Interface: reflect.TypeOf((*Main)(nil)).Elem(),
Impl: reflect.TypeOf(fooImpl{}),
Refs: `⟦48699770:KoDeDgE:github.com/go-kod/kod/Main→github.com/go-kod/kod/tests/graphcase/test1Controller⟧`,
},
{
Name: "github.com/go-kod/kod/Main",
Interface: reflect.TypeOf((*foo)(nil)).Elem(),
Impl: reflect.TypeOf(fooImpl{}),
Refs: `⟦48699770:KoDeDgE:github.com/go-kod/kod/tests/graphcase/test1Controller→github.com/go-kod/kod/Main⟧`,
},
}
err := checkCircularDependency(regs)
if err == nil {
t.Fatal("unexpected checkCircularDependency success")
}
const want = "components [github.com/go-kod/kod/Main], error vertex already exists"
if !strings.Contains(err.Error(), want) {
t.Fatalf("checkCircularDependency: got %q, want %q", err, want)
}
}
func TestCycleRegistrations(t *testing.T) {
type test1Controller interface{}
type test1ControllerImpl struct{ Ref[io.Reader] }
type mainImpl struct{ Ref[test1Controller] }
regs := []*Registration{
{
Name: "github.com/go-kod/kod/Main",
Interface: reflect.TypeOf((*Main)(nil)).Elem(),
Impl: reflect.TypeOf(mainImpl{}),
Refs: `⟦48699770:KoDeDgE:github.com/go-kod/kod/Main→github.com/go-kod/kod/test1Controller⟧`,
},
{
Name: "github.com/go-kod/kod/test1Controller",
Interface: reflect.TypeOf((*test1Controller)(nil)).Elem(),
Impl: reflect.TypeOf(test1ControllerImpl{}),
Refs: `⟦b8422d0e:KoDeDgE:github.com/go-kod/kod/test1Controller→github.com/go-kod/kod/Main⟧`,
},
}
err := checkCircularDependency(regs)
if err == nil {
t.Fatal("unexpected checkCircularDependency success")
}
const want = "components [github.com/go-kod/kod/test1Controller] and [github.com/go-kod/kod/Main] have cycle Ref"
if !strings.Contains(err.Error(), want) {
t.Fatalf("checkCircularDependency: got %q, want %q", err, want)
}
}