diff --git a/config/config.go b/config/config.go index 2ad90b0..fbdc7d9 100644 --- a/config/config.go +++ b/config/config.go @@ -63,6 +63,13 @@ func (cfg *Base) Add(child lib.Config) { cfg.children = append(cfg.children, child) } +func (cfg *Base) WithAction(pattern string, spec lib.ActionSpec) lib.Config { + act := ActionConfig(pattern, nil) + act.specs = append(act.specs, spec) + cfg.actions = append(cfg.actions, act) + return cfg +} + // implementation-specific methods and functions func (cfg *Base) WithStep(step lib.StepProc) *Base { @@ -83,6 +90,25 @@ func MakeBase(name string, starter lib.StartProc) *Base { } } +// action configuration + +type ActBase struct { + pattern string + specs []lib.ActionSpec +} + +func (act *ActBase) Pattern() string { + return act.pattern +} + +func (act *ActBase) Specs() []lib.ActionSpec { + return act.specs +} + +func ActionConfig(pattern string, specs []lib.ActionSpec) *ActBase { + return &ActBase{pattern, specs} +} + // overridable settings type Settings map[string]string diff --git a/lib/lib.go b/lib/lib.go index 28ddaca..93f0c46 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -25,6 +25,7 @@ type Config interface { Actions() []ActionConfig Children() []Config Add(Config) + WithAction(string, ActionSpec) Config } type Services map[string]Context diff --git a/testing/testing.go b/testing/testing.go index 4d52bc6..ea64d24 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -40,7 +40,7 @@ func SetUp(tbase *testing.T) *T { func SetUpApp(tbase *testing.T, cfg lib.Config) *T { t := SetUp(tbase) - t.Ctx = context.AppContext(cfg) + t.Ctx = context.AppContext(cfg).WithState(t) cfg.Starter()(t.Ctx) time.Sleep(100 * time.Millisecond) return t diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 3b150fb..d78e42f 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -21,12 +21,19 @@ func Config() lib.Config { Base: b("config", config.Start), ConfigFormat: "etc", }) - cfg.Add(config.Cfg{ - Base: b("test-receiver", core.Start), - }) + cfg.Add(b("test-receiver", core.Start)) + // WithAction("*", action.Base{ + // Handler: AH_Receiver, + // } return &cfg } +// register action handlers from ..._test.go here: + +var ( + AH_Receiver lib.ActionHandler +) + // collect here the names of fields that may be overridden via // explicit Override() or SCOPES_* environment settings. const ( diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 030c2a4..7ef1ce8 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -4,6 +4,7 @@ import ( tbase "testing" "git.sr.ht/~cco/go-scopes/app" + "git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib/message" "git.sr.ht/~cco/go-scopes/testing" "git.sr.ht/~cco/go-scopes/tests/etc" @@ -39,3 +40,15 @@ func SendTest(t *testing.T) { rcvr = message.SimpleAddress("test-receiver") rcvr.Send(ctx, msg) } + +// action handlers + +func Receiver(act lib.Action) bool { + t := act.Context().Parent().State().(*testing.T) + t.AssertEqual(act.Message().Action(), "demo") + return true +} + +func init() { + etc.AH_Receiver = Receiver +}