-
Notifications
You must be signed in to change notification settings - Fork 4
/
registry_test.go
124 lines (111 loc) · 3.65 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package kod
import (
"context"
"io"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-kod/kod/internal/registry"
)
func TestFill(t *testing.T) {
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)
}
}
func TestGetImpl(t *testing.T) {
k, err := newKod(context.Background())
require.NoError(t, err)
_, err = k.getImpl(context.Background(), reflect.TypeOf(struct{}{}))
assert.Error(t, err) // Should fail for unregistered type
}
func TestGetIntf(t *testing.T) {
k, err := newKod(context.Background())
require.NoError(t, err)
_, err = k.getIntf(context.Background(), reflect.TypeOf((*interface{})(nil)).Elem())
assert.Error(t, err) // Should fail for unregistered interface
}