vise/go/engine/engine_test.go

123 lines
2.2 KiB
Go
Raw Normal View History

2023-04-01 11:58:02 +02:00
package engine
import (
"bytes"
"context"
2023-04-01 15:47:03 +02:00
"fmt"
"io/ioutil"
2023-04-01 11:58:02 +02:00
"path"
"testing"
"git.defalsify.org/festive/resource"
2023-04-02 10:07:53 +02:00
"git.defalsify.org/festive/state"
"git.defalsify.org/festive/testdata"
)
var (
dataGenerated bool = false
dataDir string = testdata.DataDir
2023-04-01 11:58:02 +02:00
)
type FsWrapper struct {
*resource.FsResource
2023-04-01 15:47:03 +02:00
st *state.State
2023-04-01 11:58:02 +02:00
}
func NewFsWrapper(path string, st *state.State) FsWrapper {
rs := resource.NewFsResource(path)
2023-04-01 11:58:02 +02:00
return FsWrapper {
&rs,
st,
}
}
func (r FsWrapper) RenderTemplate(sym string, values map[string]string) (string, error) {
return resource.DefaultRenderTemplate(r, sym, values)
2023-04-01 11:58:02 +02:00
}
2023-04-01 15:47:03 +02:00
func(fs FsWrapper) one(ctx context.Context) (string, error) {
return "one", nil
}
func(fs FsWrapper) inky(ctx context.Context) (string, error) {
return "tinkywinky", nil
}
2023-04-01 11:58:02 +02:00
func(fs FsWrapper) FuncFor(sym string) (resource.EntryFunc, error) {
2023-04-01 15:47:03 +02:00
switch sym {
case "one":
return fs.one, nil
case "inky":
return fs.inky, nil
2023-04-01 15:47:03 +02:00
}
return nil, fmt.Errorf("function for %v not found", sym)
}
func(fs FsWrapper) GetCode(sym string) ([]byte, error) {
sym += ".bin"
fp := path.Join(fs.Path, sym)
r, err := ioutil.ReadFile(fp)
return r, err
2023-04-01 11:58:02 +02:00
}
2023-04-02 10:07:53 +02:00
func generateTestData(t *testing.T) {
if dataGenerated {
return
}
var err error
dataDir, err = testdata.Generate()
if err != nil {
t.Fatal(err)
}
}
2023-04-01 11:58:02 +02:00
func TestEngineInit(t *testing.T) {
st := state.NewState(17).WithCacheSize(1024)
2023-04-02 10:07:53 +02:00
generateTestData(t)
2023-04-01 11:58:02 +02:00
ctx := context.TODO()
rs := NewFsWrapper(dataDir, &st)
2023-04-01 15:47:03 +02:00
en := NewEngine(&st, &rs)
2023-04-02 10:07:53 +02:00
err := en.Init("root", ctx)
2023-04-01 11:58:02 +02:00
if err != nil {
t.Fatal(err)
}
w := bytes.NewBuffer(nil)
err = en.WriteResult(w)
if err != nil {
t.Fatal(err)
}
b := w.Bytes()
expect_str := `hello world
1:do the foo
2:go to the bar`
if !bytes.Equal(b, []byte(expect_str)) {
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect_str, b)
2023-04-01 11:58:02 +02:00
}
2023-04-02 10:07:53 +02:00
input := []byte("1")
2023-04-06 11:08:40 +02:00
_, err = en.Exec(input, ctx)
2023-04-01 15:47:03 +02:00
if err != nil {
t.Fatal(err)
}
r := st.Where()
2023-04-02 10:07:53 +02:00
if r != "foo" {
t.Fatalf("expected where-string 'foo', got %s", r)
}
w = bytes.NewBuffer(nil)
err = en.WriteResult(w)
if err != nil {
t.Fatal(err)
}
b = w.Bytes()
expect := `this is in foo
it has more lines
0:to foo
1:go bar`
if !bytes.Equal(b, []byte(expect)) {
t.Fatalf("expected\n\t%s\ngot:\n\t%s\n", expect, b)
2023-04-01 15:47:03 +02:00
}
2023-04-01 11:58:02 +02:00
}