vise/engine/loop_test.go

127 lines
2.4 KiB
Go
Raw Normal View History

package engine
import (
"bytes"
"context"
"fmt"
"log"
"strings"
"testing"
2023-04-14 10:59:37 +02:00
"git.defalsify.org/vise/cache"
"git.defalsify.org/vise/resource"
"git.defalsify.org/vise/state"
)
func TestLoopTop(t *testing.T) {
generateTestData(t)
ctx := context.TODO()
st := state.NewState(0)
rs := resource.NewFsResource(dataDir)
ca := cache.NewCache().WithCacheSize(1024)
2023-04-13 01:38:33 +02:00
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &st, &rs, ca, ctx)
err := en.Init("root", ctx)
if err != nil {
t.Fatal(err)
}
input := []string{
"2",
"j",
"1",
}
inputStr := strings.Join(input, "\n")
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
outputBuf := bytes.NewBuffer(nil)
log.Printf("running with input: %s", inputBuf.Bytes())
2023-04-13 01:38:33 +02:00
err = Loop(&en, inputBuf, outputBuf, ctx)
if err != nil {
t.Fatal(err)
}
location, _ := st.Where()
if location != "foo" {
fmt.Errorf("expected location 'foo', got %s", location)
}
}
func TestLoopBackForth(t *testing.T) {
generateTestData(t)
ctx := context.TODO()
st := state.NewState(0)
rs := resource.NewFsResource(dataDir)
ca := cache.NewCache().WithCacheSize(1024)
2023-04-13 01:38:33 +02:00
cfg := Config{
Root: "root",
}
en := NewEngine(cfg, &st, &rs, ca, ctx)
err := en.Init("root", ctx)
if err != nil {
t.Fatal(err)
}
input := []string{
"1",
"0",
"1",
"0",
}
inputStr := strings.Join(input, "\n")
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
outputBuf := bytes.NewBuffer(nil)
log.Printf("running with input: %s", inputBuf.Bytes())
2023-04-13 01:38:33 +02:00
err = Loop(&en, inputBuf, outputBuf, ctx)
if err != nil {
t.Fatal(err)
}
}
func TestLoopBrowse(t *testing.T) {
generateTestData(t)
ctx := context.TODO()
st := state.NewState(0)
rs := resource.NewFsResource(dataDir)
ca := cache.NewCache().WithCacheSize(1024)
cfg := Config{
OutputSize: 68,
2023-04-13 01:38:33 +02:00
Root: "root",
}
2023-04-13 01:38:33 +02:00
en := NewEngine(cfg, &st, &rs, ca, ctx)
err := en.Init("root", ctx)
if err != nil {
t.Fatal(err)
}
input := []string{
"1",
"2",
"00",
"11",
"00",
}
inputStr := strings.Join(input, "\n")
inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a))
outputBuf := bytes.NewBuffer(nil)
log.Printf("running with input: %s", inputBuf.Bytes())
2023-04-13 01:38:33 +02:00
err = Loop(&en, inputBuf, outputBuf, ctx)
if err != nil {
t.Fatal(err)
}
location, idx := st.Where()
if location != "long" {
fmt.Errorf("expected location 'long', got %s", location)
}
if idx != 1 {
fmt.Errorf("expected idx 1, got %v", idx)
}
}