@ 18e20f574cf1ffbd092fda0b63e7b07430be77dc | history
package gov2lisp
import (
"os"
"path/filepath"
"reflect"
"testing"
"time"
"government/lisp2gov"
"government/program"
"government/sexpr"
)
// TestRoundTrip compiles each testdata file, decodes the .gov and
// decompiles it. The text must equal the source in normal form, and read
// back to the source's forms. A bug anywhere between reading the source
// and printing the decoded program shows up here, in either direction.
func TestRoundTrip(t *testing.T) {
files, _ := filepath.Glob("../testdata/*.gov.lisp")
examples, _ := filepath.Glob("../examples/*.gov.lisp")
files = append(files, examples...)
if len(files) == 0 {
t.Fatal("no programs found")
}
for _, file := range files {
src, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
want, err := sexpr.ReadAll(string(src))
if err != nil {
t.Fatal(err)
}
data, err := lisp2gov.Compile(string(src), lisp2gov.Options{})
if err != nil {
t.Fatal(err)
}
p, err := program.Load(data, program.Options{At: time.Now()})
if err != nil {
t.Fatalf("%s: %v", file, err)
}
got := Decompile(p.Forms)
if normal := Decompile(want); got != normal {
t.Errorf("%s: decompiled text differs from the source in normal form", file)
}
back, err := sexpr.ReadAll(got)
if err != nil {
t.Errorf("%s: decompiled text does not read: %v", file, err)
continue
}
if !reflect.DeepEqual(back, want) {
t.Errorf("%s: decompiled text reads as different forms", file)
}
}
}
func TestDecompileNormalForm(t *testing.T) {
forms, _ := sexpr.ReadAll("'x ( + 1 3.10 )\n; gone\n\"a\\tb\"")
if got, want := Decompile(forms), "(quote x)\n(+ 1 3.1)\n\"a\\tb\"\n"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}