@ 5c83a9c6b185121b6a79d1e44755763853b4b6d5 | history
package gov2lisp
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"government/ast"
"government/lisp2gov"
"government/program"
"government/sexpr"
)
func decompiled(t *testing.T, src string) string {
t.Helper()
data, err := lisp2gov.Compile(src, lisp2gov.Options{})
if err != nil {
t.Fatal(err)
}
p, err := program.Load(data, program.Options{At: time.Now()})
if err != nil {
t.Fatal(err)
}
return DecompileTree(p.Tree)
}
// TestRoundTrip compiles each program, decodes the .gov and decompiles it.
// The text must read back to the source's forms with the source's notes,
// and compiling it again must give the same text: 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)
}
wantForms, wantNotes, err := readWithNotes(string(src))
if err != nil {
t.Fatal(err)
}
text := decompiled(t, string(src))
gotForms, gotNotes, err := readWithNotes(text)
if err != nil {
t.Errorf("%s: decompiled text does not read: %v", file, err)
continue
}
if !reflect.DeepEqual(gotForms, wantForms) {
t.Errorf("%s: decompiled text reads as different forms", file)
}
if !reflect.DeepEqual(gotNotes, wantNotes) {
t.Errorf("%s: notes differ:\n got %q\n want %q", file, gotNotes, wantNotes)
}
if again := decompiled(t, text); again != text {
t.Errorf("%s: decompiling twice gives different text", file)
}
}
}
// readWithNotes reads src as values, plus the note of each node, by id.
func readWithNotes(src string) ([]sexpr.Value, map[uint64]string, error) {
datums, err := sexpr.ReadProgram(src)
if err != nil {
return nil, nil, err
}
tree, err := ast.Build(datums)
if err != nil {
return nil, nil, err
}
notes := map[uint64]string{}
var walk func(n *ast.Node)
walk = func(n *ast.Node) {
if n.Note != "" {
notes[n.ID] = n.Note
}
for _, c := range n.Children {
walk(c)
}
}
for _, f := range tree.Forms {
walk(f)
}
forms := make([]sexpr.Value, len(datums))
for i, d := range datums {
forms[i] = d.Value
}
return forms, notes, nil
}
func TestLongNoteSurvives(t *testing.T) {
// 450 characters: three user notices in the cert, one comment again.
long := strings.Repeat("The quick brown fox jumps. ", 17)[:450]
text := decompiled(t, "; "+long+"\n(+ 1 2)\n")
if text != "; "+long+"\n(+ 1 2)\n" {
t.Errorf("got %q", text)
}
}
func TestNotesInsideForms(t *testing.T) {
src := "(define area\n ; width times height\n (lambda (w h)\n (* w ; the width\n h)))\n"
text := decompiled(t, src)
want := "(define\n area\n ; width times height\n (lambda\n (w h)\n (*\n ; the width\n w\n h)))\n"
if text != want {
t.Errorf("got\n%s\nwant\n%s", text, want)
}
}
func TestDecompileNormalForm(t *testing.T) {
forms, _ := sexpr.ReadAll("'x ( + 1 3.10 )\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)
}
}