go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 48d610aa4014389343bde4932bee97f6dfb6fe46
parent 0d23e0dbb57fe63b6626527fddc86649cfc20f8f
Author: lash <dev@holbrook.no>
Date:   Thu, 12 Sep 2024 19:33:24 +0100

Get >80% coverage on menuresource tests

Diffstat:
MMakefile | 3+--
Aresource/resource_test.go | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 122 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile @@ -31,6 +31,5 @@ doc: make -C doc cov: - #go test -cover ./... -coverprofile=cover.out - go test -cover ./vm/... -coverprofile=cover.out + go test -cover ./... -coverprofile=cover.out go tool cover -html=cover.out diff --git a/resource/resource_test.go b/resource/resource_test.go @@ -0,0 +1,121 @@ +package resource + +import ( + "context" + "errors" + "testing" +) + +func codeGet(ctx context.Context, nodeSym string) ([]byte, error) { + switch nodeSym { + case "bar": + return []byte("deafbeef"), nil + } + return nil, errors.New("unknown code") +} + +func menuGet(ctx context.Context, menuSym string) (string, error) { + switch menuSym { + case "baz": + return "xyzzy", nil + } + return "", errors.New("unknown code") + +} + +func templateGet(ctx context.Context, nodeSym string) (string, error) { + switch nodeSym { + case "tinkywinky": + return "inky pinky {{.foo}} blinky clyde", nil + } + return "", errors.New("unknown code") +} + +func entryFunc(ctx context.Context, nodeSym string, input []byte) (Result, error) { + return Result{ + Content: "42", + }, nil +} + +func funcGet(ctx context.Context, loadSym string) (EntryFunc, error) { + switch loadSym { + case "dipsy": + return entryFunc, nil + } + return nil, errors.New("unknown code") +} + + + +func TestMenuResourceSetters(t *testing.T) { + var err error + ctx := context.Background() + rs := NewMenuResource() + _, err = rs.GetCode(ctx, "foo") + if err == nil { + errors.New("expected error") + } + + rs.WithCodeGetter(codeGet) + _, err = rs.GetCode(ctx, "foo") + if err == nil { + errors.New("expected error") + } + + rs.WithCodeGetter(codeGet) + _, err = rs.GetCode(ctx, "bar") + if err != nil { + t.Fatal(err) + } + + _, err = rs.GetMenu(ctx, "bar") + if err == nil { + errors.New("expected error") + } + + rs.WithMenuGetter(menuGet) + _, err = rs.GetMenu(ctx, "bar") + if err == nil { + errors.New("expected error") + } + + rs.WithMenuGetter(menuGet) + _, err = rs.GetMenu(ctx, "baz") + if err != nil { + t.Fatal(err) + } + + _, err = rs.GetTemplate(ctx, "baz") + if err == nil { + errors.New("expected error") + } + + rs.WithTemplateGetter(templateGet) + _, err = rs.GetTemplate(ctx, "baz") + if err == nil { + errors.New("expected error") + } + + rs.WithTemplateGetter(templateGet) + _, err = rs.GetTemplate(ctx, "tinkywinky") + if err != nil { + t.Fatal(err) + } + + _, err = rs.FuncFor(ctx, "tinkywinky") + if err == nil { + errors.New("expected error") + } + + rs.WithEntryFuncGetter(funcGet) + _, err = rs.FuncFor(ctx, "tinkywinky") + if err == nil { + errors.New("expected error") + } + + rs.WithEntryFuncGetter(funcGet) + _, err = rs.FuncFor(ctx, "dipsy") + if err != nil { + t.Fatal(err) + } +}