2023-03-31 11:52:04 +02:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2023-03-31 17:12:14 +02:00
|
|
|
"bytes"
|
2023-03-31 11:52:04 +02:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-03-31 17:12:14 +02:00
|
|
|
"log"
|
2023-03-31 11:52:04 +02:00
|
|
|
"testing"
|
2023-03-31 11:59:55 +02:00
|
|
|
"text/template"
|
2023-03-31 11:52:04 +02:00
|
|
|
|
2023-03-31 11:59:55 +02:00
|
|
|
"git.defalsify.org/festive/resource"
|
2023-03-31 20:24:30 +02:00
|
|
|
"git.defalsify.org/festive/router"
|
2023-03-31 11:52:04 +02:00
|
|
|
"git.defalsify.org/festive/state"
|
|
|
|
)
|
|
|
|
|
2023-03-31 17:12:14 +02:00
|
|
|
var dynVal = "three"
|
|
|
|
|
2023-03-31 11:52:04 +02:00
|
|
|
type TestResource struct {
|
2023-03-31 19:17:43 +02:00
|
|
|
state *state.State
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
2023-03-31 11:59:55 +02:00
|
|
|
func getOne(input []byte, ctx context.Context) (string, error) {
|
|
|
|
return "one", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTwo(input []byte, ctx context.Context) (string, error) {
|
|
|
|
return "two", nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 17:12:14 +02:00
|
|
|
func getDyn(input []byte, ctx context.Context) (string, error) {
|
|
|
|
return dynVal, nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 19:17:43 +02:00
|
|
|
type TestStatefulResolver struct {
|
|
|
|
state *state.State
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (r *TestResource) getEachArg(input []byte, ctx context.Context) (string, error) {
|
|
|
|
return r.state.PopArg()
|
|
|
|
}
|
|
|
|
|
2023-03-31 11:52:04 +02:00
|
|
|
func (r *TestResource) Get(sym string) (string, error) {
|
|
|
|
switch sym {
|
|
|
|
case "foo":
|
|
|
|
return "inky pinky blinky clyde", nil
|
|
|
|
case "bar":
|
2023-03-31 11:59:55 +02:00
|
|
|
return "inky pinky {{.one}} blinky {{.two}} clyde", nil
|
2023-03-31 17:12:14 +02:00
|
|
|
case "baz":
|
|
|
|
return "inky pinky {{.baz}} blinky clyde", nil
|
|
|
|
case "three":
|
|
|
|
return "{{.one}} inky pinky {{.three}} blinky clyde {{.two}}", nil
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
return "", fmt.Errorf("unknown symbol %s", sym)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TestResource) Render(sym string, values map[string]string) (string, error) {
|
|
|
|
v, err := r.Get(sym)
|
2023-03-31 11:59:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-03-31 14:24:14 +02:00
|
|
|
tp, err := template.New("tester").Option("missingkey=error").Parse(v)
|
2023-03-31 11:59:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := bytes.NewBuffer([]byte{})
|
2023-03-31 14:24:14 +02:00
|
|
|
err = tp.Execute(b, values)
|
2023-03-31 11:59:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) {
|
|
|
|
switch sym {
|
|
|
|
case "one":
|
|
|
|
return getOne, nil
|
|
|
|
case "two":
|
|
|
|
return getTwo, nil
|
2023-03-31 17:12:14 +02:00
|
|
|
case "dyn":
|
|
|
|
return getDyn, nil
|
2023-03-31 19:17:43 +02:00
|
|
|
case "arg":
|
|
|
|
return r.getEachArg, nil
|
2023-03-31 11:59:55 +02:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("invalid function: '%s'", sym)
|
2023-03-31 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
2023-03-31 14:06:59 +02:00
|
|
|
st := state.NewState(5)
|
2023-03-31 11:52:04 +02:00
|
|
|
rs := TestResource{}
|
2023-03-31 16:03:54 +02:00
|
|
|
b := []byte{0x00, 0x01, 0x03}
|
|
|
|
b = append(b, []byte("foo")...)
|
|
|
|
r, _, err := Run(b, st, &rs, context.TODO())
|
2023-03-31 11:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error on valid opcode: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b = []byte{0x01, 0x02}
|
2023-03-31 16:03:54 +02:00
|
|
|
r, _, err = Run(b, st, &rs, context.TODO())
|
2023-03-31 11:52:04 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("no error on invalid opcode")
|
|
|
|
}
|
|
|
|
_ = r
|
|
|
|
}
|
2023-03-31 11:59:55 +02:00
|
|
|
|
2023-03-31 19:17:43 +02:00
|
|
|
func TestRunLoadRender(t *testing.T) {
|
2023-03-31 14:06:59 +02:00
|
|
|
st := state.NewState(5)
|
2023-03-31 16:03:54 +02:00
|
|
|
st.Down("barbarbar")
|
2023-03-31 11:59:55 +02:00
|
|
|
rs := TestResource{}
|
|
|
|
sym := "one"
|
|
|
|
ins := append([]byte{uint8(len(sym))}, []byte(sym)...)
|
2023-03-31 16:03:54 +02:00
|
|
|
ins = append(ins, 0x0a)
|
2023-03-31 11:59:55 +02:00
|
|
|
var err error
|
2023-03-31 16:03:54 +02:00
|
|
|
st, _, err = RunLoad(ins, st, &rs, context.TODO())
|
2023-03-31 11:59:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
m, err := st.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r, err := rs.Render("foo", m)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
expect := "inky pinky blinky clyde"
|
|
|
|
if r != expect {
|
|
|
|
t.Errorf("Expected %v, got %v", []byte(expect), []byte(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err = rs.Render("bar", m)
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error for render of bar: %v" ,err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sym = "two"
|
|
|
|
ins = append([]byte{uint8(len(sym))}, []byte(sym)...)
|
2023-03-31 16:03:54 +02:00
|
|
|
ins = append(ins, 0)
|
|
|
|
st, _, err = RunLoad(ins, st, &rs, context.TODO())
|
2023-03-31 11:59:55 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
m, err = st.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r, err = rs.Render("bar", m)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
expect = "inky pinky one blinky two clyde"
|
|
|
|
if r != expect {
|
|
|
|
t.Errorf("Expected %v, got %v", expect, r)
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 16:24:29 +02:00
|
|
|
|
|
|
|
func TestRunMultiple(t *testing.T) {
|
|
|
|
st := state.NewState(5)
|
|
|
|
rs := TestResource{}
|
2023-03-31 17:12:14 +02:00
|
|
|
b := []byte{}
|
2023-03-31 20:24:30 +02:00
|
|
|
b = NewLine(b, LOAD, []string{"one"}, nil, []uint8{0})
|
|
|
|
b = NewLine(b, LOAD, []string{"two"}, nil, []uint8{42})
|
2023-03-31 16:24:29 +02:00
|
|
|
st, _, err := Run(b, st, &rs, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 17:12:14 +02:00
|
|
|
|
|
|
|
func TestRunReload(t *testing.T) {
|
|
|
|
st := state.NewState(5)
|
|
|
|
rs := TestResource{}
|
|
|
|
b := []byte{}
|
2023-03-31 20:24:30 +02:00
|
|
|
b = NewLine(b, LOAD, []string{"dyn"}, nil, []uint8{0})
|
|
|
|
b = NewLine(b, MAP, []string{"dyn"}, nil, nil)
|
2023-03-31 17:12:14 +02:00
|
|
|
st, _, err := Run(b, st, &rs, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r, err := st.Val("dyn")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if r != "three" {
|
|
|
|
t.Errorf("expected result 'three', got %v", r)
|
|
|
|
}
|
|
|
|
dynVal = "baz"
|
|
|
|
b = []byte{}
|
2023-03-31 20:24:30 +02:00
|
|
|
b = NewLine(b, RELOAD, []string{"dyn"}, nil, nil)
|
2023-03-31 17:12:14 +02:00
|
|
|
st, _, err = Run(b, st, &rs, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r, err = st.Val("dyn")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
log.Printf("dun now %s", r)
|
|
|
|
if r != "baz" {
|
|
|
|
t.Errorf("expected result 'baz', got %v", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:24:30 +02:00
|
|
|
func TestRunArg(t *testing.T) {
|
|
|
|
st := state.NewState(5)
|
|
|
|
rt := router.NewRouter()
|
|
|
|
rt.Add("foo", "bar")
|
|
|
|
rt.Add("baz", "xyzzy")
|
|
|
|
b := []byte{0x03}
|
|
|
|
b = append(b, []byte("baz")...)
|
|
|
|
b = append(b, rt.ToBytes()...)
|
|
|
|
var err error
|
|
|
|
st, b, err = Apply(b, []byte{}, st, nil, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2023-03-31 17:12:14 +02:00
|
|
|
}
|
2023-03-31 20:24:30 +02:00
|
|
|
l := len(b)
|
|
|
|
if l != 0 {
|
|
|
|
t.Errorf("expected empty remainder, got length %v: %v", l, b)
|
2023-03-31 17:12:14 +02:00
|
|
|
}
|
2023-03-31 20:24:30 +02:00
|
|
|
r := st.Where()
|
|
|
|
if r != "xyzzy" {
|
|
|
|
t.Errorf("expected where-state baz, got %v", r)
|
2023-03-31 17:12:14 +02:00
|
|
|
}
|
|
|
|
}
|
2023-03-31 20:24:30 +02:00
|
|
|
|
|
|
|
func TestRunArgInvalid(t *testing.T) {
|
|
|
|
st := state.NewState(5)
|
|
|
|
rt := router.NewRouter()
|
|
|
|
rt.Add("foo", "bar")
|
|
|
|
rt.Add("baz", "xyzzy")
|
|
|
|
b := []byte{0x03}
|
|
|
|
b = append(b, []byte("bar")...)
|
|
|
|
b = append(b, rt.ToBytes()...)
|
|
|
|
var err error
|
|
|
|
st, b, err = Apply(b, []byte{}, st, nil, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
l := len(b)
|
|
|
|
if l != 0 {
|
|
|
|
t.Errorf("expected empty remainder, got length %v: %v", l, b)
|
|
|
|
}
|
|
|
|
r := st.Where()
|
|
|
|
if r != "_catch" {
|
|
|
|
t.Errorf("expected where-state _catch, got %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 20:40:54 +02:00
|
|
|
func TestRunArgInstructions(t *testing.T) {
|
|
|
|
st := state.NewState(5)
|
2023-03-31 21:05:57 +02:00
|
|
|
rs := TestResource{}
|
|
|
|
|
2023-03-31 20:40:54 +02:00
|
|
|
rt := router.NewRouter()
|
|
|
|
rt.Add("foo", "bar")
|
|
|
|
b := []byte{0x03}
|
|
|
|
b = append(b, []byte("foo")...)
|
|
|
|
b = append(b, rt.ToBytes()...)
|
|
|
|
|
|
|
|
bi := NewLine([]byte{}, LOAD, []string{"one"}, nil, []uint8{0})
|
2023-03-31 22:08:06 +02:00
|
|
|
bi = NewLine(bi, LOAD, []string{"two"}, nil, []uint8{3})
|
2023-03-31 20:40:54 +02:00
|
|
|
bi = NewLine(bi, MAP, []string{"one"}, nil, nil)
|
|
|
|
bi = NewLine(bi, MAP, []string{"two"}, nil, nil)
|
|
|
|
var err error
|
|
|
|
st, b, err = Apply(b, bi, st, &rs, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
l := len(b)
|
|
|
|
if l != 0 {
|
|
|
|
t.Errorf("expected empty remainder, got length %v: %v", l, b)
|
|
|
|
}
|
|
|
|
loc := st.Where()
|
|
|
|
if loc != "bar" {
|
|
|
|
t.Errorf("expected where-state _catch, got %v", loc)
|
|
|
|
}
|
|
|
|
m, err := st.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
r, err := rs.Render(loc, m)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_ = r
|
|
|
|
}
|
2023-03-31 21:05:57 +02:00
|
|
|
|
|
|
|
func TestRunMoveAndBack(t *testing.T) {
|
|
|
|
st := state.NewState(5)
|
|
|
|
rs := TestResource{}
|
|
|
|
rt := router.NewRouter()
|
|
|
|
rt.Add("foo", "bar")
|
|
|
|
b := []byte{0x03}
|
|
|
|
b = append(b, []byte("foo")...)
|
|
|
|
b = append(b, rt.ToBytes()...)
|
|
|
|
bi := NewLine([]byte{}, LOAD, []string{"one"}, nil, []uint8{0})
|
|
|
|
|
|
|
|
var err error
|
|
|
|
st, b, err = Apply(b, bi, st, &rs, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
l := len(b)
|
|
|
|
if l != 0 {
|
|
|
|
t.Errorf("expected empty remainder, got length %v: %v", l, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
rt = router.NewRouter()
|
|
|
|
rt.Add("foo", "baz")
|
|
|
|
b = []byte{0x03}
|
|
|
|
b = append(b, []byte("foo")...)
|
|
|
|
b = append(b, rt.ToBytes()...)
|
|
|
|
bi = NewLine([]byte{}, LOAD, []string{"two"}, nil, []uint8{0})
|
|
|
|
st, b, err = Apply(b, bi, st, &rs, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
l = len(b)
|
|
|
|
if l != 0 {
|
|
|
|
t.Errorf("expected empty remainder, got length %v: %v", l, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
rt = router.NewRouter()
|
|
|
|
rt.Add("foo", "_")
|
|
|
|
b = []byte{0x03}
|
|
|
|
b = append(b, []byte("foo")...)
|
|
|
|
b = append(b, rt.ToBytes()...)
|
|
|
|
st, b, err = Apply(b, []byte{}, st, &rs, context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
l = len(b)
|
|
|
|
if l != 0 {
|
|
|
|
t.Errorf("expected empty remainder, got length %v: %v", l, b)
|
|
|
|
}
|
|
|
|
loc := st.Where()
|
|
|
|
if loc != "bar" {
|
|
|
|
t.Errorf("expected where-string 'bar', got %v", loc)
|
|
|
|
}
|
|
|
|
}
|