|
| 1 | +package kod |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/go-kod/kod/internal/reflects" |
| 10 | + "github.com/go-kod/kod/internal/registry" |
| 11 | +) |
| 12 | + |
| 13 | +func TestValidateUnregisteredRef(t *testing.T) { |
| 14 | + type foo interface{} |
| 15 | + type fooImpl struct{ Ref[io.Reader] } |
| 16 | + regs := []*registry.Registration{ |
| 17 | + { |
| 18 | + Name: "foo", |
| 19 | + Iface: reflects.TypeFor[foo](), |
| 20 | + Impl: reflects.TypeFor[fooImpl](), |
| 21 | + }, |
| 22 | + } |
| 23 | + err := validateRegistrations(regs) |
| 24 | + if err == nil { |
| 25 | + t.Fatal("unexpected validateRegistrations success") |
| 26 | + } |
| 27 | + const want = "component io.Reader was not registered" |
| 28 | + if !strings.Contains(err.Error(), want) { |
| 29 | + t.Fatalf("validateRegistrations: got %q, want %q", err, want) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// TestValidateNoRegistrations tests that validateRegistrations succeeds on an |
| 34 | +// empty set of registrations. |
| 35 | +func TestValidateNoRegistrations(t *testing.T) { |
| 36 | + if err := validateRegistrations(nil); err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestMultipleRegistrations(t *testing.T) { |
| 42 | + type foo interface{} |
| 43 | + type fooImpl struct{ Ref[io.Reader] } |
| 44 | + regs := []*Registration{ |
| 45 | + { |
| 46 | + Name: "github.com/go-kod/kod/Main", |
| 47 | + Iface: reflect.TypeOf((*Main)(nil)).Elem(), |
| 48 | + Impl: reflect.TypeOf(fooImpl{}), |
| 49 | + Refs: `⟦48699770:KoDeDgE:github.com/go-kod/kod/Main→github.com/go-kod/kod/tests/graphcase/test1Controller⟧`, |
| 50 | + }, |
| 51 | + { |
| 52 | + Name: "github.com/go-kod/kod/Main", |
| 53 | + Iface: reflect.TypeOf((*foo)(nil)).Elem(), |
| 54 | + Impl: reflect.TypeOf(fooImpl{}), |
| 55 | + Refs: `⟦48699770:KoDeDgE:github.com/go-kod/kod/tests/graphcase/test1Controller→github.com/go-kod/kod/Main⟧`, |
| 56 | + }, |
| 57 | + } |
| 58 | + err := checkCircularDependency(regs) |
| 59 | + if err == nil { |
| 60 | + t.Fatal("unexpected checkCircularDependency success") |
| 61 | + } |
| 62 | + const want = "components [github.com/go-kod/kod/Main], error vertex already exists" |
| 63 | + if !strings.Contains(err.Error(), want) { |
| 64 | + t.Fatalf("checkCircularDependency: got %q, want %q", err, want) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestCycleRegistrations(t *testing.T) { |
| 69 | + type test1Controller interface{} |
| 70 | + type test1ControllerImpl struct{ Ref[io.Reader] } |
| 71 | + type mainImpl struct{ Ref[test1Controller] } |
| 72 | + regs := []*Registration{ |
| 73 | + { |
| 74 | + Name: "github.com/go-kod/kod/Main", |
| 75 | + Iface: reflect.TypeOf((*Main)(nil)).Elem(), |
| 76 | + Impl: reflect.TypeOf(mainImpl{}), |
| 77 | + Refs: `⟦48699770:KoDeDgE:github.com/go-kod/kod/Main→github.com/go-kod/kod/test1Controller⟧`, |
| 78 | + }, |
| 79 | + { |
| 80 | + Name: "github.com/go-kod/kod/test1Controller", |
| 81 | + Iface: reflect.TypeOf((*test1Controller)(nil)).Elem(), |
| 82 | + Impl: reflect.TypeOf(test1ControllerImpl{}), |
| 83 | + Refs: `⟦b8422d0e:KoDeDgE:github.com/go-kod/kod/test1Controller→github.com/go-kod/kod/Main⟧`, |
| 84 | + }, |
| 85 | + } |
| 86 | + err := checkCircularDependency(regs) |
| 87 | + if err == nil { |
| 88 | + t.Fatal("unexpected checkCircularDependency success") |
| 89 | + } |
| 90 | + const want = "components [github.com/go-kod/kod/test1Controller] and [github.com/go-kod/kod/Main] have cycle Ref" |
| 91 | + if !strings.Contains(err.Error(), want) { |
| 92 | + t.Fatalf("checkCircularDependency: got %q, want %q", err, want) |
| 93 | + } |
| 94 | +} |
0 commit comments