vise/go/engine/engine_test.go

98 lines
1.9 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"
"text/template"
"testing"
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/festive/state"
"git.defalsify.org/festive/resource"
)
type FsWrapper struct {
*resource.FsResource
2023-04-01 15:47:03 +02:00
st *state.State
2023-04-01 11:58:02 +02:00
}
2023-04-01 15:47:03 +02:00
func NewFsWrapper(path string, st *state.State, ctx context.Context) FsWrapper {
2023-04-01 11:58:02 +02:00
rs := resource.NewFsResource(path, ctx)
return FsWrapper {
&rs,
st,
}
}
func (r FsWrapper) RenderTemplate(sym string, values map[string]string) (string, error) {
v, err := r.GetTemplate(sym)
if err != nil {
return "", err
}
tp, err := template.New("tester").Option("missingkey=error").Parse(v)
if err != nil {
return "", err
}
b := bytes.NewBuffer([]byte{})
err = tp.Execute(b, values)
if err != nil {
return "", err
}
return b.String(), err
}
2023-04-01 15:47:03 +02:00
func(fs FsWrapper) one(ctx context.Context) (string, error) {
return "one", 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
}
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
}
func TestEngineInit(t *testing.T) {
st := state.NewState(17).WithCacheSize(1024)
dir := path.Join(testdataloader.GetBasePath(), "testdata")
ctx := context.TODO()
2023-04-01 15:47:03 +02:00
rs := NewFsWrapper(dir, &st, ctx)
en := NewEngine(&st, &rs)
2023-04-01 11:58:02 +02:00
err := en.Init(ctx)
if err != nil {
t.Fatal(err)
}
w := bytes.NewBuffer(nil)
err = en.WriteResult(w)
if err != nil {
t.Fatal(err)
}
b := w.Bytes()
if !bytes.Equal(b, []byte("hello world")) {
t.Fatalf("expected result 'hello world', got %v", b)
}
2023-04-02 00:56:02 +02:00
input := []byte("ooo")
2023-04-01 15:47:03 +02:00
err = en.Exec(input, ctx)
if err != nil {
t.Fatal(err)
}
r := st.Where()
if r != "bar" {
t.Fatalf("expected where-string 'bar', got %v", r)
}
2023-04-01 11:58:02 +02:00
}