go-vise

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

commit ce50317bc4d660e2327a3b6ae9447cfed595d688
parent 1ebff7b4d1182803d51d6d581b0dce6793c038ea
Author: lash <dev@holbrook.no>
Date:   Wed, 11 Sep 2024 16:55:22 +0100

Reach >80% engine test coverage

Diffstat:
MMakefile | 2+-
Mengine/db.go | 4+++-
Mengine/db_test.go | 152+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mstate/state_test.go | 22+++++++++++++++++++---
4 files changed, 175 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -32,5 +32,5 @@ doc: cov: #go test -cover ./... -coverprofile=cover.out - go test -cover ./state/... -coverprofile=cover.out + go test -cover ./engine/... -coverprofile=cover.out go tool cover -html=cover.out diff --git a/engine/db.go b/engine/db.go @@ -390,7 +390,9 @@ func(en *DefaultEngine) Init(ctx context.Context) (bool, error) { if err != nil { return false, err } - + if en.dbg != nil { + en.dbg.Break(en.st, en.ca) + } logg.DebugCtxf(ctx, "end new init VM run", "code", b) en.st.SetCode(b) err = en.st.SetInput(inSave) diff --git a/engine/db_test.go b/engine/db_test.go @@ -1,15 +1,18 @@ package engine import ( + "bytes" "context" "fmt" "io" "os" + "strings" "testing" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/state" + "git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/vm" memdb "git.defalsify.org/vise.git/db/mem" ) @@ -29,6 +32,12 @@ func codeGet(ctx context.Context, s string) ([]byte, error) { case "root": b = vm.NewLine(nil, vm.HALT, nil, nil, nil) b = vm.NewLine(b, vm.LOAD, []string{"foo"}, []byte{0x0}, nil) + case "tinkywinky": + b = vm.NewLine(nil, vm.MOVE, []string{"dipsy"}, nil, nil) + b = vm.NewLine(b, vm.LOAD, []string{"foo"}, []byte{0x0}, nil) + case "dipsy": + b = vm.NewLine(nil, vm.HALT, nil, nil, nil) + b = vm.NewLine(b, vm.LOAD, []string{"foo"}, []byte{0x0}, nil) default: err = fmt.Errorf("unknown code symbol '%s'", s) } @@ -60,6 +69,115 @@ func TestDbEngineMinimal(t *testing.T) { } } +func TestDbEngineNoResource(t *testing.T) { + cfg := Config{} + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + NewEngine(cfg, nil) +} + +func TestDbEngineStateNil(t *testing.T) { + cfg := Config{} + rs := resource.NewMenuResource() + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + NewEngine(cfg, rs).WithState(nil) +} + +func TestDbEngineCacheNil(t *testing.T) { + cfg := Config{} + rs := resource.NewMenuResource() + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + NewEngine(cfg, rs).WithMemory(nil) +} + +func TestDbEnginePersisterNil(t *testing.T) { + cfg := Config{} + rs := resource.NewMenuResource() + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + NewEngine(cfg, rs).WithPersister(nil) +} + +func TestDbEngineFirstNil(t *testing.T) { + cfg := Config{} + rs := resource.NewMenuResource() + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + NewEngine(cfg, rs).WithFirst(nil) +} + +func TestDbEngineStateDup(t *testing.T) { + cfg := Config{} + rs := resource.NewMenuResource() + st := state.NewState(0) + en := NewEngine(cfg, rs).WithState(st) + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + en.WithState(st) +} + + +func TestDbEngineCacheDup(t *testing.T) { + cfg := Config{} + rs := resource.NewMenuResource() + ca := cache.NewCache() + en := NewEngine(cfg, rs).WithMemory(ca) + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + en.WithMemory(ca) +} + +func TestDbEnginePersisterDup(t *testing.T) { + ctx := context.Background() + cfg := Config{} + rs := resource.NewMenuResource() + store := memdb.NewMemDb() + store.Connect(ctx, "") + pe := persist.NewPersister(store) + en := NewEngine(cfg, rs).WithPersister(pe) + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + en.WithPersister(pe) +} + +func TestDbEngineFirstDup(t *testing.T) { + cfg := Config{} + rs := resource.NewMenuResource() + en := NewEngine(cfg, rs).WithFirst(flagSet) + defer func() { + if r := recover(); r == nil { + t.Fatal("did not panic") + } + }() + en.WithFirst(flagSet) +} + func TestDbEngineRoot(t *testing.T) { nul := getNull() defer nul.Close() @@ -134,3 +252,37 @@ func TestDbEnginePersist(t *testing.T) { t.Fatal(err) } } + +func TestDbEngineDebug(t *testing.T) { + w := bytes.NewBuffer(nil) + ctx := context.Background() + cfg := Config{ + Root: "tinkywinky", + FlagCount: 1, + } + rs := resource.NewMenuResource() + rs = rs.WithCodeGetter(codeGet) + rs.AddLocalFunc("foo", flagSet) + dbg := NewSimpleDebug(w) + en := NewEngine(cfg, rs).WithDebug(dbg) + c, err := en.Init(ctx) + if err != nil { + t.Fatal(err) + } + if !c { + t.Fatal("expected true") + } + if len(w.Bytes()) == 0 { + t.Fatal("expected non-empty debug") + } +} + +func TestDbConfigString(t *testing.T) { + cfg := Config{ + Root: "tinkywinky", + } + s := cfg.String() + if !strings.Contains(s, "tinky") { + t.Fatalf("expected contains 'tinky', got: '%s'", s) + } +} diff --git a/state/state_test.go b/state/state_test.go @@ -428,11 +428,28 @@ func TestStateReset(t *testing.T) { st.Next() st.Next() st.SetInput([]byte("xyzzy")) - err := st.Restart() + + r, err := st.Top() + if r { + t.Fatal("expected false") + } + + // check that cloneempty doesnt affect the position of the original + o := st.CloneEmpty() + r, err = o.Top() + if err == nil { + t.Fatal("expected error") + } + r, err = st.Top() + if r { + t.Fatal("expected false") + } + + err = st.Restart() if err != nil { t.Fatal(err) } - r, err := st.Top() + r, err = st.Top() if err != nil { t.Fatal(err) } @@ -449,7 +466,6 @@ func TestStateReset(t *testing.T) { if v > 0 { t.Fatalf("expected 0, got %d", v) } - } func TestStateLanguage(t *testing.T) {