vise/go/engine/engine_test.go

142 lines
2.6 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/cache"
2023-04-01 11:58:02 +02:00
"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(fs FsWrapper) one(sym string, ctx context.Context) (string, error) {
2023-04-01 15:47:03 +02:00
return "one", nil
}
func(fs FsWrapper) inky(sym string, 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) {
2023-04-02 10:07:53 +02:00
generateTestData(t)
2023-04-01 11:58:02 +02:00
ctx := context.TODO()
st := state.NewState(17)
rs := NewFsWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
en := NewEngine(Config{}, &st, &rs, ca)
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)
2023-04-12 09:30:35 +02:00
err = en.WriteResult(w, ctx)
2023-04-01 11:58:02 +02:00
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)
2023-04-12 09:30:35 +02:00
err = en.WriteResult(w, ctx)
if err != nil {
t.Fatal(err)
}
b = w.Bytes()
expect := `this is in foo
it has more lines
0:to foo
1:go bar
2:see long`
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
}
2023-04-06 13:41:36 +02:00
func TestEngineExecInvalidInput(t *testing.T) {
generateTestData(t)
ctx := context.TODO()
st := state.NewState(17)
2023-04-06 13:41:36 +02:00
rs := NewFsWrapper(dataDir, &st)
ca := cache.NewCache().WithCacheSize(1024)
en := NewEngine(Config{}, &st, &rs, ca)
2023-04-06 13:41:36 +02:00
err := en.Init("root", ctx)
if err != nil {
t.Fatal(err)
}
_, err = en.Exec([]byte("_foo"), ctx)
if err == nil {
t.Fatalf("expected fail on invalid input")
}
}