government / internal / lisptest / lisptest.go

@ 26a72779eff9f25b53bae86607e9e4f6cd54934d | history


// Package lisptest runs GOVERNMENT test files: ordinary Lisp source whose
// expected results are written in comments after each top-level form.
//
//	(+ 1 2)        ;=> 3
//	(car '())      ;=! car needs a pair
//
// ";=> text" means the form's printed value must be exactly text.
// ";=! text" means evaluating the form must fail with an error whose
// message contains text, ignoring case. An annotation belongs to the last
// top-level form that starts before it. A form without one must simply not
// fail.
//
// An error ends only its own form, so one file can hold many error cases.
package lisptest

import (
	"fmt"
	"strings"

	"government/eval"
	"government/sexpr"
)

type Expect int

const (
	ExpectSuccess Expect = iota // no annotation: must not fail
	ExpectValue                 // ;=>
	ExpectError                 // ;=!
)

type Case struct {
	Form   sexpr.Value
	Pos    sexpr.Pos
	Expect Expect
	Want   string
}

type File struct {
	Name   string
	Cases  []Case
	reader *sexpr.Reader
}

func Parse(name, src string) (*File, error) {
	r := sexpr.NewReader(src)
	f := &File{Name: name, reader: r}
	for {
		v, pos, err := r.ReadWithPos()
		if err != nil {
			return nil, fmt.Errorf("%s:%v", name, err)
		}
		if v == nil {
			break
		}
		f.Cases = append(f.Cases, Case{Form: v, Pos: pos})
	}

	for _, c := range r.Comments() {
		expect, want, ok := annotation(c.Text)
		if !ok {
			continue
		}
		i := lastCaseBefore(f.Cases, c.Pos)
		switch {
		case i < 0:
			return nil, fmt.Errorf("%s:%v: annotation before any form", name, c.Pos)
		case f.Cases[i].Expect != ExpectSuccess:
			return nil, fmt.Errorf("%s:%v: second annotation for the form at %v", name, c.Pos, f.Cases[i].Pos)
		case want == "":
			return nil, fmt.Errorf("%s:%v: empty annotation", name, c.Pos)
		}
		f.Cases[i].Expect, f.Cases[i].Want = expect, want
	}
	return f, nil
}

func annotation(comment string) (Expect, string, bool) {
	switch {
	case strings.HasPrefix(comment, "=>"):
		return ExpectValue, strings.TrimSpace(comment[2:]), true
	case strings.HasPrefix(comment, "=!"):
		return ExpectError, strings.TrimSpace(comment[2:]), true
	}
	return ExpectSuccess, "", false
}

func lastCaseBefore(cases []Case, p sexpr.Pos) int {
	found := -1
	for i, c := range cases {
		if c.Pos.Line < p.Line || (c.Pos.Line == p.Line && c.Pos.Col < p.Col) {
			found = i
		}
	}
	return found
}

// Evaluator runs one top-level form, keeping definitions between calls.
type Evaluator interface {
	Eval(form sexpr.Value) (sexpr.Value, error)
}

// Reporter is the part of *testing.T that Run uses.
type Reporter interface {
	Helper()
	Errorf(format string, args ...any)
}

// Run evaluates every case in order and reports each mismatch.
func Run(t Reporter, f *File, ev Evaluator) {
	t.Helper()
	for _, c := range f.Cases {
		v, err := ev.Eval(c.Form)
		at := fmt.Sprintf("%s:%v: %s", f.Name, c.Pos, abbreviate(sexpr.Print(c.Form)))
		switch {
		case c.Expect == ExpectError && err == nil:
			t.Errorf("%s\n\tgot value %s, want an error containing %q", at, sexpr.Print(v), c.Want)
		case c.Expect == ExpectError:
			if !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(c.Want)) {
				t.Errorf("%s\n\tgot error %s, want one containing %q", at, f.Describe(err), c.Want)
			}
		case err != nil:
			t.Errorf("%s\n\tunexpected error %s", at, f.Describe(err))
		case c.Expect == ExpectValue && sexpr.Print(v) != c.Want:
			t.Errorf("%s\n\tgot  %s\n\twant %s", at, sexpr.Print(v), c.Want)
		}
	}
}

// Describe formats an error with the source position of the subexpression
// that failed, when the evaluator recorded one.
func (f *File) Describe(err error) string {
	e, ok := err.(*eval.Error)
	if !ok || e.Expr == nil {
		return err.Error()
	}
	pos, ok := f.reader.Position(e.Expr)
	if !ok {
		return err.Error()
	}
	return fmt.Sprintf("%s (at %v: %s)", err, pos, abbreviate(sexpr.Print(e.Expr)))
}

func abbreviate(s string) string {
	const max = 60
	s = strings.Join(strings.Fields(s), " ")
	if len(s) > max {
		return s[:max-3] + "..."
	}
	return s
}

// WithForms returns a copy of f whose cases evaluate forms instead of the
// forms read from source, keeping each case's annotation and position.
// The test runner uses it to run a file's compiled .gov program against
// the expectations written in its source.
func (f *File) WithForms(forms []sexpr.Value) (*File, error) {
	if len(forms) != len(f.Cases) {
		return nil, fmt.Errorf("%s: %d forms for %d cases", f.Name, len(forms), len(f.Cases))
	}
	g := *f
	g.Cases = make([]Case, len(f.Cases))
	for i, c := range f.Cases {
		c.Form = forms[i]
		g.Cases[i] = c
	}
	return &g, nil
}