Files
Archduino/internal/module/registry_test.go

62 lines
1.7 KiB
Go

package module
import (
"context"
"testing"
)
type fakeModule struct {
id string
deps []string
}
func (f fakeModule) Meta() Meta { return Meta{ID: f.id, Name: f.id, Deps: f.deps} }
func (f fakeModule) Detect(context.Context) (State, error) { return nil, nil }
func (f fakeModule) Plan(context.Context, State) (Plan, error) { return Plan{}, nil }
func (f fakeModule) PlanForce(context.Context) (Plan, error) { return Plan{}, nil }
func (f fakeModule) Apply(context.Context, Plan, Reporter) error { return nil }
func (f fakeModule) Verify(context.Context) error { return nil }
func TestRegistry(t *testing.T) {
t.Run("register then get", func(t *testing.T) {
r := NewRegistry()
if err := r.Register(fakeModule{id: "a"}); err != nil {
t.Fatal(err)
}
m, ok := r.Get("a")
if !ok || m.Meta().ID != "a" {
t.Fatalf("want module a, got %v ok=%v", m, ok)
}
})
t.Run("duplicate register errors", func(t *testing.T) {
r := NewRegistry()
_ = r.Register(fakeModule{id: "a"})
if err := r.Register(fakeModule{id: "a"}); err == nil {
t.Fatal("expected duplicate error")
}
})
t.Run("empty id errors", func(t *testing.T) {
r := NewRegistry()
if err := r.Register(fakeModule{id: ""}); err == nil {
t.Fatal("expected empty-id error")
}
})
t.Run("all is sorted by id", func(t *testing.T) {
r := NewRegistry()
_ = r.Register(fakeModule{id: "b"})
_ = r.Register(fakeModule{id: "a"})
_ = r.Register(fakeModule{id: "c"})
all := r.All()
got := []string{all[0].Meta().ID, all[1].Meta().ID, all[2].Meta().ID}
want := []string{"a", "b", "c"}
for i := range got {
if got[i] != want[i] {
t.Fatalf("All() = %v, want %v", got, want)
}
}
})
}