vise/testdata/testdata.go

194 lines
4.4 KiB
Go
Raw Permalink Normal View History

2023-04-02 10:07:53 +02:00
package testdata
import (
"fmt"
"io/ioutil"
"os"
"path"
testdataloader "github.com/peteole/testdata-loader"
"git.defalsify.org/festive/vm"
)
type genFunc func() error
var (
BaseDir = testdataloader.GetBasePath()
DataDir = ""
dirLock = false
)
func out(sym string, b []byte, tpl string, data map[string]string) error {
2023-04-02 10:07:53 +02:00
fp := path.Join(DataDir, sym)
err := ioutil.WriteFile(fp, []byte(tpl), 0644)
if err != nil {
return err
}
fb := sym + ".bin"
fp = path.Join(DataDir, fb)
err = ioutil.WriteFile(fp, b, 0644)
if err != nil {
return err
}
if data == nil {
return nil
}
for k, v := range data {
fb := k + ".txt"
fp = path.Join(DataDir, fb)
err = ioutil.WriteFile(fp, []byte(v), 0644)
if err != nil {
return err
}
}
2023-04-02 10:07:53 +02:00
return nil
}
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.HALT, nil, nil, nil)
2023-04-02 10:07:53 +02:00
b = vm.NewLine(b, vm.INCMP, []string{"1", "foo"}, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"2", "bar"}, nil, nil)
tpl := "hello world"
return out("root", b, tpl, nil)
2023-04-02 10:07:53 +02:00
}
func foo() error {
b := []byte{}
b = vm.NewLine(b, vm.MOUT, []string{"0", "to foo"}, nil, nil)
b = vm.NewLine(b, vm.MOUT, []string{"1", "go bar"}, nil, nil)
b = vm.NewLine(b, vm.MOUT, []string{"2", "see long"}, nil, nil)
2023-04-02 10:07:53 +02:00
b = vm.NewLine(b, vm.LOAD, []string{"inky"}, []byte{20}, nil)
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"0", "_"}, nil, nil)
2023-04-02 10:07:53 +02:00
b = vm.NewLine(b, vm.INCMP, []string{"1", "baz"}, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"2", "long"}, nil, nil)
2023-04-02 10:07:53 +02:00
data := make(map[string]string)
data["inky"] = "one"
2023-04-02 10:07:53 +02:00
tpl := `this is in foo
it has more lines`
return out("foo", b, tpl, data)
2023-04-02 10:07:53 +02:00
}
func bar() error {
b := []byte{}
b = vm.NewLine(b, vm.LOAD, []string{"pinky"}, []byte{0}, nil)
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"*", "^"}, nil, nil)
2023-04-02 10:07:53 +02:00
tpl := "this is bar - any input will return to top"
2023-04-02 10:07:53 +02:00
data := make(map[string]string)
data["pinky"] = "two"
return out("bar", b, tpl, data)
2023-04-02 10:07:53 +02:00
}
func baz() error {
b := []byte{}
b = vm.NewLine(b, vm.MAP, []string{"inky"}, nil, nil)
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
tpl := "this is baz which uses the var {{.inky}} in the template."
return out("baz", b, tpl, nil)
2023-04-02 10:07:53 +02:00
}
func long() error {
b := []byte{}
b = vm.NewLine(b, vm.MOUT, []string{"0", "back"}, nil, nil)
b = vm.NewLine(b, vm.MNEXT, []string{"00", "nexxt"}, nil, nil)
b = vm.NewLine(b, vm.MPREV, []string{"11", "prevvv"}, nil, nil)
2023-04-10 18:38:23 +02:00
b = vm.NewLine(b, vm.LOAD, []string{"longdata"}, []byte{0x00}, nil)
b = vm.NewLine(b, vm.MAP, []string{"longdata"}, nil, nil)
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"0", "_"}, nil, nil)
2023-04-12 08:29:23 +02:00
b = vm.NewLine(b, vm.INCMP, []string{"00", ">"}, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"11", "<"}, nil, nil)
tpl := `data
{{.longdata}}`
data := make(map[string]string)
data["longdata"] = `INKY 12
PINKY 5555
BLINKY 3t7
CLYDE 11
TINKYWINKY 22
DIPSY 666
LALA 111
POO 222
`
return out("long", b, tpl, data)
}
2023-04-06 11:55:11 +02:00
func defaultCatch() error {
b := []byte{}
b = vm.NewLine(b, vm.MOUT, []string{"0", "back"}, nil, nil)
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"*", "_"}, nil, nil)
2023-04-06 11:55:11 +02:00
tpl := "invalid input"
return out("_catch", b, tpl, nil)
2023-04-06 11:55:11 +02:00
}
2023-04-02 10:07:53 +02:00
func generate() error {
err := os.MkdirAll(DataDir, 0755)
if err != nil {
return err
}
fns := []genFunc{root, foo, bar, baz, long, defaultCatch}
2023-04-02 10:07:53 +02:00
for _, fn := range fns {
err = fn()
if err != nil {
return err
}
}
return nil
}
2023-04-12 19:04:36 +02:00
// Generate outputs bytecode, templates and content symbols to a temporary directory.
//
// This directory can in turn be used as data source for the the resource.FsResource object.
2023-04-02 10:07:53 +02:00
func Generate() (string, error) {
dir, err := ioutil.TempDir("", "festive_testdata_")
if err != nil {
return "", err
}
DataDir = dir
dirLock = true
err = generate()
return dir, err
}
2023-04-12 19:04:36 +02:00
// Generate outputs bytecode, templates and content symbols to a specified directory.
//
// The directory must exist, and must not have been used already in the same code execution.
//
// This directory can in turn be used as data source for the the resource.FsResource object.
2023-04-02 10:07:53 +02:00
func GenerateTo(dir string) error {
if dirLock {
return fmt.Errorf("directory already overridden")
}
DataDir = dir
dirLock = true
return generate()
}