From 92cefca3cb7c0c2048c6c2f0f4e2c9a531490631 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sat, 3 Jun 2023 11:44:32 +0200 Subject: [PATCH] testing: use testing.Start as app starter --- common/common.go | 19 ++++++++++++++++++- testing/testing.go | 7 +++++++ tests/etc/etc.go | 3 ++- tests/scopes_test.go | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/common/common.go b/common/common.go index 7bfb3cf..78eeb81 100644 --- a/common/common.go +++ b/common/common.go @@ -9,11 +9,15 @@ type Config interface { type Services map[string]Context +type ContextState interface{} + type Context interface { Config() Config Parent() Context Services() Services ChildContext(Config) Context + State() ContextState + WithState(ContextState) Context } type StartFct = func(Context) @@ -24,6 +28,7 @@ type context struct { cfg Config parent Context children []Context + state ContextState } func (ctx *context) Config() Config { @@ -45,8 +50,20 @@ func (ctx *context) ChildContext(cfg Config) Context { return cctx } +func (ctx *context) State() ContextState { + return ctx.state +} + +func (ctx *context) WithState(state ContextState) Context { + ctx.state = state + return ctx +} + func makeCtx(cfg Config, parent Context) *context { - return &context{cfg, parent, nil} + return &context{ + cfg: cfg, + parent: parent, + } } // top-level application context diff --git a/testing/testing.go b/testing/testing.go index 3034374..826c7d4 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -6,9 +6,16 @@ import ( "regexp" "testing" + "git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes/common" ) +func Start(ctx common.Context) { + scopes.Start(ctx) +} + +// definitions + type Map map[string]interface{} type T struct { diff --git a/tests/etc/etc.go b/tests/etc/etc.go index 71d0734..3ff9bea 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -4,13 +4,14 @@ import ( "git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes/common" "git.sr.ht/~cco/go-scopes/config" + "git.sr.ht/~cco/go-scopes/testing" ) func Config() common.Config { ovr := Overrides().Use b := config.MakeBase cfg := scopes.Cfg{ - Base: b("dummy", scopes.Start), + Base: b("testing", testing.Start), Home: ovr(".", HOME), AppType: "standard", } diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 91f7982..4adedf6 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -15,7 +15,7 @@ func TestConfig(tb *tbase.T) { func ConfigTest(t *testing.T) { cfg := etc.Config() - t.AssertEqual(cfg.Name(), "dummy") + t.AssertEqual(cfg.Name(), "testing") appCfg := cfg.(*scopes.Cfg) t.AssertEqual(appCfg.Home, "tests") confCtx := t.Ctx.Services()["config"]