government / src / lisp_test.go

@ 52e34195229c6f9cac00b26701541ddac8a02b66 | history


package government_test

import (
	"os"
	"path/filepath"
	"reflect"
	"testing"
	"time"

	"nmyk.io/government/eval"
	"nmyk.io/government/internal/lisptest"
	"nmyk.io/government/lisp2gov"
	"nmyk.io/government/program"
)

// TestLispFiles compiles every language test program (lisptest.Files) to a .gov program,
// with and without shared subexpressions, runs that program through the
// whole pipeline (ratify, policy graph, decode, evaluate), and checks each
// form against the expectation written in the source.
func TestLispFiles(t *testing.T) {
	paths, err := lisptest.Files()
	if err != nil {
		t.Fatal(err)
	}
	for _, path := range paths {
		for _, noShare := range []bool{false, true} {
			name := filepath.Base(path)
			if noShare {
				name += "/no-share"
			}
			t.Run(name, func(t *testing.T) { testLispFile(t, path, noShare) })
		}
	}
}

func testLispFile(t *testing.T, path string, noShare bool) {
	src, err := os.ReadFile(path)
	if err != nil {
		t.Fatal(err)
	}
	f, err := lisptest.Parse(path, string(src))
	if err != nil {
		t.Fatal(err)
	}
	data, err := lisp2gov.Compile(string(src), lisp2gov.Options{NoShare: noShare})
	if err != nil {
		t.Fatal(err)
	}
	p, err := program.Load(data, program.Options{At: time.Now()})
	if err != nil {
		t.Fatal(err)
	}
	compiled, err := f.WithForms(p.Forms)
	if err != nil {
		t.Fatal(err)
	}
	for i, c := range f.Cases {
		if !reflect.DeepEqual(c.Form, p.Forms[i]) {
			t.Errorf("%s:%v: the program decodes to a different form", path, c.Pos)
		}
	}
	lisptest.Run(t, compiled, eval.NewTopLevel())
}