government / lisp_test.go

@ b16190dee4ae781544f8119bec44f00f5224c810 | history


package government_test

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

	"government/eval"
	"government/internal/lisptest"
	"government/lisp2gov"
	"government/program"
)

// TestLispFiles compiles every testdata/*.gov.lisp file to a .gov program,
// 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 := filepath.Glob("testdata/*.gov.lisp")
	if err != nil {
		t.Fatal(err)
	}
	if len(paths) == 0 {
		t.Fatal("no testdata/*.gov.lisp files found")
	}
	for _, path := range paths {
		t.Run(filepath.Base(path), func(t *testing.T) {
			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{})
			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())
		})
	}
}