go-vise

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

commit dda338ffd68e5d36429bab3fbb1dceef54297fa7
parent 9a7c7d1aacaa47d29d2dd91d01d74680c82d0a0a
Author: lash <dev@holbrook.no>
Date:   Fri, 21 Apr 2023 21:29:24 +0100

Add engine language render test

Diffstat:
Mengine/engine_test.go | 43++++++++++++++++++++++++++++++++++++++++++-
Mresource/fs.go | 1+
Mtestdata/testdata.go | 24+++++++++++++++++++++++-
3 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/engine/engine_test.go b/engine/engine_test.go @@ -124,7 +124,8 @@ func TestEngineInit(t *testing.T) { b := w.Bytes() expect_str := `hello world 1:do the foo -2:go to the bar` +2:go to the bar +3:language template` if !bytes.Equal(b, []byte(expect_str)) { t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect_str, b) @@ -265,3 +266,43 @@ func TestLanguageSet(t *testing.T) { t.Fatalf("expected 'fett', got '%s'", r) } } + +func TestLanguageRender(t *testing.T) { + generateTestData(t) + ctx := context.TODO() + st := state.NewState(0) + rs := NewFsWrapper(dataDir, &st) + ca := cache.NewCache() + + en := NewEngine(Config{ + Root: "root", + }, &st, &rs, ca, ctx) + + var err error + _, err = en.Init(ctx) + if err != nil { + t.Fatal(err) + } + + + b := vm.NewLine(nil, vm.LOAD, []string{"set_lang"}, []byte{0x01, 0x00}, nil) + b = vm.NewLine(b, vm.MOVE, []string{"lang"}, nil, nil) + st.SetCode(b) + + _, err = en.Exec([]byte("nor"), ctx) + if err != nil { + t.Fatal(err) + } + + br := bytes.NewBuffer(nil) + _, err = en.WriteResult(br, ctx) + if err != nil { + t.Fatal(err) + } + expect := "dette endrer" + r := br.String() + if r[:len(expect)] != expect { + t.Fatalf("expected %s, got %s", expect, r[:len(expect)]) + } + +} diff --git a/resource/fs.go b/resource/fs.go @@ -41,6 +41,7 @@ func(fsr FsResource) GetTemplate(sym string, ctx context.Context) (string, error var err error r, err = ioutil.ReadFile(fpl) if err != nil { + Logg.DebugCtxf(ctx, "err", "err", err) if errors.Is(err, os.ErrNotExist) { if fpl != fp { r, err = ioutil.ReadFile(fp) diff --git a/testdata/testdata.go b/testdata/testdata.go @@ -53,9 +53,11 @@ func root() error { b := []byte{} b = vm.NewLine(b, vm.MOUT, []string{"1", "do the foo"}, nil, nil) b = vm.NewLine(b, vm.MOUT, []string{"2", "go to the bar"}, nil, nil) + b = vm.NewLine(b, vm.MOUT, []string{"3", "language template"}, nil, nil) b = vm.NewLine(b, vm.HALT, nil, nil, nil) b = vm.NewLine(b, vm.INCMP, []string{"1", "foo"}, nil, nil) b = vm.NewLine(b, vm.INCMP, []string{"2", "bar"}, nil, nil) + b = vm.NewLine(b, vm.INCMP, []string{"3", "lang"}, nil, nil) tpl := "hello world" @@ -147,13 +149,33 @@ func defaultCatch() error { return out("_catch", b, tpl, nil) } +func lang() error { + b := []byte{} + b = vm.NewLine(b, vm.MOUT, []string{"0", "back"}, nil, nil) + b = vm.NewLine(b, vm.LOAD, []string{"inky"}, []byte{20}, nil) + b = vm.NewLine(b, vm.MAP, []string{"inky"}, nil, nil) + b = vm.NewLine(b, vm.HALT, nil, nil, nil) + b = vm.NewLine(b, vm.INCMP, []string{"*", "_"}, nil, nil) + + tpl := "this changes with language {{.inky}}" + + err := out("lang", b, tpl, nil) + if err != nil { + return err + } + + tpl = "dette endrer med språket {{.inky}}" + fp := path.Join(DataDir, "lang_nor") + return os.WriteFile(fp, []byte(tpl), 0600) +} + func generate() error { err := os.MkdirAll(DataDir, 0755) if err != nil { return err } - fns := []genFunc{root, foo, bar, baz, long, defaultCatch} + fns := []genFunc{root, foo, bar, baz, long, lang, defaultCatch} for _, fn := range fns { err = fn() if err != nil {